跳转到内容

模組:Wikisource further reading/sandbox

维基百科,自由的百科全书
local p = {};


out=""


function p.getWikisourceInfo()
	local id=mw.wikibase.getEntityIdForCurrentPage()
	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.
	local title
	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
	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=""
					end
					out=out.."\n: [[File:Wikisource-logo.svg|16x16px|class=noviewer|alt=维基文库中的相关文本:|link=]] 《[[s:"..wikisourcePageTitle..wikisourceSection.."|"..wikisourcePageTitle.."]]》"
				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.."]]》"

					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 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=""
					end
					out=out.."\n: [[File:Wikisource-logo.svg|16x16px|class=noviewer|alt=维基文库中的相关文本:|link=]] 《[[s:"..wikisourcePageTitle..wikisourceSection.."|"..wikisourcePageName.."]]》"
					out=out..",出自"
					local bookId=data["mainsnak"]["datavalue"]["value"]["id"]
					local bookName=mw.wikibase.getLabel( bookId )
					local bookAuthor=mw.wikibase.getBestStatements(bookId, "P50")
					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
			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( claims ~= nil and claims["P4517"] ~= nil )
	then
		P4517 = claims["P4517"]
		local siteLabel = mw.wikibase.getLabel("Q5100670")
		local ctp = nil
		for i, data in ipairs(P4517) do
			if (data["mainsnak"] ~= nil and data["mainsnak"]["datavalue"] ~= nil and data["mainsnak"]["datavalue"]["value"] ~= nil)
			then
				ctp = data["mainsnak"]["datavalue"]["value"]
			end
		end
		
		local formatUrl
		local pClaims = mw.wikibase.getEntity("P4517")["claims"]
		if (pClaims ~= nil and pClaims["P1630"] ~= nil)
		then
			local P1630 = pClaims["P1630"]
			for i, data in ipairs(P1630) do
				if (data["mainsnak"] ~= nil and data["mainsnak"]["datavalue"] ~= nil and data["mainsnak"]["datavalue"]["value"] ~= nil)
				then
				formatUrl = data["mainsnak"]["datavalue"]["value"]
				end
			end	
		end
		--local formatUrl = "https://ctext.org/searchbooks.pl?if=gb&searchu="
		if (formatUrl ~= nil)
		then
			local destUrl = formatUrl..ctp
			destUrl = string.format(formatUrl, ctp)
			destUrl = string.gsub(formatUrl,"$1", ctp)
			out = out.."                  \n在"..siteLabel.."中["..destUrl.." 阅读"..title.."]"
		end
		
		--out = out..P4517
	end
	
	return out
end

return p;