jenkins-bot has submitted this change and it was merged.

Change subject: Factor database access out of WikiPageEntityRevisionLookup
......................................................................


Factor database access out of WikiPageEntityRevisionLookup

Introduces WikiPageEntityMetaDataLookup which is able to retrieve
the meta data that WikiPageEntityRevisionLookup needs for both accessing
entities and to verify that an entity revision from cache is still
the latest version.

WikiPageEntityMetaDataLookup also supports getting meta data for
multiple entities at once, but we're not yet making use of that.
That will be used for implementing T87238.

Change-Id: Ie7f76921e1d1ffd2e67c33c597e9116f2148ed3d
---
M client/includes/store/sql/DirectSqlStore.php
A lib/includes/store/sql/WikiPageEntityMetaDataLookup.php
M lib/includes/store/sql/WikiPageEntityRevisionLookup.php
M repo/includes/store/sql/SqlStore.php
M repo/tests/phpunit/includes/store/WikiPageEntityRevisionLookupTest.php
A repo/tests/phpunit/includes/store/sql/WikiPageEntityMetaDataLookupTest.php
M repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
7 files changed, 432 insertions(+), 194 deletions(-)

Approvals:
  Daniel Kinzler: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/client/includes/store/sql/DirectSqlStore.php 
b/client/includes/store/sql/DirectSqlStore.php
index 514e3f9..8085351 100644
--- a/client/includes/store/sql/DirectSqlStore.php
+++ b/client/includes/store/sql/DirectSqlStore.php
@@ -25,6 +25,7 @@
 use Wikibase\Lib\Store\RevisionBasedEntityLookup;
 use Wikibase\Lib\Store\SiteLinkLookup;
 use Wikibase\Lib\Store\SiteLinkTable;
+use Wikibase\Lib\Store\WikiPageEntityMetaDataLookup;
 use Wikibase\Lib\Store\WikiPageEntityRevisionLookup;
 use Wikibase\Store\EntityIdLookup;
 
@@ -311,7 +312,7 @@
 
                $rawLookup = new WikiPageEntityRevisionLookup(
                        $this->contentCodec,
-                       $this->entityIdParser,
+                       new WikiPageEntityMetaDataLookup( 
$this->entityIdParser, $this->repoWiki ),
                        $this->repoWiki
                );
 
diff --git a/lib/includes/store/sql/WikiPageEntityMetaDataLookup.php 
b/lib/includes/store/sql/WikiPageEntityMetaDataLookup.php
new file mode 100644
index 0000000..246566a
--- /dev/null
+++ b/lib/includes/store/sql/WikiPageEntityMetaDataLookup.php
@@ -0,0 +1,242 @@
+<?php
+
+namespace Wikibase\Lib\Store;
+
+use DBAccessBase;
+use DatabaseBase;
+use DBQueryError;
+use ResultWrapper;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdParser;
+
+/**
+ * Service for looking up meta data about one or more entities as needed for
+ * loading entities from WikiPages (via Revision) or to verify an entity 
against
+ * page.page_latest.
+ *
+ * @since 0.5
+ *
+ * @license GNU GPL v2+
+ * @author Daniel Kinzler
+ * @author Marius Hoch < h...@online.de >
+ */
+class WikiPageEntityMetaDataLookup extends DBAccessBase {
+
+       /**
+        * @var EntityIdParser
+        */
+       private $entityIdParser;
+
+       /**
+        * @param EntityIdParser $entityIdParser
+        * @param string|bool $wiki The name of the wiki database to use (use 
false for the local wiki)
+        */
+       public function __construct(
+               EntityIdParser $entityIdParser,
+               $wiki = false
+       ) {
+               parent::__construct( $wiki );
+
+               // TODO: migrate table away from using a numeric field so we no 
longer need this!
+               $this->entityIdParser = $entityIdParser;
+       }
+
+       /**
+        * @param EntityId[] $entityIds
+        * @param string $mode (EntityRevisionLookup::LATEST_FROM_SLAVE or 
EntityRevisionLookup::LATEST_FROM_MASTER)
+        *
+        * @throws DBQueryError
+        * @return array entity id serialization -> stdClass or false if no 
such entity exists
+        */
+       public function loadRevisionInformation( array $entityIds, $mode ) {
+               $rows = array();
+
+               if ( $mode !== EntityRevisionLookup::LATEST_FROM_MASTER ) {
+                       $rows = $this->selectRevisionInformationMultiple( 
$entityIds, DB_SLAVE );
+               }
+
+               $loadFromMaster = array();
+               foreach ( $entityIds as $entityId ) {
+                       if ( !isset( $rows[$entityId->getSerialization()] ) || 
!$rows[$entityId->getSerialization()] ) {
+                               $loadFromMaster[] = $entityId;
+                       }
+               }
+
+               if ( $loadFromMaster ) {
+                       $rows = array_merge(
+                               $rows,
+                               $this->selectRevisionInformationMultiple( 
$loadFromMaster, DB_MASTER )
+                       );
+               }
+
+               return $rows;
+       }
+
+       /**
+        * @param EntityId $entityId
+        * @param string $mode (EntityRevisionLookup::LATEST_FROM_SLAVE or 
EntityRevisionLookup::LATEST_FROM_MASTER)
+        *
+        * @return stdClass|false false if no such entity exists
+        */
+       public function loadRevisionInformationByEntityId( EntityId $entityId, 
$mode ) {
+               $rows = $this->loadRevisionInformation( array( $entityId ), 
$mode );
+               return $rows[$entityId->getSerialization()];
+       }
+
+       /**
+        * @param EntityId $entityId
+        * @param int $revisionId
+        *
+        * @throws DBQueryError
+        * @return object|bool
+        */
+       public function loadRevisionInformationByRevisionId( EntityId 
$entityId, $revisionId ) {
+               $row = $this->selectRevisionInformationById( $entityId, 
$revisionId, DB_SLAVE );
+
+               if ( !$row ) {
+                       // Try loading from master
+                       wfDebugLog(  __CLASS__, __FUNCTION__ . ': try to load ' 
. $entityId
+                               . " with $revisionId from DB_MASTER." );
+
+                       $row = $this->selectRevisionInformationById( $entityId, 
$revisionId, DB_MASTER );
+               }
+
+               return $row;
+       }
+
+       /**
+        * Fields we need to select to load a revision
+        *
+        * @return string[]
+        */
+       private function selectFields() {
+               return array(
+                       'rev_id',
+                       'rev_content_format',
+                       'rev_timestamp',
+                       'page_latest',
+                       'old_id',
+                       'old_text',
+                       'old_flags'
+               );
+       }
+
+       /**
+        * Selects revision information from the page, revision, and text 
tables.
+        *
+        * @param EntityId $entityId The entity to query the DB for.
+        * @param int $revisionId The desired revision id
+        * @param int $connType DB_SLAVE or DB_MASTER
+        *
+        * @throws DBQueryError If the query fails.
+        * @return object|bool a raw database row object, or false if no such 
entity revision exists.
+        */
+       private function selectRevisionInformationById( EntityId $entityId, 
$revisionId, $connType ) {
+               $db = $this->getConnection( $connType );
+
+               $join = array();
+
+               // pick text via rev_text_id
+               $join['text'] = array( 'INNER JOIN', 'old_id=rev_text_id' );
+               $join['page'] = array( 'INNER JOIN', 'rev_page=page_id' );
+
+               wfDebugLog( __CLASS__, __FUNCTION__ . ": Looking up revision 
$revisionId of " . $entityId );
+
+               $row = $db->selectRow(
+                       array( 'revision', 'text', 'page' ),
+                       $this->selectFields(),
+                       array( 'rev_id' => $revisionId ),
+                       __METHOD__,
+                       array(),
+                       $join
+               );
+
+               $this->releaseConnection( $db );
+
+               return $row;
+       }
+
+       /**
+        * Selects revision information from the page, revision, and text 
tables.
+        * Returns an array like entityid -> object or false (if not found).
+        *
+        * @param EntityId[] $entityIds The entities to query the DB for.
+        * @param int $connType DB_SLAVE or DB_MASTER
+        *
+        * @throws DBQueryError If the query fails.
+        * @return array entity id serialization -> stdClass or false if no 
such entity exists
+        */
+       private function selectRevisionInformationMultiple( array $entityIds, 
$connType ) {
+               $db = $this->getConnection( $connType );
+
+               $join = array();
+               $fields = $this->selectFields();
+               // To be able to link rows with entity ids
+               $fields[] = 'epp_entity_id';
+               $fields[] = 'epp_entity_type';
+
+               $tables = array( 'page', 'revision', 'text', 
'wb_entity_per_page' );
+
+               // pick page via epp_page_id
+               $join['page'] = array( 'INNER JOIN', 'epp_page_id=page_id' );
+
+               // pick latest revision via page_latest
+               $join['revision'] = array( 'INNER JOIN', 'page_latest=rev_id' );
+
+               // pick text via rev_text_id
+               $join['text'] = array( 'INNER JOIN', 'old_id=rev_text_id' );
+
+               $res = $db->select( $tables, $fields, $this->getEppWhere( 
$entityIds, $db ), __METHOD__, array(), $join );
+
+               $this->releaseConnection( $db );
+
+               return $this->indexResultByEntityId( $entityIds, $res );
+       }
+
+       /**
+        * Takes a ResultWrapper and indexes the returned rows based on the 
serialized
+        * entity id of the entities they refer to.
+        *
+        * @param EntityId[] $entityIds
+        * @param ResultWrapper $res
+        *
+        * @return array entity id serialization -> stdClass or false if no 
such entity exists
+        */
+       private function indexResultByEntityId( array $entityIds, ResultWrapper 
$res ) {
+               $rows = array();
+               // Create a key based map from the rows just returned to reduce
+               // the complexity below.
+               foreach ( $res as $row ) {
+                       $rows[$row->epp_entity_type . $row->epp_entity_id] = 
$row;
+               }
+
+               $result = array();
+               foreach ( $entityIds as $entityId ) {
+                       $result[$entityId->getSerialization()] = false;
+
+                       $key = $entityId->getEntityType() . 
$entityId->getNumericId();
+                       if ( isset( $rows[$key] ) ) {
+                               $result[$entityId->getSerialization()] = 
$rows[$key];
+                       }
+               }
+
+               return $result;
+       }
+
+       /**
+        * @param EntityId[] $entityIds
+        * @param DatabaseBase $db
+        * @return string
+        */
+       private function getEppWhere( array $entityIds, DatabaseBase $db ) {
+               foreach ( $entityIds as &$entityId ) {
+                       $where[] = $db->makeList( array(
+                               'epp_entity_id' => $entityId->getNumericId(),
+                               'epp_entity_type' => $entityId->getEntityType()
+                       ), LIST_AND );
+               }
+
+               return $db->makeList( $where, LIST_OR );
+       }
+
+}
diff --git a/lib/includes/store/sql/WikiPageEntityRevisionLookup.php 
b/lib/includes/store/sql/WikiPageEntityRevisionLookup.php
index 26f6316..e89f889 100644
--- a/lib/includes/store/sql/WikiPageEntityRevisionLookup.php
+++ b/lib/includes/store/sql/WikiPageEntityRevisionLookup.php
@@ -3,11 +3,10 @@
 namespace Wikibase\Lib\Store;
 
 use DBAccessBase;
-use DBQueryError;
 use MWContentSerializationException;
 use Revision;
 use Wikibase\DataModel\Entity\EntityId;
-use Wikibase\DataModel\Entity\EntityIdParser;
+use Wikibase\Lib\Store\WikiPageEntityMetaDataLookup;
 use Wikibase\EntityRevision;
 
 /**
@@ -23,31 +22,30 @@
 class WikiPageEntityRevisionLookup extends DBAccessBase implements 
EntityRevisionLookup {
 
        /**
-        * @var EntityIdParser
-        */
-       private $entityIdParser;
-
-       /**
         * @var EntityContentDataCodec
         */
        private $contentCodec;
 
        /**
+        * @var WikiPageEntityMetaDataLookup
+        */
+       private $entityMetaDataLookup;
+
+       /**
         * @param EntityContentDataCodec $contentCodec
-        * @param EntityIdParser $entityIdParser
+        * @param WikiPageEntityMetaDataLookup $entityMetaDataLookup
         * @param string|bool $wiki The name of the wiki database to use (use 
false for the local wiki)
         */
        public function __construct(
                EntityContentDataCodec $contentCodec,
-               EntityIdParser $entityIdParser,
+               WikiPageEntityMetaDataLookup $entityMetaDataLookup,
                $wiki = false
        ) {
                parent::__construct( $wiki );
 
                $this->contentCodec = $contentCodec;
 
-               // TODO: migrate table away from using a numeric field so we no 
longer need this!
-               $this->entityIdParser = $entityIdParser;
+               $this->entityMetaDataLookup = $entityMetaDataLookup;
        }
 
        /**
@@ -74,7 +72,11 @@
                /** @var EntityRevision $entityRevision */
                $entityRevision = null;
 
-               $row = $this->loadRevisionRow( $entityId, $revisionId );
+               if ( is_int( $revisionId ) ) {
+                       $row = 
$this->entityMetaDataLookup->loadRevisionInformationByRevisionId( $entityId, 
$revisionId );
+               } else {
+                       $row = 
$this->entityMetaDataLookup->loadRevisionInformationByEntityId( $entityId, 
$revisionId );
+               }
 
                if ( $row ) {
                        /** @var EntityRedirect $redirect */
@@ -120,188 +122,14 @@
         * @return int|false
         */
        public function getLatestRevisionId( EntityId $entityId, $mode = 
self::LATEST_FROM_SLAVE ) {
-               $row = null;
+               $rows = $this->entityMetaDataLookup->loadRevisionInformation( 
array( $entityId ), $mode );
+               $row = $rows[$entityId->getSerialization()];
 
-               if ( $mode !== self::LATEST_FROM_MASTER ) {
-                       $row = $this->selectPageLatest( $entityId, DB_SLAVE );
-               }
-
-               if ( !$row ) {
-                       $row = $this->selectPageLatest( $entityId, DB_MASTER );
-               }
-
-               if ( $row ) {
+               if ( $row && $row->page_latest ) {
                        return (int)$row->page_latest;
                }
 
                return false;
-       }
-
-       /**
-        * @param EntityId $entityId
-        * @param int|string $revisionId
-        *
-        * @throws DBQueryError
-        * @return object|null
-        */
-       private function loadRevisionRow( EntityId $entityId, $revisionId ) {
-               $row = null;
-
-               if ( $revisionId !== self::LATEST_FROM_MASTER ) {
-                       $row = $this->selectRevisionRow( $entityId, 
$revisionId, DB_SLAVE );
-               }
-
-               if ( !$row ) {
-                       // try loading from master
-                       wfDebugLog(  __CLASS__, __FUNCTION__ . ': try to load ' 
. $entityId
-                               . " with $revisionId from DB_MASTER." );
-
-                       $row = $this->selectRevisionRow( $entityId, 
$revisionId, DB_MASTER );
-               }
-
-               return $row;
-       }
-
-       /**
-        * Fields we need to select to load a revision
-        *
-        * @return string[]
-        */
-       private function selectFields() {
-               return array(
-                       'rev_id',
-                       'rev_content_format',
-                       'rev_timestamp',
-                       'old_id',
-                       'old_text',
-                       'old_flags'
-               );
-       }
-
-       /**
-        * Selects revision information from the page, revision, and text 
tables.
-        *
-        * @param EntityId $entityId The entity to query the DB for.
-        * @param int|string $revisionId The desired revision id, or 
LATEST_FROM_SLAVE or LATEST_FROM_MASTER.
-        * @param int $connType DB_SLAVE or DB_MASTER
-        *
-        * @throws DBQueryError If the query fails.
-        * @return object|null a raw database row object, or null if no such 
entity revision exists.
-        */
-       private function selectRevisionRow( EntityId $entityId, $revisionId, 
$connType ) {
-               $db = $this->getConnection( $connType );
-
-               $where = array();
-               $join = array();
-
-               if ( is_int( $revisionId ) ) {
-                       $tables = array( 'revision', 'text' );
-
-                       // pick revision by id
-                       $where['rev_id'] = $revisionId;
-
-                       // pick text via rev_text_id
-                       $join['text'] = array( 'INNER JOIN', 
'old_id=rev_text_id' );
-
-                       wfDebugLog( __CLASS__, __FUNCTION__ . ": Looking up 
revision $revisionId of " . $entityId );
-               } else {
-                       $tables = array( 'page', 'revision', 'text', 
'wb_entity_per_page' );
-
-                       $entityId = $this->getProperEntityId( $entityId );
-
-                       // pick entity by id
-                       $where['epp_entity_id'] = $entityId->getNumericId();
-                       $where['epp_entity_type'] = $entityId->getEntityType();
-
-                       // pick page via epp_page_id
-                       $join['page'] = array( 'INNER JOIN', 
'epp_page_id=page_id' );
-
-                       // pick latest revision via page_latest
-                       $join['revision'] = array( 'INNER JOIN', 
'page_latest=rev_id' );
-
-                       // pick text via rev_text_id
-                       $join['text'] = array( 'INNER JOIN', 
'old_id=rev_text_id' );
-
-                       wfDebugLog( __CLASS__, __FUNCTION__ . ': Looking up 
latest revision of ' . $entityId );
-               }
-
-               $res = $db->select( $tables, $this->selectFields(), $where, 
__METHOD__, array(), $join );
-
-               if ( !$res ) {
-                       // this can only happen if the DB is set to ignore 
errors, which shouldn't be the case...
-                       $error = $db->lastError();
-                       $errno = $db->lastErrno();
-
-                       throw new DBQueryError( $db, $error, $errno, '', 
__METHOD__ );
-               }
-
-               $this->releaseConnection( $db );
-
-               if ( !( $row = $res->fetchObject() ) ) {
-                       $row = null;
-               }
-
-               return $row;
-       }
-
-       /**
-        * Selects page information from the page table.
-        *
-        * @since 0.4
-        *
-        * @param EntityId $entityId The entity to query the DB for.
-        * @param int $connType DB_SLAVE or DB_MASTER
-        *
-        * @throws DBQueryError If the query fails.
-        * @return object|null a raw database row object, or null if no such 
entity revision exists.
-        */
-       protected function selectPageLatest( EntityId $entityId, $connType = 
DB_SLAVE ) {
-               $db = $this->getConnection( $connType );
-
-               $tables = array(
-                       'page',
-                       'wb_entity_per_page',
-               );
-
-               $where = array();
-               $join = array();
-
-               $entityId = $this->getProperEntityId( $entityId );
-
-               // pick entity by id
-               $where['epp_entity_id'] = $entityId->getNumericId();
-               $where['epp_entity_type'] = $entityId->getEntityType();
-
-               // pick page via epp_page_id
-               $join['page'] = array( 'INNER JOIN', 'epp_page_id=page_id' );
-
-               $res = $db->select( $tables, 'page_latest', $where, __METHOD__, 
array(), $join );
-
-               if ( !$res ) {
-                       // this can only happen if the DB is set to ignore 
errors, which shouldn't be the case...
-                       $error = $db->lastError();
-                       $errno = $db->lastErrno();
-                       throw new DBQueryError( $db, $error, $errno, '', 
__METHOD__ );
-               }
-
-               $this->releaseConnection( $db );
-
-               if ( !( $row = $res->fetchObject() ) ) {
-                       $row = null;
-               }
-
-               return $row;
-       }
-
-       /**
-        * @todo: migrate table away from using a numeric field & get rid of 
this function
-        *
-        * @param EntityId $id
-        *
-        * @return mixed
-        */
-       protected function getProperEntityId( EntityId $id ) {
-               return $this->entityIdParser->parse( $id->getSerialization() );
        }
 
        /**
diff --git a/repo/includes/store/sql/SqlStore.php 
b/repo/includes/store/sql/SqlStore.php
index 1a84ebb..262a224 100644
--- a/repo/includes/store/sql/SqlStore.php
+++ b/repo/includes/store/sql/SqlStore.php
@@ -24,6 +24,7 @@
 use Wikibase\Lib\Store\SiteLinkCache;
 use Wikibase\Lib\Store\SiteLinkTable;
 use Wikibase\Lib\Store\Sql\SqlEntityInfoBuilderFactory;
+use Wikibase\Lib\Store\WikiPageEntityMetaDataLookup;
 use Wikibase\Lib\Store\WikiPageEntityRevisionLookup;
 use Wikibase\Repo\Store\DispatchingEntityStoreWatcher;
 use Wikibase\Repo\Store\EntityPerPage;
@@ -288,7 +289,7 @@
 
                $wikiPageEntityLookup = new WikiPageEntityRevisionLookup(
                        $contentCodec,
-                       $wikibaseRepo->getEntityIdParser(),
+                       new WikiPageEntityMetaDataLookup( 
$wikibaseRepo->getEntityIdParser() ),
                        false
                );
 
@@ -579,7 +580,7 @@
 
                $rawLookup = new WikiPageEntityRevisionLookup(
                        $this->contentCodec,
-                       $this->entityIdParser,
+                       new WikiPageEntityMetaDataLookup( $this->entityIdParser 
),
                        false
                );
 
diff --git 
a/repo/tests/phpunit/includes/store/WikiPageEntityRevisionLookupTest.php 
b/repo/tests/phpunit/includes/store/WikiPageEntityRevisionLookupTest.php
index ee121b4..546de21 100644
--- a/repo/tests/phpunit/includes/store/WikiPageEntityRevisionLookupTest.php
+++ b/repo/tests/phpunit/includes/store/WikiPageEntityRevisionLookupTest.php
@@ -7,6 +7,7 @@
 use Wikibase\EntityRevision;
 use Wikibase\Lib\Store\EntityLookup;
 use Wikibase\Lib\Store\EntityRedirect;
+use Wikibase\Lib\Store\WikiPageEntityMetaDataLookup;
 use Wikibase\Lib\Store\WikiPageEntityRevisionLookup;
 use Wikibase\Repo\WikibaseRepo;
 
@@ -75,7 +76,7 @@
 
                return new WikiPageEntityRevisionLookup(
                        
WikibaseRepo::getDefaultInstance()->getEntityContentDataCodec(),
-                       new BasicEntityIdParser(),
+                       new WikiPageEntityMetaDataLookup( new 
BasicEntityIdParser() ),
                        false
                );
        }
diff --git 
a/repo/tests/phpunit/includes/store/sql/WikiPageEntityMetaDataLookupTest.php 
b/repo/tests/phpunit/includes/store/sql/WikiPageEntityMetaDataLookupTest.php
new file mode 100644
index 0000000..83c90d8
--- /dev/null
+++ b/repo/tests/phpunit/includes/store/sql/WikiPageEntityMetaDataLookupTest.php
@@ -0,0 +1,164 @@
+<?php
+
+namespace Wikibase\Repo\Tests\Store\Sql;
+
+use MediaWikiTestCase;
+use Wikibase\DataModel\Entity\BasicEntityIdParser;
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Term\AliasGroupList;
+use Wikibase\DataModel\Term\Fingerprint;
+use Wikibase\DataModel\Term\TermList;
+use Wikibase\Lib\Store\WikiPageEntityMetaDataLookup;
+use Wikibase\Repo\WikibaseRepo;
+use Wikibase\StringNormalizer;
+use Wikibase\Term;
+use Wikibase\TermSqlIndex;
+
+/**
+ * This test needs to be in repo, although the class is in lib as we can't 
alter
+ * the data without repo functionality.
+ *
+ * @covers Wikibase\Lib\Store\WikiPageEntityMetaDataLookup
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ * @group WikibaseStore
+ * @group Database
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < h...@online.de >
+ */
+class WikiPageEntityMetaDataLookupTest extends MediaWikiTestCase {
+
+       /**
+        * @var EntityRevision[]
+        */
+       private $data = array();
+
+       protected function setUp() {
+               parent::setUp();
+
+               if ( !$this->data ) {
+                       global $wgUser;
+
+                       $store = 
WikibaseRepo::getDefaultInstance()->getEntityStore();
+                       for ( $i = 0; $i < 3; $i++ ) {
+                               $this->data[] = $store->saveEntity( new Item(), 
'WikiPageEntityMetaDataLookupTest', $wgUser, EDIT_NEW );
+                       }
+
+                       $entity = $this->data[2]->getEntity();
+                       $entity->getFingerprint()->setLabel( 'en', 'Updated' );
+                       $this->data[2] = $store->saveEntity( $entity, 
'WikiPageEntityMetaDataLookupTest', $wgUser );
+               }
+       }
+
+       /**
+        * @return WikiPageEntityMetaDataLookup
+        */
+       private function getWikiPageEntityMetaDataLookup() {
+               return new WikiPageEntityMetaDataLookup( new 
BasicEntityIdParser() );
+       }
+
+       public function testLoadRevisionInformationById_latest() {
+               $entityRevision = $this->data[0];
+
+               $result = $this->getWikiPageEntityMetaDataLookup()
+                       ->loadRevisionInformationByRevisionId( 
$entityRevision->getEntity()->getId(), $entityRevision ->getRevisionId() );
+
+               $this->assertEquals( $entityRevision->getRevisionId(), 
$result->rev_id );
+               $this->assertEquals( $entityRevision->getRevisionId(), 
$result->page_latest );
+       }
+
+       public function testLoadRevisionInformationById_old() {
+               $entityRevision = $this->data[2];
+
+               $result = $this->getWikiPageEntityMetaDataLookup()
+                       ->loadRevisionInformationByRevisionId(
+                               $entityRevision->getEntity()->getId(),
+                               $entityRevision ->getRevisionId() - 1 // There 
were two edits to this item in sequence
+                       );
+
+               $this->assertEquals( $entityRevision->getRevisionId() - 1, 
$result->rev_id );
+               // Page latest should reflect that this is not the latest 
revision
+               $this->assertEquals( $entityRevision->getRevisionId(), 
$result->page_latest );
+       }
+
+       public function testLoadRevisionInformationById_wrongRevision() {
+               $entityRevision = $this->data[2];
+
+               $result = $this->getWikiPageEntityMetaDataLookup()
+                       ->loadRevisionInformationByRevisionId(
+                               $entityRevision->getEntity()->getId(),
+                               $entityRevision ->getRevisionId() * 2 // 
Doesn't exist
+                       );
+
+               $this->assertSame( false, $result );
+       }
+
+       public function testLoadRevisionInformationById_notFound() {
+               $result = $this->getWikiPageEntityMetaDataLookup()
+                       ->loadRevisionInformationByRevisionId(
+                               new ItemId( 'Q823487354' ),
+                               823487354
+                       );
+
+               $this->assertSame( false, $result );
+       }
+
+       public function testLoadRevisionInformationByEntityId_found() {
+               $result = $this->getWikiPageEntityMetaDataLookup()
+                       ->loadRevisionInformationByEntityId(
+                               $this->data[0]->getEntity()->getId(),
+                               'master'
+                       );
+
+               $this->assertEquals( $this->data[0]->getRevisionId(), 
$result->rev_id );
+       }
+
+       public function testLoadRevisionInformationByEntityId_notFound() {
+               $result = $this->getWikiPageEntityMetaDataLookup()
+                       ->loadRevisionInformationByEntityId(
+                               new ItemId( 'Q823487354' ),
+                               'master'
+                       );
+
+               $this->assertSame( false, $result );
+       }
+
+       public function testLoadRevisionInformation() {
+               $entityIds = array(
+                       $this->data[0]->getEntity()->getId(),
+                       $this->data[1]->getEntity()->getId(),
+                       new ItemId( 'Q823487354' ), // Doesn't exist
+                       $this->data[2]->getEntity()->getId()
+               );
+
+               $result = $this->getWikiPageEntityMetaDataLookup()
+                       ->loadRevisionInformation( $entityIds, DB_SLAVE );
+
+               $serializedEntityIds = array();
+               foreach( $entityIds as $entityId ) {
+                       $serializedEntityIds[] = $entityId->getSerialization();
+               }
+
+               // Verify that all requested entity ids are part of the result
+               $this->assertEquals( $serializedEntityIds, array_keys( $result 
) );
+
+               // Verify revision ids
+               $this->assertEquals(
+                       $result[$serializedEntityIds[0]]->rev_id, 
$this->data[0]->getRevisionId()
+               );
+               $this->assertEquals(
+                       $result[$serializedEntityIds[1]]->rev_id, 
$this->data[1]->getRevisionId()
+               );
+               $this->assertEquals(
+                       $result[$serializedEntityIds[3]]->rev_id, 
$this->data[2]->getRevisionId()
+               );
+
+               // Verify that Q823487354 (doesn't exist) is not part of the 
result
+               $this->assertFalse( $result['Q823487354'] );
+       }
+}
diff --git a/repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php 
b/repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
index e704d98..fbb9df0 100644
--- a/repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
+++ b/repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
@@ -20,6 +20,7 @@
 use Wikibase\Lib\Store\WikiPageEntityRevisionLookup;
 use Wikibase\Repo\Content\EntityContentFactory;
 use Wikibase\Repo\Store\SQL\EntityPerPageTable;
+use Wikibase\Lib\Store\WikiPageEntityMetaDataLookup;
 use Wikibase\Repo\Store\WikiPageEntityStore;
 use Wikibase\Repo\WikibaseRepo;
 use Wikibase\SqlIdGenerator;
@@ -61,7 +62,7 @@
 
                $lookup = new WikiPageEntityRevisionLookup(
                        $contentCodec,
-                       $this->getEntityIdParser(),
+                       new WikiPageEntityMetaDataLookup( 
$this->getEntityIdParser(), false ),
                        false
                );
 

-- 
To view, visit https://gerrit.wikimedia.org/r/200209
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie7f76921e1d1ffd2e67c33c597e9116f2148ed3d
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <h...@online.de>
Gerrit-Reviewer: Adrian Lang <adrian.he...@wikimedia.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: 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