Ugrás a tartalomhoz

Modul:Csehországi járás települései

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

Csehországi járás települései[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: létrehozás]

require('strict')
local wd = require"Modul:Wikidata"
local p = {}
local cats = {}
local district

local function renderTrackingCategories()
	local result = {}
	for _, cat in ipairs(cats) do
		table.insert(result, "[[Kategória:" .. cat .. "]]")
	end
	return table.concat(result)
end

local function formatError(msg)
	return '<strong class="error">Csehországi járás települései: ' .. msg .. '</strong>' .. 
		'[[Kategória:Figyelmet igénylő Wikidata-adatok]]'
end

local function isOfType(itemId, typeId)
	for _, s in ipairs(mw.wikibase.getAllStatements(itemId, 'P31')) do
		if s.rank ~= 'deprecated' and s.mainsnak.snaktype == 'value' then
			local datavalue = s.mainsnak.datavalue
			if datavalue.type == 'wikibase-entityid' and datavalue.value.id == typeId then
				return true
			end
		end
	end
	return false
end

local function getDistrict(itemId, level)
	if level == 0 then
		return nil
	end
	for _, s in ipairs(mw.wikibase.getBestStatements(itemId, 'P131')) do
		if district then
			return
		end
		if s.mainsnak.snaktype == 'value' then
			local statementItemId = s.mainsnak.datavalue.value.id
			if isOfType(statementItemId, 'Q548611') then
				district = s
				return
			end
			getDistrict(statementItemId, level - 1)
		end
	end
end

local function getMunicipalities(itemId, level)
	if level == 0 then
		return {}
	end
	local municipalityStatements = {}
	for _, s in ipairs(mw.wikibase.getBestStatements(itemId, 'P150')) do
		if s.mainsnak.snaktype == 'value' then
			local statementItemId = s.mainsnak.datavalue.value.id
			if isOfType(statementItemId, 'Q5153359') then
				table.insert(municipalityStatements, s)
			else
				for _, statement in ipairs(getMunicipalities(statementItemId, level - 1)) do
					table.insert(municipalityStatements, statement)
				end
			end
		end
	end
	return municipalityStatements
end

function p.main(frame)
	local districtId, title
	local item = mw.wikibase.getEntity()
	if not item then
		return nil
	end
	if wd.containsPropertyWithValue(item, "P31", "Q5153359") or wd.containsPropertyWithValue(item, "P31", "Q15978299") then
		getDistrict(item.id, 4)
		if not district then
			return '[[Kategória:Figyelmet igénylő Wikidata-adatok]]'
		end
		districtId = district.mainsnak.datavalue.value.id
		title = wd.formatStatement(district)
		if not title then
			title = mw.wikibase.getLabelByLang(districtId, 'cs')
			if not title then
				title = "járás"
			else
				title = title .. " járás"
			end
		end
	elseif wd.containsPropertyWithValue(item, "P31", "Q548611") then
		districtId = item.id
		title = item:getLabel()
		if not title then
			title = item:getSitelink()
		end
	else
		return nil
	end
	local municipalitySnaks = getMunicipalities(districtId, 3)
	if #municipalitySnaks == 0 then
		return nil
	end
	local municipalities = {}
	for _, s in ipairs(municipalitySnaks) do
		table.insert(municipalities, wd.formatStatement(s))
	end
	local label = title
	if mw.ustring.sub(label, 1, 2) == "[[" then
		local i = mw.ustring.find(label, "|")
		label = mw.ustring.sub(label, i and i + 1 or 3)
	end
	local nevelo
	if mw.ustring.find("ČŠŽ", mw.ustring.sub(label, 1, 1), 1, true) then
		nevelo = "A"
	else
		nevelo = "A" .. require"Modul:Nyelvtani modul".az{label}:sub(2)
	end
	title = nevelo .. " " .. title .. " települései"
	return require"Modul:Navbox"._navbox{
		['navbar'] = "plain",
		['cím'] = title,
		['listaosztály'] = "hlist",
		['lista1'] = '\n* ' .. table.concat(municipalities, '\n* ') .. '\n'
	} .. renderTrackingCategories()
end

return p