Modul:Homokozó/84.3.90.28/Wikidata
Megjelenés
Homokozó/84.3.90.28/Wikidata[mi ez?] • [dokumentáció: mutat, ] • [tesztek: sikeres: 1, sikertelen: 0, kihagyva: 0 (részletek)]
local p = {}
local i18n = {
["errors"] = {
["property-param-not-provided"] = "Hiányzó ''property='' paraméter.",
["entity-not-found"] = "Nem létező Wikidata-elem.",
["unknown-claim-type"] = "Ismeretlen az állítás típusa.",
["unknown-snak-type"] = "Ismeretlen a snak típusa.",
["unknown-datavalue-type"] = "Ismeretlen az érték típusa.",
["unknown-entity-type"] = "Ismeretlen a Wikidata-elem típusa.",
["unknown-value-module"] = "A ''value-module'' és ''value-function'' paramétert egyszerre kell beállítani.",
["value-module-not-found"] = "A ''value-module'' nem létező modulra mutat.",
["value-function-not-found"] = "A ''value-function'' nem létező funkcióra mutat.",
["globecoordinate-element"] = "Az érték típusa ''globecoordinate'' : kell ''element=latitude'' vagy ''longitude''.",
["invalid-value"] = "Érvénytelen érték",
},
["somevalue"] = "''nem ismert''",
["novalue"] = "''nincs''"
}
local function firstValue(statements)
for _, statement in ipairs(statements) do
if statement.rank == 'preferred' then
return {statement}
end
end
for _, statement in ipairs(statements) do
if statement.rank == 'normal' then
return {statement}
end
end
return {}
end
local function withRank(statements, ranks)
local result = {}
for _i, statement in ipairs(statements) do
for _j, rank in ipairs(ranks) do
if statement.rank == rank then
table.insert(result, statement)
break
end
end
end
return result
end
function getEntityFromId(id)
if id then
return mw.wikibase.getEntityObject(id)
end
return mw.wikibase.getEntityObject()
end
function getEntityIdFromValue(value)
local prefix = ''
if value['entity-type'] == 'item' then
prefix = 'q'
elseif value['entity-type'] == 'property' then
prefix = 'p'
else
return formatError('unknown-entity-type')
end
return prefix .. value['numeric-id']
end
function formatError( key )
return '<strong class="error">' .. i18n.errors[key] .. '</strong>'
end
function formatStatements(options)
local property
if not options.property then
return formatError('property-param-not-provided')
else
property = options.property:upper()
end
--Get entity
local entity = nil
if options.entity and type(options.entity) == "table" then
entity = options.entity
else
entity = getEntityFromId(options.entityId)
end
if not entity then
return '' --TODO error?
end
if not entity.claims or not entity.claims[property] then
return '' --TODO error?
end
local statements = entity.claims[property]
if options.rank == 'preferred' then options.rank = nil end -- FIXME Hack amíg kipucoljuk a sablonokat
if options.rank ~= 'all' then
if not options.rank then
local preferred = withRank(statements, {'preferred'})
if #preferred > 0 then
statements = preferred
else
statements = withRank(statements, {'normal'})
end
elseif options.rank == 'valid' then
statements = withRank(statements, {'normal', 'preferred'})
else
statements = withRank(statements, {options.rank})
end
end
if options.first then
statements = firstValue(statements)
end
--Format statement and concat them cleanly
local formattedStatements = {}
for _, statement in ipairs(statements) do
local fs
if property == 'P1082' then -- population
fs = require('Modul:Wikidata/Population').formatStatement(statement, options)
else
fs = formatStatement(statement, options)
end
if fs then
if options['felsorolás'] == 'lista' then
fs = '* ' .. fs
elseif options['felsorolás'] == 'számozott lista' then
fs = '# ' .. fs
end
table.insert(formattedStatements, fs)
end
end
if options['felsorolás'] == 'lista' or options['felsorolás'] == 'számozott lista' then
return table.concat(formattedStatements, '\n')
elseif options['felsorolás'] == 'sorok' then
return table.concat(formattedStatements, '<br>')
elseif options['felsorolás'] == 'szöveg' then
return mw.text.listToText(formattedStatements)
elseif options.separator or options.conjunction then
options.separator = options.separator and string.gsub(options.separator, ' ', ' ')
options.conjunction = options.conjunction and string.gsub(options.conjunction, ' ', ' ')
return mw.text.listToText(formattedStatements, options.separator, options.conjunction)
else
return table.concat(formattedStatements, '<br>')
end
end
function formatStatement( statement, options )
if not statement.type or statement.type ~= 'statement' then
return formatError( 'unknown-claim-type' )
end
return formatSnak( statement.mainsnak, options )
--TODO reference and qualifiers
end
function formatSnak( snak, options )
if snak.snaktype == 'somevalue' then
return i18n['somevalue']
elseif snak.snaktype == 'novalue' then
return i18n['novalue']
elseif snak.snaktype == 'value' then
return formatDatavalue( snak.datavalue, options )
else
return formatError( 'unknown-snak-type' )
end
end
function formatDatavalue(datavalue, options)
--Use the customize handler if provided
if options['value-module'] or options['value-function'] then
if not options['value-module'] or not options['value-function'] then
return formatError( 'unknown-value-module' )
end
local formatter = require ('Module:' .. options['value-module'])
if formatter == nil then
return formatError( 'value-module-not-found' )
end
local fun = formatter[options['value-function']]
if fun == nil then
return formatError( 'value-function-not-found' )
end
return fun( datavalue.value, options )
end
--Default formatters
if datavalue.type == 'wikibase-entityid' then
return formatEntityId(getEntityIdFromValue(datavalue.value), options)
elseif datavalue.type == 'string' then
return datavalue.value --TODO ids + media
elseif datavalue.type == 'time' then
return formatTimeValue(datavalue.value, options)
elseif datavalue.type == 'globecoordinate' then
if options.element == 'latitude' then
return datavalue.value['latitude']
elseif options.element == 'longitude' then
return datavalue.value['longitude']
else
return formatError( 'globecoordinate-element' )
end
elseif datavalue.type == 'url' then
return datavalue.value['url']
elseif datavalue.type == 'quantity' then
return options.format == 'raw' and datavalue.value.amount or mw.getContentLanguage():formatNum(tonumber(datavalue.value.amount))
elseif datavalue.type == 'monolingualtext' then
return datavalue.value.text
else
return formatError('unknown-datavalue-type')
end
end
function formatEntityId( entityId, options )
local label = mw.wikibase.label( entityId )
local link = mw.wikibase.sitelink( entityId )
if link and options.link ~= 'nem' then
if options.link == 'csak' then
return link
elseif label then
return '[[' .. link .. '|' .. label .. ']]'
else
return '[[' .. link .. ']]'
end
else
return label --TODO what if no links and label + fallback language?
end
end
function formatTimeValue(value, options)
if options.format == 'raw' then
return value.time
else
local time = require('Modul:Time').newFromWikidataValue(value)
if time then
return time:formatDate(options)
else
return formatError('invalid-value') .. ' [[Kategória:Érvénytelen Wikidata-értékre hivatkozó lapok]]'
end
end
end
function p.formatStatements(frame)
local args = {}
if frame == mw.getCurrentFrame() then
args = frame:getParent().args
for k, v in pairs(frame.args) do
args[k] = v
end
else
args = frame
end
--If a value if already set, use it
if args.value and args.value ~= '' then
return args.value ~= '-' and args.value or ''
end
return formatStatements(args)
end
return p