WMDE-leszek has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/361071 )

Change subject: Fetch all site link data from item to show badges in Other 
Project Sidebar
......................................................................

Fetch all site link data from item to show badges in Other Project Sidebar

Site links data returned SiteLinkLookup does not include information
on badges. In order to get badge information, item data must be
loaded, as it includes complete (ie. incl. badges) site link data.

Bug: T73887
Change-Id: I2ac7bb3329bdac917523428eb5eba42ee0ecdca5
---
M client/includes/Hooks/OtherProjectsSidebarGenerator.php
M client/includes/Hooks/OtherProjectsSidebarGeneratorFactory.php
M client/includes/WikibaseClient.php
M 
client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorFactoryTest.php
M client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorTest.php
M client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
6 files changed, 98 insertions(+), 22 deletions(-)


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

diff --git a/client/includes/Hooks/OtherProjectsSidebarGenerator.php 
b/client/includes/Hooks/OtherProjectsSidebarGenerator.php
index 346e903..20065cd 100644
--- a/client/includes/Hooks/OtherProjectsSidebarGenerator.php
+++ b/client/includes/Hooks/OtherProjectsSidebarGenerator.php
@@ -6,6 +6,8 @@
 use Site;
 use SiteLookup;
 use Title;
+use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Services\Lookup\EntityLookup;
 use Wikibase\DataModel\SiteLink;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\Lib\Store\SiteLinkLookup;
@@ -35,6 +37,11 @@
        private $siteLookup;
 
        /**
+        * @var EntityLookup
+        */
+       private $entityLookup;
+
+       /**
         * @var SidebarLinkBadgeDisplay
         */
        private $sidebarLinkBadgeDisplay;
@@ -48,6 +55,7 @@
         * @param string $localSiteId
         * @param SiteLinkLookup $siteLinkLookup
         * @param SiteLookup $siteLookup
+        * @param EntityLookup $entityLookup
         * @param SidebarLinkBadgeDisplay $sidebarLinkBadgeDisplay
         * @param string[] $siteIdsToOutput
         */
@@ -55,12 +63,14 @@
                $localSiteId,
                SiteLinkLookup $siteLinkLookup,
                SiteLookup $siteLookup,
+               EntityLookup $entityLookup,
                SidebarLinkBadgeDisplay $sidebarLinkBadgeDisplay,
                array $siteIdsToOutput
        ) {
                $this->localSiteId = $localSiteId;
                $this->siteLinkLookup = $siteLinkLookup;
                $this->siteLookup = $siteLookup;
+               $this->entityLookup = $entityLookup;
                $this->sidebarLinkBadgeDisplay = $sidebarLinkBadgeDisplay;
                $this->siteIdsToOutput = $siteIdsToOutput;
        }
@@ -202,7 +212,13 @@
         * @return SiteLink[]
         */
        private function getSiteLinks( ItemId $itemId ) {
-               return $this->siteLinkLookup->getSiteLinksForItem( $itemId );
+               /** @var Item $item */
+               $item = $this->entityLookup->getEntity( $itemId );
+               if ( $item === null ) {
+                       return [];
+               }
+
+               return $item->getSiteLinkList()->toArray();
        }
 
        /**
diff --git a/client/includes/Hooks/OtherProjectsSidebarGeneratorFactory.php 
b/client/includes/Hooks/OtherProjectsSidebarGeneratorFactory.php
index 44aaccd..932ac1b 100644
--- a/client/includes/Hooks/OtherProjectsSidebarGeneratorFactory.php
+++ b/client/includes/Hooks/OtherProjectsSidebarGeneratorFactory.php
@@ -3,6 +3,7 @@
 namespace Wikibase\Client\Hooks;
 
 use SiteLookup;
+use Wikibase\DataModel\Services\Lookup\EntityLookup;
 use Wikibase\Lib\Store\SiteLinkLookup;
 use Wikibase\SettingsArray;
 
@@ -33,24 +34,26 @@
        private $siteLookup;
 
        /**
+        * @var EntityLookup
+        */
+       private $entityLookup;
+
+       /**
         * @var SidebarLinkBadgeDisplay
         */
        private $sidebarLinkBadgeDisplay;
 
-       /**
-        * @param SettingsArray $settings
-        * @param SiteLinkLookup $siteLinkLookup
-        * @param SiteLookup $siteLookup
-        */
        public function __construct(
                SettingsArray $settings,
                SiteLinkLookup $siteLinkLookup,
                SiteLookup $siteLookup,
+               EntityLookup $entityLookup,
                SidebarLinkBadgeDisplay $sidebarLinkBadgeDisplay
        ) {
                $this->settings = $settings;
                $this->siteLinkLookup = $siteLinkLookup;
                $this->siteLookup = $siteLookup;
+               $this->entityLookup = $entityLookup;
                $this->sidebarLinkBadgeDisplay = $sidebarLinkBadgeDisplay;
        }
 
@@ -73,6 +76,7 @@
                        $this->settings->getSetting( 'siteGlobalID' ),
                        $this->siteLinkLookup,
                        $this->siteLookup,
+                       $this->entityLookup,
                        $this->sidebarLinkBadgeDisplay,
                        $this->settings->getSetting( 'otherProjectsLinks' )
                );
diff --git a/client/includes/WikibaseClient.php 
b/client/includes/WikibaseClient.php
index ccd821c..52283c9 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -1051,6 +1051,7 @@
                        $this->settings,
                        $this->getStore()->getSiteLinkLookup(),
                        $this->siteLookup,
+                       $this->getStore()->getEntityLookup(),
                        $this->getSidebarLinkBadgeDisplay()
                );
        }
diff --git 
a/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorFactoryTest.php
 
b/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorFactoryTest.php
index f5fd95c..0b4c966 100644
--- 
a/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorFactoryTest.php
+++ 
b/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorFactoryTest.php
@@ -8,6 +8,7 @@
 use Wikibase\Client\Hooks\OtherProjectsSidebarGenerator;
 use Wikibase\Client\Hooks\OtherProjectsSidebarGeneratorFactory;
 use Wikibase\Client\Hooks\SidebarLinkBadgeDisplay;
+use Wikibase\DataModel\Services\Lookup\EntityLookup;
 use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
 use Wikibase\SettingsArray;
 use Wikibase\Lib\Tests\MockRepository;
@@ -41,6 +42,7 @@
                        $settings,
                        $siteLinkLookup,
                        $siteStore,
+                       $this->getMock( EntityLookup::class ),
                        $sidebarLinkBadgeDisplay
                );
 
diff --git 
a/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorTest.php 
b/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorTest.php
index 3692792..fec13f9 100644
--- a/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorTest.php
+++ b/client/tests/phpunit/includes/Hooks/OtherProjectsSidebarGeneratorTest.php
@@ -11,9 +11,12 @@
 use TestSites;
 use Wikibase\Client\Hooks\OtherProjectsSidebarGenerator;
 use Wikibase\Client\Hooks\SidebarLinkBadgeDisplay;
+use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup;
 use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
 use Wikibase\DataModel\SiteLink;
+use Wikibase\DataModel\SiteLinkList;
 use Wikibase\DataModel\Term\Term;
 use Wikibase\Lib\Store\SiteLinkLookup;
 
@@ -29,6 +32,11 @@
  */
 class OtherProjectsSidebarGeneratorTest extends \MediaWikiTestCase {
 
+       const TEST_ITEM_ID = 'Q123';
+       const BADGE_ITEM_ID = 'Q4242';
+       const BADGE_ITEM_LABEL = 'Badge Label';
+       const BADGE_CSS_CLASS = 'badge-class';
+
        /**
         * @dataProvider projectLinkSidebarProvider
         */
@@ -42,6 +50,7 @@
                        'enwiki',
                        $this->getSiteLinkLookup(),
                        $this->getSiteLookup(),
+                       $this->getEntityLookup(),
                        $sidebarLinkBadgeDisplay,
                        $siteIdsToOutput
                );
@@ -67,10 +76,11 @@
                ];
                $wikipediaLink = [
                        'msg' => 'wikibase-otherprojects-wikipedia',
-                       'class' => 'wb-otherproject-link 
wb-otherproject-wikipedia badge-Q4242 badge-class',
+                       'class' => 'wb-otherproject-link 
wb-otherproject-wikipedia ' .
+                               'badge-' . self::BADGE_ITEM_ID . ' ' . 
self::BADGE_CSS_CLASS,
                        'href' => 'https://en.wikipedia.org/wiki/Nyan_Cat',
                        'hreflang' => 'en',
-                       'itemtitle' => 'Badge Label',
+                       'itemtitle' => self::BADGE_ITEM_LABEL,
                ];
 
                return [
@@ -111,13 +121,14 @@
                        'enwiki',
                        $this->getSiteLinkLookup(),
                        $this->getSiteLookup(),
+                       $this->getEntityLookup(),
                        $this->getSidebarLinkBadgeDisplay(),
                        $siteIdsToOutput
                );
 
                $this->assertEquals(
                        $result,
-                       
$otherProjectSidebarGenerator->buildProjectLinkSidebarFromItemId( new ItemId( 
'Q123' ) )
+                       
$otherProjectSidebarGenerator->buildProjectLinkSidebarFromItemId( new ItemId( 
self::TEST_ITEM_ID ) )
                );
        }
 
@@ -138,6 +149,7 @@
                        'enwiki',
                        $this->getSiteLinkLookup(),
                        $this->getSiteLookup(),
+                       $this->getEntityLookup(),
                        $this->getSidebarLinkBadgeDisplay(),
                        $siteIdsToOutput
                );
@@ -170,17 +182,19 @@
                );
                $wikipediaLink = array(
                        'msg' => 'wikibase-otherprojects-wikipedia',
-                       'class' => 'wb-otherproject-link 
wb-otherproject-wikipedia badge-Q4242 badge-class',
+                       'class' => 'wb-otherproject-link 
wb-otherproject-wikipedia ' .
+                               'badge-' . self::BADGE_ITEM_ID . ' ' . 
self::BADGE_CSS_CLASS,
                        'href' => 'https://en.wikipedia.org/wiki/Nyan_Cat',
                        'hreflang' => 'en',
-                       'itemtitle' => 'Badge Label',
+                       'itemtitle' => self::BADGE_ITEM_LABEL,
                );
                $changedWikipedaLink = array(
                        'msg' => 'wikibase-otherprojects-wikipedia',
-                       'class' => 'wb-otherproject-link 
wb-otherproject-wikipedia badge-Q4242 badge-class',
+                       'class' => 'wb-otherproject-link 
wb-otherproject-wikipedia ' .
+                               'badge-' . self::BADGE_ITEM_ID . ' ' . 
self::BADGE_CSS_CLASS,
                        'href' => 'https://en.wikipedia.org/wiki/Cat',
                        'hreflang' => 'en',
-                       'itemtitle' => 'Badge Label',
+                       'itemtitle' => self::BADGE_ITEM_LABEL,
                );
 
                return array(
@@ -194,7 +208,7 @@
                                                ),
                                                $sidebar
                                        );
-                                       $this->assertSame( 'Q123', 
$itemId->getSerialization() );
+                                       $this->assertSame( self::TEST_ITEM_ID, 
$itemId->getSerialization() );
                                },
                                array( 'enwiktionary', 'enwiki', 'enwikiquote' 
),
                                array( $wikipediaLink, $wikiquoteLink, 
$wiktionaryLink )
@@ -272,6 +286,7 @@
                        'enwiki',
                        $lookup,
                        $this->getSiteLookup(),
+                       $this->getEntityLookup(),
                        $this->getSidebarLinkBadgeDisplay(),
                        array( 'enwiki' )
                );
@@ -288,7 +303,7 @@
                $this->mergeMwGlobalArrayValue( 'wgHooks', array(
                        'WikibaseClientOtherProjectsSidebar' => array(
                                function ( ItemId $itemId, $sidebar ) use ( 
&$called ) {
-                                       $this->assertSame( 'Q123', 
$itemId->getSerialization() );
+                                       $this->assertSame( self::TEST_ITEM_ID, 
$itemId->getSerialization() );
                                        $this->assertSame( array(), $sidebar );
                                        $called = true;
                                },
@@ -299,6 +314,7 @@
                        'enwiki',
                        $this->getSiteLinkLookup(),
                        $this->getSiteLookup(),
+                       $this->getEntityLookup(),
                        $this->getSidebarLinkBadgeDisplay(),
                        array( 'unknown-site' )
                );
@@ -330,21 +346,35 @@
         * @return SiteLinkLookup
         */
        private function getSiteLinkLookup() {
-               $Q123 = new ItemId( 'Q123' );
+               $itemId = new ItemId( self::TEST_ITEM_ID );
 
                $lookup = $this->getMock( SiteLinkLookup::class );
                $lookup->expects( $this->any() )
                        ->method( 'getItemIdForLink' )
-                       ->will( $this->returnValue( $Q123 ) );
+                       ->will( $this->returnValue( $itemId ) );
 
                $lookup->expects( $this->any() )
                        ->method( 'getSiteLinksForItem' )
-                       ->with( $Q123 )
+                       ->with( $itemId )
                        ->will( $this->returnValue( array(
                                new SiteLink( 'enwikiquote', 'Nyan Cat' ),
-                               new SiteLink( 'enwiki', 'Nyan Cat', [ new 
ItemId( 'Q4242' ) ] ),
+                               new SiteLink( 'enwiki', 'Nyan Cat' ),
                                new SiteLink( 'enwiktionary', 'Nyan Cat' )
                        ) ) );
+
+               return $lookup;
+       }
+
+       private function getEntityLookup() {
+               $item = new Item( new ItemId( self::TEST_ITEM_ID ) );
+               $item->setSiteLinkList( new SiteLinkList( [
+                       new SiteLink( 'enwikiquote', 'Nyan Cat' ),
+                       new SiteLink( 'enwiki', 'Nyan Cat', [ new ItemId( 
self::BADGE_ITEM_ID ) ] ),
+                       new SiteLink( 'enwiktionary', 'Nyan Cat' ),
+               ] ) );
+
+               $lookup = new InMemoryEntityLookup();
+               $lookup->addEntity( $item );
 
                return $lookup;
        }
@@ -355,12 +385,12 @@
        private function getSidebarLinkBadgeDisplay() {
                $labelDescriptionLookup = $this->getMock( 
LabelDescriptionLookup::class );
                $labelDescriptionLookup->method( 'getLabel' )
-                       ->with( new ItemId( 'Q4242' ) )
-                       ->will( $this->returnValue( new Term( 'en', 'Badge 
Label' ) ) );
+                       ->with( new ItemId( self::BADGE_ITEM_ID ) )
+                       ->will( $this->returnValue( new Term( 'en', 
self::BADGE_ITEM_LABEL ) ) );
 
                return new SidebarLinkBadgeDisplay(
                        $labelDescriptionLookup,
-                       [ 'Q4242' => 'badge-class' ],
+                       [ self::BADGE_ITEM_ID => self::BADGE_CSS_CLASS ],
                        new Language( 'en' )
                );
        }
diff --git 
a/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php 
b/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
index bbb0a6e..4c27873 100644
--- a/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
+++ b/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
@@ -19,6 +19,7 @@
 use Wikibase\Client\WikibaseClient;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup;
 use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
 use Wikibase\DataModel\SiteLink;
 use Wikibase\DataModel\SiteLinkList;
@@ -195,6 +196,27 @@
                );
        }
 
+       private function getEntityLookup() {
+               $item1 = new Item( new ItemId( 'Q1' ) );
+               $item1->setSiteLinkList( new SiteLinkList( [
+                       new SiteLink( 'dewiki', 'Sauerstoff', [ 
$this->getBadgeItem()->getId() ] ),
+                       new SiteLink( 'enwiki', 'Oxygen' ),
+                       new SiteLink( 'commonswiki', 'Oxygen' ),
+               ] ) );
+
+               $item7 = new Item( new ItemId( 'Q7' ) );
+               $item7->setSiteLinkList( new SiteLinkList( [
+                       new SiteLink( 'dewiki', 'User:Foo' ),
+                       new SiteLink( 'enwiki', 'User:Foo' ),
+                       new SiteLink( 'commonswiki', 'User:Foo' ),
+               ] ) );
+
+               $lookup = new InMemoryEntityLookup();
+               $lookup->addEntity( $item1 );
+               $lookup->addEntity( $item7 );
+               return $lookup;
+       }
+
        private function getOtherProjectsSidebarGeneratorFactory(
                SettingsArray $settings,
                SiteLinkLookup $siteLinkLookup
@@ -209,6 +231,7 @@
                        $settings,
                        $siteLinkLookup,
                        $this->getSiteLookup(),
+                       $this->getEntityLookup(),
                        $sidebarLinkBadgeDisplay
                );
        }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2ac7bb3329bdac917523428eb5eba42ee0ecdca5
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: WMDE-leszek <leszek.mani...@wikimedia.de>

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

Reply via email to