Extension:Wikibase Client/Lua

Recently, 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 Wikibase extension that powers both wikidata.org and 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


 * mw.wikibase.label( id )
 * takes an ID and returns the label in the language of the client Wiki


 * mw.wikibase.sitelink( id )
 * takes an ID and returns the link with the element

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 for readability): entities: q7259: 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 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 es: language: es           value: matemu00e1tica britu00e1nica considerada el primer programador de la historia fr: language: fr           value: Mathu00e9maticienne britannique it: language: it           value: matematica inglese, considerata la prima programmatrice ru: language: ru           value: u0410u043du0433u043bu0438u0447u0430u043du043au0430-u043cu0430u0442u0435u043cu0430u0442u0438u043a id: q7259 labels: ar: language: ar           value: u0622u062fu0627 u0644u0648u0641u0644u0627u064au0633 az: language: az           value: Ada Lavleys be: language: be           value: u0410u0434u0430 u041bu0430u045eu043bu0435u0439u0441 bg: language: bg           value: u0410u0434u0430 u041bu044au0432u043bu0435u0439u0441 bn: language: bn           value: u0985u09cdu09afu09beu09a1u09be u09b2u09beu09adu09b2u09c7u09b8 bs: language: bs           value: Ada Lovelace ca: language: ca           value: Ada Lovelace cs: language: cs           value: Ada Lovelace da: language: da           value: Ada Lovelace de: language: de           value: Ada Lovelace de-ch: language: de-ch value: Ada Lovelace de-formal: language: de-formal value: Ada Lovelace el: language: el           value: u0386u03bdu03c4u03b1 u039bu03acu03b2u03bbu03b5u03cau03c2 en: language: en           value: Ada Lovelace en-ca: language: en-ca value: Ada Lovelace en-gb: language: en-gb value: Ada Lovelace eo: language: eo           value: Ada Lovelace es: language: es           value: Ada Lovelace et: language: et           value: Ada Lovelace eu: language: eu           value: Ada Lovelace fa: language: fa           value: u0627u06ccu062fu0627 u0644u0627u0648u0644u06ccu0633 fi: language: fi           value: Ada Lovelace fr: language: fr           value: Ada Lovelace ga: language: ga           value: Ada Lovelace gl: language: gl           value: Ada Augusta Lovelace gv: language: gv           value: Ada Lovelace he: language: he           value: u05e2u05d3u05d4 u05dcu05d0u05d1u05dcu05d9u05d9u05e1 hr: language: hr           value: Ada Lovelace ht: language: ht           value: Ada Lovelace hu: language: hu           value: Ada Lovelace hy: language: hy           value: u0531u0564u0561 u053cu0561u057eu056cu0565u0575u057d id: language: id           value: Ada Lovelace is: language: is           value: Ada Lovelace it: language: it           value: Ada Lovelace ja: language: ja           value: u30a8u30a4u30c0u30fbu30e9u30d6u30ecu30b9 ko: language: ko           value: uc5d0uc774ub2e4 ub7ecube0cub808uc774uc2a4 la: language: la           value: Ada Lovelace lb: language: lb           value: Ada Lovelace lt: language: lt           value: Ada Lovelace lv: language: lv           value: Ada Lavleisa ml: language: ml           value: u0d05u0d21 u0d32u0d35u0d4d u0d32u0d47u0d38u0d4d mt: language: mt           value: Ada Lovelace nb: language: nb           value: Ada Byron Lovelace ne: language: ne           value: u090fu0921u093e u0932u092du0932u0947u0938 nl: language: nl           value: Ada Lovelace no: language: no           value: Ada Byron Lovelace oc: language: oc           value: Ada Lovelace pl: language: pl           value: Ada Lovelace pms: language: pms value: Ada Byron King pt: language: pt           value: Ada Lovelace pt-br: language: pt-br value: Ada Lovelace ro: language: ro           value: Ada Lovelace ru: language: ru           value: u041bu0430u0432u043bu0435u0439u0441, u0410u0434u0430 sk: language: sk           value: Augusta Ada Kingovu00e1 sq: language: sq           value: Ada Lovelace sr: language: sr           value: u0415u0458u0434u0430 u041au0438u043du0433 u041bu0430u0432u043bu0435u0458u0441 sv: language: sv           value: Ada Lovelace ta: language: ta           value: u0b85u0b9fu0bbe u0bb2u0bb5u0bcdu0bb2u0bc7u0bb8u0bcd tg: language: tg           value: u0410u0434u0430 u041bu043eu0432u043bu0435u0439u0441 th: language: th           value: u0e40u0e2du0e14u0e32 u0e44u0e1au0e23u0e2du0e19 tr: language: tr           value: Ada Lovelace uk: language: uk           value: u0410u0434u0430 u041bu0430u0432u043bu0435u0439u0441 vi: language: vi           value: Ada Lovelace yo: language: yo           value: Ada Lovelace zh: language: zh           value: u611bu9054u00b7u52d2u8299u857eu7d72 zh-min-nan: language: zh-min-nan value: Ada Lovelace lastrevid: 13508531 modified: 2013-03-19T15:17:11Z ns: 0 pageid: 8463 sitelinks: afwiki: site: afwiki title: Ada Lovelace arwiki: site: arwiki title: u0622u062fu0627 u0644u0648u0641u0644u0627u064au0633 azwiki: site: azwiki title: Ada Lavleys bewiki: site: bewiki title: u0410u0434u0430 u041bu0430u045eu043bu0435u0439u0441 bgwiki: site: bgwiki title: u0410u0434u0430 u041bu044au0432u043bu0435u0439u0441 bnwiki: site: bnwiki title: u0985u09cdu09afu09beu09a1u09be u09b2u09beu09adu09b2u09c7u09b8 bswiki: site: bswiki title: Ada Lovelace cawiki: site: cawiki title: Ada Lovelace cswiki: site: cswiki title: Ada Lovelace dawiki: site: dawiki title: Ada Lovelace dewiki: site: dewiki title: Ada Lovelace elwiki: site: elwiki title: u0386u03bdu03c4u03b1 u039bu03acu03b2u03bbu03b5u03cau03c2 enwiki: site: enwiki title: Ada Lovelace eowiki: site: eowiki title: Ada Lovelace eswiki: site: eswiki title: Ada Lovelace etwiki: site: etwiki title: Ada Lovelace euwiki: site: euwiki title: Ada Lovelace fawiki: site: fawiki title: u0627u06ccu062fu0627 u0644u0627u0648u0644u06ccu0633 fiwiki: site: fiwiki title: Ada Lovelace frwiki: site: frwiki title: Ada Lovelace gawiki: site: gawiki title: Ada Lovelace glwiki: site: glwiki title: Ada Augusta Lovelace gvwiki: site: gvwiki title: Ada Lovelace hewiki: site: hewiki title: u05e2u05d3u05d4 u05dcu05d0u05d1u05dcu05d9u05d9u05e1 hrwiki: site: hrwiki title: Ada Lovelace htwiki: site: htwiki title: Ada Lovelace huwiki: site: huwiki title: Ada Lovelace hywiki: site: hywiki title: u0531u0564u0561 u053cu0561u057eu056cu0565u0575u057d idwiki: site: idwiki title: Ada Lovelace iswiki: site: iswiki title: Ada Lovelace itwiki: site: itwiki title: Ada Lovelace jawiki: site: jawiki title: u30a8u30a4u30c0u30fbu30e9u30d6u30ecu30b9 kowiki: site: kowiki title: uc5d0uc774ub2e4 ub7ecube0cub808uc774uc2a4 lawiki: site: lawiki title: Ada Lovelace lbwiki: site: lbwiki title: Ada Lovelace ltwiki: site: ltwiki title: Ada Lovelace lvwiki: site: lvwiki title: Ada Lavleisa mlwiki: site: mlwiki title: u0d05u0d21 u0d32u0d35u0d4du200cu0d32u0d47u0d38u0d4d mtwiki: site: mtwiki title: Ada Lovelace newiki: site: newiki title: u090fu0921u093e u0932u092du0932u0947u0938 nlwiki: site: nlwiki title: Ada Lovelace nowiki: site: nowiki title: Ada Byron Lovelace ocwiki: site: ocwiki title: Ada Lovelace plwiki: site: plwiki title: Ada Lovelace pmswiki: site: pmswiki title: Ada Byron King ptwiki: site: ptwiki title: Ada Lovelace rowiki: site: rowiki title: Ada Lovelace ruwiki: site: ruwiki title: u041bu0430u0432u043bu0435u0439u0441, u0410u0434u0430 simplewiki: site: simplewiki title: Ada Lovelace skwiki: site: skwiki title: Augusta Ada Kingovu00e1 sqwiki: site: sqwiki title: Ada Lovelace srwiki: site: srwiki title: u0415u0458u0434u0430 u041au0438u043du0433 u041bu0430u0432u043bu0435u0458u0441 svwiki: site: svwiki title: Ada Lovelace tawiki: site: tawiki title: u0b85u0b9fu0bbe u0bb2u0bb5u0bcdu0bb2u0bc7u0bb8u0bcd tgwiki: site: tgwiki title: u0410u0434u0430 u041bu043eu0432u043bu0435u0439u0441 thwiki: site: thwiki title: u0e40u0e2du0e14u0e32 u0e40u0e25u0e34u0e1fu0e40u0e25u0e0b trwiki: site: trwiki title: Ada Lovelace ukwiki: site: ukwiki title: u0410u0434u0430 u041bu0430u0432u043bu0435u0439u0441 viwiki: site: viwiki title: Ada Lovelace yowiki: site: yowiki title: Ada Lovelace zh_min_nanwiki: site: zh_min_nanwiki title: Ada Lovelace zhwiki: site: zhwiki title: u611bu9054u00b7u52d2u8299u857eu7d72 title: Q7259 type: item success: 1

Example
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".

local entity = mw.wikibase.getEntity local father = mw.wikibase.label( "Q" .. entity.claims.p107[0].mainsnak.datavalue.value["numeric-id"]) -- father == "Lord Byron"

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.