Modul:Homokozó/JulesWinnfield-hu/Wikidata/Population

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

Homokozó/JulesWinnfield-hu/Wikidata/Population[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: létrehozás]

local function formatReference(reference, options)
    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 not args.title and reference.snaks.P1476 then
		for _, snak in ipairs(reference.snaks.P1476) do
			if not args.title or (snak.datavalue and snak.datavalue.value.language == 'hu') then
				args.title = snak.datavalue.value.text
			end
		end
	end

    if args.url and args.title then
        return frame:extensionTag('ref', frame:expandTemplate{title = 'Cite web', args = args}, {name = 'P1082'})
    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, {name = 'P1082'}) or ''
    end
end

local function formatStatement(statement, options)
    local population = tonumber(statement.mainsnak.datavalue.value.amount)
    local text = (population < 10000 and tostring(population) or mw.getContentLanguage():formatNum(population)) .. ' fő'
    
    if statement.qualifiers and statement.qualifiers['P585'] then  -- dátum
        text = text .. ' ' .. mw.text.tag(
            'span', 
            {style = 'font-size:90%; white-space:nowrap;'}, 
            '(' .. formatSnak(statement.qualifiers['P585'][1], {link = 'nem'}) .. ')'
        )
    end
    
    if statement.references then
        local references = {}
        for _, reference in ipairs(statement.references) do
            table.insert(references, formatReference(reference, options))
        end
        
        text = text .. table.concat(references)
    end
    
    return text .. '[[Kategória:Wikidatából megjelenített népesség]]'  -- NOTE: Bevezetés idejére.
end

function formatSnaks(snaks, options)
    local formattedSnaks = {}
    for _, snak in ipairs(snaks) do
        table.insert(formattedSnaks, formatSnak(snak, options))
    end
    return mw.text.listToText(formattedSnaks, options.separator, options.conjunction)
end

return {formatStatement = formatStatement}