Modulo:Wikidata link

Versione del 9 mag 2020 alle 20:57 di Valerio Bozzolan (discussione | contributi) (added missing line)
Info Istruzioni per l'uso
Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Wikidata link/man (modifica · cronologia)
Sandbox: Modulo:Wikidata link/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Wikidata link/test (modifica · cronologia · esegui)

Il modulo Wikidata link genera un wikilink ad una voce Wikipedia a partire dal codice di un elemento Wikidata.

Questo template è stato pensato per semplificare la creazione di nuove voci. Infatti, verrà inserito un link rosso se la voce non esiste in Wikipedia.

Il suo uso dovrebbe avvenire mediante {{WikidataRedLink}}.


local p = {}

---
-- Return a wikilink (also a red link)
--
-- Example: [[Universe]] from "Q1"
--
-- Note that the link can be a red link.
-- The label will be used as title in that case.
--
-- @param  {string} entity Entity Q-ID
-- @return {string}        Wikilink to the entity ID
function p._main( entity )

	-- the default URL is just the entity Q-id, for further troubleshooting
	local link = entity

	-- get the sitelink of this entity
	local siteLink = mw.wikibase.getSitelink( entity )

	-- get the label of this entity
	local label = mw.wikibase.getLabel( entity )

	if siteLink then
		-- prepare an internal wikilink to an existing page
		link = string.format( '[[%s]]', siteLink )
	else
		if label then
			-- prepare a red link to an unexisting page
			link = string.format( '[[%s]]', label )
		end	
	end

	-- return the wikilink
	return link
end

---
-- Return a wikilink (also a red link)
--
-- Example: [[Universe]] from "Q1"
--
-- Note that the link can be a red link.
-- The label will be used as title in that case.
--
-- @param  {object} frame
-- @return {string} Wikilink to the entity ID
function p.main( frame )

	-- module to handle arguments
	local getArgs = require( 'Module:Arguments' ).getArgs

	-- arguments passed to the module (or from the template)
	local args = getArgs( frame )

	-- the entity can be the arg 'id' or the 1st argument
	-- anyway it's optional and the current page will be used if nil
	local entity = args.id or args[1]

	-- see the p.main() function reference
	return p._main( entity )
end

return p