Modul:Román település infobox

Ez a lap műszaki védett
A Wikipédiából, a szabad enciklopédiából

Román település infobox[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: létrehozás]

A Wikidatában keverednek a településekhez és a közigazgatási egységekhez kapcsolt szócikkek. A legtöbb esetben, a nyelvi hivatkozások a településekhez vannak kötve, de a városoknál és a községeknél a közigazgatási egységhez, ezért hol itt, hol ott kell megjeleníteni. Mind a kettő egyelőre nem elérhető.

Használat: Sablon:Román település infobox

require('strict')
local wd = require 'Modul:Wikidata'
local getArgs = require 'Modul:Arguments'.getArgs
local tabular = require('Modul:Tabular data')

local p = {}

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))
    
	local magyarLakossag
	if level == 2 then
		magyarLakossag = tabular._lookup {
			search_column = 'itemId',
			output_column = 'col3',
			search_value = item.id,
			'Romania/RPL 2021/Tab 2.2.2.tab'
		}
	end
	if magyarLakossag then
		local total = tabular._lookup {
			search_column = 'itemId',
			output_column = 'col1',
			search_value = item.id,
			'Romania/RPL 2021/Tab 2.2.2.tab'
		}
		magyarLakossag = ('%s (%.0f%%, 2021)%s'):format(
			frame:expandTemplate { title = 'szám', args = { magyarLakossag } },
			magyarLakossag / total * 100,
			frame:extensionTag('ref', frame:expandTemplate { title = 'cite web', args = {
				url = 'https://www.recensamantromania.ro/rezultate-rpl-2021/rezultate-definitive/',
				title = '2021-es romániai népszámlálás',
				accessdate = '2024-01-21',
				year = '2023',
				publisher = 'Nemzeti Statisztikai Intézet',
				language = 'ro'
			} }))
	end
	if not magyarLakossag then
		magyarLakossag = args['magyar lakosság']
	end
	if magyarLakossag then
		table.insert(result, newInfoboxRow('Magyar lakosság', magyarLakossag))
	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)
	local itemId = mw.wikibase.getEntityIdForCurrentPage()
	if not itemId then
		return nil
	end
	local st = mw.wikibase.getBestStatements(itemId, 'P1383')
	if not next(st) then
		return nil
	end
	local p36 = mw.wikibase.getBestStatements(itemId, 'P36')
	if not next(p36) then
		return nil
	end
	local seat = p36[1].mainsnak.datavalue.value.id
	local settlements = {}
	for _, st in ipairs(st) do
		if st.mainsnak.datavalue.value.id ~= seat then
			table.insert(settlements, wd.formatStatement(st))
		end
	end
	if not next(settlements) then
		return nil
	end
	local data
	if #settlements == 1 then
		data = settlements[1]
	else
		table.sort(settlements, function(a, b)
			local function extractSortKey(s)
				if s:sub(1, 2) == '[[' then
					s = s:sub(3)
				end
				if s:sub(-2) == ']]' then
					s = s:sub(1, -3)
				end
				local i = s:find('|', 1, true)
				if i then
					s = s:sub(i + 1)
				end
				return s
			end
			return extractSortKey(a) < extractSortKey(b)
		end)
		data = frame:expandTemplate { title='Plainlist', args={ '\n*' .. table.concat(settlements, '\n*') .. '\n' } }
		if #settlements > 6 then
			data = frame:expandTemplate { title='Show', args={ 'Lista', data } }
		end
	end
	return newInfoboxRow('Beosztott falvak', data)
end

return p