Hoo man has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/371070 )
Change subject: Don't throw if someone accesses mw.wikibase.entity.notAPropertyId ...................................................................... Don't throw if someone accesses mw.wikibase.entity.notAPropertyId Also make getBestStatements complain if the passed property id is not valid. Change-Id: I28971864dc664593eac1155485cf82e351f4ed63 --- M client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua M client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua 2 files changed, 36 insertions(+), 11 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/70/371070/1 diff --git a/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua b/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua index 112cba9..5859056 100644 --- a/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua +++ b/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua @@ -39,20 +39,25 @@ entity.claims = {} local pseudoClaimsMetatable = {} - pseudoClaimsMetatable.__index = function( emptyTable, propertyID ) - php.addStatementUsage(entity.id, propertyID) - return actualEntityClaims[propertyID] + pseudoClaimsMetatable.__index = function( emptyTable, propertyId ) + if not propertyId:match( '^P%d+$' ) then + -- Don't even attempt to track the usage if we don't have a valid property id. + return actualEntityClaims[propertyId] + end + + php.addStatementUsage(entity.id, propertyId) + return actualEntityClaims[propertyId] end - pseudoClaimsMetatable.__newindex = function( emptyTable, propertyID, data ) + pseudoClaimsMetatable.__newindex = function( emptyTable, propertyId, data ) error( 'Entity cannot be modified' ) end - local logNext = function( emptyTable, propertyID ) - if propertyID ~= nil then - php.addStatementUsage(entity.id, propertyID) + local logNext = function( emptyTable, propertyId ) + if propertyId ~= nil then + php.addStatementUsage( entity.id, propertyId ) end - return next( actualEntityClaims, propertyID ) + return next( actualEntityClaims, propertyId ) end pseudoClaimsMetatable.__pairs = function( emptyTable ) @@ -187,6 +192,12 @@ -- -- @param {string} propertyId methodtable.getBestStatements = function( entity, propertyId ) + checkType( 'getBestStatements', 1, propertyId, 'string' ) + + if not propertyId:match( '^P%d+$' ) then + error('Invalid property id passed to mw.wikibase.entity.getBestStatements: "' .. propertyId .. '"') + end + if entity.claims == nil or not entity.claims[propertyId] then return {} end diff --git a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua index 2c173bf..c0f7311 100644 --- a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua +++ b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua @@ -214,6 +214,12 @@ entity['claims']['P321'] = "" end +local function testClaimsAccessIndex(propertyId) + local entity = mw.wikibase.getEntityObject( 'Q32487' ) + + return entity['claims'][propertyId] +end + local tests = { -- Unit Tests @@ -221,14 +227,18 @@ { name = 'mw.wikibase.entity exists', func = testExists, type='ToString', expect = { 'table' } }, - { name = 'mw.wikibase.testClaimsPairSize', func = testClaimsPairSize, + { name = 'mw.wikibase.entity.claims pair size', func = testClaimsPairSize, expect = { 1 } }, - { name = 'mw.wikibase.testClaimsPairContent', func = testClaimsPairContent, + { name = 'mw.wikibase.claims pair content', func = testClaimsPairContent, expect = { {P321={}, P4321={}}, } }, - { name = 'mw.wikibase.testClaimsNewIndex', func = testClaimsNewIndex, + { name = 'mw.wikibase.claims new index', func = testClaimsNewIndex, expect = 'Entity cannot be modified' + }, + { name = 'mw.wikibase.claims access invalid index', func = testClaimsAccessIndex, + args = { 'something' }, + expect = { nil } }, { name = 'mw.wikibase.entity.create with empty table', func = testCreate, args = { {} }, @@ -332,6 +342,10 @@ args = { {} }, expect = "bad argument #1 to 'getSitelink' (string, number or nil expected, got table)" }, + { name = 'mw.wikibase.entity.getBestStatements invalid property id', func = testGetBestStatements, type='ToString', + args = { 'dsfkljsd' }, + expect = 'Invalid property id passed to mw.wikibase.entity.getBestStatements: "dsfkljsd"' + }, { name = 'mw.wikibase.entity.getBestStatements 1', func = testGetBestStatements, args = { 'P321' }, expect = { {} } -- To view, visit https://gerrit.wikimedia.org/r/371070 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I28971864dc664593eac1155485cf82e351f4ed63 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