Extension talk:Wikibase Client/Lua

mw.wikibase.getEntity
Is it possible to pass id to this function in order to get any entity? --DixonD (talk) 20:57, 27 March 2013 (UTC)
 * It is not possible yet. --Lydia Pintscher (WMDE) (talk) 10:54, 28 March 2013 (UTC)
 * Is it intentionally restricted or not? Bennylin (talk) 16:46, 18 September 2013 (UTC)
 * Look at the bug-49805 to develop. --Rical (talk) 23:58, 18 September 2013 (UTC)

Change Jens Ohlig's code at https://github.com/wikimedia/mediawiki-extensions-Wikibase/blob/master/client/resources/mw.wikibase.lua

from to I think this would work. Bennylin (talk) 18:19, 23 September 2013 (UTC)


 * I first answer on the bug-49805. Then I see your edit here. Very interesting, thanks. But probably with another function name like wikibase.get_Entity to not disturb the original Scribunto, and to test comparing the two fonctions. How to try it, because here I'm not admin, only in fr.wikisource. --Rical (talk) 22:58, 23 September 2013 (UTC)


 * Yes this would work. We have however intentionally restricted it and do not plan to change this for the time being. We need to solve caching and scalabillity issues first before we can allow access to data from arbitrary items. I know this isn't great but unfortunately needed. --Lydia Pintscher (WMDE) (talk) 21:37, 27 September 2013 (UTC)


 * Thanks for the confirmation. That's what I thought. Bennylin (talk) 10:35, 1 October 2013 (UTC)


 * Similar issue is with  and , which would have second argument (  and   accordingly) allowing to retrieve label in foreign language and interwiki link. Are there any chances for it? Or shall I wait until the bug described above is resolved? Paweł Ziemian (talk) 18:55, 17 October 2013 (UTC)

Troble while accessing personalia
I've written w:ru:Module:Wikibase but met a strange problem. If I try to write construction on any page having facts on Wikidata (Moscow, Mars, arctic fox), I get a pretty table. But on any personalia page (Jimbo, ru:Цзэн Чэн, Ossie Davis) I get only an error on line 51 of mw.wikibase.lua (attempts to index sitelinks which is nil). What to do? Ignatus (talk) 21:23, 3 April 2013 (UTC)
 * This is indeed a strange error. The sitelinks returned are definitely not nil on the items you gave as an example. A first glance at the code of the module you wrote didn't show anything unusual. Does the error persist? --Jens Ohlig (talk) 13:18, 5 April 2013 (UTC)
 * Yes, it's still an error with comment . Ah, I've got what causes this error: it's while trying to get   (male sex for humans, has no sitelinks). The function should be fixed to return nil silently in such cases, I believe. Ignatus (talk) 18:54, 6 April 2013 (UTC)
 * You are correct. I will commit a patch to solve this. Thanks for the error report! --Jens Ohlig (talk) 01:43, 7 April 2013 (UTC)
 * The patch now awaits approval at Gerrit Code Review--Jens Ohlig (talk) 01:54, 7 April 2013 (UTC)
 * And abandoned the patch. User:Tpt offered a better solution which I just merged. --Jens Ohlig (talk) 11:00, 8 April 2013 (UTC)
 * I've rewritten the module to catch it with pcall, now it works on all known pages. Ignatus (talk) 19:31, 6 April 2013 (UTC)
 * And if the identifier is not  (e.g.  ), module raises an error which is uncatchable; I think it also should be somehow moderated. Ignatus (talk) 19:18, 6 April 2013 (UTC)
 * I clarified this in the documentation. Thanks again. --Jens Ohlig (talk) 02:14, 7 April 2013 (UTC)


 * This issue has been raised 30/3 by User:Tpt who issued a patch here https://gerrit.wikimedia.org/r/#/c/56737/ HenkvD (talk) 17:27, 7 April 2013 (UTC)
 * Merged it. --Jens Ohlig (talk) 11:00, 8 April 2013 (UTC)

Hopes for getEntity and getProperty

 * Now, Lua calls to wikibase give many Script Errors. In this module, used in this page, I wrote the function wikidata. But better not blocking error messages could be usefull for many users.
 * To never receive Script Errors, I use :

local dat = wd.entity.claims if dat.p18 then wd.image = dat.p18[0].mainsnak.datavalue.value end -- (because dat["p18"][0] do not works)
 * Like DixonD upper, I hope  and
 * Even exactly  which return a string or nil, and where the element code is not necessary inside a page-element, but where it permits to document the module or the templates elsewhere out of main space, and to build genealogies or species classifications.
 * Now Lua modules work in fr.wikisource, but  do not works. Thanks for a better future. --Rical (talk) 16:02, 13 June 2013 (UTC)

Array numbering
In the examples, it is traditionaly for Lua from 1, but really, since I know, it's from 0. Ignatus (talk) 10:09, 20 July 2013 (UTC)

New interface to wikidata
I do not see differences between result from the deprecated mw.wikibase.getEntity and the new mw.wikibase.getEntityObject. I expected at least two changes: 1) all property names used as indexes in claims array begins with upper case letter P, and 2) the arrays with property values are indexed from 1. What is the difference except the new methods? May I switch to the new interface immediately or is better to wait because the new interface is not stable yet? Paweł Ziemian (talk) 21:57, 28 February 2014 (UTC)
 * Thanks for noticing, this indeed is a bug! It will be fixed by https://gerrit.wikimedia.org/r/116236 which will presumably be deployed on Monday - Hoo man (talk) 15:59, 1 March 2014 (UTC)
 * Good to know. Thanks. I have a second question/proposal about formatPropertyValues function. I called it on property with multiple values and the result was a list of formatted values separated with comma. How about expanding the interface of the function with two optional arguments separator and last_separator, which default to comma if not given, and last_separator defaults to separator if it is given. This could make more human friendly outputs for readers i.e. "consists of bla1, bla2, bla3, and bla4" or in case of unknown date of birth "1900-01-11 or 1900-01-12". Paweł Ziemian (talk) 21:38, 1 March 2014 (UTC)


 * The fix works! Paweł Ziemian (talk) 18:26, 3 March 2014 (UTC)

WDQ interface
Any chances to get some/any WDQ query interface ( http://wdq.wmflabs.org/api_documentation.html )? Let it be even sync request with timeoutError in say 5 sec out of 10 total allowed. Some  Any chances for that? --Neolexx (talk) 20:20, 4 September 2014 (UTC)

Arbitrary access
Just to confirm that arbitrary access is working as expected. I've taken a function from en:Module:Wikidata and created a sandbox module at nl:Module:Sandbox/RexxS/AA that works when invoked from my sandbox on Dutch Wikipedia nl:Gebruiker:RexxS/Kladblok. The corresponding sandbox module at en:Module:Sandbox/RexxS/AA at present gives "Lua error in Module:Sandbox/RexxS/AA at line 17: Access to arbitrary items has been disabled" when invoked from my English sandbox en:User:RexxS/sandbox. Ready to go when AA is enabled on en-wp. Cheers --RexxS (talk) 22:53, 11 May 2015 (UTC)
 * Awesome! Thank you :) --Lydia Pintscher (WMDE) (talk) 10:09, 12 May 2015 (UTC)
 * Did you also check that the value gets updated when changed on Wikidata? I think this is a kind of sensitive point we should test now. -- Bene* (talk) 11:16, 12 May 2015 (UTC)
 * I must admit I have never checked that a value fetched from the mw.wikibase.getEntity call is updated when the value is changed on Wikidata. As I know of no caching in operation, I would be amazed if the value fetched was ever out of sync with the Wikidata database. It should be an easy job to check that though - feel free to use my nl:Gebruiker:RexxS/Kladblok to check a Wikidata entry before and after changing it. --RexxS (talk) 21:51, 27 May 2015 (UTC)


 * Is any function like  planned to appear? It's difficult to refer data associated with specific pages by Wikidata ids, not their titles, and I currently can't see a way to do it. Ignatus (talk) 15:02, 23 June 2015 (UTC)
 * But all referred data are available as ids in claims of the loaded entity. Why do you want to refer by not-stable name, and what is the name? Paweł Ziemian (talk) 20:42, 23 June 2015 (UTC)
 * In Russian Wikipedia, in articles about administrative divisions there are tables of settlement in the division (example) where there are included such data as name, population and status. Currently, these data are mostly kept in modules written in Lua, but we had better them from Wikidata. So, I want to pass to a template in the article "Андреевская (Вожегодский район);Анисимовская (Бекетовское сельское поселение);…", not "Q4065685;Q4066371;…" — article code just needs to be editable. Ignatus (talk) 20:10, 28 June 2015 (UTC)
 * I hope such list will be available automatically when query is implemented. Any settlement should have its parent division set in specific property, and the query ask for all settlements, with the specified division. The result of that query should consists of all the required data (including labels and counts). I hope the last thing to do in Lua module that retrieves the data is to put all of them into nice table or graph for presentation purposes. But all of it is my wish. There are some rumors about queries but nobody knows what exactly it will be yet. I think that looking for item by name is very inefficient and ambiguous. There is nothing unusual that more than one item has the same label (name). In that case the hypothetical function would return an array of identifiers, and you have next problem. Paweł Ziemian (talk) 18:12, 1 July 2015 (UTC)

Get time properties in ISO 8601 format
In the module s:fr:Module:Author3 used in s:fr:Auteur:Nelson Mandela, I would compare dates-from-wikibase to detect abnormal lifetime and some other errors like partial dates. But getting the usual format "april 23 1997" this is not easy. I must first convert dates to numbers, which is complex. If I could get these dates in ISO 8601 format this could be easier! --Rical (talk) 18:06, 17 September 2015 (UTC)
 * @Rical: This module could help you . Matěj Suchánek (talk) 19:11, 17 September 2015 (UTC)
 * Nice! @Matěj, many thanks!
 * My module is based on s:fr:Module:ControlArgs able to convert any module as multilingual and centralisable. But I need to finalize it and use it on 2 wikis before to submit it to the community. --Rical (talk) 19:50, 17 September 2015 (UTC)

testing for a valid QID ?
Is there any way to test if a particular string is indeed a valid QID in wikidata (or another wikibase)? I can call mw.wikibase.getEntity(wikidata_qid) but this results in the beautifully striking red Lua error message "The ID entered is unknown to the system. Please use a valid entity ID.". I would like a way to test for validity that doesn't result in that red error message. Thanks I9606 (talk) I put that code into Module:Sandbox, and it doesn't work:
 * I tried wrapping the call to getEntity in a protected call (pcall) but I still get the error message thrown before it can be caught - though that pattern does work for a locally generated error. I9606 (talk)
 * example lua: that does not work as expected (never returns false, errors out before it can) ->   local worked, entity = pcall(mw.wikibase.getEntity, "Q14865053")
 * I have the same question. Did you find a solution ? Regards Liné1 (talk) 13:37, 25 May 2016 (UTC)
 * I've encountered this problem too. Pinging - Evad37 (talk) 04:19, 24 August 2016 (UTC)
 * Can you help? --Lydia Pintscher (WMDE) (talk) 07:32, 24 August 2016 (UTC)
 * The following should do what you want:  returns   if the entity does not exist:   — Preceding unsigned comment added by Hoo man (talk • contribs) 00:01, 25 August 2016





- Evad37 (talk) 07:38, 25 August 2016 (UTC)

Similarly when using pcall:





or when using xpcall:





- Evad37 (talk) 04:31, 26 August 2016 (UTC)


 * Reported in Phabricator as T143970 - Evad37 (talk) 04:58, 26 August 2016 (UTC)


 * Use "mw.wikibase.getEntityUrl( entityId )", e.g.  — Preceding unsigned comment added by 197.218.88.180 (talk • contribs) 15:54, 9 February 2018 (UTC)
 * Yes, sadly that does not work as mw.wikibase.getEntityUrl will gladly create URLs for invalid and nonexistent entity IDs. 2600:1700:EDB0:A060:14D0:1D18:969:4211 12:40, 21 April 2018 (UTC)

"also known as"
how do i get also known as text, i want first item in 'also known as section' like we have mw.wikibase.description(id), can we have mw.wikibase.alsoknownas(id)(1) --Mdmahir (talk) 17:25, 13 February 2016 (UTC)
 * +1 E.g. for GNU General Public License (Q7603) get,   etc. --Valerio Bozzolan (talk) 01:57, 6 March 2017 (UTC)
 * I believe you are looking for the list of aliases. This can be done via Scribunto Lua, however, currently only via the expensive mw.wikibase.entity and looking at its "aliases" field. A request could be made for mw.wikibase.getAliases/mw.wikibase.getAliasesWithLang and mw.wikibase.entity:getAliases/mw.wikibase.entity:getAliasesWithLang. In a similar vein, short of mw.wikibase.entity, we do not have a means to get sitelink badges, entity types (although we can guess based on the first character of the eID: Q or P) or the schema version. Perhaps an argument could be made to get some sort of abbreviated entity object without the "claims" (which is usually the heaviest part). 2600:1700:EDB0:A060:14D0:1D18:969:4211 13:06, 21 April 2018 (UTC)

Look up QID based on page name or wikidata property?
Is there a way to look up wikidata q-code based on article name or wikidata property. For example I am on Commons:Category:Paul Abadie which has an old style interlanguage link to en:Paul Abadie; can a lua code run on that page be given en:Paul Abadie and look up q-code for this article? Or may be it is possible to look up using this extension which item's d:Property:P373 is set to Commons:Category:Paul Abadie (answer: ). --Jarekt (talk) 19:12, 18 May 2016 (UTC)
 * Is what you are looking for T135442? --Lydia Pintscher (WMDE) (talk) 07:35, 24 August 2016 (UTC)
 * Yes precisely --Jarekt (talk) 19:08, 25 October 2016 (UTC)

Problem with mw.wikibase.label
Hello, there is a problem with this function mw.wikibase.label. Now, if there is an opposite value of the item in the local wiki; it gives the value in the local wiki, and if not, it returns the value of the English opposite of the item. Sometimes I used this function, and I don't need it to return the english value, but I need only the local wiki value of the item (If exist). Is there a way to skip this problem?. Or fix it Maybe?.--ASammour (talk) 18:40, 7 November 2016 (UTC)
 * Use  (for details, see T138163, T143094 and T145138). Matěj Suchánek (talk) 20:22, 30 December 2016 (UTC)

mw.wikibase.entity:formatStatements
Hello everybody, I am not sure if mw.wikibase.entity:formatStatements works for me on my private wiki or if I am making something wrong; check this example:

e = mw.wikibase.getEntity('Q1208') e:formatStatements('P29')

Lua error in console input at line 8: attempt to call method 'formatStatements' (a nil value).

It seems I can execute any other entity method, but not this one.

My Lua is at version 55.1 and Scribuntu is – (a665621) 00:03, 25 October 2016, so all pretty recent. Has this method changed or been removed at some point?
 * If I remember correctly, this method has been implemented very recently, so you'll have to update. Matěj Suchánek (talk) 20:17, 30 December 2016 (UTC)


 * Thanks Matěj! What part of Mediawiki do I have to update, Scributo, the Lua binary…? Also, what exactly is the output of this method? Does it generate Links to items, or pages of items, or is it just plain text with special wikitext characters escaped? --Dragan Espenschied (talk) 22:28, 30 December 2016 (UTC)
 * The Wikibase extension since it holds the code for the data access (but I don't know if there are some further dependecies like MW core). Oh, and I forgot that there are also some switches to enable this.
 * The output should be exactly what you get from #statements, see . Matěj Suchánek (talk) 10:07, 31 December 2016 (UTC)

mw.wikibase.entity:formatPropertyValues documentation
The doc seems to imply that formatPropertyValues return a simple string, while apparently it returns a table containing a value and a label. Can I add that to the doc ? I took me 15 minutes to figure out why my module went wrong because of that. Zolo (talk) 12:06, 12 August 2017 (UTC)
 * It may have taken you that long to find the information but it is already documented in the example comment as: -- Return a table like: { value = "Formatted claim value", label = "Label of the Property" }. It can be found in Extension:Wikibase Client/Lua (and previously at the time you asked as Special:PermanentLink/2489735). 50.53.21.2 06:57, 21 January 2018 (UTC)

What means RANK_TRUTH, RANK_PREFERRED ... ?

 * RANK_TRUTH, RANK_PREFERRED... seem not enough described.
 * What means each of these RANK? in which case are they usefull?
 * What they change when a Lua coder uses RANK_PREFERRED?
 * What happens if the Lua coder select another one of them? --Rical (talk) 21:12, 5 February 2018 (UTC)
 * You are probably referring to Extension:Wikibase Client/Lua. See d:Help:Ranks for what they mean. Matěj Suchánek (talk) 10:08, 6 February 2018 (UTC)
 * I continue here because I started here but the rigth place is in Extension:Wikibase Client/Lua.
 * I search RANK_TRUTH... in mediawiki, wikibase, meta, test2 but find them only in Extension:Wikibase Client/Lua and d:Help:Ranks.
 * Where to find other documentations?
 * Do you agree if I write in d:Help:Ranks something like:
 * Ranks are a mean to represent the degree of confidence and obsolescence of each data in Wikibase.
 * Each rank contain a table of some values until 0. Examples: children from a parent or books from an author.
 * From the best to the worse, confidence levels are: RANK_TRUTH = 3, RANK_PREFERRED = 2, RANK_NORMAL = 1, RANK_DEPRECATED = 0
 * RANK_TRUTH is the confidence in a data which permit to say "that is enough sure, no doubt are known in wikibase".
 * RANK_PREFERRED is less sure but enough to be used in most cases.
 * RANK_NORMAL is the least admissible confidence to use.
 * RANK_DEPRECATED means that a data exists, but obsolete or not sure.
 * --Rical (talk) 15:07, 7 February 2018 (UTC)
 * update --Rical (talk)
 * This is not content suitable for documentation meant to Wikidata users. Moreover, your descriptions of individual ranks are inaccurate. As you would read in the linked help, they are not based on a degree of confidence. Additionally, RANK_TRUTH is not an actual rank. I'm not sure about its meaning, though (likely a fallback for RANK_PREFERRED?). Matěj Suchánek (talk) 09:09, 8 February 2018 (UTC)
 * RANK_TRUTH was used for "claims that are not statements" in 2013, but it was removed in 2015 (data model version 3.0). It is no longer in the Wikibase RDF vocabulary; I don't know whether it can be removed without notice from lua.wiki (well, the equivalent on Gerrit) because it was part of the interface, but probably it should be somehow. GreenReaper (talk) 14:27, 29 August 2019 (UTC)

Caching
The docs don't mention caching anywhere. Is the client expected to cache results if needed, or is there some level of caching already performed by the extension itself? – Jberkel (talk) 11:38, 21 February 2018 (UTC)
 * At most 15 entities are cached, see mw.wikibase.lua. --Tacsipacsi (talk) 23:20, 3 March 2018 (UTC)
 * And only the entity objects returned via mw.wikibase.entity are cached. If you use another interface like mw.wikibase.getBestStatements there is no caching provided. 2600:1700:EDB0:A060:14D0:1D18:969:4211 14:07, 21 April 2018 (UTC)

Redirects
The manual doesn't describe how to access entities though redirects. For instance is redirected to. Access to statements with mw.wikibase.getBestStatements('Q26837454') or mw.wikibase.getAllStatements('Q26837454') works fine, but I cannot get sitelinks and labels. mw.wikibase.getLabel('Q26837454') and mw.wikibase.getSitelink('Q26837454') both gives nil.

How do I get labels and sitelinks in a module when my entity is redirected, or alternatively how do I find out that it is redirected and get the redirection target? --Dipsacus fullonum (talk) 15:26, 21 August 2018 (UTC)
 * See T96478, T192462 and T157868. Matěj Suchánek (talk) 17:18, 21 August 2018 (UTC)
 * Ugh! It seems that I have to use the expensive wikibase.getEntity for no good reason to get sitelinks and labels in a reliable way. That is very bad news. What a waste. But thank you for the answer. --Dipsacus fullonum (talk) 18:22, 21 August 2018 (UTC)
 * PS. The manual should be updated to warn against using the direct access but unreliable functions. --Dipsacus fullonum (talk) 18:25, 21 August 2018 (UTC)

Error with mw.wikibase
Hi all,

For some references, and without knowing why, I have the following error: Lua error in Module:Citation/CS1/Identifiers at line 47: attempt to index a nil value.

For your information, Module:Citation/CS1/Identifiers was imported directly from Wikipedia (see the content as hosted on my personal wiki here: https://pastebin.com/YPnKm9Jc).

Especially, this is the line (47) that generates the error: wd_article = mw.wikibase.getEntity (options.q):getSitelink (this_wiki_code .. 'wiki');

I tried to install Wikibase: I've followed the tutorial, installed both the repository and the client, and I have the "Add links" section.

I've run php maintenance/runJobs.php and php maintenance/update.php, but the error is still here.

Do anyone know what is the problem here?

Thank you, — Preceding unsigned comment added by Hwgen22 (talk • contribs) 15:26, 6 December 2018 (UTC)
 * Where does options.q come from? Normally you have to check whether  is nil, and only proceed by calling its method getSitelink if it isn’t. —Tacsipacsi (talk) 22:09, 6 December 2018 (UTC)
 * Well, I've followed the tutorial from citation tool page and I've directly imported Module:Citation/CS1/Identifiers from Wikipedia (https://en.wikipedia.org/wiki/Module:Citation/CS1/Identifiers). options.q comes from here. It seems that even wikibase here is nil. And to be honest, I don't really know what does that mean. I've installed Wikibase Client and Repository, hoping that it would solve my problem, but I still have this error that appears without knowing why. — Preceding unsigned comment added by Hwgen22 (talk • contribs) 09:23, 7 December 2018 (UTC)
 * It’s easy to find out whether mw.wikibase is nil: just execute  in the Lua console (it’s on the bottom of all module editing pages). By the way, do you need it? I don’t know how this module works, but changing the line too   (or even better, deleting lines 46–51 entirely) eliminates this script error for sure. —Tacsipacsi (talk) 23:32, 7 December 2018 (UTC)
 * I tried what you said and mw.wikibase is not nil (Lua console returns "table"). To be honest, I'm not sure I need it. This module is used by the citation tool, in order to add and manage references on articles. By following the trace of error, it seems that the error is raised when trying to handle (create? retrieve? something else?) DOI. If I remove the DOI from the template, I have the same error but for the ISSN. If I remove it too, it works. I tried to remove the if condition and what it contains to see if this helps, and after running all jobs manually (for those who will experience the same error, just execute php maintenance/runJobs.php from the wiki folder, inside a terminal), I have no more error. On the reference list, the DOI is displayed correctly, in this format: doi:, where doi is a link to my local Wiki. I thought that creating the page on my wiki would potentially resolve the problem but if I put again the code between the if statement, the error appears again. In any case, I will comment the if statement for now, hoping that no side effects will appear because of such change. I will try to see on another discussion page (maybe the one related to this module, or to the citation tool) if someone as any clue about this problem. Thank you very much for the time you took to respond, and for your help!

FYI I reported this to the maintainers of the module and they plan to fix it. There is a solution there as well which I have tried and works (which is equivalent to just deleting it, but makes it dependant on mw.wikibase being enabled): w:Help_talk:Citation_Style_1. Mvolz (talk) 10:52, 6 February 2019 (UTC)
 * This worked. Seraphim System (talk) 01:58, 24 February 2019 (UTC)

Instructions to Fix

In summary, this fix worked for me:
 * Click on "Module:Citation/CS1/Identifiers at line 47: attempt to index a nil value.", then go into the module "Module:Citation/CS1/Identifiers".
 * Delete lines 46 to 51. To find the correct line numbers, it might help to first copy the text into an editor such as SubLime.
 * Refresh the scripts by running this in the terminal in the Wiki folder:

php maintenance/runJobs.php