jenkins-bot has submitted this change and it was merged. ( 
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, 48 insertions(+), 13 deletions(-)

Approvals:
  jenkins-bot: Verified
  Thiemo Mättig (WMDE): Looks good to me, approved



diff --git a/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua 
b/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua
index 112cba9..989a572 100644
--- a/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua
+++ b/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua
@@ -26,6 +26,13 @@
        RANK_DEPRECATED = 0
 }
 
+-- Is this a valid property id (Pnnn)?
+--
+-- @param {string} propertyId
+local isValidPropertyId = function( propertyId )
+       return type( propertyId ) == 'string' and propertyId:match( 
'^P[1-9]%d*$' )
+end
+
 -- Function to mask an entity's claims table in order to log access
 -- to individual claims of an entity.
 -- Code for logging based on: http://www.lua.org/pil/13.4.4.html
@@ -39,20 +46,24 @@
        entity.claims = {}
 
        local pseudoClaimsMetatable = {}
-       pseudoClaimsMetatable.__index = function( emptyTable, propertyID )
-               php.addStatementUsage(entity.id, propertyID)
-               return actualEntityClaims[propertyID]
+       pseudoClaimsMetatable.__index = function( emptyTable, propertyId )
+               if isValidPropertyId( propertyId ) then
+                       -- Only attempt to track the usage if we have a valid 
property id.
+                       php.addStatementUsage( entity.id, propertyId )
+               end
+
+               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 isValidPropertyId( propertyId ) then
+                       php.addStatementUsage( entity.id, propertyId )
                end
-               return next( actualEntityClaims, propertyID )
+               return next( actualEntityClaims, propertyId )
        end
 
        pseudoClaimsMetatable.__pairs = function( emptyTable )
@@ -187,6 +198,12 @@
 --
 -- @param {string} propertyId
 methodtable.getBestStatements = function( entity, propertyId )
+       checkType( 'getBestStatements', 1, propertyId, 'string' )
+
+       if not isValidPropertyId( propertyId ) 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
@@ -240,7 +257,7 @@
        )
 
        local label
-       if propertyLabelOrId:match( '^P%d+$' ) then
+       if isValidPropertyId( propertyLabelOrId ) then
                label = mw.wikibase.label( propertyLabelOrId )
        end
 
diff --git 
a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua
 
b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua
index 2c173bf..3d20045 100644
--- 
a/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua
+++ 
b/client/tests/phpunit/includes/DataAccess/Scribunto/LuaWikibaseEntityLibraryTests.lua
@@ -211,7 +211,13 @@
 
 local function testClaimsNewIndex()
        local entity = mw.wikibase.getEntityObject( 'Q32487' )
-       entity['claims']['P321'] = ""
+       entity.claims['P321'] = ""
+end
+
+local function testClaimsAccessIndex( propertyId )
+       local entity = mw.wikibase.getEntityObject( 'Q32487' )
+
+       return entity.claims[propertyId]
 end
 
 
@@ -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.entity.claims pair content', func = 
testClaimsPairContent,
          expect = { {P321={}, P4321={}}, }
        },
-       { name = 'mw.wikibase.testClaimsNewIndex', func = testClaimsNewIndex,
+       { name = 'mw.wikibase.entity.claims new index', func = 
testClaimsNewIndex,
          expect = 'Entity cannot be modified'
+       },
+       { name = 'mw.wikibase.entity.claims access invalid index', func = 
testClaimsAccessIndex,
+         args = { 'something' },
+         expect = { nil }
        },
        { name = 'mw.wikibase.entity.create with empty table', func = 
testCreate,
          args = { {} },
@@ -332,6 +342,14 @@
          args = { {} },
          expect = "bad argument #1 to 'getSitelink' (string, number or nil 
expected, got table)"
        },
+       { name = 'mw.wikibase.entity.getBestStatements bad property id 1', func 
= testGetBestStatements, type='ToString',
+         args = { function() end },
+         expect = "bad argument #1 to 'getBestStatements' (string expected, 
got function)"
+       },
+       { name = 'mw.wikibase.entity.getBestStatements bad property id 2', func 
= testGetBestStatements, type='ToString',
+         args = { 'P01' },
+         expect = 'Invalid property id passed to 
mw.wikibase.entity.getBestStatements: "P01"'
+       },
        { 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: merged
Gerrit-Change-Id: I28971864dc664593eac1155485cf82e351f4ed63
Gerrit-PatchSet: 7
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: Eranroz <eranro...@gmail.com>
Gerrit-Reviewer: Halfak <ahalfa...@wikimedia.org>
Gerrit-Reviewer: Hall1467 <hall1...@umn.edu>
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

Reply via email to