Eranroz has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/371651 )
Change subject: xkill - lazy track labels/sitelinks/claims ...................................................................... xkill - lazy track labels/sitelinks/claims Hide labels/sitelinks similar to claims in lazy table and log access usage. xkill usage from EntityAccessor Bug: T172914 Change-Id: Iac5ac53edeb4616cae1b4aa8be5ffabd2d72032d --- M client/includes/DataAccess/Scribunto/EntityAccessor.php M client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php M client/includes/DataAccess/Scribunto/WikibaseLuaEntityBindings.php M client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua M client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php 5 files changed, 100 insertions(+), 40 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/51/371651/1 diff --git a/client/includes/DataAccess/Scribunto/EntityAccessor.php b/client/includes/DataAccess/Scribunto/EntityAccessor.php index 6a48c09..23f49c6 100644 --- a/client/includes/DataAccess/Scribunto/EntityAccessor.php +++ b/client/includes/DataAccess/Scribunto/EntityAccessor.php @@ -120,8 +120,6 @@ $entityId = $this->entityIdParser->parse( $prefixedEntityId ); - $this->usageAccumulator->addAllUsage( $entityId ); - try { $entityObject = $this->entityLookup->getEntity( $entityId ); } catch ( RevisionedUnresolvedRedirectException $ex ) { diff --git a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php index a2b2c94..54db1c6 100644 --- a/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php +++ b/client/includes/DataAccess/Scribunto/Scribunto_LuaWikibaseEntityLibrary.php @@ -135,6 +135,25 @@ } /** + * Add a label usage (called once specific labels are accessed). + * + * @param string $entityId The Entity from which the statements were accessed. + * @param string $langCode Language code of the labels accessed. + */ + public function addLabelUsage( $entityId, $langCode ) { + $this->getImplementation()->addLabelUsage( $entityId, $langCode ); + } + + /** + * Add a sitelinks usage (called once specific sitelinks are accessed). + * + * @param string $entityId The Entity from which the statements were accessed. + */ + public function addSiteLinksUsage( $entityId ) { + $this->getImplementation()->addSiteLinksUsage( $entityId ); + } + + /** * Register mw.wikibase.entity.lua library * * @return array @@ -149,6 +168,8 @@ 'formatStatements' => [ $this, 'formatStatements' ], 'formatPropertyValues' => [ $this, 'formatPropertyValues' ], 'addStatementUsage' => [ $this, 'addStatementUsage' ], + 'addLabelUsage' => [ $this, 'addLabelUsage' ], + 'addSiteLinksUsage' => [ $this, 'addSiteLinksUsage' ], ]; return $this->getEngine()->registerInterface( diff --git a/client/includes/DataAccess/Scribunto/WikibaseLuaEntityBindings.php b/client/includes/DataAccess/Scribunto/WikibaseLuaEntityBindings.php index a2715ca..6f2cac1 100644 --- a/client/includes/DataAccess/Scribunto/WikibaseLuaEntityBindings.php +++ b/client/includes/DataAccess/Scribunto/WikibaseLuaEntityBindings.php @@ -123,6 +123,28 @@ } /** + * Add a label usage (called once specific labels are accessed). + * + * @param string $entityId The Entity from which the statements were accessed. + * @param string $langCode Language code the labels accessed. + */ + public function addLabelUsage( $entityId, $langCode ) { + $entityId = $this->entityIdParser->parse( $entityId ); + //TODO Eran: need to parse langcode for validity? + $this->usageAccumulator->addLabelUsage( $entityId, $langCode ); + } + + /** + * Add a statement usage (called once specific statements are accessed). + * + * @param string $entityId The Entity from which the statements were accessed. + */ + public function addSiteLinksUsage( $entityId ) { + $entityId = $this->entityIdParser->parse( $entityId ); + $this->usageAccumulator->addSiteLinksUsage( $entityId ); + } + + /** * Get global site ID (e.g. "enwiki") * This is basically a helper function. * @TODO: Make this part of mw.site in the Scribunto extension. diff --git a/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua b/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua index 989a572..f555d55 100644 --- a/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua +++ b/client/includes/DataAccess/Scribunto/mw.wikibase.entity.lua @@ -33,44 +33,78 @@ 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. + +-- Log access to claims of entity +-- +-- @param {string} entityId +-- @param {string} propertyId +local addPropertyUsage = function(entityId, propertyId ) + if isValidPropertyId( propertyId ) then + -- Only attempt to track the usage if we have a valid property id. + php.addStatementUsage( entityId, propertyId ) + end +end + +-- Log access to labels of entity +-- +-- @param {string} entityId +-- @param {string} langCode +local addLabelUsage = function(entityId, langCode ) + php.addLabelUsage( entityId, langCode ) +end + + +-- Log access to sitelinks of entity +-- +-- @param {string} entityId +local addSiteLinksUsage = function( entityId ) + php.addSiteLinksUsage( entityId ) +end + + +-- Function to mask an entity's subtables in order to log access -- Code for logging based on: http://www.lua.org/pil/13.4.4.html -- -- @param {table} entity -local maskClaimsTable = function( entity ) - if entity.claims == nil then +-- @param {string} tableName +-- @param usage function +local maskEntityTable = function( entity, tableName, usageFunc ) + if entity[tableName] == nil then return entity end - local actualEntityClaims = entity.claims - entity.claims = {} + local actualEntityTable = entity[tableName] + entity[tableName] = {} - local pseudoClaimsMetatable = {} - 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] + local pseudoTableMetatable = {} + pseudoTableMetatable.__index = function( emptyTable, key ) + usageFunc( entity.id, key ) + return actualEntityTable[key] end - pseudoClaimsMetatable.__newindex = function( emptyTable, propertyId, data ) + pseudoTableMetatable.__newindex = function( emptyTable, key, data ) error( 'Entity cannot be modified' ) end - local logNext = function( emptyTable, propertyId ) - if isValidPropertyId( propertyId ) then - php.addStatementUsage( entity.id, propertyId ) - end - return next( actualEntityClaims, propertyId ) + local logNext = function( emptyTable, key ) + usageFunc( entity.id, key ) + return next( actualEntityTable, key ) end - pseudoClaimsMetatable.__pairs = function( emptyTable ) + pseudoTableMetatable.__pairs = function( emptyTable ) return logNext, {}, nil end - setmetatable( entity.claims, pseudoClaimsMetatable ) + setmetatable( entity[tableName], pseudoTableMetatable ) + return entity +end + +-- Function to mask an entity's subtables in order to log access and prevent modifications +-- +-- @param {table} entity +local maskEntityTables = function (entity ) + entity = maskEntityTable( entity, 'claims', addPropertyUsage ) + entity = maskEntityTable( entity, 'labels', addLabelUsage ) + entity = maskEntityTable( entity, 'sitelinks', addSiteLinksUsage ) return entity end @@ -91,7 +125,7 @@ error( 'mw.wikibase.entity must not be constructed using legacy data' ) end - local entity = maskClaimsTable( data ) + local entity = maskEntityTables( data ) setmetatable( entity, metatable ) return entity diff --git a/client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php b/client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php index 67c50c1..c0492b8 100644 --- a/client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php +++ b/client/tests/phpunit/includes/DataAccess/Scribunto/EntityAccessorTest.php @@ -104,21 +104,6 @@ } } - public function testGetEntity_usage() { - $item = $this->getItem(); - $itemId = $item->getId(); - - $entityLookup = new MockRepository(); - - $usages = new HashUsageAccumulator(); - $entityAccessor = $this->getEntityAccessor( $entityLookup, $usages ); - - $entityAccessor->getEntity( $itemId->getSerialization() ); - $this->assertTrue( - $this->hasUsage( $usages->getUsages(), $item->getId(), EntityUsage::ALL_USAGE ), 'all usage' - ); - } - public function getEntityProvider() { $item = $this->getItem(); -- To view, visit https://gerrit.wikimedia.org/r/371651 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iac5ac53edeb4616cae1b4aa8be5ffabd2d72032d Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Eranroz <eranro...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits