Module:IncubatorInterwiki

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

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-tarask',
	['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'
}

-- Lookup for standard language codes (used in Incubator) to language codes used
-- in dbnames in Wikimedia/Wikidata
local lang2db_map = {
	['be-tarask'] = 'be_x_old',
	['gsw'] = 'als',
	['lzh'] = 'zh_classical',
	['nan'] = 'zh_min_nan',
	['nb'] = 'no',
	['nrf'] = 'nrm',
	['rup'] = 'roa_rup',
	['sgs'] = 'bat_smg',
	['vro'] = 'fiu_vro',
	['yue'] = 'zh_yue'
}

-- Mapping of Incubator test wiki project codes to database suffixes (used in Wikidata)
-- and interwiki prefixes.
local project_map = {
	['Wb'] = {'wikibooks', 'b:'},
	['Wn'] = {'wikinews', 'n:'},
	['Wp'] = {'wiki', ''},
	['Wq'] = {'wikiquote', 'q:'},
	['Wt'] = {'wiktionary', 'wikt:'},
	['Wy'] = {'wikivoyage', 'voy:'}
}

-- Mapping of database suffixes and special sitelinks to interface message keys
-- (like MediaWiki:Wikibase-otherprojects-commons) and URLs/URL patterns
local db_map = {
	['wikibooks'] = {'wikibooks', 'https://$1.wikibooks.org/wiki/'},
	['wikinews'] = {'wikinews', 'https://$1.wikinews.org/wiki/'},
	['wiki'] = {'wikipedia', 'https://$1.wikipedia.org/wiki/'},
	['wikiquote'] = {'wikiquote', 'https://$1.wikiquote.org/wiki/'},
	['wikisource'] = {'wikisource', 'https://$1.wikisource.org/wiki/'},
	['wikiversity'] = {'wikiversity', 'https://$1.wikiversity.org/wiki/'},
	['wikivoyage'] = {'wikivoyage', 'https://$1.wikivoyage.org/wiki/'},
	['wiktionary'] = {'wiktionary', 'https://$1.wiktionary.org/wiki/'},
	['commonswiki'] = {'commons', 'https://commons.wikimedia.org/wiki/'},
	['mediawikiwiki'] = {'mediawiki', 'https://mediawiki.org/wiki/'},
	['metawiki'] = {'meta', 'https://meta.wikimedia.org/wiki/'},
	['sourceswiki'] = {'sources', 'https://wikisource.org/wiki/'},
	['specieswiki'] = {'species', 'https://wikispecies.org/wiki/'},
	['wikidatawiki'] = {'wikidata', 'https://www.wikidata.org/wiki/'},
	['wikimaniawiki'] = {'wikimania', 'https://wikimania.wikimedia.org/wiki/'}
}

local function startsWith(str, start)
	return str:sub(1, #start) == start
end

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

-- Check the validity of a page title format, and return the project & langcode if it is valid
local function getPrefix(title)
	local pagenamesplit = mw.text.split(title, '/')
	if mw.ustring.match(pagenamesplit[1], 'W[bnpqty]') and #pagenamesplit >= 3 then
		return { pagenamesplit[1], pagenamesplit[2] }
	else
		return { 'Wp', 'NOLANGUAGECODE' }
	end
end

p.interwiki = function(frame)
	local s = {'[[Category:Maintenance:Pages using the interwiki template]]'}
	local qid = frame.args.qid or frame:getParent().args[1] or frame:getParent().args.qid or ''
	local prefix = getPrefix(mw.title.getCurrentTitle().text)
	local project = prefix[1]
	local thislang = prefix[2]
	thislang = lang2db_map[thislang] or thislang
	if not project then
		return nil
	elseif not mw.ustring.match( qid, '^[Qq][1-9]%d*$' ) then
		s = {'[[Category:Maintenance:Pages with invalid QID in interwiki template]]'}
	else
		local dbsuffix = project_map[project][1]
		local iwprefix = project_map[project][2]
		if qid then
			local entity = mw.wikibase.getEntity(qid)
			if entity and entity.sitelinks then
				for i, j in pairs(entity.sitelinks) do
					if db_map[j.site] or startsWith(j.site, thislang) then
						local sitedbsuffix = db_map[j.site] or db_map[mw.ustring.sub(j.site, #thislang+1)]
						if sitedbsuffix then
							local span = '<span class="wminc-otherprojects" data-project="' .. sitedbsuffix[1] .. '" data-url="' .. mw.ustring.gsub(mw.ustring.gsub(sitedbsuffix[2], '$1', thislang), '_', '-') .. mw.uri.encode(j.title, 'WIKI') .. '"></span>'
							table.insert(s, span)
						end
					elseif endsWith(j.site, dbsuffix) then
						local lang = mw.ustring.gsub(j.site, dbsuffix, '')
						if langcode[lang] or #lang <= 3 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 .. ':' .. iwprefix .. j.title .. ']]'
							-- Special case for the Shawiya Wiktionary;
							-- unfortunately there is no way to link to it via
							-- interlanguage link from anything other than other
							-- Wiktionaries, so we need to skip it.
							if j.site == 'shywiktionary' or j.site == 'btmwiktionary' then
								iwtext = '<!--' .. iwtext .. '-->'
							end
							table.insert(s, iwtext)
						end
					end
				end
			end
		end
	end
	table.sort(s)
	return table.concat(s, '\n')
end

return p