模組:Wikisource further reading

local p = {};

out=""
function p.getCommonsInfo(commonsType)
    commonsType = commonsType or "withParentheses"
	local id=mw.wikibase.getEntityIdForCurrentPage()
	---If there is a interwiki link for Wikimedia Commons, show it.
	local commonsPage=mw.wikibase.getEntity( id )["sitelinks"]["commonswiki"]
	P373=mw.wikibase.getBestStatements( id , "P373")[1]
	if( commonsPage ~= nil ) then
		if( commonsType == "withoutParentheses") then
			commonsTitle=commonsPage["title"]
			out=out.."[[File:Commons-logo.svg|16x16px|class=noviewer|alt=|link=|维基共享资源]] 在[[维基共享资源]]'''[[commons:"..commonsTitle.."|阅览影像]]'''"
			---Wikimedia Commons category.
			local P373=mw.wikibase.getBestStatements( id , "P373")[1]
			if( P373 and P373.mainsnak.datavalue and ("Category:"..P373.mainsnak.datavalue.value~=commonsTitle)) then
				out=out.."、[[commons:Category:"..P373.mainsnak.datavalue.value.."|'''分类''']]"
			end

		else
			commonsTitle=commonsPage["title"]
			out=out.."([[File:Commons-logo.svg|16x16px|class=noviewer|alt=|link=|维基共享资源]] 在[[维基共享资源]]'''[[commons:"..commonsTitle.."|阅览影像]]'''"
			---Wikimedia Commons category.
			if( P373 and P373.mainsnak.datavalue and ("Category:"..P373.mainsnak.datavalue.value~=commonsTitle)) then
				out=out.."、[[commons:Category:"..P373.mainsnak.datavalue.value.."|'''分类''']]"
			end
			out=out..")"
		end
	elseif ( P373 and P373.mainsnak.datavalue) then
		if( commonsType == "withoutParentheses") then
			out=out.."[[File:Commons-logo.svg|16x16px|class=noviewer|alt=|link=|维基共享资源]] 在[[维基共享资源]]'''[[commons:Category:"..P373.mainsnak.datavalue.value.."|阅览影像]]'''"
		else
			out=out.."([[File:Commons-logo.svg|16x16px|class=noviewer|alt=|link=|维基共享资源]] 在[[维基共享资源]]'''[[commons:Category:"..P373.mainsnak.datavalue.value.."|阅览影像]]''')"
		end
	end
	return out;
end



function p.getWikisourceInfo()
	local id=mw.wikibase.getEntityIdForCurrentPage()
	local commonsNotShown=true
	local pageName=mw.wikibase.getLabel(id,"zh-hant")

	local out='<span class="mw-editsection-like plainlinks" style="float: right;"><span class="mw-editsection-bracket">[</span>[[wikidata:'..id..'|<span class="nomobile">在维基数据</span>编<span class="nomobile">辑</span>]]<span class="mw-editsection-bracket">]</span></span>'
	local wikisourcePage=mw.wikibase.getEntity( id )["sitelinks"]["zhwikisource"]

--If there is a interwiki link for Wikisource, show it.
	if( wikisourcePage ~= nil ) then
		title=wikisourcePage["title"]
		if( string.match(title, "Author:") ) then
			out=out.."\n: [[File:Wikisource-logo.svg|16x16px|class=noviewer|alt=|link=|维基文库]] 在[[维基文库]]'''[[s:"..title.."|阅读此作者作品]]'''"
		else
			out=out.."\n: [[File:Wikisource-logo.svg|16x16px|class=noviewer|alt=|link=|维基文库]] 在[[维基文库]]'''[[s:"..title.."|阅读本作品原文]]'''"
		end
		out=out..p.getCommonsInfo()
		commonsNotShown=false
	end
	local claims=mw.wikibase.getEntity( id )["claims"]

--If there are versions (P747) with a Wikisource link, show them.
	if( claims ~= nil and claims["P747"] ~= nil )
	then
		P747=claims["P747"]
		for i, data in ipairs(P747) do
			if( data["qualifiers"]~= nil and data["qualifiers"]["P805"]~= nil)
			then
				local wikisourceBook=data["qualifiers"]["P805"]
				local wikisourceId=wikisourceBook[1]["datavalue"]["value"]["id"]
				local ws=mw.wikibase.getEntity( wikisourceId )["sitelinks"]
				if( ws~=nil and ws["zhwikisource"]~=nil ) then
					local zhws=ws["zhwikisource"]
					local wikisourcePageTitle=zhws["title"]
					local wikisourcePageName=mw.wikibase.getLabel( wikisourceId ) or wikisourcePageTitle
					if( type(data["qualifiers"]["P958"]) == "table") 
					then
						wikisourceSection="#"..data["qualifiers"]["P958"][1]["datavalue"]["value"]
					else 
						wikisourceSection="#"..pageName
					end
					out=out.."\n: [[File:Wikisource-logo.svg|16x16px|class=noviewer|alt=维基文库中的相关文本:|link=|维基文库]] 《[[s:"..wikisourcePageTitle..wikisourceSection.."|"..wikisourcePageTitle.."]]》"
					if commonsNotShown then
						out=out..p.getCommonsInfo()
						commonsNotShown=false
					end
				end
			else

				local wikisourceId =data["mainsnak"]["datavalue"]["value"]["id"]
				local ws=mw.wikibase.getEntity( wikisourceId )["sitelinks"]
				if( ws~=nil and ws["zhwikisource"]~=nil ) then
					local wikisourcePageTitle=ws["zhwikisource"]["title"]
					local wikisourcePageName=mw.wikibase.getLabel( wikisourceId ) or wikisourcePageTitle
					out=out.."\n: [[File:Wikisource-logo.svg|16x16px|class=noviewer|alt=维基文库中的相关文本:|link=|维基文库]] 《[[s:"..wikisourcePageTitle.."|"..wikisourcePageTitle.."]]》"
					if commonsNotShown then
						out=out..p.getCommonsInfo()
						commonsNotShown=false
					end
					local zhWikiLink=mw.wikibase.getEntity( wikisourceId )["sitelinks"]["zhwiki"]
					if( zhWikiLink~=nil ) 
					then
						out=out.."([["..zhWikiLink["title"].."|介绍]])"
					end
			end
			end 
		end 
	end


--If there are described by source (P1343) with a Wikisource link, show them.
	if( claims ~= nil and claims["P1343"] ~= nil )
	then
		P1343=claims["P1343"]
		for i, data in ipairs(P1343) do
			if( data["qualifiers"]~= nil and data["qualifiers"]["P805"]~= nil)
			then
				local wikisourceBook=data["qualifiers"]["P805"]
				local bookId=data["mainsnak"]["datavalue"]["value"]["id"]
				local bookName=mw.wikibase.getLabel( bookId )
				local bookAuthor=mw.wikibase.getBestStatements(bookId, "P50")

				for i2, data2 in ipairs(wikisourceBook) do
					local wikisourceId=data2["datavalue"]["value"]["id"]
					local ws=mw.wikibase.getEntity( wikisourceId )["sitelinks"]
					if( ws~=nil and ws["zhwikisource"]~=nil ) then
						local zhws=ws["zhwikisource"]
						local wikisourcePageTitle=zhws["title"]
						local wikisourcePageName=mw.wikibase.getLabel( wikisourceId ) or wikisourcePageTitle
						if( data["qualifiers"]["P958"]~= nil and data["qualifiers"]["P958"][i]~= nil ) 
						then
							wikisourceSection="#"..data["qualifiers"]["P958"][i]["datavalue"]["value"]
						else 
							wikisourceSection="#"..pageName
						end
						out=out.."\n: [[File:Wikisource-logo.svg|16x16px|class=noviewer|alt=维基文库中的相关文本:|link=|维基文库]] 《[[s:"..wikisourcePageTitle..wikisourceSection.."|"..wikisourcePageName.."]]》"
						out=out..",出自"
						if( bookAuthor[1]~=nil )
						then
							if bookAuthor[1]['mainsnak']['snaktype'] == 'value' then
								local bookAuthorId=bookAuthor[1]['mainsnak']['datavalue']['value']["id"]
								local bookAuthorName=mw.wikibase.getLabel( bookAuthorId )
								local bookAuthorLink=mw.wikibase.getEntity( bookAuthorId )["sitelinks"]["zhwiki"]
								if( bookAuthorLink~=nil ) 
								then
								out=out.."[["..bookAuthorLink["title"].."|"..bookAuthorName.."]]"
								else 
									out=out..bookAuthorName
								end
							end
						end
						local zhWikiLink=mw.wikibase.getEntity( bookId )["sitelinks"]["zhwiki"]
						if( zhWikiLink~=nil ) 
						then
							out=out.."《[["..zhWikiLink["title"].."|"..bookName.."]]》"
						else 
							out=out.."《"..bookName.."》"
						end
					end
				end
			else

				local wikisourceId =data["mainsnak"]["datavalue"]["value"]["id"]
				local ws=mw.wikibase.getEntity( wikisourceId )["sitelinks"]
				if( ws~=nil and ws["zhwikisource"]~=nil ) then
					local wikisourcePageTitle=ws["zhwikisource"]["title"]
					local wikisourcePageName=mw.wikibase.getLabel( wikisourceId ) or wikisourcePageTitle
					out=out.."\n: [[File:Wikisource-logo.svg|16x16px|class=noviewer|alt=维基文库中的相关文本:|link=|维基文库]] 《[[s:"..wikisourcePageTitle.."|"..wikisourcePageName.."]]》"
			end
			end 
		end 
	end

--If there is no Wikisource link yet and P31 is human (Q5), show commons link.

	if commonsNotShown then
		P31=mw.wikibase.getBestStatements(id , "P31")[1]
		if(P31 ~= nil )then
			if(P31.mainsnak.datavalue.value.id=="Q5")then
				out=out.."\n: "..p.getCommonsInfo("withoutParentheses")
			end
		end
	end
	return out

end

return p;