Page MenuHomePhabricator

In Lua modules, there is no way to test for validity of Wikidata entity IDs
Closed, ResolvedPublic


In Lua, mw.wikibase.getEntity(id) will return a entity object if "id" is a valid Wikidata entity id, e.g. 'Q42'. However, if the id isn't valid (e.g. 'Q42000000000000000000000000000') it results in a big red Lua error message "The ID entered is unknown to the system. Please use a valid entity ID." This happens even if pcall() or xpcall() is used.

Actual results/demonstartion: See mw:Extension talk:Wikibase Client/Lua § testing for a valid QID ? (permalink to lua code in Module:Sandbox)

Expected results: One or more of the following:

  • mw.wikibase.getEntity(invalid_id) retuns nil
  • pcall() and/or xpcall() catch the error, and behave as one would expect
  • another function, perhaps named mw.wikibase.checkEntity(id), that returns true if the id is valid, or false if the id is invalid
  • some other way of determining validity without actually throwing an error

Event Timeline

Evad37 created this task.Aug 26 2016, 4:55 AM
Restricted Application added a subscriber: Aklapper. · View Herald TranscriptAug 26 2016, 4:55 AM
hoo added subscribers: daniel, aude, Lucie, hoo.

I see that implementing this in user land code is not trivial (my approach didn't properly check the numerical part of the ids). Also re-implementing this becomes a mess with more entity types with different requirements for the id format.

How about a mw.wikibase.isValidEntityIdSerialization( string ) which returns true or false?

Anomie added a subscriber: Anomie.Aug 26 2016, 2:41 PM

In order for the error to be caught by pcall, you need to throw a Scribunto_LuaError rather than a ScribuntoException.

hoo added a comment.Aug 26 2016, 2:44 PM

In order for the error to be caught by pcall, you need to throw a Scribunto_LuaError rather than a ScribuntoException.

Good point, we might also want to do this… but if someone specifically wants to test whether an id is valid, going with pcall and getEntity seems like a lot of hassle (and it also means we load the whole entity in case it is valid, which might not be needed).

Uzume added a subscriber: Uzume.Jan 15 2018, 9:18 AM

We need a resolveEntityId(eid) that returns nil when there is no such entity. It should also handle redirects from merged items, etc. (also solving T157868). It could also potentially work like resolvePropertyId and return a valid entity ID when given an unambiguous label or alias.

Ahecht added a subscriber: Ahecht.Feb 1 2018, 6:34 PM
Ahecht awarded a token.Feb 1 2018, 6:35 PM
hoo claimed this task.Mar 27 2018, 3:46 AM

Change 423259 had a related patch set uploaded (by Hoo man; owner: Hoo man):
[mediawiki/extensions/Wikibase@master] Introduce mw.wikibase.isValidEntityId

Change 423260 had a related patch set uploaded (by Hoo man; owner: Hoo man):
[mediawiki/extensions/Wikibase@master] Introduce mw.wikibase.hasEntity

Change 423259 merged by jenkins-bot:
[mediawiki/extensions/Wikibase@master] Introduce mw.wikibase.isValidEntityId

Change 423260 merged by jenkins-bot:
[mediawiki/extensions/Wikibase@master] Introduce mw.wikibase.entityExists

hoo closed this task as Resolved.Apr 6 2018, 1:18 PM
hoo removed a project: Patch-For-Review.

By the end of next week mw.wikibase.isValidEntityId and mw.wikibase.entityExists should be available on all Wikimedia wikis.

I'll update the documentation ( once that happened.

Ahecht rescinded a token.Apr 8 2018, 10:02 PM
Uzume added a comment.Apr 12 2018, 2:53 PM

Does entityExists properly handle redirects (e.g., merged entities) and if so how do we get the entity ID we are redirected to?

For example, what does mw.wikibase.entityExists(Q404) yield? and how do I find out that refers to Q395?


mw.wikibase.entityExists('Q404') returns false

hoo added a comment.Apr 18 2018, 3:42 PM

mw.wikibase.entityExists('Q404') returns false

Indeed, and that is IMO a bug. I just opened T192462: mw.wikibase.entityExists returns false for redirected entities for this.

hoo added a comment.Apr 18 2018, 4:25 PM

Note: Until T192462 is resolved, I wont add this to Extension:Wikibase Client/Lua.

Aklapper removed a subscriber: Anomie.Oct 16 2020, 5:39 PM