jenkins-bot has submitted this change and it was merged. Change subject: Default mw.wikibase.* lua functions to the connected item of a page ......................................................................
Default mw.wikibase.* lua functions to the connected item of a page Default mw.wikibase.label and mw.wikibase.description to use connected item, if id is not specified. Bug: T99078 Change-Id: Ief78b1c8f591d1da8a6ca03e6cb02c339954a99d --- M client/includes/DataAccess/Scribunto/mw.wikibase.lua M client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua M docs/lua.wiki 3 files changed, 54 insertions(+), 12 deletions(-) Approvals: Hoo man: Looks good to me, approved jenkins-bot: Verified diff --git a/client/includes/DataAccess/Scribunto/mw.wikibase.lua b/client/includes/DataAccess/Scribunto/mw.wikibase.lua index 03b0e47..acbb3fe 100644 --- a/client/includes/DataAccess/Scribunto/mw.wikibase.lua +++ b/client/includes/DataAccess/Scribunto/mw.wikibase.lua @@ -32,6 +32,15 @@ return pageEntityId end + -- Get the entity id of the connected item, if id is nil. Cached. + local getIdOfConnectedItemIfNil = function( id ) + if id == nil then + return getEntityIdForCurrentPage() + end + + return id + end + -- Get the mw.wikibase.entity object for a given id. Cached. local getEntityObject = function( id ) if entities[ id ] == nil then @@ -59,47 +68,62 @@ end end - -- Get the mw.wikibase.entity object for the current page + -- Get the mw.wikibase.entity object for the current page or for the + -- specified id. + -- + -- @param id wikibase.getEntity = function( id ) checkTypeMulti( 'getEntity', 1, id, { 'string', 'nil' } ) + id = getIdOfConnectedItemIfNil( id ) + if id == nil then - id = getEntityIdForCurrentPage() + return nil end if not php.getSetting( 'allowArbitraryDataAccess' ) and id ~= getEntityIdForCurrentPage() then error( 'Access to arbitrary items has been disabled.', 2 ) end - if id == nil then - return nil - else - return getEntityObject( id ) - end + return getEntityObject( id ) end -- getEntityObject is an alias for getEntity as these used to be different. wikibase.getEntityObject = wikibase.getEntity - -- Get the label for the given entity id (in content language) + -- Get the label for the given entity id, if specified, or of the + -- connected entity, if exists. (in content language) -- -- @param id wikibase.label = function( id ) - checkType( 'label', 1, id, 'string' ) + checkTypeMulti( 'label', 1, id, { 'string', 'nil' } ) + + id = getIdOfConnectedItemIfNil( id ) + + if id == nil then + return nil + end return php.getLabel( id ) end - -- Get the description for the given entity id (in content language) + -- Get the description for the given entity id, if specified, or of the + -- connected entity, if exists. (in content language) -- -- @param id wikibase.description = function( id ) - checkType( 'description', 1, id, 'string' ) + checkTypeMulti( 'description', 1, id, { 'string', 'nil' } ) + + id = getIdOfConnectedItemIfNil( id ) + + if id == nil then + return nil + end return php.getDescription( id ) end - -- Get the local sitelink title for the given entity id (if one exists) + -- Get the local sitelink title for the given entity id. -- -- @param id wikibase.sitelink = function( id ) diff --git a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua index 7a6a85d..995cfaa 100644 --- a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua +++ b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseLibraryTests.lua @@ -87,12 +87,20 @@ args = { 'Q1224342342' }, expect = { nil } }, + { name = 'mw.wikibase.label (connected item)', func = mw.wikibase.label, type='ToString', + args = {}, + expect = { 'Lua Test Item' } + }, { name = 'mw.wikibase.label (no label)', func = mw.wikibase.label, type='ToString', args = { 'Q32488' }, expect = { nil } }, { name = 'mw.wikibase.description', func = mw.wikibase.description, type='ToString', args = { 'Q32487' }, + expect = { 'Description of Q32487' } + }, + { name = 'mw.wikibase.description (connected item)', func = mw.wikibase.description, type='ToString', + args = {}, expect = { 'Description of Q32487' } }, { name = 'mw.wikibase.description (no such item)', func = mw.wikibase.description, type='ToString', @@ -107,6 +115,10 @@ args = { 'Q32487' }, expect = { 'WikibaseClientDataAccessTest' } }, + { name = 'mw.wikibase.sitelink (connected item)', func = mw.wikibase.sitelink, type='ToString', + args = {}, + expect = "bad argument #1 to 'sitelink' (string expected, got nil)" + }, { name = 'mw.wikibase.sitelink', func = mw.wikibase.sitelink, type='ToString', args = { 'Q32488' }, expect = { nil } diff --git a/docs/lua.wiki b/docs/lua.wiki index 372edfe..a9679cf 100644 --- a/docs/lua.wiki +++ b/docs/lua.wiki @@ -19,8 +19,11 @@ Alias for [[#mw.wikibase.getEntity|mw.wikibase.getEntity]]. === mw.wikibase.label === +<code>wikibase.label()</code><br> <code>wikibase.label( id )</code><br> Takes an item ID and returns the label in the language of the local Wiki. + +If no ID was specified, then the label of the item connected to the current page will be returned, if the page is indeed connected and a label exists in the wiki's language or in a fallback language. An example call might look like this: <source lang="lua"> @@ -37,9 +40,12 @@ </source> === mw.wikibase.description === +<code>wikibase.description()</code><br> <code>wikibase.description( id )</code><br> Takes an item ID and returns the description in the language of the local Wiki. +If no ID was specified, then the description of the item connected to the current page will be returned, if the page is indeed connected and a description exists in the wiki's language or in a fallback language. + An example call might look like this: <source lang="lua"> mw.wikibase.description( 'Q42' ) -- Returns the description of the item as a string, like "capital of Germany" -- To view, visit https://gerrit.wikimedia.org/r/210907 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ief78b1c8f591d1da8a6ca03e6cb02c339954a99d Gerrit-PatchSet: 14 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: Jackmcbarn <jackmcb...@gmail.com> Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits