[MediaWiki-commits] [Gerrit] mediawiki...Wikibase[master]: Don't throw if someone accesses mw.wikibase.entity.notAPrope...

2017-08-11 Thread jenkins-bot (Code Review)
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 = { 

[MediaWiki-commits] [Gerrit] mediawiki...Wikibase[master]: Don't throw if someone accesses mw.wikibase.entity.notAPrope...

2017-08-10 Thread Hoo man (Code Review)
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