Jump to content

Module:Interwiki

From Wikisource

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

local p = { }

thiswiki = 'sourceswiki' -- the code of this wiki, HAVE TO BE CHANGED WHEN COPYING MODULE
thislanguage = 'mul' -- the code of the present language

-- language-codes with underscore '_', have to be translated to '-', to be readable for MediaWiki
-- add more codes, when new are added to the Wikimedia-family
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'
	}

p.interwiki = function(frame)
	local s = {}
	local qids = frame.args.qid or frame:getParent().args.qid
	if qids then
		local qidtable = mw.text.split(qids,',')
		for q = 1, #qidtable do
			qid = qidtable[q]
			if qid ~= mw.wikibase.getEntityIdForCurrentPage() then
				table.insert(s, '<li id="t-wikibase" style="display:none;">[[d:' .. qid .. '|' .. frame:callParserFunction{ name = 'int', args = 'Wikibase-dataitem' } .. ']]</li>')
			end
			local entity = mw.wikibase.getEntity(qid)
			if entity and entity.sitelinks then
				for i, j in pairs(entity.sitelinks) do
					if j.site ~= thiswiki then -- excludes this wiki
						if mw.ustring.sub( j.site, -10 ) == 'wikisource' then -- excludes non-Wikisource projects
							local lang = langcode[mw.ustring.sub( j.site, 1, -11 )] or mw.ustring.sub( j.site, 1, -11 )
							table.insert(s, '[[' .. lang .. ':' .. j.title .. ']]' ) -- put together a interwiki-link to other projects
						end
					end
				end
			end
		end
	end
	table.insert(s, "[[Category:Pages with interwiki links from Module:Interwiki]]")
	return table.concat(s, '')

end

return p