Jump to content

Module:IncubatorInterwiki

Fi Wikipedia

Documentation for this module may be created at Module:IncubatorInterwiki/doc

-- This module should remained unused in the Wikimedia Incubator.
-- It is meant to be exported to newly created wikis and be used there
-- with the {{INTERWIKI}} template until Wikidata support is enabled
-- for the wiki, and the template and module can be removed and deleted.
local p = {}

-- language codes with underscore '_', have to be translated to '-', to be readable for MediaWiki
local langcode = {
	['bat_smg'] = 'bat-smg',
	['be_x_old'] = 'be-x-old',
	['cbk_zam'] = 'cbk-zam',
	['fiu_vro'] = 'fiu-vro',
	['map_bms'] = 'map-bms',
	['nds_nl'] = 'nds-nl',
	['roa_rup'] = 'roa-rup',
	['roa_tara'] = 'roa-tara',
	['zh_classical'] = 'zh-classical',
	['zh_min_nan'] = 'zh-min-nan', -- a comma has to be added when new lines are added
	['zh_yue'] = 'zh-yue'
}

local function endsWith(str, ending)
	return ending == '' or str:sub(-#ending) == ending
end

local function getProjectAndLanguage()
	local server = mw.ustring.gsub(mw.site.server, '//', '')
	local serversplit = mw.text.split(server, '%.')
	local lang = serversplit[1]
	local project = mw.ustring.gsub(serversplit[2], 'wikipedia', 'wiki')
	return {lang, project}
end

p.interwiki = function(frame)
	local s = {}
	-- If the page is connected to Wikidata, this module should do nothing.
	if mw.wikibase.getEntityIdForCurrentPage() then
		return ''
	end
	local qid = frame.args.qid or frame:getParent().args[1] or frame:getParent().args.qid or ''
	local thislang = getProjectAndLanguage()[1]
	local thisproject = getProjectAndLanguage()[2]
	thislang = mw.ustring.gsub(thislang, '-', '_')
	thislang = mw.ustring.gsub(thislang, 'be_tarask', 'be_x_old')
	if not mw.ustring.match( qid, '^[Qq][1-9]%d*$' ) then
		return ''
	else
		if qid then
			local entity = mw.wikibase.getEntity(qid)
			if entity and entity.sitelinks then
				for i, j in pairs(entity.sitelinks) do
					if endsWith(j.site, thisproject) then
						local lang = mw.ustring.gsub(j.site, thisproject, '')
						if (langcode[lang] or #lang <= 3) and lang ~= thislang then
							-- Special case for the Norwegian Bokmål Wikipedia;
							-- use 'nb' instead of 'no' for that one site
							if j.site == 'nowiki' then
								lang = 'nb'
							end
							lang = langcode[lang] or lang
							local iwtext = '[[' .. lang .. ':' .. j.title .. ']]'
							table.insert(s, iwtext)
						end
					end
				end
			end
		end
	end
	return table.concat(s, '\n')
end

return p