Módulo:Enlace externo

De Hispanopedia

La documentación para este módulo puede ser creada en Módulo:Enlace externo/doc

local p = {}

-- Patrones de URL locales, o sea el valor de P1630 de cada propiedad.
-- POR QUÉ: Wikibase está inerte en Hispanopedia (las consultas Lua a Wikidata
-- devuelven vacío, ver CLAUDE.md), así que el módulo no puede obtener el patrón
-- por sí mismo y estas plantillas quedaban con error de Lua en 431 páginas.
-- PARA AÑADIR UNA PROPIEDAD: mirar su P1630 en Wikidata y añadir la línea aquí.
-- Consultados en Wikidata el 2026-08-08.
local PatronesLocales = {
	P1146 = 'https://worldathletics.org/athletes/-/$1',                                   -- World Athletics
	P1984 = 'https://www.animenewsnetwork.com/encyclopedia/manga.php?id=$1',               -- Anime News Network (manga)
	P1985 = 'https://www.animenewsnetwork.com/encyclopedia/anime.php?id=$1',               -- Anime News Network (anime)
	P2002 = 'https://x.com/$1',                                                            -- Twitter (X)
	P2003 = 'https://www.instagram.com/$1/',                                               -- Instagram
	P2013 = 'https://www.facebook.com/$1',                                                 -- Facebook
	P2696 = 'https://gymnastics.sport/site/athletes/bio_detail.php?id=$1&type=licence',    -- FIG
	P3180 = 'https://vndb.org/$1',                                                         -- VNDB
	P3789 = 'https://t.me/s/$1',                                                           -- Telegram
}
local ModuloArgumentos = require('Módulo:Arguments')
local ModuloWikidata = require('Module:Wikidata')
local ModuloPaginas = require('Módulo:Páginas')
local argumentos
local Entidad

function p.Plantilla(frame)
	argumentos = ModuloArgumentos.getArgs(frame, { trim = true })
	Entidad = mw.wikibase.getEntityObject(argumentos.id) or mw.wikibase.getEntityObject(argumentos.entidad) or {}
	local Titulo = argumentos['título'] or argumentos['titulo'] or obtenerEtiquetaWikidata() or ModuloPaginas.nombrePagina({desambiguar='sí'})
	local Imagen
	if not argumentos['imagen'] or argumentos['imagen'] == 'no' or argumentos['imagen'] == '' then
		Imagen = ''
	elseif argumentos['imagen'] == 'sí' or argumentos['imagen'] == 'defecto' then
		Imagen = '[[File:Internet-web-browser.svg|16px|link=|alt=Icono]] '
	elseif argumentos['imagen'] then
		Imagen = '[[File:' .. argumentos['imagen'] .. '|16px|link=|alt=Icono]] '
	end
	local Nombre
	if argumentos['nombre'] and argumentos['enlacenombre'] then
		Nombre = ' en [[' .. frame:preprocess('{{desenlazar|' .. argumentos['enlacenombre'] .. '}}') .. '|' .. frame:preprocess('{{desenlazar|' .. argumentos['nombre'] .. '}}') ..']]'
	elseif argumentos['nombre'] then
		Nombre = ' en ' .. argumentos['nombre']
	else
		Nombre = ''
	end
	-- Definición de URL
	local URL
	local Patron = argumentos['patrón'] or argumentos['patron']
	local Propiedad = argumentos['propiedad']
	local Id = argumentos['identificador']
	local Nota = argumentos['nota']
	if Nota then
		Nota = ' (' .. Nota .. ')'
	else
		Nota = ''
	end
	if Patron then
		URL = Patron
	elseif Propiedad then
		-- Se intenta primero Wikidata y, si no hay dato, se cae a la tabla local.
		-- La cadena va protegida paso a paso: con Wikibase inerte esto devolvía
		-- nil y el módulo reventaba al indexar .claims (mismo estilo de guarda
		-- que ya usan obtenerEtiquetaWikidata y propiedad más abajo).
		local PatronWikidata
		local EntidadPropiedad = mw.wikibase.getEntityObject(Propiedad)
		local Reclamaciones = EntidadPropiedad and EntidadPropiedad.claims
		local Declaracion = Reclamaciones and Reclamaciones.P1630 and Reclamaciones.P1630[1]
		local Snak = Declaracion and Declaracion.mainsnak
		local Valor = Snak and Snak.datavalue
		PatronWikidata = ( Valor and Valor.value ) or PatronesLocales[Propiedad]
		if PatronWikidata then
			URL = PatronWikidata
		else
			return '<span class="error">Error: sin patrón en la propiedad de Wikidata ' .. Propiedad .. '.</span>'
		end
	else
		return '<span class="error">Error: debe pasar una propiedad o un patrón como parámetro.</span>'
	end
	if Id then
		URL = mw.ustring.gsub(URL, '%$1', Id)
	elseif Propiedad then
		local IdWikidata = propiedad(Propiedad, {['linkback']='no', ['uno']='sí'}) or ''
		if IdWikidata and IdWikidata ~= '' then
			URL = mw.ustring.gsub(URL, '%$1', IdWikidata)
		else
			return '<span class="error">Error: sin identificador en la propiedad de Wikidata ' .. Propiedad .. '.</span>'
		end
	else
		return '<span class="error">Error: debe pasar al menos un identificador o una propiedad como parámetro junto al patrón.</span>'
	end
	local Prefijo = argumentos['prefijo'] or ''
	if Prefijo ~= '' then
		Prefijo = Prefijo .. ' '
	end
	local Sufijo = argumentos['sufijo'] or ''
	if Sufijo ~= '' then
		Sufijo = ' ' .. Sufijo
	end
	return Imagen .. Prefijo .. '['.. URL .. ' ' .. Titulo .. ']' .. Sufijo .. Nombre .. Nota
end

function obtenerEtiquetaWikidata()
	if Entidad and Entidad.labels and Entidad.labels.es then
		return Entidad.labels.es.value
	end
end

function propiedad(idPropiedad,opciones)
	if Entidad and Entidad.claims and Entidad.claims[idPropiedad] then
		if not opciones then
			opciones = {['linkback']='sí'}
		end
		opciones.entityId  = Entidad.id
		opciones.propiedad = idPropiedad
		valorPropiedad = ModuloWikidata.getPropiedad(opciones,Entidad.claims[idPropiedad])
		return valorPropiedad
	end
end

return p