Mòdul:Wikibase: diferència entre les revisions

De la Viquipèdia, l'enciclopèdia lliure
Contingut suprimit Contingut afegit
corr.
item buit a validpropertyid
Línia 66: Línia 66:
function p.validpropertyid(frame)
function p.validpropertyid(frame)
local property = frame.args[1]
local property = frame.args[1]
local entity = mw.wikibase.getEntityObject(frame.args.item)
local item = frame.args.item; if item == '' then item = nil end
local entity = mw.wikibase.getEntityObject(item)
if not entity then return end
if not entity then return end
if not entity.claims then return end
if not entity.claims then return end

Revisió del 21:27, 21 gen 2019

Icona de documentació de mòdul Documentació del mòdul [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]

Mòdul Wikibase (codi · ús · discussió · proves · tests · casos prova | subpàgines · enllaços)

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Mòdul que proporciona funcions bàsiques d'accés a Wikidata. Està copiat de la documentació de mw:Extension:Wikibase Client/Lua. Per a funcions més desenvolupades adaptades a les necessitats de la Viquipèdia, vegeu Mòdul:Wikidades.

Funcions:

id

Retorna l'id de l'element de Wikidata corresponent a la pàgina indicada pel títol o a la pàgina actual. Sintaxi:

{{#invoke:wikibase|id|<títol>}}
{{#invoke:wikibase|id}}

En cas de no existir l'element retorna una cadena buida "".

Per exemple, en la pàgina Ajuda:Japonès {{#invoke:wikibase|id|{{BASEPAGENAME}}}} donarà l'id corresponent a Japonès, el mateix que s'obté en aquesta pàgina amb {{#invoke:wikibase|id}}.

label

Retorna l'etiqueta d'un element donat. Sintaxi:

{{#invoke:wikibase|label}}
{{#invoke:wikibase|label|<id>|<lang>}}

Sense cap paràmetre retorna l'etiqueta de l'element associat a la pàgina actual. Proporcionant l'id retornarà l'etiqueta corresponent. En cas de no existir o ser erroni no retorna res. Amb un segon paràmetre amb un codi de llengua obté l'etiqueta en la llengua especificada. Vegeu {{label}}.

Exemple: {{#invoke:wikibase|label|Q11799}} → Pau

description

Retorna la descripció d'una entitat. Sintaxi:

{{#invoke:wikibase|description}}
{{#invoke:wikibase|description|<id>|<lang>}}

Per defecte retorna la descripció de l'element associat a la pàgina actual. Proporcionant l'id, d'un element Q o propietat P, retornarà la descripció corresponent. Amb un segon paràmetre amb un codi de llengua obté la descripció en la llengua especificada.

Exemple: {{#invoke:wikibase|description|Q11799}} → Error de l'script: la funció "description" no existeix.

getSiteLink

Retorna el títol de pàgina local per un element donat. Sintaxi:

{{#invoke:wikibase|getSiteLink|<id>|<wiki>}}

Sense cap paràmetre retorna la pròpia pàgina. Proporcionant l'id retornarà la pàgina local enllaçada en l'element corresponent. En cas de no existir o ser erroni no retorna res. Amb un segon paràmetre amb un codi de wiki obté la pàgina en el wiki especificat (enwiki, frwiki,... cawiktionary, etc.)

Exemple: {{#invoke:wikibase|getSiteLink|Q11799}} → Error de l'script: la funció "getSiteLink" no existeix.

-- Module:Wikibase
local p = {}

-- Return the item ID of the item linked to the current page.
function p.id(frame)
    entity = mw.wikibase.getEntityObject()
    if entity == nil then
        return "cap"
    end
    return entity.id
end
 
-- Return the label of a given data item, optionally in a given language.
function p.label(frame)
    if frame.args[1] == nil then
        entity = mw.wikibase.getEntityObject()
        if not entity then return nil end
        id = entity.id
    else
        id = frame.args[1]
    end
    if frame.args[2] then
    	return mw.wikibase.getLabelByLang(id, frame.args[2])
    end
    return mw.wikibase.label( id )
end

-- Return the language code of the label of a given data item.
function p.label_lang(frame)
	local id
    if frame.args[1] == nil then
        entity = mw.wikibase.getEntityObject()
        if not entity then return nil end
        id = entity.id
    else
        id = frame.args[1]
    end
    local _, lang = mw.wikibase.getLabelWithLang(id)
    return lang
end

-- Return the local page about a given data item, optionary in a given wiki.
function p.page(frame)
    if frame.args[1] == nil then
        entity = mw.wikibase.getEntityObject()
        if not entity then return nil end
        id = entity.id
    else
        id = frame.args[1]
    end
    return mw.wikibase.sitelink(id, frame.args[2])
end

-- Return the first value of given property of the item linked to the current page.
function p.firstproperty(frame)
    local property = frame.args[1]
    local entity = mw.wikibase.getEntityObject()
    if not entity then return nil end
    if not entity.claims then return nil end
    local hasProp = entity.claims[property]
    if not hasProp then return nil end
    return hasProp[1].mainsnak.datavalue.value
end

-- Expansion of {{#if:{{#property:...}} }}, returns the first value of given property or nil if not isValidEntityId
function p.validpropertyid(frame)
	local property = frame.args[1]
	local item = frame.args.item; if item == '' then item = nil end
	local entity = mw.wikibase.getEntityObject(item)
	if not entity then return end
	if not entity.claims then return end
	local hasProp = entity.claims[property]
	if not hasProp then return end
	if not hasProp[1].mainsnak.datavalue then return end
	if not hasProp[1].mainsnak.datavalue.value.id then return end
	if not mw.wikibase.isValidEntityId(hasProp[1].mainsnak.datavalue.value.id) then return end
	return hasProp[1].mainsnak.datavalue.value.id
end

return p