jenkins-bot has submitted this change and it was merged. Change subject: Make mw.wikibase.getEntityObject() actually return non-Legacy data ......................................................................
Make mw.wikibase.getEntityObject() actually return non-Legacy data Also fixed the module to no longer guard against this, so that the integration tests will catch this in the future. On top of that, we now disallow the creation of mw.wikibase.entity objects with legacy / invalid data. Change-Id: Ie52d887b9a2dda98187cc23ce400614ca1bf2fac --- M client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php M client/includes/scribunto/WikibaseLuaBindings.php M client/includes/scribunto/mw.wikibase.entity.lua M client/includes/scribunto/mw.wikibase.lua M client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua M client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua M client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php M client/tests/phpunit/includes/scribunto/WikibaseLuaBindingsTest.php 8 files changed, 45 insertions(+), 16 deletions(-) Approvals: WikidataJenkins: Verified Daniel Kinzler: Looks good to me, approved jenkins-bot: Verified diff --git a/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php b/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php index 68d2e05..c4e9719 100644 --- a/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php +++ b/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php @@ -75,7 +75,7 @@ * @throws ScribuntoException * @return array */ - public function getEntity( $prefixedEntityId = null, $legacyStyle = true ) { + public function getEntity( $prefixedEntityId, $legacyStyle ) { $this->checkType( 'getEntity', 1, $prefixedEntityId, 'string' ); $this->checkType( 'getEntity', 2, $legacyStyle, 'boolean' ); try { diff --git a/client/includes/scribunto/WikibaseLuaBindings.php b/client/includes/scribunto/WikibaseLuaBindings.php index 65f7c44..74f0894 100644 --- a/client/includes/scribunto/WikibaseLuaBindings.php +++ b/client/includes/scribunto/WikibaseLuaBindings.php @@ -116,9 +116,13 @@ $entityArr = $serializer->getSerialized( $entityObject ); - if ( !$legacyStyle ) { + if ( $legacyStyle ) { + // Mark the output as Legacy so that we can easily distinguish the styles in Lua + $entityArr['schemaVersion'] = 1; + } else { // Renumber the entity as Lua uses 1-based array indexing $this->renumber( $entityArr ); + $entityArr['schemaVersion'] = 2; } return $entityArr; diff --git a/client/includes/scribunto/mw.wikibase.entity.lua b/client/includes/scribunto/mw.wikibase.entity.lua index e53467b..8889ab5 100644 --- a/client/includes/scribunto/mw.wikibase.entity.lua +++ b/client/includes/scribunto/mw.wikibase.entity.lua @@ -24,8 +24,12 @@ -- -- @param data entity.create = function( data ) - if type( data ) ~= 'table' then - error( 'The entity data must be a table' ) + if type( data ) ~= 'table' or type( data.schemaVersion ) ~= 'number' then + error( 'The entity data must be a table obtained via mw.wikibase.getEntityObject' ) + end + + if data.schemaVersion < 2 then + error( 'mw.wikibase.entity must not be constructed using legacy data' ) end local entity = data @@ -95,10 +99,8 @@ local n = 0 for k, v in pairs( entity.claims ) do - if string.match( k, '^%u%d+' ) ~= nil then - n = n + 1 - properties[n] = k - end + n = n + 1 + properties[n] = k end return properties diff --git a/client/includes/scribunto/mw.wikibase.lua b/client/includes/scribunto/mw.wikibase.lua index 1e93e70..d72c1e9 100644 --- a/client/includes/scribunto/mw.wikibase.lua +++ b/client/includes/scribunto/mw.wikibase.lua @@ -19,7 +19,7 @@ local entity = false local getEntityObject = function( id ) - local entity = php.getEntity( id ) + local entity = php.getEntity( id, false ) if type( entity ) ~= 'table' then return nil end diff --git a/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua b/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua index a53ea9d..dd977e3 100644 --- a/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua +++ b/client/tests/phpunit/includes/scribunto/LuaWikibaseEntityLibraryTests.lua @@ -10,6 +10,7 @@ -- A test item (the structure isn't complete... but good enough for tests) local testItem = { id = "Q123", + schemaVersion = 2, claims = { P321 = {}, P4321 = {} @@ -30,6 +31,10 @@ title = 'Русский' } } +} +-- A legacy test "item" +local testItemLegacy = { + schemaVersion = 1 } local getNewTestItem = function() @@ -89,9 +94,9 @@ { name = 'mw.wikibase.entity exists', func = testExists, type='ToString', expect = { 'table' } }, - { name = 'mw.wikibase.entity.create 1', func = testCreate, type='ToString', + { name = 'mw.wikibase.entity.create 1', func = testCreate, args = { {} }, - expect = { {} } + expect = 'The entity data must be a table obtained via mw.wikibase.getEntityObject' }, { name = 'mw.wikibase.entity.create 2', func = testCreate, type='ToString', args = { testItem }, @@ -99,7 +104,11 @@ }, { name = 'mw.wikibase.entity.create 2', func = testCreate, type='ToString', args = { nil }, - expect = 'The entity data must be a table' + expect = 'The entity data must be a table obtained via mw.wikibase.getEntityObject' + }, + { name = 'mw.wikibase.entity.create 3', func = testCreate, type='ToString', + args = { testItemLegacy }, + expect = 'mw.wikibase.entity must not be constructed using legacy data' }, { name = 'mw.wikibase.entity.getLabel 1', func = testGetLabel, type='ToString', args = { 'de' }, diff --git a/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua b/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua index 3944b9b..3559c4a 100644 --- a/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua +++ b/client/tests/phpunit/includes/scribunto/LuaWikibaseLibraryTests.lua @@ -13,8 +13,16 @@ return type( mw.wikibase.getEntity() ) end +local function testGetEntitySchemaVersion() + return mw.wikibase.getEntity().schemaVersion +end + local function testGetEntityObjectType() return type( mw.wikibase.getEntityObject() ) +end + +local function testGetEntityObjectSchemaVersion() + return mw.wikibase.getEntityObject().schemaVersion end local function testLabel() @@ -33,9 +41,15 @@ { name = 'mw.wikibase.getEntity (type)', func = testGetEntityType, type='ToString', expect = { 'table' } }, + { name = 'mw.wikibase.getEntity (schema version)', func = testGetEntitySchemaVersion, + expect = { 1 } + }, { name = 'mw.wikibase.getEntityObject (type)', func = testGetEntityObjectType, type='ToString', expect = { 'table' } }, + { name = 'mw.wikibase.getEntityObject (schema version)', func = testGetEntityObjectSchemaVersion, + expect = { 2 } + }, { name = 'mw.wikibase.label', func = testLabel, type='ToString', expect = { 'Lua Test Item' } }, diff --git a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php index 68f7341..7a12c2c 100644 --- a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php +++ b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryTest.php @@ -49,20 +49,20 @@ public function testGetEntity() { $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(); - $entity = $luaWikibaseLibrary->getEntity( 'Q888' ); + $entity = $luaWikibaseLibrary->getEntity( 'Q888', false ); $this->assertEquals( array( null ), $entity ); } public function testGetEntityInvalidIdType() { $this->setExpectedException( 'ScribuntoException' ); $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(); - $luaWikibaseLibrary->getEntity( array() ); + $luaWikibaseLibrary->getEntity( array(), false ); } public function testGetEntityInvalidEntityId() { $this->setExpectedException( 'ScribuntoException' ); $luaWikibaseLibrary = $this->newScribuntoLuaWikibaseLibrary(); - $luaWikibaseLibrary->getEntity( 'X888' ); + $luaWikibaseLibrary->getEntity( 'X888', false ); } public function testGetEntityId() { diff --git a/client/tests/phpunit/includes/scribunto/WikibaseLuaBindingsTest.php b/client/tests/phpunit/includes/scribunto/WikibaseLuaBindingsTest.php index 2ea5994..6665eee 100644 --- a/client/tests/phpunit/includes/scribunto/WikibaseLuaBindingsTest.php +++ b/client/tests/phpunit/includes/scribunto/WikibaseLuaBindingsTest.php @@ -83,7 +83,7 @@ $item2->setId( new ItemId( 'Q9999' ) ); return array( - array( array( 'id', 'type', 'descriptions', 'labels', 'sitelinks' ), $item, $entityLookup ), + array( array( 'id', 'type', 'descriptions', 'labels', 'sitelinks', 'schemaVersion' ), $item, $entityLookup ), array( array(), $item2, $entityLookup ) ); } -- To view, visit https://gerrit.wikimedia.org/r/116236 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ie52d887b9a2dda98187cc23ce400614ca1bf2fac Gerrit-PatchSet: 5 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Hoo man <h...@online.de> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: Jens Ohlig <jens.oh...@wikimedia.de> Gerrit-Reviewer: WikidataJenkins <wikidata-servi...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits