| Contenidos de Wikipedia en español bajo licencia CC BY-SA 4.0 ⇔ Mapas de OpenStreetMap bajo licencia ODbL |
Diferencia entre revisiones de «Módulo:Wikidata/Formato lugar»
De Hispanopedia
mejor usar la frame en vez de las opciones |
permitir obtener el país a una fecha; si el lugar ha pertenecido a varios países tomar el actual si es posible en vez del primero definido en Wikidata |
||
| Línea 31: | Línea 31: | ||
return modulo[pFuncion] | return modulo[pFuncion] | ||
end | |||
function obtenerOcurrenciaAFecha(tabla, fecha) | |||
if not tabla or not fecha then | |||
return | |||
end | |||
local elementoTabla = funcion('Módulo:Tablas', 'elemento') | |||
local fechaInicio, fechaFin | |||
--if true then return require('Módulo:Tablas').tostring(tabla) end | |||
-- | |||
for k,v in pairs(tabla) do | |||
fechaInicio = elementoTabla(v,'qualifiers','P580',1,'datavalue','value','time') | |||
fechaFin = elementoTabla(v,'qualifiers','P582',1,'datavalue','value','time') | |||
if fechaInicio and fechaFin then | |||
if fecha >= fechaInicio and | |||
fecha <= fechaFin then | |||
return k | |||
end | |||
elseif fechaInicio then | |||
if fecha >= fechaInicio then | |||
return k | |||
end | |||
elseif fechaFin then | |||
if fecha <= fechaFin then | |||
return k | |||
end | |||
end | |||
end | |||
end | |||
function obtenerOcurrenciaRangoPreferente(tabla) | |||
if not tabla then | |||
return | |||
end | |||
for k,v in pairs(tabla) do | |||
if v.rank == 'preferred' then | |||
return k | |||
end | |||
end | |||
end | end | ||
| Línea 36: | Línea 79: | ||
local entidadTerritorialAdministrativaEnlazada | local entidadTerritorialAdministrativaEnlazada | ||
local idPais, enlacePais, etiquetaPais, paisEnlazado | local idPais, enlacePais, etiquetaPais, paisEnlazado | ||
local fecha | |||
-- Validar que está informado el id del lugar. | -- Validar que está informado el id del lugar. | ||
if not valor or not valor['numeric-id'] then | if not valor or not valor['numeric-id'] then | ||
return | return | ||
end | |||
if opciones then | |||
fecha = opciones.fecha | |||
end | end | ||
| Línea 76: | Línea 124: | ||
etiquetaLugar = elementoTabla(entidad,'labels','es','value') | etiquetaLugar = elementoTabla(entidad,'labels','es','value') | ||
lugarEnlazado = enlazar(enlaceLugar, etiquetaLugar, idLugar, opciones) | lugarEnlazado = enlazar(enlaceLugar, etiquetaLugar, idLugar, opciones) | ||
-- | |||
-- Obtener el país del lugar. | |||
-- | |||
local paisesLugar = elementoTabla(entidad,'claims','P17') | |||
if paisesLugar then | |||
-- Primero se intenta con el país en la fecha de las opciones, luego | |||
-- el país con rango máximo (el actual) y luego el primer país. | |||
local ocurrenciaPais = obtenerOcurrenciaAFecha(paisesLugar, fecha) or | |||
obtenerOcurrenciaRangoPreferente(paisesLugar) or | |||
1 | |||
enlacePais, etiquetaPais, idPais = obtenerDatos(elementoTabla(paisesLugar,ocurrenciaPais,'mainsnak','datavalue','value')) | |||
paisEnlazado = enlazar(enlacePais, etiquetaPais, idPais) | |||
end | |||
end | end | ||
Revisión del 19:50 11 mar 2018
La documentación para este módulo puede ser creada en Módulo:Wikidata/Formato lugar/doc
local p = {}
function p.formatoLugar(valor, opciones, frame, calificativos)
-- Función que devuelve algo de la forma Lugar, entidad territorial administrativa, país
-- Guardamos en las opciones una tabla con los módulos utilizados para hacer menos accesos.
--
function funcion(pModulo, pFuncion)
local modulos, modulo
if not pModulo or not pFuncion then
return
end
if not frame then
return require(pModulo)[pFuncion]
end
modulos = frame['módulos']
if not modulos then
modulos = {}
frame['módulos'] = modulos
end
modulo = modulos[pModulo]
if not modulo then
modulo = require(pModulo)
modulos[pModulo] = modulo
end
return modulo[pFuncion]
end
function obtenerOcurrenciaAFecha(tabla, fecha)
if not tabla or not fecha then
return
end
local elementoTabla = funcion('Módulo:Tablas', 'elemento')
local fechaInicio, fechaFin
--if true then return require('Módulo:Tablas').tostring(tabla) end
--
for k,v in pairs(tabla) do
fechaInicio = elementoTabla(v,'qualifiers','P580',1,'datavalue','value','time')
fechaFin = elementoTabla(v,'qualifiers','P582',1,'datavalue','value','time')
if fechaInicio and fechaFin then
if fecha >= fechaInicio and
fecha <= fechaFin then
return k
end
elseif fechaInicio then
if fecha >= fechaInicio then
return k
end
elseif fechaFin then
if fecha <= fechaFin then
return k
end
end
end
end
function obtenerOcurrenciaRangoPreferente(tabla)
if not tabla then
return
end
for k,v in pairs(tabla) do
if v.rank == 'preferred' then
return k
end
end
end
local idLugar, enlaceLugar, etiquetaLugar, lugarEnlazado
local entidadTerritorialAdministrativaEnlazada
local idPais, enlacePais, etiquetaPais, paisEnlazado
local fecha
-- Validar que está informado el id del lugar.
if not valor or not valor['numeric-id'] then
return
end
if opciones then
fecha = opciones.fecha
end
local elementoTabla = funcion('Módulo:Tablas' , 'elemento')
local enlazar = funcion('Módulo:Wikidata/Formatos', 'enlazar')
local obtenerDatos = funcion('Módulo:Wikidata/Formatos', 'obtenerDatos')
-- Obtener primero la entidad territorial administrativa y el país de los calificativos si es posible
-- No se obtiene primero el lugar, que sería lo más lógico, porque se obtendrá de forma diferente según
-- estén o no informados los calificativos
if calificativos then
entidadTerritorialAdministrativaEnlazada = enlazar(obtenerDatos(elementoTabla(calificativos,'P131',1,'datavalue','value')))
enlacePais, etiquetaPais, idPais = obtenerDatos(elementoTabla(calificativos,'P17',1,'datavalue','value'))
paisEnlazado = enlazar(enlacePais, etiquetaPais, idPais)
end
if paisEnlazado then
enlaceLugar, etiquetaLugar, idLugar = obtenerDatos(valor)
lugarEnlazado = enlazar(enlaceLugar, etiquetaLugar, idLugar)
else
-- Si el lugar no tiene el calificativo de país obtenerlo de la propiedad país de la
-- entidad de Wikidata del lugar.
-- De momento no se obtiene la entidad administrativa (tiene unas barbaridades...)
local entidad
idLugar = 'Q' .. valor['numeric-id']
entidad = mw.wikibase.getEntityObject(idLugar)
if not entidad then
return
end
enlaceLugar = elementoTabla(entidad,'sitelinks','eswiki','title')
etiquetaLugar = elementoTabla(entidad,'labels','es','value')
lugarEnlazado = enlazar(enlaceLugar, etiquetaLugar, idLugar, opciones)
--
-- Obtener el país del lugar.
--
local paisesLugar = elementoTabla(entidad,'claims','P17')
if paisesLugar then
-- Primero se intenta con el país en la fecha de las opciones, luego
-- el país con rango máximo (el actual) y luego el primer país.
local ocurrenciaPais = obtenerOcurrenciaAFecha(paisesLugar, fecha) or
obtenerOcurrenciaRangoPreferente(paisesLugar) or
1
enlacePais, etiquetaPais, idPais = obtenerDatos(elementoTabla(paisesLugar,ocurrenciaPais,'mainsnak','datavalue','value'))
paisEnlazado = enlazar(enlacePais, etiquetaPais, idPais)
end
end
if idLugar == idPais then -- Si el lugar es un país, por ejemplo, estados Unidos, en Wikidata tiene la propiedad país informada con Estados Unidos.
return lugarEnlazado
else
local separadosPorComa = funcion('Módulo:Formato texto', 'separadosPorComa')
return separadosPorComa({lugarEnlazado, entidadTerritorialAdministrativaEnlazada, paisEnlazado})
end
end
--*****************************************************************************
-- Alias de formatos para utilizar en la plantilla propiedad
--*****************************************************************************
p['lugar'] = p['formatoLugar']
return p