Modul:Homokozó/Tacsipacsi/Wikidata-népesség

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

Homokozó/Tacsipacsi/Wikidata-népesség[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: létrehozás]

local p = {}

local wikidatamodul = require('Modul:Wikidata')

local function getStatement(entityId)
	local max_rank = 1
	local RANK = wikidatamodul.RANK
	entity = mw.wikibase.getEntityObject(entityId)
	for _, s in pairs(entity.claims['P1082']) do
		if RANK[s.rank] > max_rank then
            max_rank = RANK[s.rank]
        end
    end
	return s
end

local function formatReference(reference, options, template)
    local frame = mw.getCurrentFrame()
    local args = {}
 
    args.url = reference.snaks['P854'] and formatSnak(reference.snaks['P854'][1], options)
    args.title = reference.snaks['P357'] and formatSnak(reference.snaks['P357'][1], options)
    args.author = reference.snaks['P50'] and formatSnak(reference.snaks['P50'][1], options)
    args.date = reference.snaks['P577'] and (reference.snaks['P577'][1].datavalue.value.precision >= 11 and string.sub(reference.snaks['P577'][1].datavalue.value.time, 9, 18) or nil)
    args.year = reference.snaks['P577'] and (reference.snaks['P577'][1].datavalue.value.precision < 11 and string.sub(reference.snaks['P577'][1].datavalue.value.time, 9, 12) or nil)
    args.publisher = reference.snaks['P123'] and formatSnak(reference.snaks['P123'][1], options)
    args.language = reference.snaks['P364'] and formatSnaks(reference.snaks['P364'], {link = 'nem'})
    args.accessdate = reference.snaks['P813'] and string.sub(reference.snaks['P813'][1].datavalue.value.time, 9, 18)
 
    if args.url and args.url:sub(1, 17) == 'http://www.ksh.hu' then
        args.url = args.url:gsub('p_lang=EN', 'p_lang=HU')
    end
	
	if template then
		if args.url and args.title then
			return frame:extensionTag('ref', frame:expandTemplate{title = 'Cite web', args = args})
		else
			local text = ''
			for _, arg in pairs(args) do text = text .. ', ' .. arg end
			text = mw.ustring.sub(text, 3)
			return text ~= '' and frame:extensionTag('ref', text) or ''
		end
	else
		return args
	end
end

local function number(statement, template)
    local num = tonumber(statement.mainsnak.datavalue.value.amount)
    if template then num = tostring(num) .. '[[Kategória:Wikidatából megjelenített népesség]]' --[[ NOTE: Bevezetés idejére. ]] end
	return num
end

local function date(statement, template)
	local date = statement.qualifiers['P585'][1] or nil
	if not date then
		if template then return ''
		else return nil end
	end
	if template then date = formatSnak(date, {link = 'nem'}) end
	return date
end

local function ref(statement, template)
	local ref
	if statement.references then
        local references = {}
        for _, reference in ipairs(statement.references) do
            table.insert(references, formatReference(reference, options, template))
        end
 
        if template then ref = table.concat(references) else ref = references end
    else
    	if template then ref = '' else ref = nil end
    end
end

local function formatTemplate(statement)
	local population = number(statement, false)
	local text = (population < 10000 and tostring(population) or mw.getContentLanguage():formatNum(population)) .. ' fő'

	if date(statement, false) then
		text = text .. ' ' .. mw.text.tag(
            'span', 
            {style = 'font-size:90%; white-space:nowrap;'}, 
            '(' .. date(statement, true) .. ')'
        )
    end
 
    text = text .. ref(statement, true)
    return text
end
 
function p.population(option, statement, template)
	if statement.type == 'string' then statement = getStatement(statement) end
	if option == 'number' or option == 'szám' then return number(statement, template)
	elseif option == 'date' or option == 'year' or option == 'time' or option == 'dátum' or option == 'év' or option == 'idő' or option == 'időpont' then return date(statement, template)
	elseif option == 'ref' or option == 'reference' or option == 'forr' or option == 'forrás' then return ref(statement, template)
	elseif template then return formatTemplate(statement)
	else return {number = number(statement), date = date(statement), ref = ref(statement)} end
end

function p.formatStatement2(frame)
	-- sablonparaméterek megszerzése
    local args = {}
    if frame == mw.getCurrentFrame() then
        args = frame:getParent().args
        for k, v in pairs(frame.args) do
            args[k] = v
        end
    else
        args = frame
    end
    -- ha van sablon második paraméterében megadva érték, visszatérés azzal, ha kötőjel, visszatérés üresen
    if args.value and args.value ~= '' then
        return args.value ~= '-' and args.value or ''
    end
    local entityId = args.entityId
	if entityId == '' then entityId = nil end
    -- egyébként az eggyel feljebbi függvény meghívása template=true értékkel (sablonból hívva, szöveggé alakítja az értékeket)
	return p.population(args[1], entityId, true)
end

function p.formatStatement(statement, args)
	return p.population(args.element, statement, args.templete or true)
end
 
return p