Aude has uploaded a new change for review.

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

Change subject: Use LabelDescriptionLookup in LanguageLinkBadgeDisplay
......................................................................

Use LabelDescriptionLookup in LanguageLinkBadgeDisplay

instead of loading entities to get labels.

Bug: T120838
Change-Id: I9ec47a2649ac326eefc3707ea3c4257adc079dea
---
M client/includes/Hooks/LanguageLinkBadgeDisplay.php
M client/includes/Hooks/SidebarHookHandlers.php
M client/includes/WikibaseClient.php
M client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php
M client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
M client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php
6 files changed, 81 insertions(+), 91 deletions(-)


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

diff --git a/client/includes/Hooks/LanguageLinkBadgeDisplay.php 
b/client/includes/Hooks/LanguageLinkBadgeDisplay.php
index 0a9240e..8acfbe1 100644
--- a/client/includes/Hooks/LanguageLinkBadgeDisplay.php
+++ b/client/includes/Hooks/LanguageLinkBadgeDisplay.php
@@ -8,7 +8,8 @@
 use Sanitizer;
 use Title;
 use Wikibase\DataModel\Entity\ItemId;
-use Wikibase\DataModel\Services\Lookup\EntityLookup;
+use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
+use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookupException;
 use Wikibase\DataModel\SiteLink;
 
 /**
@@ -24,9 +25,9 @@
 class LanguageLinkBadgeDisplay {
 
        /**
-        * @var EntityLookup
+        * @var LabelDescriptionLookup;
         */
-       protected $entityLookup;
+       protected $labelDescriptionLookup;
 
        /**
         * @var array
@@ -39,12 +40,16 @@
        protected $language;
 
        /**
-        * @param EntityLookup $entityLookup
+        * @param LabelDescriptionLookup $labelDescriptionLookup
         * @param array $badgeClassNames
         * @param Language $language
         */
-       public function __construct( EntityLookup $entityLookup, array 
$badgeClassNames, Language $language ) {
-               $this->entityLookup = $entityLookup;
+       public function __construct(
+               LabelDescriptionLookup $labelDescriptionLookup,
+               array $badgeClassNames,
+               Language $language
+       ) {
+               $this->labelDescriptionLookup = $labelDescriptionLookup;
                $this->badgeClassNames = $badgeClassNames;
                $this->language = $language;
        }
@@ -119,19 +124,19 @@
         * added to the link's title attribute, so the can be effectively 
ignored
         * on this client wiki.
         *
-        * @param ItemId[] $badges
+        * @param ItemId[] $badgesIds
         *
         * @return array An associative array with the keys 'class' and 
'itemtitle' with assigned
         * string values. These fields correspond to the fields in the 
description array for language
         * links used by the SkinTemplateGetLanguageLink hook and expected by 
the applyBadges()
         * function.
         */
-       private function getBadgeInfo( array $badges ) {
+       private function getBadgeInfo( array $badgeIds ) {
                $classes = array();
                $labels = array();
 
-               foreach ( $badges as $badge ) {
-                       $badgeSerialization = $badge->getSerialization();
+               foreach ( $badgeIds as $badgeId ) {
+                       $badgeSerialization = $badgeId->getSerialization();
                        $classes[] = 'badge-' . Sanitizer::escapeClass( 
$badgeSerialization );
 
                        // nicer classes for well known badges
@@ -140,7 +145,7 @@
                                $classes[] = Sanitizer::escapeClass( 
$this->badgeClassNames[$badgeSerialization] );
 
                                // add label (but only if this badge is well 
known on this wiki)
-                               $label = $this->getLabel( $badge );
+                               $label = $this->getLabel( $badgeId );
 
                                if ( $label !== null ) {
                                        $labels[] = $label;
@@ -159,21 +164,22 @@
        /**
         * Returns the label for the given badge.
         *
-        * @param ItemId $badge
+        * @param ItemId $badgeId
         *
         * @return string|null
         */
-       private function getLabel( ItemId $badge ) {
-               $entity = $this->entityLookup->getEntity( $badge );
-               if ( !$entity ) {
+       private function getLabel( ItemId $badgeId ) {
+               try {
+                       $term = $this->labelDescriptionLookup->getLabel( 
$badgeId );
+               } catch ( LabelDescriptionLookupException $ex ) {
                        return null;
                }
 
-               $title = $entity->getLabel( $this->language->getCode() );
-               if ( !$title ) {
-                       return null;
+               if ( $term !== null ) {
+                       return $term->getText();
                }
-               return $title;
+
+               return null;
        }
 
 }
diff --git a/client/includes/Hooks/SidebarHookHandlers.php 
b/client/includes/Hooks/SidebarHookHandlers.php
index c69ca37..b95fae8 100644
--- a/client/includes/Hooks/SidebarHookHandlers.php
+++ b/client/includes/Hooks/SidebarHookHandlers.php
@@ -70,26 +70,14 @@
        }
 
        public static function newFromGlobalState() {
-               global $wgLang;
-               StubUserLang::unstub( $wgLang );
-
                $wikibaseClient = WikibaseClient::getDefaultInstance();
+
                $settings = $wikibaseClient->getSettings();
-
-               $namespaceChecker = $wikibaseClient->getNamespaceChecker();
-
-               $entityLookup = $wikibaseClient->getStore()->getEntityLookup();
                $badgeClassNames = $settings->getSetting( 'badgeClassNames' );
 
-               $badgeDisplay = new LanguageLinkBadgeDisplay(
-                       $entityLookup,
-                       is_array( $badgeClassNames ) ? $badgeClassNames : 
array(),
-                       $wgLang
-               );
-
                return new SidebarHookHandlers(
-                       $namespaceChecker,
-                       $badgeDisplay,
+                       $wikibaseClient->getNamespaceChecker(),
+                       $wikibaseClient->getLanguageLinkBadgeDisplay(),
                        
$wikibaseClient->getOtherProjectsSidebarGeneratorFactory(),
                        $settings->getSetting( 'otherProjectsLinksBeta' ),
                        $settings->getSetting( 'otherProjectsLinksByDefault' )
diff --git a/client/includes/WikibaseClient.php 
b/client/includes/WikibaseClient.php
index 8013b5a..73b9ea2 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -670,10 +670,11 @@
                global $wgLang;
                StubObject::unstub( $wgLang );
 
+               $labelDescriptionLookupFactory = 
$this->getLanguageFallbackLabelDescriptionLookupFactory();
                $badgeClassNames = $this->settings->getSetting( 
'badgeClassNames' );
 
                return new LanguageLinkBadgeDisplay(
-                       $this->getEntityLookup(),
+                       
$labelDescriptionLookupFactory->newLabelDescriptionLookup( $wgLang ),
                        is_array( $badgeClassNames ) ? $badgeClassNames : 
array(),
                        $wgLang
                );
diff --git 
a/client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php 
b/client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php
index de50f35..c823938 100644
--- a/client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php
+++ b/client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php
@@ -11,6 +11,7 @@
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\SiteLink;
+use Wikibase\DataModel\Term\Term;
 use Wikibase\Test\MockRepository;
 
 /**
@@ -27,49 +28,32 @@
  */
 class LanguageLinkBadgeDisplayTest extends \MediaWikiTestCase {
 
-       private function getItems() {
-               $items = array();
-
-               $item = new Item( new ItemId( 'Q1' ) );
-               $links = $item->getSiteLinkList();
-               $links->addNewSiteLink( 'dewiki', 'Georg Friedrich Haendel' );
-               $links->addNewSiteLink( 'nlwiki', 'Georg Friedrich Haendel' );
-               $links->addNewSiteLink( 'enwiki', 'George Frideric Handel', 
array( new ItemId( 'Q3' ), new ItemId( 'Q2' ) ) );
-               $items[] = $item;
-
-               $item = new Item( new ItemId( 'Q21' ) );
-               $links = $item->getSiteLinkList();
-               $links->addNewSiteLink( 'dewiki', 'Benutzer:Testbenutzer' );
-               $links->addNewSiteLink( 'enwiki', 'User:Testuser', array( new 
ItemId( 'Q3' ), new ItemId( 'Q4' ) ) );
-               $items[] = $item;
-
-               $item = new Item( new ItemId( 'Q3' ) );
-               $item->setLabel( 'en', 'Good article' );
-               $item->setLabel( 'de', 'Lesenswerter Artikel' );
-               $items[] = $item;
-
-               $item = new Item( new ItemId( 'Q4' ) );
-               $item->setLabel( 'en', 'Featured article' );
-               $item->setLabel( 'de', 'Exzellenter Artikel' );
-               $items[] = $item;
-
-               return $items;
-       }
-
        /**
         * @return LanguageLinkBadgeDisplay
         */
        private function getLanguageLinkBadgeDisplay() {
-               $entityLookup = new MockRepository();
+               $labelLookup = $this->getMockBuilder(
+                               
'Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup'
+                       )
+                       ->disableOriginalConstructor()
+                       ->getMock();
 
-               foreach ( $this->getItems() as $item ) {
-                       $entityLookup->putEntity( $item );
-               }
+               $labelLookup->expects( $this->any() )
+                       ->method( 'getLabel' )
+                       ->will( $this->returnCallback( function( $entityId ) {
+                               if ( $entityId->getSerialization() === 'Q3' ) {
+                                       return new Term( 'de', 'Lesenswerter 
Artikel' );
+                               } elseif ( $entityId->getSerialization() === 
'Q4' ) {
+                                       return new Term( 'de', 'Exzellenter 
Artikel' );
+                               }
+
+                               return null;
+                       } ) );
 
                $badgeClassNames = array( 'Q4' => 'foo', 'Q3' => 'bar' );
 
                return new LanguageLinkBadgeDisplay(
-                       $entityLookup,
+                       $labelLookup,
                        $badgeClassNames,
                        Language::factory( 'de' )
                );
diff --git 
a/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php 
b/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
index 9f2af07..4c7374f 100644
--- a/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
+++ b/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
@@ -19,6 +19,7 @@
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\SiteLink;
 use Wikibase\DataModel\SiteLinkList;
+use Wikibase\DataModel\Term\Term;
 use Wikibase\InterwikiSorter;
 use Wikibase\LangLinkHandler;
 use Wikibase\Lib\Store\SiteLinkLookup;
@@ -159,12 +160,6 @@
                $mockRepo = $this->getMockRepo( $links );
                $mockRepo->putEntity( $this->getBadgeItem() );
 
-               $badgeDisplay = new LanguageLinkBadgeDisplay(
-                       $mockRepo,
-                       array( 'Q17' => 'featured' ),
-                       Language::factory( 'en' )
-               );
-
                $parserOutputDataUpdater = new ClientParserOutputDataUpdater(
                        $this->getOtherProjectsSidebarGeneratorFactory( 
$settings, $mockRepo ),
                        $mockRepo,
@@ -173,7 +168,7 @@
                );
 
                $langLinkHandler = new LangLinkHandler(
-                       $badgeDisplay,
+                       $this->getBadgeDisplay(),
                        $namespaceChecker,
                        $mockRepo,
                        $mockRepo,
@@ -197,6 +192,24 @@
                );
        }
 
+       private function getBadgeDisplay() {
+               $labelDescriptionLookup = $this->getMockBuilder(
+                               
'Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup'
+                       )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $labelDescriptionLookup->expects( $this->any() )
+                       ->method( 'getLabel' )
+                       ->will( $this->returnValue( new Term( 'en', 'featured' 
) ) );
+
+               return new LanguageLinkBadgeDisplay(
+                       $labelDescriptionLookup,
+                       array( 'Q17' => 'featured' ),
+                       Language::factory( 'en' )
+               );
+       }
+
        private function getOtherProjectsSidebarGeneratorFactory(
                SettingsArray $settings,
                SiteLinkLookup $siteLinkLookup
diff --git a/client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php 
b/client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php
index e52937e..8036c02 100644
--- a/client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php
+++ b/client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php
@@ -16,7 +16,7 @@
 use Wikibase\Client\WikibaseClient;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
-use Wikibase\DataModel\Services\Lookup\EntityLookup;
+use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
 use Wikibase\DataModel\SiteLink;
 use Wikibase\DataModel\SiteLinkList;
 use Wikibase\NamespaceChecker;
@@ -46,20 +46,20 @@
        /**
         * @param array[] $siteLinksPerItem
         *
-        * @return EntityLookup
+        * @return LabelDescriptionLookup
         */
-       private function getEntityLookup( array $siteLinksPerItem ) {
-               $repo = new MockRepository();
+       private function getLabelDescriptionLookup( array $siteLinksPerItem ) {
+               $labelLookup = $this->getMockBuilder(
+                               
'Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup'
+                       )
+                       ->disableOriginalConstructor()
+                       ->getMock();
 
-               foreach ( $siteLinksPerItem as $idString => $siteLinks ) {
-                       $item = new Item( new ItemId( $idString ) );
-                       $item->setSiteLinkList( new SiteLinkList( $siteLinks ) 
);
-                       $repo->putEntity( $item );
-               }
+               $labelLookup->expects( $this->any() )
+                         ->method( 'getLabel' )
+                         ->will( $this->returnValue( 'o' ) );
 
-               $repo->putEntity( $this->getBadgeItem() );
-
-               return $repo;
+               return $labelLookup;
        }
 
        /**
@@ -146,10 +146,8 @@
                $namespaces = $settings->getSetting( 'namespaces' );
                $namespaceChecker = new NamespaceChecker( array(), $namespaces 
);
 
-               $entityLookup = $this->getEntityLookup( $siteLinksPerItem );
-
                $badgeDisplay = new LanguageLinkBadgeDisplay(
-                       $entityLookup,
+                       $this->getLabelDescriptionLookup( $siteLinksPerItem ),
                        array( 'Q17' => 'featured' ),
                        $en
                );

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

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

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

Reply via email to