Page MenuHomePhabricator

Add convenience function getBadges() to Lua
Closed, ResolvedPublic5 Estimated Story PointsFeature

Description

Main components:

  • Wikibase-Lua

User story:
As a Wikipedia template editor / Lua programmer, I want to find the badges a specific Wikipedia article has using Lua in order to use it in my Lua module and template.

Problem:
Getting information about badges using Lua is currently possible, but it requires loading the whole entity. This is problematic in some items as it can cause Lua errors (e.g. needs too much memory).

Solution:
Add a Lua convenience function getBadges (similar to getSitelink).
See: https://doc.wikimedia.org/Wikibase/master/php/docs_topics_lua.html#mw.wikibase.getSitelink

This would be run similarly to the 'getSitelink' i.e.

-- @param {string} itemId
-- @param {string} [globalSiteId]
function wikibase.getBadges( itemId, globalSiteId )

and the return value is a Lua table (with numeric indices), containing zero or more badges (identified by the corresponding badge QID).

The globalSiteID can remain empty and this will default to the local wiki.

BDD:
GIVEN a Wikipedia article
AND a Wikidata Item has sitelinks with badges for this article
WHEN a Lua 'getBadges' convenience function is run
THEN a Lua table with the QIDs of the badges is returned

Acceptance criteria:

  • A Lua 'getBadges' convenience function is available
  • mark the function as expensive, unless we can figure out a fast way to retrieve the badges
  • Wikibade: Lua page is updated with the information on how the 'getBadges' convenience function will operate

Notes
For engineers, you need to have:

  • Scribunto extension installed
  • configure at least one badge
  • Configure a Lua console to call this function

See P43183 for example LocalSettings.php configurations

Original:
https://www.wikidata.org/wiki/Wikidata:Report_a_technical_problem/Archive/2022/04#Add_a_lua_function_%22getBadges%22

Event Timeline

This might be a nice hackathon project or something to get someone onboarded.

This comment was removed by Manuel.
Tacsipacsi changed the subtype of this task from "Task" to "Feature Request".

Could one of the people who would like to have this please give an example of what the call and the return value would be for this?

I suppose the call could be exactly the same as for getSitelink, i.e.

-- @param {string} itemId
-- @param {string} [globalSiteId]
function wikibase.getBadges( itemId, globalSiteId )

and the return value could be a Lua table (with numeric indices), containing zero or more badges.

Michael set the point value for this task to 5.Jan 24 2023, 10:02 AM
Michael moved this task from Unified DOT Backlog to Sprint-∞ on the Wikidata Dev Team board.

Change 885034 had a related patch set uploaded (by Hoo man; author: Hoo man):

[mediawiki/extensions/Wikibase@master] Update EntityUsage::SITELINK_USAGE's documentation

https://gerrit.wikimedia.org/r/885034

Change 885039 had a related patch set uploaded (by Hoo man; author: Hoo man):

[mediawiki/extensions/Wikibase@master] Add getBadges convenience function to Lua

https://gerrit.wikimedia.org/r/885039

Change 885034 merged by jenkins-bot:

[mediawiki/extensions/Wikibase@master] Update EntityUsage::SITELINK_USAGE's documentation

https://gerrit.wikimedia.org/r/885034

Change 885039 merged by jenkins-bot:

[mediawiki/extensions/Wikibase@master] Add getBadges convenience function to Lua

https://gerrit.wikimedia.org/r/885039

Arian_Bozorg subscribed.

This looks good to me :)

Thanks so much

We have an use case on French wiki, where we want a list of all foreign badges of a given article.

  • Previously, we were retrieving the whole entity, then we could iterate the sitelinks table. But retrieving the whole entity was eating a lot of memory (it was a bit slow too).
  • Thanks to this new getBadges() function, a new approach has been developed: we iterate a hardcoded list of wikis, and we call getBadges() for each of these. It uses a lot less memory (and it's a bit faster too). The downside with this approach is that we have to use a hardcoded list of wikis, whereas the previous approach was exhaustively retrieving the badges.

Here is the code change: https://fr.wikipedia.org/w/index.php?title=Module:Cat%C3%A9gorisation_badges&type=revision&diff=202204202&oldid=194574538

I thought about adding new functions like getAllBadges() or getAllSitelinks(), but that would introduce discrepancies with the existing functions:

  • if adding a getAllBadges() function, why not adding the same for getSitelink()?
  • but adding getAllSitelinks() (notice the singular/plural difference with getSitelink()) would be confusing, because we have the sitelinks table, but these functions retrieve entity.sitelinks[globalSiteId].title actually.

To avoid confusion, another solution could be to add a getSitelinksTable() function. The only downside is that it still would add a function to the API, and a function that returns a "raw" table (i.e. not directly the desired properties), though this table is quite straightforward.

We have an use case on French wiki, where we want a list of all foreign badges of a given article.
<snip>

Thank you for your input, I have created a new task for that: T338942: Add a Lua function to get all sitelinks/ all badges!