Page MenuHomePhabricator

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

Description

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

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?

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

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).

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.

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

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

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

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

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

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

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

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

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 (https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua) once that happened.

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?

Thanks

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

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.