Modul:Homokozó/JulesWinnfield-hu/homokozó/Modul:Román település infobox

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

Homokozó/JulesWinnfield-hu/homokozó/Modul:Román település infobox[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: létrehozás]

local wd = require"Modul:Wikidata"

local p = {}

local getArgs = require"Modul:Arguments".getArgs

local function newInfoboxRow(label, value)
    local result = {}
    table.insert(result, '<tr style="vertical-align:top;"><td>\'\'\'')
    table.insert(result, label)
    table.insert(result, '\'\'\'</td><td>')
    table.insert(result, value)
    table.insert(result, '</td></tr>\n')
    return table.concat(result)
end

local function isMunicipalityTitle(title)
	if mw.ustring.match(title, '^.- község$') or mw.ustring.match(title, '^.- község %(.-%)$') or 
		mw.ustring.match(title, '.-%(község%)$') then
		return true
	end
	return false
end

function p.population(frame, args)
	if not args then
		args = getArgs(frame, {wrappers = 'Sablon:Román település infobox'})
	end

    local result = {}
    local fs
    local level = 0
    local subdivisions = 0
    local municipalityTitle = ''
    local item = mw.wikibase.getEntity()
    if item and item.claims and item.claims.P1082 then
		fs = wd.formatStatements(frame, {property = "P1082", format = "default"}, item)

        if item.claims.P31 then
            for _, statement in ipairs(item.claims.P31) do
                local numericId = statement.mainsnak.datavalue.value['numeric-id']
                if numericId == 532 or numericId == 15921247 or numericId == 15921300 then
                    level = 3
                elseif numericId == 659103 or numericId == 16858213 or numericId == 640364 then
                    level = 2
                end
            end
        end
        
        if item.claims.P1383 then
            subdivisions = #item.claims.P1383
        end
        
        if item.claims.P131 then
        	municipalityTitle = mw.wikibase.sitelink('Q' .. item.claims.P131[1].mainsnak.datavalue.value['numeric-id']) or ''
        end
    end
    
    local title = mw.title.getCurrentTitle().text
    local kozseg = level == 2 and isMunicipalityTitle(title)
    local value
    if fs and (level ~= 2 or subdivisions == 1 or kozseg) then
        value = fs
    else
		if level == 2 and item.claims.P36 then
			local itemId = "Q" .. item.claims.P36[1].mainsnak.datavalue.value["numeric-id"]
			value = wd.formatStatements(frame, {property = "P1082", entityId = itemId, format = "default"})
		end
		if not value then
	        if mw.title.new('Sablon:Népesség/' .. title).exists then  -- new() költséges!
	            value = frame:expandTemplate{title = 'Népesség/' .. title}
	        else
	            value = frame:expandTemplate{title = 'Népesség/ismeretlen', args = {title}}
	        end
	    end
    end
    table.insert(result, newInfoboxRow('Népesség', value))
    
    if args['magyar lakosság'] then
        table.insert(result, newInfoboxRow('Magyar&nbsp;lakosság', args['magyar lakosság']))
    end
    
    if level == 2 or not isMunicipalityTitle(municipalityTitle) then
	    local label = 'Község&nbsp;népessége'
	    if fs and level == 2 then
	        if subdivisions ~= 1 and not kozseg and title ~= "Bukarest" then
	            table.insert(result, newInfoboxRow(label, fs))
	        end
	    elseif level == 3 and wd.containsPropertyWithValue(item, "P31", "Q15303838") then
	    	local itemId = "Q" .. item.claims.P131[1].mainsnak.datavalue.value["numeric-id"]
	    	fs = wd.formatStatements(frame, {property = "P1082", entityId = itemId, format = "default"})
			table.insert(result, newInfoboxRow(label, fs))
	    elseif args['község népesség'] then
	        value = args['község népesség']
	        if args['község népesség éve'] then
	            local year = mw.html.create('span')
	            year
	                :css('font-size', '90%')
	                :css('white-space', 'nowrap')
	                :wikitext('(', args['község népesség éve'], ')')
	            value = value .. ' ' .. tostring(year)
	        end
	        if args['község népesség forrás'] or args['község népesség forrásnév'] then
	            local ref = {name = 'ref', content = args['község népesség forrás']}
	            if args['község népesség forrásnév'] then
	                ref.args = {name = args['község népesség forrásnév']}
	            end
	            value = value .. frame:extensionTag(ref)
	        end
	        table.insert(result, newInfoboxRow(label, value))
	    end
    end
    
    return table.concat(result)
end

function p.administrativeTerritorialEntity(frame, args)
	if not args then
		args = getArgs(frame, {wrappers = 'Sablon:Román település infobox'})
	end

	local item = mw.wikibase.getEntityObject()
	if item and item.claims and item.claims.P131 and item.claims.P31 then
		local level
		for _, statement in ipairs(item.claims.P31) do
			local numericId = statement.mainsnak.datavalue.value['numeric-id']
			if numericId == 532 or numericId == 15921247 or numericId == 15921300 then
				level = 3
			elseif numericId == 659103 or numericId == 16858213 or numericId == 640364 then
				level = 2
			end
		end
		if level ~= 3 then
			return ''
		end
		local itemId = 'Q' .. item.claims.P131[1].mainsnak.datavalue.value['numeric-id']
		local title = mw.wikibase.sitelink(itemId)
		if not title or not isMunicipalityTitle(title) then
			return ''
		end
		local label = title
		label = mw.ustring.gsub(label, ' %(.-%)$', '')
		label = mw.ustring.gsub(label, ' község$', '')
		return newInfoboxRow('Község', '[[' .. title .. '|' .. label .. ']]')
	end
	return ''
end

function p.containsSettlement(frame, args)
	local item = mw.wikibase.getEntityObject()
	if item and item.claims and item.claims.P131 and item.claims.P31 then
		local level
		for _, statement in ipairs(item.claims.P31) do
			local numericId = statement.mainsnak.datavalue.value["numeric-id"]
			if numericId == 532 or numericId == 15921247 or numericId == 15921300 then
				level = 3
			elseif numericId == 659103 or numericId == 16858213 or numericId == 640364 then
				level = 2
			end
		end
	    if level == 3 and wd.containsPropertyWithValue(item, "P31", "Q34841063") then
	    	return wd.formatStatements(frame, {property = "P1383", entityId = "Q" .. item.claims.P131[1].mainsnak.datavalue.value["numeric-id"]})
	    end
	end
end

return p