Modul:Homokozó/JulesWinnfield-hu/homokozó/Modul:Wikidata
Megjelenés
Homokozó/JulesWinnfield-hu/homokozó/Modul:Wikidata[mi ez?] • [dokumentáció: mutat, ] • [tesztek: létrehozás]
local p = {}
local getArgs = require('Modul:Arguments').getArgs
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, item)
if not options.property then
return formatError('property-param-not-provided')
end
local property = options.property:upper()
--Get entity
local entity = item
if not entity then
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 ~= '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' and options.format == 'default' 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
local function formatQualifiers(statement, options)
local result, startDate, endDate = {}
for key, snaks in pairs(statement.qualifiers) do
if key == 'P580' then
startDate = formatSnak(snaks[1], {link = 'nem'})
elseif key == 'P582' then
endDate = formatSnak(snaks[1], {link = 'nem'})
else
for _, snak in ipairs(snaks) do
table.insert(result, formatSnak(snak, {link = 'nem'}))
end
end
end
if startDate and startDate ~= '' or endDate and endDate ~= '' then
table.insert(result, 1, (startDate or '') .. '–' .. (endDate or ''))
end
return table.concat(result, ', ')
end
local function formatSnaks(snaks, options)
local formattedSnaks = {}
for _, snak in ipairs(snaks) do
table.insert(formattedSnaks, formatSnak(snak, options))
end
return mw.text.listToText(formattedSnaks, options.separator, options.conjunction)
end
local function formatReference(reference, options)
if reference.snaks.P854 and (reference.snaks.P357 or reference.snaks.P1476) then -- url and title
local args = {}
args.url = reference.snaks['P854'] and formatSnak(reference.snaks['P854'][1], options)
args.title = reference.snaks['P357'] and formatSnak(reference.snaks['P357'][1], options)
args.author = reference.snaks['P50'] and formatSnak(reference.snaks['P50'][1], options)
args.date = reference.snaks['P577'] and (reference.snaks['P577'][1].datavalue.value.precision >= 11 and
string.sub(reference.snaks['P577'][1].datavalue.value.time, 9, 18) or nil)
args.year = reference.snaks['P577'] and (reference.snaks['P577'][1].datavalue.value.precision < 11 and
string.sub(reference.snaks['P577'][1].datavalue.value.time, 9, 12) or nil)
args.publisher = reference.snaks['P123'] and formatSnak(reference.snaks['P123'][1], options)
args.language = reference.snaks['P364'] and formatSnaks(reference.snaks['P364'], {link = 'nem'})
args.accessdate = reference.snaks['P813'] and string.sub(reference.snaks['P813'][1].datavalue.value.time, 9, 18)
if args.url and args.url:sub(1, 17) == 'http://www.ksh.hu' then
args.url = args.url:gsub('p_lang=EN', 'p_lang=HU')
end
if not args.title and reference.snaks.P1476 then
for _, snak in ipairs(reference.snaks.P1476) do
if not args.title or (snak.datavalue and snak.datavalue.value.language == 'hu') then
args.title = snak.datavalue.value.text
end
end
end
return mw.getCurrentFrame():expandTemplate{title = 'Cite web', args = args}
else
local result = {}
for _, referenceSnaks in pairs(reference.snaks) do
for _, snak in ipairs(referenceSnaks) do
mw.log('Wikidata: DATA_TYPE ' .. snak.datavalue.type)
table.insert(result, formatSnak(snak, snak.datatype == 'time' and {link = 'nem'} or options))
end
end
return table.concat(result, ', ')
end
end
function formatStatement(statement, options)
if not statement.type or statement.type ~= 'statement' then
return formatError('unknown-claim-type')
end
local result = formatSnak(statement.mainsnak, options)
--TODO reference and qualifiers
if result and result ~= '' then
if options.showQualifiers and statement.qualifiers then
local formattedQualifiers = formatQualifiers(statement, options)
if formattedQualifiers and formattedQualifiers ~= '' then
result = result .. ' <small>(' .. formattedQualifiers .. ')</small>'
end
end
if options.showReferences and statement.references then
local frame = mw.getCurrentFrame()
for _, reference in ipairs(statement.references) do
local formattedReference = formatReference(reference, options)
if formattedReference and formattedReference ~= '' then
result = result .. frame:extensionTag('ref', formattedReference, {name = reference.hash})
end
end
end
end
return result
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 and label ~= link and mw.ustring.sub(link, 1, 10) ~= 'Kategória:' 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, args, item)
if not args then
args = getArgs(frame, {removeBlanks = false})
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, item)
end
return p