Hoo man has uploaded a new change for review. https://gerrit.wikimedia.org/r/144965
Change subject: Lua: Allow arbitrary item access ...................................................................... Lua: Allow arbitrary item access This also changes the tests slightly, as we now have hardcoded item ids. Bug: 47930 Change-Id: I78607bfd984cbc99192faaa5d8ca4ec331de3ab7 --- M client/includes/scribunto/mw.wikibase.lua M client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua M client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php M docs/lua.wiki 4 files changed, 40 insertions(+), 15 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/65/144965/1 diff --git a/client/includes/scribunto/mw.wikibase.lua b/client/includes/scribunto/mw.wikibase.lua index 4b904f3..d3871cb 100644 --- a/client/includes/scribunto/mw.wikibase.lua +++ b/client/includes/scribunto/mw.wikibase.lua @@ -61,8 +61,14 @@ end -- Get the mw.wikibase.entity object for the current page - wikibase.getEntityObject = function() - local id = getEntityIdForCurrentPage() + wikibase.getEntityObject = function( id ) + if id ~= nil and type( id ) ~= 'string' then + error( 'Id must be either of type string or nil, ' .. type( id ) .. ' given' ) + end + + if id == nil then + id = getEntityIdForCurrentPage() + end if id == nil then return nil diff --git a/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua b/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua index 3559c4a..2b5cf80 100644 --- a/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua +++ b/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua @@ -25,14 +25,8 @@ return mw.wikibase.getEntityObject().schemaVersion end -local function testLabel() - local entity = mw.wikibase.getEntityObject() - return mw.wikibase.label( entity.id ) -end - -local function testSitelink() - local entity = mw.wikibase.getEntityObject() - return mw.wikibase.sitelink( entity.id ) +local function testGetEntityObjectForeignLabel() + return mw.wikibase.getEntityObject( 'Q199024' ):getLabel( 'de' ) end local tests = { @@ -50,10 +44,23 @@ { name = 'mw.wikibase.getEntityObject (schema version)', func = testGetEntityObjectSchemaVersion, expect = { 2 } }, - { name = 'mw.wikibase.label', func = testLabel, type='ToString', + { name = "mw.wikibase.getEntityObject (foreign access - doesn't exist)", func = mw.wikibase.getEntityObject, + args = { 'Q1223214234' }, + expect = { nil } + }, + { name = "mw.wikibase.getEntityObject (foreign access)", func = testGetEntityObjectForeignLabel, + expect = { 'Arbitrary access \\o/' } + }, + { name = 'mw.wikibase.getEntityObject (id must be string)', func = mw.wikibase.getEntityObject, + args = { 123 }, + expect = 'Id must be either of type string or nil, number given' + }, + { name = 'mw.wikibase.label', func = mw.wikibase.label, type='ToString', + args = { 'Q32487' }, expect = { 'Lua Test Item' } }, - { name = 'mw.wikibase.sitelink', func = testSitelink, type='ToString', + { name = 'mw.wikibase.sitelink', func = mw.wikibase.sitelink, type='ToString', + args = { 'Q32487' }, expect = { 'WikibaseClientLuaTest' } } } diff --git a/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php b/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php index 2089a36..2587d22 100644 --- a/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php +++ b/client/tests/phpunit/includes/scribunto/WikibaseLuaIntegrationTestItemSetUpHelper.php @@ -8,6 +8,7 @@ use Wikibase\DataModel\Claim\Claim; use Wikibase\DataModel\Claim\Statement; use Wikibase\DataModel\Entity\Item; +use Wikibase\DataModel\Entity\ItemId; use Wikibase\DataModel\Entity\Property; use Wikibase\DataModel\Entity\PropertyId; use Wikibase\DataModel\SiteLink; @@ -77,7 +78,10 @@ 'en' => 'Test all the code paths' ); - $this->createTestItem( $labels, array( $statement1, $statement2 ), $siteLinks ); + $this->createTestItem( new ItemId( 'Q32487' ), $labels, array( $statement1, $statement2 ), $siteLinks ); + + // Create another test item to test arbitrary access + $this->createTestItem( new ItemId( 'Q199024' ), array( 'de' => 'Arbitrary access \o/' ) ); } /** @@ -96,14 +100,16 @@ } /** + * @param ItemId $id * @param string[] $labels * @param Claim[]|null $claims * @param SiteLink[]|null $siteLinks * * @return Item */ - protected function createTestItem( array $labels, array $claims = null, array $siteLinks = null ) { + protected function createTestItem( ItemId $id, array $labels, array $claims = null, array $siteLinks = null ) { $item = Item::newEmpty(); + $item->setId( $id ); $item->setLabels( $labels ); if ( is_array( $siteLinks ) ) { diff --git a/docs/lua.wiki b/docs/lua.wiki index adc49e4..f8856b4 100644 --- a/docs/lua.wiki +++ b/docs/lua.wiki @@ -5,7 +5,13 @@ === mw.wikibase.getEntityObject === <code>wikibase.getEntityObject()</code><br> -Gets a [[#mw.wikibase.entity|mw.wikibase.entity]] table with data of the Wikibase item connected to the current page. +<code>wikibase.getEntityObject( id )</code><br> +Gets a [[#mw.wikibase.entity|mw.wikibase.entity]] table with data of the Wikibase item requested by id. If no id was given, the item connected to the current page will be returned. + +An example call might look like this: +<source lang="lua"> +mw.wikibase.getEntityObject( 'Q42' ) -- Returns a a [[#mw.wikibase.entity|mw.wikibase.entity]] table for the Item with the id Q42 +</source> === mw.wikibase.label === <code>wikibase.label( id )</code><br> -- To view, visit https://gerrit.wikimedia.org/r/144965 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I78607bfd984cbc99192faaa5d8ca4ec331de3ab7 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Hoo man <h...@online.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits