Modul:Adresa
Vzhled
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
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 |
local wd = require 'Modul:Wikidata'
local lib = require 'Modul:Wikidata/lib'
local getArgs = (require 'Modul:Arguments').getArgs
local p = {}
p.returnString = ""
p.returnTable = {}
local function tobool(v)
return v and ( (type(v)=="number") and (v==1) or ( (type(v)=="string") and (v=="true") ) )
end
--mw.logObject(p.addressParts);
local function appendToReturnString(text, concatChar)
if (p.returnString ~= "") then
p.returnString = p.returnString .. concatChar .. text
else
p.returnString = p.returnString .. text
end
return p.returnString
end
local function getReturnString()
return p.returnString
end
local function addToReturnTable(poradi, typeOfField, text, concatChar)
p.returnTable[poradi] = {text = text, typeOfField=typeOfField, concatChar = concatChar}
end
local function getReturnTable()
return p.returnTable
end
function p.address(frame)
local addressParts = {'ulice', 'cp', 'co', 'obec', 'psc', 'stat', 'misto'};
local defaultValues = {ulice = true, cp=true, co=true, obec=true, psc=true, stat=true, misto = true, addclass=true}
local qualifiers = {obec = 'P159', ulice = 'P669', cp = 'P4856', co = 'P670', psc = 'P281', stat = 'P17', misto = 'P276'}
local options = {};
local poradi = 2
local isStreet = false
local args = getArgs(frame, { removeBlanks = true })
for k,v in pairs(addressParts) do
if (args[v] == nil) then
args[v] = defaultValues[v]
end
end
if (args['addclass'] == nil) then
args['addclass'] = defaultValues['addclass']
end
mw.logObject(args)
--options.id = 'Q11379488';
--options.id = 'Q12039010';
options.property = 'P159';
options.addclass = false
local entity = wd.formatStatementsFromLua(options);
--mw.logObject(entity)
if not (entity == nil) then
for k,v in pairs( addressParts ) do
if (v == 'obec') then
--appendToReturnString(entity, ", ")
addToReturnTable(poradi, v, entity, ", ")
else
if (tobool(tostring(args[v])) == true) then
options.qualifier = qualifiers[v]
options.addClass = false
--mw.logObject(options)
local concat = ''
if (v == 'co' and tobool(tostring(args['cp'])) == true and tobool(tostring(args['co'])) == true ) then
concat = '/'
elseif (v == 'cp' and tobool(tostring(args['ulice'])) == true and tobool(tostring(args['cp'])) == true ) then
concat = ' '
else
concat = ', '
end
--mw.logObject(wd.getQualifier(options));
if (wd.getRawQualifier(options)) then
if (v == 'ulice') then
isStreet = true
end
addToReturnTable(poradi, v, wd.getQualifier(options), concat)
--appendToReturnString(wd.getQualifier(options), concat)
end
--Wikidata.formatStatements(entity);
end
end
poradi = poradi + 1
end
if tobool(tostring(args['stat'])) == true then
local statOptions = {}
statOptions.id = wd.getRawValue(options)
statOptions.property = 'P17'
statOptions.addclass = false
statOptions.limit = 1
local statEntity = wd.formatStatementsFromLua(statOptions);
if not (statEntity == nil) then
addToReturnTable(poradi+1, 'stat', statEntity, ", ")
--appendToReturnString(statEntity, ", ")
end
end
end
--mw.logObject(isStreet)
local preTable = {}
if (not isStreet) then
--city before cp/co
for k,v in pairs(getReturnTable()) do
if (v.typeOfField == 'obec') then
preTable[2] = v
elseif (v.typeOfField == 'misto') then
preTable[999] = v
else
preTable[k + 1] = v
end
--mw.logObject(k)
--mw.logObject(v)
end
else
for k,v in pairs(getReturnTable()) do
if (v.typeOfField == 'misto') then
preTable[999] = v
else
preTable[k + 1] = v
end
--mw.logObject(k)
--mw.logObject(v)
end
end
p.returnTable = preTable
mw.logObject(getReturnTable())
--https://stackoverflow.com/questions/26160327/sorting-a-lua-table-by-key
local t = getReturnTable()
local tkeys = {}
-- populate the table that holds the keys
for k in pairs(t) do table.insert(tkeys, k) end
-- sort the keys
table.sort(tkeys)
-- use the keys to retrieve the values in the sorted order
local lenArray = 0
for _, k in ipairs(tkeys) do
local textToReturnString = ''
if (t[k].typeOfField == 'psc') then
textToReturnString = mw.ustring.gsub(t[k].text, '(%d%d%d)[%s% ]-(%d%d)', '[http://regiony.kurzy.cz/psc/%1%2/ %1 %2]')
elseif (t[k].typeOfField == 'misto') then
textToReturnString = '(' .. t[k].text .. ')'
else
textToReturnString = t[k].text
end
appendToReturnString(textToReturnString, t[k].concatChar)
lenArray = lenArray + 1
end
local cat = ''
if (lenArray > 0) then
cat = '[[Kategorie:Monitoring:Adresa z Wikidat]]'
end
--for k,v in pairs(getReturnTable()) do
-- appendToReturnString(v.text, v.concatChar)
--end
--mw.logObject(getReturnTable())
mw.logObject(table.getn(getReturnTable()))
if (tobool(tostring(args['addclass'])) == true) then
mw.logObject(lib.addWdClass(getReturnString()) .. cat)
return lib.addWdClass(getReturnString()) .. cat;
else
mw.logObject(getReturnString() .. cat)
return getReturnString() .. cat;
end
end
return p