local i18n = {
["errors"] = {
["property-param-not-provided"] = "Parametro ''property'' non fornito.",
["entity-not-found"] = "Entità non trovata.",
["unknown-claim-type"] = "Tipo asserzione sconosciuta.",
["unknown-snak-type"] = "Tipo di snak sconosciuto.",
["unknown-datavalue-type"] = "Tipo di dato sconosciuto.",
["unknown-entity-type"] = "Tipo di entità sconosciuta.",
["unknown-value-module"] = "Devi impostare entrambi i parametri: ''value-module'' e ''value-function''.",
["value-module-not-found"] = "Modulo indicato da ''value-module'' non trovato.",
["value-function-not-found"] = "Funzione indicata da ''value-function'' non trovata."
},
["somevalue"] = "''valore sconosciuto''",
["novalue"] = "''nessun valore''"
}
function getEntityFromId( id )
if id then
return mw.wikibase.getEntity( id )
end
return mw.wikibase.getEntity()
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 '<span class="error">' .. i18n.errors[key] .. '</span>'
end
function formatStatements( options )
if not options.property then
return formatError( 'property-param-not-provided' )
end
--Get entity
local entity = getEntityFromId( options.entityId )
if not entity then
return -- formatError( 'entity-not-found' )
end
if (entity.claims == nil) or (not entity.claims[string.lower(options.property)]) then
return '' --TODO error?
end
--Format statement and concat them cleanly
local formattedStatements = {}
local list_end
if options.list or options.orderedlist then
if options.list then
formattedStatements[1] = '<ul><li>'
list_end = '</li></ul>'
else
formattedStatements[1] = '<ol><li>'
list_end = '</li></ol>'
end
options.separator = '</li><li>'
options.conjunction = options.separator
end
for i, statement in pairs( entity.claims[string.lower(options.property)] ) do
table.insert( formattedStatements, formatStatement( statement, options ) )
end
if list_end then table.insert(formattedStatements, list_end) end
return mw.text.listToText( formattedStatements, options.separator, options.conjunction )
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
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 then
if label then
return '[[' .. link .. '|' .. label .. ']]'
else
return '[[' .. link .. ']]'
end
else
return label --TODO what if no links and label + fallback language?
end
end
local p = {}
function p.formatStatements( frame )
local args = frame.args
--If a value if already set, use it
if args.value and args.value ~= '' then
return args.value
end
return formatStatements( frame.args )
end
function p.N(frame)
property = string.lower(frame.args[1])
entity = mw.wikibase.getEntity()
if not entity then return 0 end
if not entity.claims then return 0 end
if not entity.claims[property] then return 0 end
local count = 0
for _ in pairs(entity.claims[property]) do count = count + 1 end
return count
end
return p