Module:Main subject

文档图示 模块文档[创建]
require("strict")
local p = {};

local function linkText(wikidataId, site)
	local text=""
	local wikiPageTitle=mw.wikibase.getSitelink( wikidataId, site )

	if(wikiPageTitle~=nil) then
		local pageName=mw.wikibase.getLabel( wikidataId ) or wikiPageTitle
		local prefix
		if(site=="zh_classicalwiki") then
			prefix=":w:zh-classical:"
			pageName=mw.wikibase.getLabelByLang( wikidataId, "lzh") or wikiPageTitle
		elseif(site=="zhwiki") then
			prefix=":w:"
		end
		if(wikiPageTitle~=nil) then
			text=text.."[["..prefix..wikiPageTitle.."|"..pageName.."]]"
		end
	end
	return text
end

function p.getInfo()
	local id=mw.wikibase.getEntityIdForCurrentPage()
	local out=""
	if(id~=nil) then
		local zhList={}
		local zhClassicList={}
		local subjects=mw.wikibase.getBestStatements(id, "P921")
		if(subjects~=nil) then
			for i, subject in ipairs(subjects) do
				local wikidataId=subject["mainsnak"]["datavalue"]["value"]["id"]
				local linkZh=linkText(wikidataId, "zhwiki")
				local linkZhClassic=linkText(wikidataId, "zh_classicalwiki")
				if(linkZh~="") then
					table.insert(zhList, linkZh)
				end
				if(linkZhClassic~="") then
					table.insert(zhClassicList, linkZhClassic)
				end
			end
		end

		if(table.getn(zhList)>0) then
			out=out.."\n: [[File:Wikipedia-logo.svg|16x16px|class=noviewer|alt=维基百科|link=]] 参阅[[w:维基百科|维基百科]]中的:"..table.concat(zhList, "、")
		end
		if(table.getn(zhClassicList)>0) then
			out=out.."\n: [[File:Wikipedia-logo.svg|16x16px|class=noviewer|alt=維基大典|link=]] 閲文言[[w:zh-classical:維基大典|維基大典]]文:"..table.concat(zhClassicList, "、")
		end
	end

	return out
end

return p;