Extension:Wikibase Client/Lua

Lua was introduced as a scripting language for MediaWiki end users, including Wikipedia editors. Lua scripting was rolled it out to all WMF wikis on 13 March 2013. An introductory tutorial to the language and its use on Mediawiki is available.

The WikibaseClient extension that provides the access to wikidata.org from the client wikis (i.e. Wikipedias that can pull data from Wikidata) introduced several functions for Lua scripting with Wikidata.

Available functions
Functions are available under the mw.wikibase namespace in Lua.

These functions are available with the most recent install:


 * mw.wikibase.getEntity
 * gets entity data of the Wikidata item connected with the current page. See the section Entity table and data structure for an example of the structure returned.''


 * mw.wikibase.label( id )
 * takes an item ID and returns the label in the language of the local wiki.


 * mw.wikibase.sitelink( id )
 * takes an item ID and returns the title of the corresponding page on the local wiki.

Functions that take id as a parameter will check if that id is a valid id (i.e. "Q" or "P" followed by numbers). An invalid id will cause an internal Scribunto error that cannot be caught by Lua.

Examples
In order to get Ada Lovelace's father, you'd access the property p107 in the claims table. This is the property ID of "father".

Note the use of mw.wikibase.label to return the name of her father rendered in the language and script of the local wiki. The return value would be "Lord Byron" on enwiki, "George Gordon Byron" on dewiki, and "조지 고든 바이런" on kowiki.

Note also that lists in claim tables start at index 0, rather than 1. Thus, if you iterate over the table using, the loop will skip the first claim associated with the property. Instead, iterate over the table manually, e.g..

In order to use code like this in your wiki, it needs to be wrapped in a Scribunto module and put in the respective module namespace on the client wiki. Below is a basic example of this:

A page on the client wiki may use wikitext to invoke the functions in Module:Wikibase like this:

Lua Test

 * ID:
 * Label:
 * Label(Q2099):
 * Label(q555555):
 * Label(xyz):
 * Page:
 * Page(Q7259):
 * Page(q555555):
 * Page(xyz):

Entity table and data structure
Calling mw.wikibase.getEntity returns a table of serialized data from the Wikibase repository (i.e. Wikidata) that can be accessed and traversed. The entity returned is always the one connected to the current page. A call to mw.wikibase.getEntity from Ada Lovelace will thus return data from Q7259.

The table returned to Lua may look like this internally (formatted and abridged for readability): aliases: de: 1:        language: de         value: Augusta Ada Byron 2:        language: de         value: Ada Byron 3:        language: de         value: Ada King 4:        language: de         value: Augusta Ada King 5:        language: de         value: Augusta Ada King, Countess of Lovelace en: 1:        language: en         value: Augusta Ada Byron 2:        language: en         value: Lady Ada 3:        language: en         value: Augusta Ada King, Countess of Lovelace 4:        language: en         value: Ada Byron 5:        language: en         value: Augusta Ada King 6:        language: en         value: Ada King fr: 1:        language: fr         value: Augusta Ada King, comtesse Lovelace 2:        language: fr         value: Anna Byron claims: p107: 1:        id: q7259$FEE5069C-DF8E-4355-A973-2B0B8BC339A8 mainsnak: datavalue: type: wikibase-entityid value: entity-type: item numeric-id: 215627 property: p107 snaktype: value references: 0:              hash: 827686a10b6e2126886dbc6755a3027fb443996 snaks: p143: 0:                       snaktype: value property: p143 datavalue: type: wikibase-entityid value: numeric-id: 48952 entity-type: item qualifiers: p459: 0:                 snaktype: value property: p459 datavalue: type: wikibase-entityid value: numeric-id: 670933 entity-type: item rank: normal type: statement p18: 1:        id: q7259$A78C2B21-2A10-4E77-B6ED-CC278204F55A mainsnak: datavalue: type: string value: Ada lovelace.jpg property: p18 snaktype: value rank: normal type: statement 2:        id: q7259$21DC3BE7-1BFB-4835-A5AF-95A4C81BA99B mainsnak: datavalue: type: string value: Ada Lovelace 1838.jpg property: p18 snaktype: value rank: normal type: statement p19: 1:        id: q7259$F0E062F0-3D91-42FB-A3E2-5AE99BA489F6 mainsnak: datavalue: type: wikibase-entityid value: entity-type: item numeric-id: 84 property: p19 snaktype: value rank: normal type: statement p20: 1:        id: q7259$24690922-EC83-4EA0-88AF-1918E72B2CF6 mainsnak: datavalue: type: wikibase-entityid value: entity-type: item numeric-id: 1633759 property: p20 snaktype: value rank: normal type: statement p21: 1:        id: q7259$E032DC24-810D-41EA-9777-0961A2AEFC3A mainsnak: datavalue: type: wikibase-entityid value: entity-type: item numeric-id: 43445 property: p21 snaktype: value rank: normal type: statement p22: 1:        id: q7259$DB006E49-122B-4238-981C-5C1D7AF63CE9 mainsnak: datavalue: type: wikibase-entityid value: entity-type: item numeric-id: 5679 property: p22 snaktype: value rank: normal type: statement p25: 1:        id: q7259$F0E0A2C1-76DC-43F0-8DA0-D0E0A5FF39BE mainsnak: datavalue: type: wikibase-entityid value: entity-type: item numeric-id: 272161 property: p25 snaktype: value rank: normal type: statement p26: 1:        id: q7259$ECE4FB4E-A402-42FA-939C-109FF2838FA2 mainsnak: datavalue: type: wikibase-entityid value: entity-type: item numeric-id: 4426480 property: p26 snaktype: value rank: normal type: statement p9: 1:        id: q7259$8D78EC59-0C49-47A3-B603-A9C61A6FD6CC mainsnak: datavalue: type: wikibase-entityid value: entity-type: item numeric-id: 2837843 property: p9           snaktype: value rank: normal type: statement descriptions: de: language: de     value: englische Mathematikerin en: language: en     value: English mathematician, considered the first computer programmer fr: language: fr     value: Mathématicienne britannique id: q7259 labels: de: language: de     value: Ada Lovelace en: language: en     value: Ada Lovelace fr: language: fr     value: Ada Lovelace sitelinks: dewiki: site: dewiki title: Ada Lovelace enwiki: site: enwiki title: Ada Lovelace frwiki: site: frwiki title: Ada Lovelace title: Q7259 type: item