Daniel Kinzler has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/143874

Change subject: Redirects should not be in wb_entity_per_page.
......................................................................

Redirects should not be in wb_entity_per_page.

Our main use case for the wb_entity_per_page table is
listing entities. We don't want to encounter redirects
when doing that.

For now, rely on core's redirect table for recording redirects.

Change-Id: I5289f69dd8c2c3f34f2bd549124dc510995ad5d7
---
M repo/includes/content/EntityHandler.php
M repo/includes/store/sql/EntityPerPageTable.php
M repo/tests/phpunit/includes/store/sql/EntityPerPageTableTest.php
M repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
4 files changed, 74 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/74/143874/1

diff --git a/repo/includes/content/EntityHandler.php 
b/repo/includes/content/EntityHandler.php
index e8da612..ee5fbc4 100644
--- a/repo/includes/content/EntityHandler.php
+++ b/repo/includes/content/EntityHandler.php
@@ -652,24 +652,27 @@
                        $entityId = $this->getIdForTitle( $title );
                }
 
-               // Register the entity in the EntityPerPage table.
-               // @todo: Only do this if the entity is new.
-               // Note that $title->exists() will already return true at this 
point
-               // even if we are just now creating the entity.
-               // @todo: if this is a redirect, record redirect target
-               $updates[] = new DataUpdateClosure(
-                       array( $this->entityPerPage, 'addEntityPage' ),
-                       $entityId,
-                       $title->getArticleID()
-               );
-
                if ( $content->isRedirect() ) {
                        // Remove the entity from the terms table since it's 
now a redirect.
                        $updates[] = new DataUpdateClosure(
                                array( $this->termIndex, 'deleteTermsOfEntity' 
),
                                $entityId
                        );
+
+                       // Unregister the entity from the EntityPerPage table.
+                       $updates[] = new DataUpdateClosure(
+                               array( $this->entityPerPage, 'deleteEntityPage' 
),
+                               $entityId,
+                               $title->getArticleID()
+                       );
                } else {
+                       // Register the entity in the EntityPerPage table.
+                       $updates[] = new DataUpdateClosure(
+                               array( $this->entityPerPage, 'addEntityPage' ),
+                               $entityId,
+                               $title->getArticleID()
+                       );
+
                        // Register the entity in the terms table.
                        $updates[] = new DataUpdateClosure(
                                array( $this->termIndex, 'saveTermsOfEntity' ),
diff --git a/repo/includes/store/sql/EntityPerPageTable.php 
b/repo/includes/store/sql/EntityPerPageTable.php
index 8eceab1..d1f79d5 100644
--- a/repo/includes/store/sql/EntityPerPageTable.php
+++ b/repo/includes/store/sql/EntityPerPageTable.php
@@ -289,4 +289,32 @@
                return $ids;
        }
 
+       /**
+        * @since 0.5
+        *
+        * @param EntityId $entityId
+        *
+        * @return int|false the ID of the page containing the entity,
+        *         or false if there is no such entity page.
+        */
+       public function getPageIdForEntity( EntityId $entityId ) {
+               $dbr = wfGetDB( DB_SLAVE );
+
+               $row = $dbr->selectRow(
+                       'wb_entity_per_page',
+                       array( 'epp_page_id' ),
+                       array(
+                               'epp_entity_type' => $entityId->getEntityType(),
+                               'epp_entity_id' => $entityId->getNumericId()
+                       ),
+                       __METHOD__
+               );
+
+               if ( !$row ) {
+                       return false;
+               }
+
+               return $row->epp_page_id;
+       }
+
 }
diff --git a/repo/tests/phpunit/includes/store/sql/EntityPerPageTableTest.php 
b/repo/tests/phpunit/includes/store/sql/EntityPerPageTableTest.php
index dd720ac..371967c 100644
--- a/repo/tests/phpunit/includes/store/sql/EntityPerPageTableTest.php
+++ b/repo/tests/phpunit/includes/store/sql/EntityPerPageTableTest.php
@@ -4,6 +4,7 @@
 
 use Wikibase\DataModel\Entity\Entity;
 use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Entity\Property;
 use Wikibase\EntityPerPageTable;
 use Wikibase\Repo\WikibaseRepo;
@@ -116,4 +117,14 @@
                );
        }
 
+       public function testGetPageIdForEntity() {
+               $entity = Item::newEmpty();
+               $entity->setId( new ItemId( 'Q7' ) );
+
+               $epp = $this->newEntityPerPageTable( array( $entity ) );
+
+               $this->assertFalse( $epp->getPageIdForEntity( new ItemId( 'Q77' 
) ) );
+               $this->assertGreaterThan( 0, $epp->getPageIdForEntity( new 
ItemId( 'Q7' ) ) );
+       }
+
 }
diff --git a/repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php 
b/repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
index ea68d88..b0c46d9 100644
--- a/repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
+++ b/repo/tests/phpunit/includes/store/sql/WikiPageEntityStoreTest.php
@@ -11,6 +11,7 @@
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Entity\PropertyId;
 use Wikibase\EntityContentFactory;
+use Wikibase\EntityId;
 use Wikibase\EntityPerPageTable;
 use Wikibase\Lib\Store\EntityRedirect;
 use Wikibase\Lib\Store\EntityRevisionLookup;
@@ -103,6 +104,8 @@
                // check that the term index got updated (via a DataUpdate).
                $termIndex = 
WikibaseRepo::getDefaultInstance()->getStore()->getTermIndex();
                $this->assertNotEmpty( $termIndex->getTermsOfEntity( $oneId ), 
'getTermsOfEntity()' );
+
+               $this->assertEntityPerPage( true, $oneId );
        }
 
        public function provideSaveEntityError() {
@@ -206,6 +209,8 @@
                $this->assertTrue( $revision->getContent()->isRedirect(), 
'EntityContent::isRedirect()' );
                $this->assertTrue( 
$revision->getContent()->getEntityRedirect()->equals( $redirect ), 
'getEntityRedirect()' );
 
+               $this->assertEntityPerPage( false, $oneId );
+
                // check that the term index got updated (via a DataUpdate).
                $termIndex = 
WikibaseRepo::getDefaultInstance()->getStore()->getTermIndex();
                $this->assertEmpty( $termIndex->getTermsOfEntity( $oneId ), 
'getTermsOfEntity' );
@@ -218,6 +223,8 @@
 
                $this->assertFalse( $revision->getTitle()->isRedirect(), 
'Title::isRedirect' );
                $this->assertFalse( $revision->getContent()->isRedirect(), 
'EntityContent::isRedirect()' );
+
+               $this->assertEntityPerPage( true, $oneId );
        }
 
        public function unsupportedRedirectProvider() {
@@ -504,6 +511,20 @@
                $this->assertEmpty( $termIndex->getTermsOfEntity( $oneId ), 
'getTermsOfEntity' );
 
                // TODO: check notifications in wb_changes table!
+
+               $this->assertEntityPerPage( false, $oneId );
+       }
+
+       private function assertEntityPerPage( $expected, EntityId $entityId ) {
+               $epp = new EntityPerPageTable();
+
+               $pageId = $epp->getPageIdForEntity( $entityId );
+
+               if ( $expected === true ) {
+                       $this->assertGreaterThan( 0, $pageId );
+               } else {
+                       $this->assertEquals( $expected, $pageId );
+               }
        }
 
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5289f69dd8c2c3f34f2bd549124dc510995ad5d7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to