Extension:Wikibase Client/Lua

From MediaWiki.org
Jump to: navigation, search

Wikibase Client provides a Lua Scribunto interface that, if activated, can be used to access data from the Wikibase repository.

mw.wikibase[edit | edit source]

In mw.wikibase general Lua functionality for working with Wikibase data can be found.

mw.wikibase.getEntity[edit | edit source]

wikibase.getEntity()
wikibase.getEntity( id )

This function is expensive when called with the id of an entity not connected to the current page.

Gets a mw.wikibase.entity table with data of the Wikibase item requested by id. If no id was given, the item connected to the current page will be returned. Requesting an item by id is only supported if arbitrary access is enabled.

An example call might look like this:

mw.wikibase.getEntity( 'Q42' ) -- Returns a mw.wikibase.entity table for the Item with the id Q42

mw.wikibase.getEntityObject[edit | edit source]

Alias for mw.wikibase.getEntity.

mw.wikibase.label[edit | edit source]

wikibase.label()
wikibase.label( id )
Takes an item ID and returns the label in the language of the local Wiki.

If no ID was specified, then the label of the item connected to the current page will be returned, if the page is indeed connected and a label exists in the wiki's language or in a fallback language.

An example call might look like this:

mw.wikibase.label( 'Q42' ) -- Returns the label of the item as a string, like "Berlin".

mw.wikibase.sitelink[edit | edit source]

wikibase.sitelink( id )
Takes an item ID and returns the title of the corresponding page title on the local Wiki. This page title can be used to link to the given page.

An example call might look like this:

mw.wikibase.sitelink( 'Q42' ) -- Returns the given items page title in the current Wiki as a string, like "Berlin".

mw.wikibase.description[edit | edit source]

wikibase.description()
wikibase.description( id )
Takes an item ID and returns the description in the language of the local Wiki.

If no ID was specified, then the description of the item connected to the current page will be returned, if the page is indeed connected and a description exists in the wiki's language or in a fallback language.

An example call might look like this:

mw.wikibase.description( 'Q42' ) -- Returns the description of the item as a string, like "capital of Germany".

mw.wikibase.renderSnak[edit | edit source]

wikibase.renderSnak( snakSerialization )
Renders a serialized Snak to text. This is useful for displaying References or Qualifiers.

An example call might look like this:

local entity = mw.wikibase.getEntityObject()
local snak = entity['claims']['P342'][1]['qualifiers']['P342'][1]

mw.wikibase.renderSnak( snak ) -- Returns the given Snak formatted as wiki text.

mw.wikibase.renderSnaks[edit | edit source]

wikibase.renderSnaks( snaksSerialization )
Renders a list of serialized Snaks to text. This is useful for displaying References or Qualifiers.

An example call might look like this:

local entity = mw.wikibase.getEntityObject()
local snaks = entity['claims']['P342'][1]['qualifiers']

mw.wikibase.renderSnaks( snaks ) -- Returns the given Snaks formatted as wiki text.

mw.wikibase.resolvePropertyId[edit | edit source]

wikibase.resolvePropertyId( propertyLabelOrId )
Returns a property id for the given label or id. This allows using the property's labels instead of ids in all places. If no property was found for the label, a nil value is returned.

An example call might look like this:

mw.wikibase.resolvePropertyId( 'father' ) -- Returns the property id for the property with label "father", like "P12".

mw.wikibase.entity[edit | edit source]

mw.wikibase.entity represents a Wikibase entity in Lua. A mw.wikibase.entity table for the item which is linked with the current page can be obtained with mw.wikibase.getEntityObject.

Functions documented as mw.wikibase.entity.name are available on the global mw.wikibase.entity table; functions documented as mw.wikibase.entity:name are methods of an mw.wikibase.entity object (see mw.wikibase.entity.create).

mw.wikibase.entity:getLabel[edit | edit source]

entity:getLabel()
entity:getLabel( langCode )
Returns the label of the entity in the language given as langCode or in the Wiki's content language.

An example call might look like this:

entity:getLabel( 'de' ) -- Returns a string, like "Berlin"

mw.wikibase.entity:getSitelink[edit | edit source]

entity:getSitelink()
entity:getSitelink( globalSiteId )
Get the title with which the entity is linked in the current Wiki. If globalSiteId is given, the title the item is linked with in the given Wiki will be returned.

An example call might look like this:

entity:getSitelink() -- Returns the items page title in the current Wiki as a string, like "Moskow"
entity:getSitelink( 'ruwiki' ) -- Returns the items page title in the Russian Wikipedia as a string, like "Москва"

mw.wikibase.entity:getProperties[edit | edit source]

entity:getProperties()
Get a table with all property ids attached to the item.

An example call might look like this:

entity:getProperties() -- Returns a table like: { "P123", "P1337" }

mw.wikibase.entity:getBestStatements[edit | edit source]

entity:getBestStatements( propertyId )
Get the best statements with the given property id. This includes all statements with rank "preferred" or, if no preferred ones exist, all statements with rank "normal". Statements with rank "deprecated" are never included.

An example call might look like this:

entity:getBestStatements( 'P12' ) -- Returns a table containing the serialization of the best statements with the property id P12

mw.wikibase.entity:formatPropertyValues[edit | edit source]

entity:formatPropertyValues( propertyLabelOrId )
entity:formatPropertyValues( propertyLabelOrId, acceptableRanks )
Get the formatted value of the claims with the given property (which is either identified by a property id, or by the label of the property). Per default only the best claims will be returned. Alternatively a table with acceptable ranks can be given as second parameter (a mapping table with all ranks can be found in mw.wikibase.entity.claimRanks).

An example call might look like this:

-- Return a table like: { value = "Formatted claim value", label = "Label of the Property" }
entity:formatPropertyValues( 'P12' )

-- As above, but uses the label of the Property instead of the id
entity:formatPropertyValues( 'father' )

-- Return the normal ranked claims with the property Id 42 (same format as above)
entity:formatPropertyValues( 'P42', { mw.wikibase.entity.claimRanks.RANK_NORMAL } )

value is an empty string ('') if there's no statement with the given property on the entity. value will be nil if the given property doesn't exist.

mw.wikibase.entity.claimRanks[edit | edit source]

The mw.wikibase.entity.claimRanks table contains a map of all available claim ranks.

The available ranks are:

  1. RANK_TRUTH
  2. RANK_PREFERRED
  3. RANK_NORMAL
  4. RANK_DEPRECATED

This can for example be used like this:

-- Return the normal ranked claims with the property id P5
entity:formatPropertyValues( 'P5', { mw.wikibase.entity.claimRanks.RANK_NORMAL } )

 -- Return all claims with id P123 (as the table passed contains all possible claim ranks)
entity:formatPropertyValues( 'P123', mw.wikibase.entity.claimRanks )