Modul:Country data

A Wikipédiából, a szabad enciklopédiából

Country data[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: sikeres: 2, sikertelen: 0, kihagyva: 0 (részletek)]

local getArgs = require('Modul:Arguments').getArgs
local countryProperty = 'P17'
local flagProperty = 'P41'
local p = {}

local function getWdData(qId, pId)
	if type(qId) == 'number' then
		qId = 'Q' .. tostring(qId)
	end
	local statements = mw.wikibase.getBestStatements(qId, pId)
	local results = {}
	for _, statement in ipairs(statements) do
		if statement.mainsnak and statement.mainsnak.snaktype == 'value' then
			table.insert(results, statement.mainsnak.datavalue)
		end
	end
	return results
end

function p.getWikidataCountries(id)
	if not id then
		return {}
	end
	local values = getWdData(id, countryProperty)
	local countries = {}
	for _, v in ipairs(values) do
		if v.type == 'wikibase-entityid' and v.value['entity-type'] == 'item' then
			table.insert( countries, tonumber(v.value['numeric-id']) )
		end
	end
	return countries
end

function p.getWikitextCountries(param)
	if type(param) == 'table' then       -- param might be a table like this:
		param = table.concat(param, '/') --   { 'Q1 / Q28', 'USA / Germany' }
	end                                  -- then the result is
	if param == '' then                  --   { 1, 28, 'USA', 'Germany' }
		return {} -- because of e.g. a blank table
	end
	if type(param) == 'string' then
		param = mw.text.split(param, '/', true)
	elseif type(param) == 'number' then
		return { param } -- probably one Wikidata ID directly
	else
		return {}
	end
	local result = {}
	for _, v in ipairs(param) do
		v = mw.text.trim(v)
		local match = tonumber(string.match(v, '^Q?(%d+)$'))
		if match then
			table.insert(result, match)
		else
			v = string.match(v, '%[%[(.+)%|?.*%]%]') or v
			table.insert(result, v)
		end
	end
	return result
end

local function generateRows(countries, size)
	size = size or '22x20px'
	local function cdRow(name) -- country data row
		if type(name) ~= 'string' then
			return nil
		end
		local titleObject = mw.title.new( 'Sablon:Country data ' .. name )
		if type(titleObject) ~= 'table' or not titleObject.exists then
			return nil
		end
		return mw.getCurrentFrame():expandTemplate{ title = "Zászló2", args = { 
			name, size = size } }
	end
	local function wikidataRow(id)
		id = 'Q' .. tostring(id)
		local title = mw.wikibase.sitelink( id )
		local label = mw.wikibase.label( id )
		if label then label = mw.getContentLanguage():ucfirst(label) end
		local link
		if title and label then
			link = string.format( '[[%s|%s]]', title, label )
		elseif title then
			link = string.format( '[[%s]]', title )
		elseif label then
			link = label
		else
			link = ''
		end
		local values = getWdData(id, flagProperty)
		if not values[1] then
			return cdRow(title) or cdRow(label) or (link ~= '' and link or nil)
		end

		return string.format(
			'<span class="flagicon" style="white-space: nowrap;">[[Fájl:%s|%s|keret|alt=|link=]] </span>%s',
			values[1].value, size, link
		)
	end
	local result = {}
	for _, v in ipairs(countries) do
		local row
		if type(v) == 'number' then
			row = wikidataRow(v)
		else
			row = cdRow(v)
		end
		if row then
			table.insert(result, row)
		end
	end
	result = table.concat( result, '<br />\n' )
	return result ~= '' and result or nil
end

function p.main(frame)
	local args = getArgs(frame)
	if args[1] == '-' then
		return nil
	end
	local countries
	if type(args[1]) == 'table' then
		countries = p.getWikitextCountries(args[1])
	else
		countries = {}
		for n, v in pairs(args) do
			if type(n) == 'number' then
				table.insert(countries, v)
			end
		end
		countries = p.getWikitextCountries(countries)
	end
	if not countries[1] then
		countries = p.getWikidataCountries(args.id or mw.wikibase.getEntityIdForCurrentPage())
	end
	if not countries[1] then
		return nil
	end
	return generateRows(countries, args.size)
end

return p