Extension:Wikibase Client/Lua

Wikibase Client provides a Lua Scribunto interface that implements functions to access data from the Wikibase repository, if the Wikibase Client configuration enables. Lua modules and wiki templates can invoke these functions.

Changes to the Wikibase Lua interface are subject to the Stable Interface Policy.

mw.wikibase
mw.wikibase has general Lua functionality for working with Wikibase data.

mw.wikibase.getEntityIdForCurrentPage
Gets the entity ID of the item connected to the current page.
 * wikibase.getEntityIdForCurrentPage

An example call might look like this:

mw.wikibase.getEntityIdForTitle
Takes a string page title in the local wiki and returns the entity ID of the connected item, if it exists. Returns nil if there's no linked item.
 * wikibase.getEntityIdForTitle( pageTitle )

mw.wikibase.getEntity

 * wikibase.getEntity
 * wikibase.getEntity( id )
 * This function is expensive when called with the ID of an entity not connected to the current page. Loading entities doesn't count as expensive if the same entity is loaded twice during a module run. However, due to restrictions in the caching, if more than 14 other entities are loaded inbetween, the entity must be fetched again, which then counts as expensive.

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

An example call might look like this:

mw.wikibase.getEntityObject
Alias for mw.wikibase.getEntity.

mw.wikibase.getBestStatements
Returns a table with the "best" statements matching the given property label or ID on the given entity ID. The definition of "best" is that the function will return "preferred" statements, if there are any, otherwise "normal" ranked statements. It will never return "deprecated" statements. This is what you usually want when surfacing values to an ordinary reader.
 * wikibase.getBestStatements( entityId, propertyLabelOrId )

An example call might look like this:

mw.wikibase.getAllStatements
Returns a table with all statements (including all ranks, even "deprecated") matching the given property label or ID on the given entity ID.
 * wikibase.getAllStatements( entityId, propertyLabelOrId )

An example call might look like this:

mw.wikibase.getEntityUrl
Takes an entity ID and returns the canonical URL to the entity in the repo.
 * wikibase.getEntityUrl
 * wikibase.getEntityUrl( id )

If no ID was specified, the URL of the item connected to the current page will be returned, if the page is connected. An example call might look like this:

mw.wikibase.getLabelWithLang
Like mw.wikibase.label, but has the language the returned label is in as an additional second return parameter.
 * wikibase.getLabelWithLang
 * wikibase.getLabelWithLang( id )

An example call might look like this:

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

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. The label will either be in the Wiki's language (on monolingual wikis) or the user's language (on multilingual Wikis), furthermore language fallbacks will be applied.

See also:
 * mw.wikibase.getLabelWithLang

An example call might look like this:

mw.wikibase.getLabelByLang
Get the label from an entity for a specific language, returns the label as string or nil if it couldn't be found. This doesn't apply any language fallbacks.
 * wikibase.getLabelByLang( id, languageCode )

Note: This should not be used to get the label in the user's language on multilingual wikis, use mw.wikibase.label for that if by any means possible.

An example call might look like this:

mw.wikibase.getDescriptionWithLang
Like mw.wikibase.description, but has the language the returned description is in as an additional second return parameter.
 * wikibase.getDescriptionWithLang
 * wikibase.getDescriptionWithLang( id )

An example call might look like this:

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

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. The description will either be in the Wiki's language (on monolingual wikis) or the user's language (on multilingual Wikis), furthermore language fallbacks will be applied.

See also:
 * mw.wikibase.getDescriptionWithLang

An example call might look like this:

mw.wikibase.sitelink
Takes an entity 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.
 * wikibase.sitelink( itemId )
 * wikibase.sitelink( itemId, globalSiteId )

When globalSiteId is given, the page title on the specified wiki is returned, rather than the one on the local wiki.

An example call might look like this:

mw.wikibase.renderSnak
Renders a serialized Snak value to wikitext escaped plain text. This is useful for displaying References or Qualifiers.
 * wikibase.renderSnak( snakSerialization )

An example call might look like this:

mw.wikibase.formatValue
Renders a serialized Snak value to rich wikitext. This is useful for displaying References or Qualifiers.
 * wikibase.formatValue( snakSerialization )

An example call might look like this:

mw.wikibase.renderSnaks
Renders a list of serialized Snak values to wikitext escaped plain text. This is useful for displaying References or Qualifiers.
 * wikibase.renderSnaks( snaksSerialization )

An example call might look like this:

mw.wikibase.formatValues
Renders a list of serialized Snak values to rich wikitext. This is useful for displaying References or Qualifiers.
 * wikibase.formatValues( snaksSerialization )

An example call might look like this:

mw.wikibase.resolvePropertyId
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.
 * wikibase.resolvePropertyId( propertyLabelOrId )

An example call might look like this:

mw.wikibase.orderProperties
Returns a table with the given property IDs ordered according to the page MediaWiki:Wikibase-SortedProperties.
 * wikibase.orderProperties( tableOfPropertyIds )

An example call might look like this:

mw.wikibase.getPropertyOrder
Returns a table with the order of property IDs as provided by the page MediaWiki:Wikibase-SortedProperties. If the page does not exist, a nil value is returned.
 * wikibase.getPropertyOrder

An example call might look like this:

mw.wikibase.entity
mw.wikibase.entity has general Lua functionality for working with Wikibase entity objects which are essentially an alternative Lua table serialization of the canonical Wikibase/DataModel/JSON serialization.

A Wikibase entity object table for the item which is linked with the current page can be obtained via mw.wikibase.getEntity.

Functions documented as  are available on the global   table; functions documented as   are methods of a Wikibase entity object table.

mw.wikibase.entity.claimRanks

 * mw.wikibase.entity.claimRanks

The table contains a map of all available claim ranks:
 * 1) RANK_TRUTH
 * 2) RANK_PREFERRED
 * 3) RANK_NORMAL
 * 4) RANK_DEPRECATED

This can for example be used like this:

mw.wikibase.entity.create

 * mw.wikibase.create( data )

Create new entity object from given data. mw.wikibase.getEntity should really be used.

Entity object
Wikibase entity objects have a number of properties and methods. As stated above, properties consist of an alternative Lua table serialization of the canonical Wikibase/DataModel/JSON serialization, however, several properties are monitored for fine grained tracking (when enabled). They are documented below (other are basically static data).

mw.wikibase.entity.claims
This includes all statements (including all ranks, even "deprecated") by property ID.
 * entity.claims

See also:
 * entity:getAllStatements to filter statements by property claims

An example call might look like this:

mw.wikibase.entity.labels
This includes all labels by language code.
 * entity.labels

See also:
 * entity:getLabelWithLang to filter labels by language

An example call might look like this:

mw.wikibase.entity.sitelinks
This includes all sitelinks by site ID.
 * entity.sitelinks

See also:
 * entity:getSitelink to filter sitelinks by site ID

An example call might look like this:

mw.wikibase.entity.descriptions
This includes all descriptions by language code.
 * entity.descriptions

See also:
 * entity:getDescriptionWithLang to filter descriptions by language

An example call might look like this:

mw.wikibase.entity.aliases
This includes all aliases by language code.
 * entity.aliases

mw.wikibase.entity:getLabel
Returns the label of the entity in the language given as langCode or in the Wiki's content language (on monolingual wikis) or the user's language (on multilingual wikis).
 * entity:getLabel
 * entity:getLabel( langCode )

Like entity:getLabelWithLang but does not include the language code in the return value.

See also:
 * wikibase.label for similar access by entity ID instead of by entity object when langCode is not supplied
 * wikibase.getLabelByLang for similar access by entity ID instead of by entity object when langCode is supplied

An example call might look like this:

mw.wikibase.entity:getDescription
Returns the description of the entity in the language given as langCode or in the Wiki's content language (on monolingual wikis) or the user's language (on multilingual wikis).
 * entity:getDescription
 * entity:getDescription( langCode )

Like entity:getDescriptionWithLang but does not include the language code in the return value.

See also:
 * wikibase.description for similar access by entity ID instead of by entity object when langCode is not supplied

An example call might look like this:

mw.wikibase.entity:getLabelWithLang
Like entity:getLabel, but has the language the returned label is in as an additional second return parameter.
 * entity:getLabelWithLang
 * entity:getLabelWithLang( langCode )

See also:
 * wikibase.getLabelWithLang for similar access by entity ID instead of by entity object when langCode is not supplied

An example call might look like this:

mw.wikibase.entity:getDescriptionWithLang
Like entity:getDescription, but has the language the returned description is in as an additional second return parameter.
 * entity:getDescriptionWithLang
 * entity:getDescriptionWithLang( langCode )

See also:
 * wikibase.getDescriptionWithLang for similar access by entity ID instead of by entity object when langCode is not supplied

An example call might look like this:

mw.wikibase.entity:getSitelink
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.
 * entity:getSitelink
 * entity:getSitelink( globalSiteId )

See also:
 * wikibase.sitelink for similar access by entity ID instead of by entity object

An example call might look like this:

mw.wikibase.entity:getBestStatements
Get the best statements with the given property label or 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.
 * entity:getBestStatements( propertyLabelOrId )

See also:
 * wikibase.getBestStatements for similar access by entity ID instead of by entity object

An example call might look like this:

mw.wikibase.entity:getAllStatements
Get the all statements with the given property label or id. This includes all statements (including all ranks, even "deprecated") matching the given property label or ID.
 * entity:getAllStatements( propertyLabelOrId )

See also:
 * wikibase.getAllStatements for similar access by entity ID instead of by entity object

An example call might look like this:

mw.wikibase.entity:getProperties
Get a table with all property IDs attached to the item.
 * entity:getProperties

An example call might look like this:

mw.wikibase.entity:formatPropertyValues
Get the values of the Statements with the given property (which is either identified by a property id, or by the label of the property), formatted as wikitext escaped plain text. 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 wikibase.entity.claimRanks).
 * entity:formatPropertyValues( propertyLabelOrId )
 * entity:formatPropertyValues( propertyLabelOrId, acceptableRanks )

See also:
 * wikibase.renderSnaks for similar access by entity ID instead of by entity object

An example call might look like this:

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

mw.wikibase.entity:formatStatements
Like entity:formatPropertyValues, but the returned values will be formatted as rich wikitext, rather than just wikitext escaped plain text.
 * entity:formatStatements( propertyLabelOrId )
 * entity:formatStatements( propertyLabelOrId, acceptableRanks )

See also:
 * wikibase.formatValues for similar access by entity ID instead of by entity object