Մոդուլ:Interwiki

Վիքիպեդիայից՝ ազատ հանրագիտարանից
Jump to navigation Jump to search

local p = { }

thiswiki = 'hywiki' -- the code of this wiki, HAVE TO BE CHANGED WHEN COPYING MODULE
thislanguage = 'hy' -- 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 have to be added when new lines are added
	['zh_yue']       = 'zh-yue'
	}

p460 = function(entity) -- access the first valid value of P460
	if entity and entity.claims and entity.claims["P460"] then
		for i, j in pairs(entity:getBestStatements( "P460" )) do
			if j.mainsnak.snaktype == 'value' then
				return 'Q' .. j.mainsnak.datavalue.value['numeric-id']
			end
		end
	end
	return nil
end

p2959 = function(entity) -- access the first valid value of P2959
	if entity and entity.claims and entity.claims["P2959"] then
		for i, j in pairs(entity:getBestStatements( "P2959" )) do
			if j.mainsnak.snaktype == 'value' then
				return 'Q' .. j.mainsnak.datavalue.value['numeric-id']
			end
		end
	end
	return nil
end

p.interwiki = function(frame)
	local s = {}
	local entity = mw.wikibase.getEntity()
	local qid = frame.args.qid or frame:getParent().args.qid or p460(entity) or p2959(entity) -- uses parameter qid of the module if it exists, otherwise follow P460 or P2959
	if frame.args.qid == '' or frame:getParent().args.qid == '' then
		qid = p460(entity) or p2959(entity)
	end
	if qid then
		local entity2 = mw.wikibase.getEntity(qid)
		if entity2 and entity2.sitelinks then
			for i, j in pairs(entity2.sitelinks) do
				if j.site ~= thiswiki and j.site ~= 'wikidatawiki' and j.site ~= 'commonswiki' and j.site ~= 'specieswiki' and j.site ~= 'metawiki' and j.site ~= 'mediawikiwiki' then -- excludes the own wiki and some wikiprojects that are not Wikipedia, even if their code ends with 'wiki'
					if mw.ustring.sub( j.site, mw.ustring.len(j.site) - 3 ) == 'wiki' then -- excludes Wikisource, Wikiquote, Wikivoyage etc
						local lang = langcode[mw.ustring.sub( j.site, 1, mw.ustring.len(j.site) - 4 )] or mw.ustring.sub( j.site, 1, mw.ustring.len(j.site) - 4 )
						if (entity and not entity.sitelinks[j.site]) or not entity then -- excludes interwiki to projects that already have sitelinks in the present page
							table.insert(s, '[[' .. lang .. ':' .. j.title .. ']]' ) -- put together a interwiki-link to other projects
						end
					end
				end
			end
		end
	end
	return table.concat(s, '')

end

return p