Modul:Adresa

Tato stránka je zamčena
Z Wikipedie, otevřené encyklopedie

Modul pro práci s adresou.

Použití

Do šablony umístěte {{#invoke:Adresa|address}} (přebírá z Wikidat) nebo {{#invoke:Adresa|addressFromArgs}} (přebírá z parametrů šablony).

Parametry

Parametry
Funkce parametr hodnoty popis
address ulice true nebo false řídí zobrazení ulice
cp true nebo false řídí zobrazení čp.
co true nebo false řídí zobrazení čo.
obec true nebo false řídí zobrazení města
stat true nebo false řídí zobrazení státu
psc true nebo false řídí zobrazení psč
misto true nebo false řídí zobrazení místa
addressFromArgs ulice wikitext
cp wikitext
co wikitext
obec wikitext
stat wikitext
psc wikitext
misto wikitext
--[[
Testovací stránky:
* Life RESCUE - záchranáři Praha (Q28949271)
* Muzeum Vysočiny Třebíč (Q12039010)
* Český západ (Q11379488)
* Alternátor - Ekotechnické centrum Třebíč (Q21522633)
* Dulwich Picture Gallery (Q1241163)
]]

local getArgs = (require 'Modul:Arguments').getArgs
local bools = {
	['1'] = true,
	['ano'] = true,
	['true'] = true,
	['yes'] = true,
}

local p = {}

local function tobool(v)
	return bools[tostring(v)] == true
end

local function concatParts(data)
	local parts = {}
	local ulice = data.ulice
	local ret, numbers, psc
	if ulice then
		numbers = {}
		if data.cp then
			table.insert(numbers, data.cp)
		end
		if data.co then
			table.insert(numbers, data.co)
		end
		if #numbers > 0 then
			table.insert(parts, ulice .. ' ' .. table.concat(numbers, '/'))
		else
			table.insert(parts, ulice)
		end
	end
	if data.obec then
		if data.cp and not ulice then
			table.insert(parts, data.obec .. ' ' .. data.cp)
		else
			table.insert(parts, data.obec)
		end
	end
	psc = data.psc
	if psc then
		psc = mw.ustring.gsub(psc, ' ', ' ')
		psc = mw.ustring.gsub(psc, '(%d%d%d)%s-(%d%d)', '%1 %2')
		table.insert(parts, psc)
	end
	if data.stat then
		table.insert(parts, data.stat)
	end
	ret = table.concat(parts, ', ')
	if data.misto then
		if #parts > 0 then
			ret = mw.ustring.format('%s (%s)', ret, data.misto)
		else
			ret = data.misto
		end
	end
	return ret
end

function p._address(args)
	local wd = require 'Modul:Wikidata'
	local lib = require 'Modul:Wikidata/lib'
	local defaultValues = {ulice = true, cp = true, co = true, obec = true, psc = true, stat = true, misto = true}
	local qualifiers = {ulice = 'P669', cp = 'P4856', co = 'P670', psc = 'P281', stat = 'P17', misto = 'P276'}
	local options = {
		id = args.id,
		property = 'P159',
		rank = 'best',
		limit = 1,
		showqualifier = {},
	}
	for k, v in pairs(defaultValues) do
		if not args[k] then
			args[k] = v
		end
	end
	for k, v in pairs(qualifiers) do
		if tobool(args[k]) then
			table.insert(options.showqualifier, v)
		end
	end

	local ret, parts
	local data = wd.getStatementsWithData(options)
	--mw.logObject(data)
	for _, st in ipairs(data) do
		parts = {}
		if tobool(args.obec) then
			parts.obec = st[1]
		end
		for k, v in pairs(qualifiers) do
			parts[k] = st[v]
		end
		if not parts.stat and tobool(args.stat) then
			parts.stat = wd.formatStatementsFromLua{
				id = options.id,
				of = 'P159',
				property = 'P17',
				addclass = false,
				limit = 1,
				rank = 'best',
			}
		end
		if next(parts) ~= nil then
			ret = concatParts(parts)
			break
		end
	end
	if ret then
		return lib.addWdClass(ret) .. '[[Kategorie:Monitoring:Adresa z Wikidat]]'
	end
	return nil
end

function p.address(frame)
	return p._address(getArgs(frame, { removeBlanks = true })) or ''
end

function p.addressFromArgs(frame)
	return concatParts(getArgs(frame, { parentFirst = true, removeBlanks = true })) or ''
end

return p