Diferencia entre revisiones de «Módulo:Wikidata»
Apariencia
Sin resumen de edición |
Sin resumen de edición |
||
| Línea 1: | Línea 1: | ||
--[[********************************************************************************* | |||
* Nombre: Módulo:Wikidata | |||
* | |||
* Descripción: Este módulo devuelve el valor o valores con o sin formato específico | |||
* a una propiedad de Wikidata. | |||
* | |||
* Fecha última revisión: 1 de febrero de 2014. | |||
* | |||
* Estado: En uso. | |||
* | |||
*********************************************************************************`-- ]] | |||
--[[ ========================================================================= | |||
Mensajes de error | |||
========================================================================= `-- ]] | |||
local i18n = { | local i18n = { | ||
["errors"] = { | ["errors"] = { | ||
| Línea 14: | Línea 30: | ||
["novalue"] = "''sin valor''" | ["novalue"] = "''sin valor''" | ||
} | } | ||
--[[ ========================================================================= | |||
Función para identificar el ítem y la propiedad | |||
========================================================================= `-- ]] | |||
function getEntityFromId( id ) | function getEntityFromId( id ) | ||
if id then | if id then | ||
| Línea 38: | Línea 57: | ||
end | end | ||
--[[ ========================================================================= | |||
Función para recopilar los valores y crear la cadena que devolverá | |||
========================================================================= `-- ]] | |||
function formatStatements( options ) | function formatStatements( options ) | ||
if options.property == 'precisión' or options.property == 'latitud' or options.property == 'longitud' then | if options.property == 'precisión' or options.property == 'latitud' or options.property == 'longitud' then | ||
| Línea 78: | Línea 100: | ||
return mw.text.listToText( formattedStatements, options.separator, options.conjunction ) | return mw.text.listToText( formattedStatements, options.separator, options.conjunction ) | ||
end | end | ||
--[[ ========================================================================= | |||
========================================================================= `-- ]] | |||
function formatStatement( statement, options ) | function formatStatement( statement, options ) | ||
| Línea 87: | Línea 113: | ||
--TODO reference and qualifiers | --TODO reference and qualifiers | ||
end | end | ||
--[[ ========================================================================= | |||
Función que comprueba el tipo de dato (snak) | |||
========================================================================= `-- ]] | |||
function formatSnak( snak, options ) | function formatSnak( snak, options ) | ||
if snak.snaktype == 'somevalue' then | if snak.snaktype == 'somevalue' then | ||
| Línea 99: | Línea 129: | ||
end | end | ||
end | end | ||
--[[ ========================================================================= | |||
Función que establece el tipo de formato en función del tipo de valor | |||
(datavalue.type) y en caso de solicitarse un formato complemetario asocia | |||
el módulo donde se establece el formato y la función de este que lo establece | |||
========================================================================= `-- ]] | |||
function formatDatavalue( datavalue, options ) | function formatDatavalue( datavalue, options ) | ||
--Use the customize handler if provided | --Use the customize handler if provided | ||
| Línea 139: | Línea 175: | ||
end | end | ||
end | end | ||
--[[ ========================================================================= | |||
Damos formato a los enlaces internos | |||
========================================================================= `-- ]] | |||
function formatEntityId( entityId, options ) | function formatEntityId( entityId, options ) | ||
local label = mw.wikibase.label( entityId ) | local label = mw.wikibase.label( entityId ) | ||
| Línea 158: | Línea 198: | ||
local p = {} | local p = {} | ||
--[[ ========================================================================= | |||
Función principal | |||
========================================================================= `-- ]] | |||
function p.formatStatements( frame ) | function p.formatStatements( frame ) | ||
marco = frame | marco = frame | ||
Revisión del 18:29 1 feb 2014
La documentación para este módulo puede ser creada en Módulo:Wikidata/doc
--[[*********************************************************************************
* Nombre: Módulo:Wikidata
*
* Descripción: Este módulo devuelve el valor o valores con o sin formato específico
* a una propiedad de Wikidata.
*
* Fecha última revisión: 1 de febrero de 2014.
*
* Estado: En uso.
*
*********************************************************************************`-- ]]
--[[ =========================================================================
Mensajes de error
========================================================================= `-- ]]
local i18n = {
["errors"] = {
["property-param-not-provided"] = "Parámetro de la propiedad no proporcionado.",
["entity-not-found"] = "Entrada no encontrada.",
["unknown-claim-type"] = "Tipo de notificación desconocida.",
["unknown-snak-type"] = "Tipo de snak desconocido.",
["unknown-datavalue-type"] = "Tipo de dato desconocido.",
["unknown-entity-type"] = "Tipo de entrada desconocido.",
["unknown-value-module"] = "Debe ajustar ambos parámetros de valor y el valor del módulo de funciones.",
["value-module-not-found"] = "No se ha encontrado el módulo apuntado por value-module.",
["value-function-not-found"] = "No se ha encontrado la función apuntada por value-function."
},
["somevalue"] = "''valor desconocido''",
["novalue"] = "''sin valor''"
}
--[[ =========================================================================
Función para identificar el ítem y la propiedad
========================================================================= `-- ]]
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
--[[ =========================================================================
Función para recopilar los valores y crear la cadena que devolverá
========================================================================= `-- ]]
function formatStatements( options )
if options.property == 'precisión' or options.property == 'latitud' or options.property == 'longitud' then
propiedad = 'p625'
else
propiedad = options.property
end
if not propiedad 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(propiedad)]) then
return '' --TODO error?
end
--Declaración de formato y concatenado limpio
local propiedad = {}
if options.property == 'latitud' or options.property == 'longitud' or options.property == 'precisión' then
propiedad = 'p625'
else
propiedad = options.property
end
local cadena = entity.claims[string.lower(propiedad)]
local s = {} -- Esto es para que el índice 0 pase a ser 1. Necesario para que todo quede en el mismo orden que en ítem
for k, v in pairs(cadena) do
s[k+1] = v
end
local formattedStatements = {}
for i, statement in pairs(s) do
table.insert( formattedStatements, formatStatement( statement, options ) )
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
--[[ =========================================================================
Función que comprueba el tipo de dato (snak)
========================================================================= `-- ]]
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
--[[ =========================================================================
Función que establece el tipo de formato en función del tipo de valor
(datavalue.type) y en caso de solicitarse un formato complemetario asocia
el módulo donde se establece el formato y la función de este que lo establece
========================================================================= `-- ]]
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 options.property == 'latitud' then --Para las coordenadas
return datavalue.value['latitude']
elseif options.property == 'longitud' then
return datavalue.value['longitude']
elseif options.property == 'precisión' then
return datavalue.value['precision']
elseif datavalue.type == 'wikibase-entityid' then
return formatEntityId( getEntityIdFromValue( datavalue.value ), options )
elseif datavalue.type == 'string' then -- tipo cadena
return datavalue.value --TODO ids + media
elseif datavalue.type == 'quantity' then -- tipo cantidad
return mw.ustring.gsub(datavalue.value['amount'], '+','').. '±' .. datavalue.value['unit']
elseif datavalue.value['latitude'] and datavalue.value['longitude'] then -- tipo coordenadas
return marco:preprocess('{{coord|' .. datavalue.value['latitude'] .. '|' ..
datavalue.value['longitude'] .. '|globe:' .. options.cuerpo .. '_type:' .. options.tipo .. '|display=' ..
options.display ..'|format=' .. options.formato..'}}')
else
return formatError( 'unknown-datavalue-type' )
end
end
--[[ =========================================================================
Damos formato a los enlaces internos
========================================================================= `-- ]]
function formatEntityId( entityId, options )
local label = mw.wikibase.label( entityId )
local link = mw.wikibase.sitelink( entityId )
if link and options['link'] ~= 'no' then
if label then
return '[[' .. link .. '|' .. label .. ']]'
else
return '[[' .. link .. ']]'
end
elseif label then
return label --TODO what if no links and label + fallback language?
else
return ''
end
end
local p = {}
--[[ =========================================================================
Función principal
========================================================================= `-- ]]
function p.formatStatements( frame )
marco = frame
local args = frame.args
local valorwikidata = formatStatements( frame.args )
if args.prioridad == 'sí' and valorwikidata ~= '' then
return formatStatements( frame.args )
elseif args.value and args.value ~= '' then
return args.value
else
return formatStatements( frame.args )
end
end
return p