Модуль:Wikidata/Interproject

Материал из Викицитатника

Для документации этого модуля может быть создана страница Модуль:Wikidata/Interproject/doc

local p = {};
 
function p.getWikibooksLink ( )
	return p.getSisterProjectLink( 'b', 'wikibooks' );
end
 
function p.getWikiquoteLink ( )
	return p.getSisterProjectLink( 'q', 'wikiquote' );
end
 
function p.getWikisourceLink ( )
	return p.getSisterProjectLink( 's', 'wikisource' );
end
 
function p.getWikipediaLink ( )
	return p.getSisterProjectLink( 'w', 'wiki' );
end

function p.getWiktionaryLink ( )
	return p.getSisterProjectLink( 'wikt', 'wiktionary' );
end
 
function p.getSisterProjectLink( projectInterwikiLettter, projectName )
	-- По умолчанию старается достать ссылку на русском, иначе - на родном (P103)
	local wbStatus, entity = pcall( mw.wikibase.getEntityObject )
	if wbStatus ~= true or not entity or not entity.sitelinks then
		return ''
	end
	if entity.sitelinks['ru' .. projectName] then
		return projectInterwikiLettter .. ':' .. entity.sitelinks['ru' .. projectName].title
	end
 
	if entity.claims then
		local propValues = entity.claims['P103']
		if propValues then
			local codes = mw.loadData( 'Module:Wikidata:Dictionary/P424' );
			for i, propClaim in pairs(propValues) do
				if propClaim.mainsnak and propClaim.mainsnak.datavalue and propClaim.mainsnak.datavalue.value and propClaim.mainsnak.datavalue.value["numeric-id"] then
					local languageWikidataId = 'Q' .. propClaim.mainsnak.datavalue.value["numeric-id"]
					local codesById = codes[languageWikidataId];
					if ( codesById ) then
						for j, code in pairs( codesById ) do
							local sitelink = entity.sitelinks[code .. projectName]
							if sitelink then
								return projectInterwikiLettter .. ':' .. code .. ':' .. sitelink.title
							end
						end
					end
				end
			end
		end
	end
 
	return ''
end
 
return p;