Модуль:Wikidata/Interproject

Википедия дан

Documentation for this module may be created at Модуль:Wikidata/Interproject/doc

local p = {};

function p.getWikisourceLink ( )
	return p.getSisterProjectLink( 's', 'wikisource' );
end

function p.getWikiquoteLink ( )
	return p.getSisterProjectLink( 'q', 'wikiquote' );
end

function p.getSisterProjectLink( projectInterwikiLettter, projectName )
	-- Автономдуу түрдө шилтемени кыргыз тилинде көрсөтөт, эгерде көрсөтпөсө, анда эне тилинде (P103)
	local entity = mw.wikibase.getEntityObject()
	if not entity or not entity.sitelinks then
		return ''
	end
	if entity.sitelinks['ky' .. projectName] then
		return projectInterwikiLettter .. ':' .. entity.sitelinks['ky' .. 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;