Ladsgroup has uploaded a new change for review.

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

Change subject: Let LabelDescriptionLookup work in action=info in client for 
EntityUsage
......................................................................

Let LabelDescriptionLookup work in action=info in client for EntityUsage

Bug: T145965
Change-Id: I6c03dd7ef67471e51807048816769a496fc1d931
---
M client/WikibaseClient.hooks.php
M client/includes/Hooks/InfoActionHookHandler.php
M client/includes/RepoLinker.php
M client/tests/phpunit/includes/Hooks/InfoActionHookHandlerTest.php
4 files changed, 112 insertions(+), 8 deletions(-)


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

diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index 476e209..1a5ee70 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -31,6 +31,7 @@
 use Wikibase\Client\WikibaseClient;
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\Lib\AutoCommentFormatter;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookupFactory;
 
 /**
  * File defining the hook handlers for the Wikibase Client extension.
@@ -409,6 +410,12 @@
 
                $namespaceChecker = $wikibaseClient->getNamespaceChecker();
                $usageLookup = $wikibaseClient->getStore()->getUsageLookup();
+               $labelDescriptionLookupFactory = new 
LanguageFallbackLabelDescriptionLookupFactory(
+                       $wikibaseClient->getLanguageFallbackChainFactory(),
+                       $wikibaseClient->getTermLookup(),
+                       $wikibaseClient->getTermBuffer()
+               );
+               $idParser = $wikibaseClient->getEntityIdParser();
 
                if ( !$namespaceChecker->isWikibaseEnabled( 
$context->getTitle()->getNamespace() ) ) {
                        // shorten out
@@ -420,7 +427,9 @@
                        $wikibaseClient->newRepoLinker(),
                        $wikibaseClient->getStore()->getSiteLinkLookup(),
                        $settings->getSetting( 'siteGlobalID' ),
-                       $usageLookup
+                       $usageLookup,
+                       $labelDescriptionLookupFactory,
+                       $idParser
                );
 
                $pageInfo = $infoActionHookHandler->handle( $context, $pageInfo 
);
diff --git a/client/includes/Hooks/InfoActionHookHandler.php 
b/client/includes/Hooks/InfoActionHookHandler.php
index 874fc4b..17ff861 100644
--- a/client/includes/Hooks/InfoActionHookHandler.php
+++ b/client/includes/Hooks/InfoActionHookHandler.php
@@ -7,8 +7,10 @@
 use Title;
 use Wikibase\Client\RepoLinker;
 use Wikibase\Client\Usage\UsageLookup;
+use Wikibase\DataModel\Entity\EntityIdParser;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\Lib\Store\SiteLinkLookup;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookupFactory;
 use Wikibase\NamespaceChecker;
 
 /**
@@ -44,13 +46,32 @@
         */
        private $usageLookup;
 
-       public function __construct( NamespaceChecker $namespaceChecker, 
RepoLinker $repoLinker,
-               SiteLinkLookup $siteLinkLookup, $siteId, UsageLookup 
$usageLookup ) {
+       /**
+        * @var LanguageFallbackLabelDescriptionLookupFactory
+        */
+       private $labelDescriptionLookupFactory;
+
+       /**
+        * @var EntityIdParser
+        */
+       private $idParser;
+
+       public function __construct(
+               NamespaceChecker $namespaceChecker,
+               RepoLinker $repoLinker,
+               SiteLinkLookup $siteLinkLookup,
+               $siteId,
+               UsageLookup $usageLookup,
+               LanguageFallbackLabelDescriptionLookupFactory 
$labelDescriptionLookupFactory,
+               EntityIdParser $idParser
+       ) {
                $this->namespaceChecker = $namespaceChecker;
                $this->repoLinker = $repoLinker;
                $this->siteLinkLookup = $siteLinkLookup;
                $this->siteId = $siteId;
                $this->usageLookup = $usageLookup;
+               $this->labelDescriptionLookupFactory = 
$labelDescriptionLookupFactory;
+               $this->idParser = $idParser;
        }
 
        /**
@@ -165,12 +186,25 @@
                        ];
                }
                $output = '';
-
+               $entityIds = array_map(
+                       function( $entityId ) {
+                               return $this->idParser->parse( $entityId );
+                       },
+                       array_keys( $usageAspectsByEntity )
+               );
+               $labelLookup = 
$this->labelDescriptionLookupFactory->newLabelDescriptionLookup(
+                       $context->getLanguage(),
+                       $entityIds
+               );
                foreach ( $usageAspectsByEntity as $entityId => $aspects ) {
+                       $label = $labelLookup->getLabel( 
$this->idParser->parse( $entityId ) );
+                       $label = $label === null ? $badgeId : $entityId;
+
                        $output .= Html::rawElement( 'li', [],
                                $this->repoLinker->buildEntityLink(
                                        $entities[$entityId],
-                                       array( 'external' )
+                                       [ 'external' ],
+                                       $label
                                )
                        );
 
diff --git a/client/includes/RepoLinker.php b/client/includes/RepoLinker.php
index 61fd669..92ace2c 100644
--- a/client/includes/RepoLinker.php
+++ b/client/includes/RepoLinker.php
@@ -118,11 +118,15 @@
         *
         * @param EntityId $entityId
         * @param array $classes
+        * @param string title
         *
         * @return string (html)
         */
-       public function buildEntityLink( EntityId $entityId, array $classes = 
array() ) {
-               $title = $entityId->getSerialization();
+       public function buildEntityLink( EntityId $entityId, array $classes = 
[], $title = null ) {
+               if ( $title === null ) {
+                       $title = $entityId->getSerialization();
+               }
+
                $class = 'wb-entity-link';
 
                if ( $classes !== array() ) {
diff --git a/client/tests/phpunit/includes/Hooks/InfoActionHookHandlerTest.php 
b/client/tests/phpunit/includes/Hooks/InfoActionHookHandlerTest.php
index a8f0cee..ec3473d 100644
--- a/client/tests/phpunit/includes/Hooks/InfoActionHookHandlerTest.php
+++ b/client/tests/phpunit/includes/Hooks/InfoActionHookHandlerTest.php
@@ -8,7 +8,10 @@
 use Wikibase\Client\Hooks\InfoActionHookHandler;
 use Wikibase\Client\RepoLinker;
 use Wikibase\Client\Usage\Sql\SqlUsageTracker;
+use Wikibase\DataModel\Entity\EntityIdParser;
 use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
+use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookupFactory;
 use Wikibase\Lib\Store\SiteLinkLookup;
 use Wikibase\Client\Usage\EntityUsage;
 use Wikibase\NamespaceChecker;
@@ -132,12 +135,32 @@
                                ->will( $this->returnValue( $entityUsage ) );
                }
 
+               $labelDescriptionLookupFactory = $this->getMockBuilder(
+                       LanguageFallbackLabelDescriptionLookupFactory::class
+               )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $labelDescriptionLookupFactory->expects( $this->any() )
+                       ->method( 'newLabelDescriptionLookup' )
+                       ->will( $this->returnCallback( [ $this, 
'newLabelDescriptionLookup' ] ) );
+
+               $idParser = $this->getMockBuilder( EntityIdParser::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $idParser->expects( $this->any() )
+                       ->method( 'parse' )
+                       ->will( $this->returnCallback( [ $this, 'parse' ] ) );
+
                $hookHandler = new InfoActionHookHandler(
                        $namespaceChecker,
                        $repoLinker,
                        $siteLinkLookup,
                        'enwiki',
-                       $sqlUsageTracker
+                       $sqlUsageTracker,
+                       $labelDescriptionLookupFactory,
+                       $idParser
                );
 
                return $hookHandler;
@@ -171,4 +194,38 @@
                return $context;
        }
 
+       /**
+        * @return LabelDescriptionLookup
+        */
+       public function newLabelDescriptionLookup() {
+               $lookup = $this->getMockBuilder( LabelDescriptionLookup::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $lookup->expects( $this->any() )
+                       ->method( 'getLabel' )
+                       ->will( $this->returnCallback( [ $this, 'getLabel' ] ) 
);
+
+               return $lookup;
+       }
+
+       /**
+        * @param EntityId $entity
+        *
+        * @return string
+        */
+       public function getLabel( $entity ) {
+               return $entity->getSerialization();
+       }
+
+       /**
+        * @param string $entity
+        *
+        * @return ItemId
+        */
+       public function parse( $entity ) {
+               // TODO: Let properties be tested too
+               return new ItemId( $entity );
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6c03dd7ef67471e51807048816769a496fc1d931
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com>

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

Reply via email to