Hoo man has uploaded a new change for review.

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

Change subject: Make SpecialGoToLinkedPage support entity redirects
......................................................................

Make SpecialGoToLinkedPage support entity redirects

Also only load the sites out of the SiteStore if actually
needed.

Bug: T78526
Change-Id: If73bda27a0a392fb8997f127dc307d270839e6bd
---
M repo/includes/specials/SpecialGoToLinkedPage.php
M repo/tests/phpunit/includes/specials/SpecialGoToLinkedPageTest.php
2 files changed, 62 insertions(+), 33 deletions(-)


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

diff --git a/repo/includes/specials/SpecialGoToLinkedPage.php 
b/repo/includes/specials/SpecialGoToLinkedPage.php
index 3dce249..8df8647 100644
--- a/repo/includes/specials/SpecialGoToLinkedPage.php
+++ b/repo/includes/specials/SpecialGoToLinkedPage.php
@@ -6,7 +6,8 @@
 use InvalidArgumentException;
 use SiteStore;
 use Wikibase\DataModel\Entity\ItemId;
-use Wikibase\Lib\Store\SiteLinkLookup;
+use Wikibase\DataModel\Entity\Item;
+use Wikibase\Lib\Store\RedirectResolvingEntityLookup;
 use Wikibase\Repo\WikibaseRepo;
 
 /**
@@ -24,9 +25,9 @@
        private $siteStore;
 
        /**
-        * @var SiteLinkLookup
+        * @var RedirectResolvingEntityLookup
         */
-       private $siteLinkLookup;
+       private $entityLookup;
 
        /**
         * @see SpecialWikibasePage::__construct
@@ -36,7 +37,7 @@
 
                $this->initServices(
                        WikibaseRepo::getDefaultInstance()->getSiteStore(),
-                       
WikibaseRepo::getDefaultInstance()->getStore()->newSiteLinkCache()
+                       WikibaseRepo::getDefaultInstance()->getEntityLookup()
                );
        }
 
@@ -45,14 +46,14 @@
         * May be used to inject mock services for testing.
         *
         * @param SiteStore $siteStore
-        * @param SiteLinkLookup $siteLinkLookup
+        * @param RedirectResolvingEntityLookup $entityLookup
         */
        public function initServices(
                SiteStore $siteStore,
-               SiteLinkLookup $siteLinkLookup
+               RedirectResolvingEntityLookup $entityLookup
        ) {
                $this->siteStore = $siteStore;
-               $this->siteLinkLookup = $siteLinkLookup;
+               $this->entityLookup = $entityLookup;
        }
 
        /**
@@ -70,23 +71,23 @@
                } catch ( InvalidArgumentException $ex ) {
                        $itemId = null;
                        $itemString = '';
-                }
+               }
 
                return array( $site, $itemId, $itemString );
        }
 
        /**
         * @param string $site
-        * @param ItemId $itemId
+        * @param ItemId|null $itemId
         * @return string|null the URL to redirect to or null if the sitelink 
does not exist
         */
-       protected function getTargetUrl( $site, $itemId ) {
+       protected function getTargetUrl( $site, ItemId $itemId = null ) {
                if ( $site === '' || $itemId === null ) {
                        return null;
                }
                $site = $this->stringNormalizer->trimToNFC( $site );
 
-               if ( !$this->siteStore->getSite( $site ) ) {
+               if ( $this->getPageName( $site, $itemId ) === null ) {
                        // HACK: If the site ID isn't known, add "wiki" to it; 
this allows the wikipedia
                        // subdomains to be used to refer to wikipedias, 
instead of requiring their
                        // full global id to be used.
@@ -96,14 +97,36 @@
                        $site .= 'wiki';
                }
 
-               $links = $this->siteLinkLookup->getLinks( array( 
$itemId->getNumericId() ), array( $site ) );
+               $pageName = $this->getPageName( $site, $itemId );
 
-               if ( isset( $links[0] ) ) {
-                       list( , $pageName, ) = $links[0];
+               if ( $pageName !== null ) {
                        $siteObj = $this->siteStore->getSite( $site );
-                       $url = $siteObj->getPageUrl( $pageName );
-                       return $url;
+
+                       return $siteObj->getPageUrl( $pageName );
                }
+
+               return null;
+       }
+
+       /**
+        * @param string $site
+        * @param ItemId|null $itemId
+        *
+        * @return string|null
+        */
+       private function getPageName( $site, ItemId $itemId ) {
+               /* @var $item Item */
+               $item = $this->entityLookup->getEntity( $itemId );
+               if ( !$item ) {
+                       return null;
+               }
+
+               $siteLinkList = $item->getSiteLinkList();
+
+               if ( $siteLinkList->hasLinkWithSiteId( $site ) ) {
+                       return $siteLinkList->getBySiteId( $site 
)->getPageName();
+               }
+
                return null;
        }
 
diff --git a/repo/tests/phpunit/includes/specials/SpecialGoToLinkedPageTest.php 
b/repo/tests/phpunit/includes/specials/SpecialGoToLinkedPageTest.php
index 212ed4a..6f42d01 100644
--- a/repo/tests/phpunit/includes/specials/SpecialGoToLinkedPageTest.php
+++ b/repo/tests/phpunit/includes/specials/SpecialGoToLinkedPageTest.php
@@ -2,10 +2,12 @@
 
 namespace Wikibase\Test;
 
-use FauxResponse;
 use Site;
 use SiteStore;
-use Wikibase\Lib\Store\SiteLinkLookup;
+use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\Lib\Store\EntityRedirect;
+use Wikibase\Lib\Store\RedirectResolvingEntityLookup;
 use Wikibase\Repo\Specials\SpecialGoToLinkedPage;
 
 /**
@@ -18,27 +20,30 @@
  *
  * @licence GNU GPL v2+
  * @author Jan Zerebecki
+ * @author Marius Hoch < [email protected] >
  */
 class SpecialGoToLinkedPageTest extends SpecialPageTestBase {
 
        /**
-        * @return SiteLinkLookup
+        * @return RedirectResolvingEntityLookup
         */
-       private function getMockSiteLinkLookup() {
-               $mock = $this->getMock( 'Wikibase\Lib\Store\SiteLinkLookup' );
+       private function getMockRepository() {
+               static $entityLookup = null;
 
-               $mock->expects( $this->any() )
-                       ->method( 'getLinks' )
-                       ->will( $this->returnCallback( function( $itemIds, 
$siteIds ) {
-                               $result = array( array( '', 'TestPageName' ) );
-                               if ( $siteIds === array( 'dewiki' ) && $itemIds 
=== array( 23 ) ) {
-                                       return $result;
-                               } else {
-                                       return null;
-                               }
-                       } ) );
+               if ( !$entityLookup ) {
+                       $mockRepo = new MockRepository();
+                       $item = new Item( new ItemId( 'Q23' ) );
+                       $item->getSiteLinkList()->addNewSiteLink( 'dewiki', 
'TestPageName' );
 
-               return $mock;
+                       $mockRepo->putEntity( $item );
+
+                       $entityRedirect = new EntityRedirect( new ItemId( 'Q24' 
), new ItemId( 'Q23' ) );
+                       $mockRepo->putRedirect( $entityRedirect );
+
+                       $entityLookup = new RedirectResolvingEntityLookup( 
$mockRepo );
+               }
+
+               return $entityLookup;
        }
 
        /**
@@ -71,7 +76,7 @@
 
                $page->initServices(
                        $this->getMockSiteStore(),
-                       $this->getMockSiteLinkLookup()
+                       $this->getMockRepository()
                );
 
                return $page;
@@ -126,6 +131,7 @@
        public function requestWithRedirectProvider() {
                $cases = array();
                $cases['found'] = array( 'dewiki/Q23', 
'http://dewiki.com/TestPageName' );
+               $cases['foundEntityRedirect'] = array( 'dewiki/Q24', 
'http://dewiki.com/TestPageName' );
                $cases['foundWithSiteIdHack'] = array( 'de/Q23', 
'http://dewiki.com/TestPageName' );
                return $cases;
        }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If73bda27a0a392fb8997f127dc307d270839e6bd
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to