模块:Plain sister

維基文庫,自由的圖書館
跳到导航 跳到搜索

此模块的文档可以在模块:Plain sister/doc创建

local p = {}

local sites = { 
-- interwiki prefix, parameter,     label and            site id (for Wikidata)
	{ 'w',           'wikipedia',   '百科', 'zhwiki' },
	{ 'w:zh-classical','zhclassicalwiki','大典','zh_classicalwiki' },
	{ 'c',           'commons',     '图册',   'commonswiki' },
	{ 'c:Category',  'commonscat',  '图册分类',  'commonswiki' },
	{ 'q',           'wikiquote',   '语录',            'zhwikiquote' },
	{ 'n',           'wikinews',    '新闻',              'zhwikinews' },
	{ 'wikt',        'wiktionary',  '词义',        'zhwiktionary' },
	{ 'b',           'wikibooks',   '教科书',          'zhwikibooks' },
	{ 'v',           'wikiversity', '课程',            'zhwikiversity' },
	{ 'wikispecies', 'wikispecies', '物种',          'specieswiki' },
	{ 'voy',         'wikivoyage',  '导游',      'zhwikivoyage' },
	{ 'bibliowiki',  'wikilivres',  '維基別庫',        '' }, -- wikilivres now bibliowiki
	{ 'bibliowiki',  'bibliowiki',  '維基別庫',        '' },
	{ 'm',           'meta',        '元维基',              'metawiki' }
}

function p.interprojetPart( frame )
	local frame = frame:getParent()
	local item  = mw.wikibase.getEntityObject()
	local links = {}

	for _, site in pairs( sites ) do
		local val = frame.args[site[2]]             -- link provided as input parameter
		if val == '' then
			val = nil;
		end
		if not val and site[4] ~= '' and item then  -- fetch it from wikidata
			val = item:getSitelink( site[4] )
			if site[4] == 'wikidatawiki' then
				val = item.id 
			elseif site[4] == 'commonswiki' and val then -- we have link to commons 
				local catFlag = (#val>9 and string.sub(val,1,9) == 'Category:')
				if (site[2]=='commonscat' and catFlag==false) or (site[2]=='commons' and catFlag==true) then
					val = nil  -- link is to a wrong namespace so let's nuke it
				elseif (site[2]=='commonscat' and catFlag==true) then
					val = string.sub(val,10) -- trim 'Category:' from the front
				end
			end		
		end
		if not val and site[2] == 'commons' and item then
			local statements = item:getBestStatements('P935' ) -- get commons gallery page from P935 property
			if statements[1] and statements[1].mainsnak.datavalue  then
				val = statements[1].mainsnak.datavalue.value  
			end
		end	
		if not val and site[2] == 'commonscat' and item then
			local statements = item:getBestStatements('P373' )   -- get commons category page from P373 property
			if statements[1] and statements[1].mainsnak.datavalue  then
				val = statements[1].mainsnak.datavalue.value 
			end
		end		

		if val then
			table.insert( links, '[[' .. site[1] .. ':' .. val .. '|' .. site[3] .. ']]' )
		end
	end

	if #links == 0 then -- links table length is zero
		return ''
	end

	return '<li class="sisitem">'
		.. '<span class="sisicon" style="padding-right:1ex;">[[Image:Wikimedia-logo.svg|frameless|18px|link=Special:sitematrix|alt=姊妹计划]]</span>'
		.. '[[Special:sitematrix|姊妹计划]]:' .. table.concat( links, ',' )
		.. '。</li>'
end

return p