Nothing Special   »   [go: up one dir, main page]

Saltar al conteníu

Módulu:Wikibase

De Wikipedia

Usu

Exemplos

  • ID: {{#invoke:Wikibase|id}} → Q8565357
  • Etiqueta(): {{#invoke:Wikibase|label}} → Módulu:Wikibase
  • Etiqueta(Q29): {{#invoke:Wikibase|label|Q29}} → España
  • Etiqueta(q555555): {{#invoke:Wikibase|label|q555555}} → Sam Webb
  • Enlace(): {{#invoke:Wikibase|page}} → Módulu:Wikibase
  • Enlace(Q29): {{#invoke:Wikibase|page|Q29}} → España
  • Enlace(q555555): {{#invoke:Wikibase|page|q555555}} → Sam Webb
  • descripcion(P166): {{#invoke:Wikibase|firstproperty|p166}}

-- 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 "nun s'alcontró'l módulu wikibase"
    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)
	return p.validproperty(frame)
end

function p.validproperty(frame)
	local property = frame.args[1]
	local item = frame.args.item or frame.args.from; if item == '' then item = nil end
	local type = frame.args.type or "id"
	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 type == "value" then return hasProp[1].mainsnak.datavalue.value 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