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

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


Make SpecialGoToLinkedPage support entity redirects

By resolving these using EntityPerPage.

In order to make this work, I added the "getRedirectForEntityId"
method to EntityPerPage. It's already implemented by the only
class implementing it.

Bug: T78526
Change-Id: If73bda27a0a392fb8997f127dc307d270839e6bd
---
M repo/includes/specials/SpecialGoToLinkedPage.php
M repo/includes/store/EntityPerPage.php
M repo/tests/phpunit/includes/specials/SpecialGoToLinkedPageTest.php
3 files changed, 76 insertions(+), 8 deletions(-)

Approvals:
  Daniel Kinzler: Looks good to me, approved
  Thiemo Mättig (WMDE): Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/repo/includes/specials/SpecialGoToLinkedPage.php 
b/repo/includes/specials/SpecialGoToLinkedPage.php
index 3dce249..9d1aaf1 100644
--- a/repo/includes/specials/SpecialGoToLinkedPage.php
+++ b/repo/includes/specials/SpecialGoToLinkedPage.php
@@ -6,6 +6,7 @@
 use InvalidArgumentException;
 use SiteStore;
 use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\Repo\Store\EntityPerPage;
 use Wikibase\Lib\Store\SiteLinkLookup;
 use Wikibase\Repo\WikibaseRepo;
 
@@ -29,14 +30,22 @@
        private $siteLinkLookup;
 
        /**
+        * @var EntityPerPage
+        */
+       private $entityPerPage;
+
+       /**
         * @see SpecialWikibasePage::__construct
         */
        public function __construct() {
                parent::__construct( 'GoToLinkedPage', '', true );
 
+               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
+
                $this->initServices(
-                       WikibaseRepo::getDefaultInstance()->getSiteStore(),
-                       
WikibaseRepo::getDefaultInstance()->getStore()->newSiteLinkCache()
+                       $wikibaseRepo->getSiteStore(),
+                       $wikibaseRepo->getStore()->newSiteLinkCache(),
+                       $wikibaseRepo->getStore()->newEntityPerPage()
                );
        }
 
@@ -46,13 +55,16 @@
         *
         * @param SiteStore $siteStore
         * @param SiteLinkLookup $siteLinkLookup
+        * @param EntityPerPage $entityPerPage
         */
        public function initServices(
                SiteStore $siteStore,
-               SiteLinkLookup $siteLinkLookup
+               SiteLinkLookup $siteLinkLookup,
+               EntityPerPage $entityPerPage
        ) {
                $this->siteStore = $siteStore;
                $this->siteLinkLookup = $siteLinkLookup;
+               $this->entityPerPage = $entityPerPage;
        }
 
        /**
@@ -77,10 +89,10 @@
 
        /**
         * @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;
                }
@@ -96,7 +108,7 @@
                        $site .= 'wiki';
                }
 
-               $links = $this->siteLinkLookup->getLinks( array( 
$itemId->getNumericId() ), array( $site ) );
+               $links = $this->loadLinks( $site, $itemId );
 
                if ( isset( $links[0] ) ) {
                        list( , $pageName, ) = $links[0];
@@ -108,6 +120,31 @@
        }
 
        /**
+        * Load the sitelink using a SiteLinkLookup. Resolves item redirects, 
if needed.
+        *
+        * @param string $site
+        * @param ItemId $itemId
+        *
+        * @return array[]
+        */
+       private function loadLinks( $site, ItemId $itemId ) {
+               $links = $this->siteLinkLookup->getLinks( array( 
$itemId->getNumericId() ), array( $site ) );
+               if ( isset( $links[0] ) ) {
+                       return $links;
+               }
+
+               // Maybe the item is a redirect: Try to resolve the redirect 
and load
+               // the links from there.
+               $redirectTarget = $this->entityPerPage->getRedirectForEntityId( 
$itemId );
+
+               if ( $redirectTarget instanceof ItemId ) {
+                       return $this->siteLinkLookup->getLinks( array( 
$redirectTarget->getNumericId() ), array( $site ) );
+               }
+
+               return array();
+       }
+
+       /**
         * @see SpecialWikibasePage::execute
         *
         * @param string|null $subPage
diff --git a/repo/includes/store/EntityPerPage.php 
b/repo/includes/store/EntityPerPage.php
index 32b053f..602beae 100644
--- a/repo/includes/store/EntityPerPage.php
+++ b/repo/includes/store/EntityPerPage.php
@@ -121,4 +121,14 @@
         * @return EntityId[]
         */
        public function getItemsWithoutSitelinks( $siteId = null, $limit = 50, 
$offset = 0 );
+
+       /**
+        * @since 0.5
+        *
+        * @param EntityId $entityId
+        *
+        * @return EntityId|null|false The ID of the redirect target, or null 
if $entityId
+        *         does not refer to a redirect, or false if $entityId is not 
known.
+        */
+       public function getRedirectForEntityId( EntityId $entityId );
 }
diff --git a/repo/tests/phpunit/includes/specials/SpecialGoToLinkedPageTest.php 
b/repo/tests/phpunit/includes/specials/SpecialGoToLinkedPageTest.php
index 212ed4a..a7aeb60 100644
--- a/repo/tests/phpunit/includes/specials/SpecialGoToLinkedPageTest.php
+++ b/repo/tests/phpunit/includes/specials/SpecialGoToLinkedPageTest.php
@@ -2,11 +2,12 @@
 
 namespace Wikibase\Test;
 
-use FauxResponse;
 use Site;
 use SiteStore;
+use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\Lib\Store\SiteLinkLookup;
 use Wikibase\Repo\Specials\SpecialGoToLinkedPage;
+use Wikibase\Repo\Store\EntityPerPage;
 
 /**
  * @covers Wikibase\Repo\Specials\SpecialGoToLinkedPage
@@ -64,6 +65,24 @@
        }
 
        /**
+        * @return EntityPerPage
+        */
+       private function getEntityPerPage() {
+               $mock = $this->getMock( 'Wikibase\Repo\Store\EntityPerPage' );
+               $mock->expects( $this->any() )
+                       ->method( 'getRedirectForEntityId' )
+                       ->will( $this->returnCallback( function( ItemId $itemId 
) {
+                               if ( $itemId->getSerialization() === 'Q24' ) {
+                                       return new ItemId( 'Q23' );
+                               } else {
+                                       return null;
+                               }
+                       } ) );
+
+               return $mock;
+       }
+
+       /**
         * @return SpecialGoToLinkedPage
         */
        protected function newSpecialPage() {
@@ -71,7 +90,8 @@
 
                $page->initServices(
                        $this->getMockSiteStore(),
-                       $this->getMockSiteLinkLookup()
+                       $this->getMockSiteLinkLookup(),
+                       $this->getEntityPerPage()
                );
 
                return $page;
@@ -126,6 +146,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: merged
Gerrit-Change-Id: If73bda27a0a392fb8997f127dc307d270839e6bd
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to