Jump to content

Module:LocalizedWikipediaPage

From Wikimania
Module documentation
local p = {}

local wikidata = require('Module:Wikidata')

function p._getEquivalentWPArticle(sourceTitle, sourceLang, targetLang)
	if not sourceLang or sourceLang == '' then
		sourceLang = 'en'
	end
	
	if not targetLang or targetLang == '' then
		targetLang = defaultlang
	end
	
	local sourceLink = ' :' .. sourceLang .. ':' .. sourceTitle
	
	if sourceLang == targetLang then
		return sourceLink
	end
	
	local sourceWiki = sourceLang .. 'wiki' -- e.g. "enwiki" is global site id for English Wikipedia
	local id = mw.wikibase.getEntityIdForTitle( sourceTitle, sourceWiki )
	
	if not id then
		return sourceLink --source page has no linked Wikidata element, let’s keep it
	end
	
	local fallbackLangs = mw.language.getFallbacksFor(targetLang)
	table.insert(fallbackLangs, 1, targetLang) --keeps targetLang as first lang to try
	
	for _, fallbackLang in ipairs(fallbackLangs) do
		local existingEquivalent = wikidata.getLink(id, 'anywikipedia', fallbackLang)
		if existingEquivalent then
			return ' ' .. existingEquivalent
		end
	end
	
	return sourceLink --source page has no interwiki, let’s keep it
end

function p.getEquivalentWPArticle(frame)
	return p._getEquivalentWPArticle(
		frame.args.sourceTitle,
		frame.args.sourceLang,
		frame.args.targetLang
	)
end

return p