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

Change subject: Use in LabelLookup (w/ EntityRetrievingTermLookup) in 
EntityIdLabelFormatter
......................................................................


Use in LabelLookup (w/ EntityRetrievingTermLookup) in EntityIdLabelFormatter

this moves the label lookup code out of the formatter,
and into the EntityRetrievingTermLookup and then injects
a LabelLookup (albeit still using EntityLookup) into
the EntityIdLabelFormatter.

in comparing the TermIndex (database) TermLookup vs this,
it seems this is better option (loading the entire entity from
memcached) of the two when doing many lookups.

and makes it easier to swap in a batching lookup or other
implementation at a later time, depending on the use case.

Change-Id: Ia6a968d289cefc2e4df917b60dc761652d8354b6
---
M lib/includes/formatters/EntityIdHtmlLinkFormatter.php
M lib/includes/formatters/EntityIdLabelFormatter.php
M lib/includes/formatters/WikibaseValueFormatterBuilders.php
A lib/includes/store/EntityRetrievingTermLookup.php
M lib/includes/store/TermLookup.php
M lib/tests/phpunit/formatters/EntityIdLabelFormatterTest.php
M lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
A lib/tests/phpunit/store/EntityRetrievingTermLookupTest.php
M repo/includes/Diff/EntityContentDiffView.php
M repo/includes/actions/EditEntityAction.php
M repo/includes/specials/SpecialItemDisambiguation.php
11 files changed, 357 insertions(+), 142 deletions(-)

Approvals:
  Hoo man: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/includes/formatters/EntityIdHtmlLinkFormatter.php 
b/lib/includes/formatters/EntityIdHtmlLinkFormatter.php
index b813795..b4f5210 100644
--- a/lib/includes/formatters/EntityIdHtmlLinkFormatter.php
+++ b/lib/includes/formatters/EntityIdHtmlLinkFormatter.php
@@ -7,8 +7,8 @@
 use Title;
 use ValueFormatters\FormatterOptions;
 use Wikibase\DataModel\Entity\EntityId;
-use Wikibase\Lib\Store\EntityLookup;
 use Wikibase\Lib\Store\EntityTitleLookup;
+use Wikibase\Lib\Store\LabelLookup;
 
 /**
  * Formats entity IDs by generating an HTML link to the corresponding page 
title.
@@ -28,10 +28,10 @@
 
        public function __construct(
                FormatterOptions $options,
-               EntityLookup $entityLookup,
+               LabelLookup $labelLookup,
                EntityTitleLookup $entityTitleLookup = null
        ) {
-               parent::__construct( $options, $entityLookup );
+               parent::__construct( $options, $labelLookup );
 
                $this->entityTitleLookup = $entityTitleLookup;
        }
diff --git a/lib/includes/formatters/EntityIdLabelFormatter.php 
b/lib/includes/formatters/EntityIdLabelFormatter.php
index b3c1d9f..1081797 100644
--- a/lib/includes/formatters/EntityIdLabelFormatter.php
+++ b/lib/includes/formatters/EntityIdLabelFormatter.php
@@ -7,8 +7,7 @@
 use ValueFormatters\FormatterOptions;
 use ValueFormatters\FormattingException;
 use Wikibase\DataModel\Entity\EntityId;
-use Wikibase\LanguageFallbackChain;
-use Wikibase\Lib\Store\EntityLookup;
+use Wikibase\Lib\Store\LabelLookup;
 use Wikibase\Lib\Store\StorageException;
 use Wikibase\Lib\Store\UnresolvedRedirectException;
 
@@ -19,8 +18,6 @@
  * @author Jeroen De Dauw < jeroended...@gmail.com >
  * @author Katie Filbert < aude.w...@gmail.com >
  * @author Daniel Kinzler
- *
- * @todo: add support for language fallback chains
  */
 class EntityIdLabelFormatter extends EntityIdFormatter {
 
@@ -39,23 +36,21 @@
        const FALLBACK_NONE = 2;
 
        /**
-        * @var EntityLookup
+        * @var LabelLookup
         */
-       protected $entityLookup;
+       private $labelLookup;
 
        /**
         * @since 0.4
         *
         * @param FormatterOptions $options Supported options: OPT_LOOKUP_LABEL 
(boolean),
         *        OPT_LABEL_FALLBACK (FALLBACK_XXX)
-        * @param EntityLookup $entityLookup
+        * @param LabelLookup $labelLookup
         *
         * @throws InvalidArgumentException
         */
-       public function __construct( FormatterOptions $options, EntityLookup 
$entityLookup ) {
+       public function __construct( FormatterOptions $options, LabelLookup 
$labelLookup ) {
                parent::__construct( $options );
-
-               $this->entityLookup = $entityLookup;
 
                $this->defaultOption( self::OPT_LOOKUP_LABEL, true );
                $this->defaultOption( self::OPT_LABEL_FALLBACK, 
self::FALLBACK_PREFIXED_ID );
@@ -76,6 +71,8 @@
                if ( !is_bool( $this->getOption( self::OPT_LOOKUP_LABEL ) ) ) {
                        throw new InvalidArgumentException( 'Bad value for 
OPT_LOOKUP_LABEL option: must be a boolean' );
                }
+
+               $this->labelLookup = $labelLookup;
        }
 
        /**
@@ -97,6 +94,7 @@
                        }
                }
 
+               // @fixme check if the entity is deleted and format differently?
                if ( !is_string( $label ) ) {
                        switch ( $this->getOption( self::OPT_LABEL_FALLBACK ) ) 
{
                                case self::FALLBACK_PREFIXED_ID:
@@ -125,35 +123,11 @@
         */
        protected function lookupEntityLabel( EntityId $entityId ) {
                try {
-                       $entity = $this->entityLookup->getEntity( $entityId );
-               } catch ( UnresolvedRedirectException $ex )  {
-                       $entity = null;
-               } catch ( StorageException $ex )  {
-                       $entity = null;
-                       wfLogWarning( 'Failed to load entity: '
-                               . $entityId->getSerialization() . ': '
-                               . $ex->getMessage() );
-               }
-
-               if ( $entity === null ) {
-                       // double redirect, deleted entity, etc
-                       throw new OutOfBoundsException( "An Entity with the id 
$entityId could not be loaded" );
-               }
-
-               /* @var LanguageFallbackChain $languageFallbackChain */
-               if ( $this->options->hasOption( 'languages' ) ) {
-                       $languageFallbackChain = $this->getOption( 'languages' 
);
-
-                       $extractedData = 
$languageFallbackChain->extractPreferredValue( $entity->getLabels() );
-
-                       if ( $extractedData === null ) {
-                               return false;
-                       } else {
-                               return $extractedData['value'];
-                       }
-               } else {
-                       $lang = $this->getOption( self::OPT_LANG );
-                       return $entity->getLabel( $lang );
+                       return $this->labelLookup->getLabel( $entityId );
+               } catch ( StorageException $ex ) {
+                       // @todo maybe handle formatting in this in a better 
way.
+                       throw new OutOfBoundsException( "An Entity with the id 
$entityId "
+                               . "could not be loaded." );
                }
        }
 
diff --git a/lib/includes/formatters/WikibaseValueFormatterBuilders.php 
b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
index 1d88396..414d722 100644
--- a/lib/includes/formatters/WikibaseValueFormatterBuilders.php
+++ b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
@@ -13,6 +13,9 @@
 use ValueFormatters\ValueFormatter;
 use Wikibase\LanguageFallbackChain;
 use Wikibase\LanguageFallbackChainFactory;
+use Wikibase\Lib\Store\EntityRetrievingTermLookup;
+use Wikibase\Lib\Store\LanguageFallbackLabelLookup;
+use Wikibase\Lib\Store\LanguageLabelLookup;
 use Wikibase\Lib\Store\EntityLookup;
 use Wikibase\Lib\Store\EntityTitleLookup;
 
@@ -234,7 +237,7 @@
         * @todo  : Sort out how the desired language is specified. We have two 
language options,
         *        each accepting different ways of specifying the language. 
That's not good.
         */
-       public function applyLanguageDefaults( $options ) {
+       public function applyLanguageDefaults( FormatterOptions $options ) {
                $languageFallbackChainFactory = new 
LanguageFallbackChainFactory();
 
                if ( !$options->hasOption( ValueFormatter::OPT_LANG ) ) {
@@ -371,7 +374,6 @@
                return $htmlFormatters;
        }
 
-
        /**
         * Returns a full set of formatters for generating HTML widgets.
         * If there are formatters defined for HTML that are not defined for 
widgets,
@@ -497,6 +499,38 @@
        }
 
        /**
+        * @param FormatterOptions $options
+        * @param WikibaseValueFormatterBuilders $builders
+        *
+        * @throws InvalidArgumentException
+        * @return LabelLookup
+        */
+       private static function newLabelLookup(
+               FormatterOptions $options,
+               WikibaseValueFormatterBuilders $builders
+       ) {
+               $termLookup = new EntityRetrievingTermLookup( 
$builders->entityLookup );
+
+               // @fixme inject the label lookup
+               if ( $options->hasOption( 'languages' ) ) {
+                       $labelLookup = new LanguageFallbackLabelLookup(
+                               $termLookup,
+                               $options->getOption( 'languages' )
+                       );
+               } else if ( $options->hasOption( ValueFormatter::OPT_LANG ) ) {
+                       $labelLookup = new LanguageLabelLookup(
+                               $termLookup,
+                               $options->getOption( ValueFormatter::OPT_LANG )
+                       );
+               } else {
+                       throw new InvalidArgumentException( 'OPT_LANG or 
languages (fallback chain) '
+                               . 'must be set in FormatterOptions.' );
+               }
+
+               return $labelLookup;
+       }
+
+       /**
         * Builder callback for use in 
WikibaseValueFormatterBuilders::$valueFormatterSpecs.
         * Used to inject services into the EntityIdLabelFormatter.
         *
@@ -509,7 +543,8 @@
                FormatterOptions $options,
                WikibaseValueFormatterBuilders $builders
        ) {
-               return new EntityIdLabelFormatter( $options, 
$builders->entityLookup );
+               $labelLookup = self::newLabelLookup( $options, $builders );
+               return new EntityIdLabelFormatter( $options, $labelLookup );
        }
 
        /**
@@ -527,7 +562,7 @@
        ) {
                return new EntityIdHtmlLinkFormatter(
                        $options,
-                       $builders->entityLookup,
+                       self::newLabelLookup( $options, $builders ),
                        $builders->entityTitleLookup
                );
        }
diff --git a/lib/includes/store/EntityRetrievingTermLookup.php 
b/lib/includes/store/EntityRetrievingTermLookup.php
new file mode 100644
index 0000000..f62b68d
--- /dev/null
+++ b/lib/includes/store/EntityRetrievingTermLookup.php
@@ -0,0 +1,132 @@
+<?php
+
+namespace Wikibase\Lib\Store;
+
+use OutOfBoundsException;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibaes\DataModel\Term\Fingerprint;
+use Wikibase\Lib\Store\EntityLookup;
+use Wikibase\Lib\Store\StorageException;
+use Wikibase\Lib\Store\UnresolvedRedirectException;
+
+/**
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < aude.w...@gmail.com >
+ */
+class EntityRetrievingTermLookup implements TermLookup {
+
+       /**
+        * @var EntityLookup
+        */
+       private $entityLookup;
+
+       /**
+        * @var Fingerprint[]
+        */
+       private $fingerprints;
+
+       /**
+        * @param EntityLookup $entityLookup
+        */
+       public function __construct( EntityLookup $entityLookup ) {
+               $this->entityLookup = $entityLookup;
+       }
+
+       /**
+        * Gets the label of an Entity with the specified EntityId and language 
code.
+        *
+        * @param EntityId $entityId
+        * @param string $languageCode
+        *
+        * @throws OutOfBoundsException
+        * @throws StorageException for entity not found
+        * @return string
+        */
+       public function getLabel( EntityId $entityId, $languageCode ) {
+               $labels = $this->getFingerprint( $entityId )->getLabels();
+               return $labels->getByLanguage( $languageCode )->getText();
+       }
+
+       /**
+        * Gets all labels of an Entity with the specified EntityId.
+        *
+        * @param EntityId $entityId
+        *
+        * @throws StorageException for entity not found
+        * @return string[]
+        */
+       public function getLabels( EntityId $entityId ) {
+               return $this->getFingerprint( $entityId 
)->getLabels()->toTextArray();
+       }
+
+       /**
+        * Gets the description of an Entity with the specified EntityId and 
language code.
+        *
+        * @param EntityId $entityId
+        * @param string $languageCode
+        *
+        * @throws OutOfBoundsException
+        * @throws StorageException for entity not found
+        * @return string
+        */
+       public function getDescription( EntityId $entityId, $languageCode ) {
+               $descriptions = $this->getFingerprint( $entityId 
)->getDescriptions();
+               return $descriptions->getByLanguage( $languageCode )->getText();
+       }
+
+       /**
+        * Gets all descriptions of an Entity with the specified EntityId.
+        *
+        * @param EntityId $entityId
+        *
+        * @throws StorageException for entity not found
+        * @return string[]
+        */
+       public function getDescriptions( EntityId $entityId ) {
+               return $this->getFingerprint( $entityId 
)->getDescriptions()->toTextArray();
+       }
+
+       /**
+        * @param EntityId
+        *
+        * @return Fingerprint
+        */
+       private function getFingerprint( EntityId $entityId ) {
+               $prefixedId = $entityId->getSerialization();
+
+               if ( !isset( $this->fingerprints[$prefixedId] ) ) {
+                       $this->fingerprints[$prefixedId] = 
$this->fetchFingerprint( $entityId );
+               }
+
+               return $this->fingerprints[$prefixedId];
+       }
+
+       /**
+        * @param EntityId
+        *
+        * @throws StorageException
+        * @return Fingerprint
+        */
+       private function fetchFingerprint( EntityId $entityId ) {
+               try {
+                       $entity = $this->entityLookup->getEntity( $entityId );
+               } catch ( UnresolvedRedirectException $ex )  {
+                       $entity = null;
+               } catch ( StorageException $ex )  {
+                       $entity = null;
+                       wfLogWarning( 'Failed to load entity: '
+                               . $entityId->getSerialization() . ': '
+                               . $ex->getMessage() );
+               }
+
+               if ( $entity === null ) {
+                       // double redirect, deleted entity, etc
+                       throw new StorageException( "An Entity with the id 
$entityId could not be loaded" );
+               }
+
+               return $entity->getFingerprint();
+       }
+
+}
diff --git a/lib/includes/store/TermLookup.php 
b/lib/includes/store/TermLookup.php
index 75bc490..067fe35 100644
--- a/lib/includes/store/TermLookup.php
+++ b/lib/includes/store/TermLookup.php
@@ -2,7 +2,9 @@
 
 namespace Wikibase\Lib\Store;
 
+use OutOfBoundsException;
 use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\Lib\Store\StorageException;
 
 /**
  * @since 0.5
@@ -18,6 +20,8 @@
         * @param EntityId $entityId
         * @param string $languageCode
         *
+        * @throws OutOfBoundsException for label not found
+        * @throws StorageException for Entity not found
         * @return string
         */
        public function getLabel( EntityId $entityId, $languageCode );
@@ -27,6 +31,7 @@
         *
         * @param EntityId $entityId
         *
+        * @throws StorageException for Entity not found
         * @return string[]
         */
        public function getLabels( EntityId $entityId );
@@ -37,6 +42,8 @@
         * @param EntityId $entityId
         * @param string $languageCode
         *
+        * @throws OutOfBoundsException for description not found
+        * @throws StorageException for Entity not found
         * @return string
         */
        public function getDescription( EntityId $entityId, $languageCode );
@@ -46,6 +53,7 @@
         *
         * @param EntityId $entityId
         *
+        * @throws StorageException for Entity not found
         * @return string[]
         */
        public function getDescriptions( EntityId $entityId );
diff --git a/lib/tests/phpunit/formatters/EntityIdLabelFormatterTest.php 
b/lib/tests/phpunit/formatters/EntityIdLabelFormatterTest.php
index 906b5b6..c4818cc 100644
--- a/lib/tests/phpunit/formatters/EntityIdLabelFormatterTest.php
+++ b/lib/tests/phpunit/formatters/EntityIdLabelFormatterTest.php
@@ -25,127 +25,52 @@
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < jeroended...@gmail.com >
  * @author Daniel Kinzler
+ * @author Katie Filbert < aude.w...@gmail.com >
  */
 class EntityIdLabelFormatterTest extends \PHPUnit_Framework_TestCase {
-
-       protected function newEntityLoader() {
-               $loader = new MockRepository();
-
-               $entity = Item::newEmpty();
-               $entity->setLabel( 'en', 'foo' );
-               $entity->setLabel( 'nl', 'bar' );
-               $entity->setLabel( 'zh-cn', '测试' );
-               $entity->setId( new ItemId( 'Q42' ) );
-
-               $loader->putEntity( $entity );
-               $loader->putRedirect( new EntityRedirect( new ItemId( 'Q23' ), 
new ItemId( 'Q42' ) ) );
-
-               return $loader;
-       }
 
        /**
         * @return array
         */
        public function validProvider() {
-               $languageFallbackChainFactory = new 
LanguageFallbackChainFactory();
-
                $argLists = array();
 
                $options = new FormatterOptions();
-               $options->setOption( EntityIdLabelFormatter::OPT_LANG, 'en' );
+               $options->setOption( EntityIdLabelFormatter::OPT_LANG, 'es' );
 
-               $argLists[] = array( new ItemId( 'Q42' ), 'foo', $options );
-
-
-               $options = new FormatterOptions();
-               $options->setOption( 'languages', 
$languageFallbackChainFactory->newFromLanguage( Language::factory( 'en' ) ) );
-
-               $argLists[] = array( new ItemId( 'q42' ), 'foo', $options );
-
-
-               $options = new FormatterOptions();
-               $options->setOption( EntityIdLabelFormatter::OPT_LANG, 'nl' );
-
-               $argLists[] = array( new ItemId( 'Q42' ), 'bar', $options );
-
+               $argLists[] = array( new ItemId( 'Q42' ), 'es', 'foo', $options 
);
 
                $options = new FormatterOptions();
                $options->setOption( EntityIdLabelFormatter::OPT_LANG, 'de' );
+               $options->setOption(
+                       EntityIdLabelFormatter::OPT_LABEL_FALLBACK,
+                       EntityIdLabelFormatter::FALLBACK_EMPTY_STRING
+               );
 
-               $argLists[] = array( new ItemId( 'Q42' ), 'Q42', $options );
-
-
-               $options = new FormatterOptions();
-               $options->setOption( 'languages', 
$languageFallbackChainFactory->newFromLanguage( Language::factory( 'de' ) ) );
-
-               $argLists[] = array( new ItemId( 'q42' ), 'foo', $options );
-
-
-               $options = new FormatterOptions();
-               $options->setOption( 'languages', 
$languageFallbackChainFactory->newFromLanguage( Language::factory( 'zh' ) ) );
-
-               $argLists[] = array( new ItemId( 'q42' ), '测试', $options );
-
-
-               $options = new FormatterOptions();
-               $options->setOption( 'languages', 
$languageFallbackChainFactory->newFromLanguage( Language::factory( 'zh-tw' ) ) 
);
-
-               $argLists[] = array( new ItemId( 'q42' ), '測試', $options );
-
-
-               $options = new FormatterOptions();
-               $options->setOption( 'languages', 
$languageFallbackChainFactory->newFromLanguage(
-                       Language::factory( 'zh-tw' ), 
LanguageFallbackChainFactory::FALLBACK_SELF
-               ) );
-
-               $argLists[] = array( new ItemId( 'q42' ), 'Q42', $options );
-
-
-               $options = new FormatterOptions();
-               $options->setOption( 'languages', 
$languageFallbackChainFactory->newFromLanguage(
-                       Language::factory( 'zh-tw' ),
-                       LanguageFallbackChainFactory::FALLBACK_SELF | 
LanguageFallbackChainFactory::FALLBACK_VARIANTS
-               ) );
-
-               $argLists[] = array( new ItemId( 'q42' ), '測試', $options );
-
-
-               $options = new FormatterOptions();
-               $options->setOption( 'languages', 
$languageFallbackChainFactory->newFromLanguage(
-                       Language::factory( 'sr' )
-               ) );
-
-               $argLists[] = array( new ItemId( 'q42' ), 'foo', $options );
-
-
-               $options = new FormatterOptions();
-               $options->setOption( EntityIdLabelFormatter::OPT_LANG, 'de' );
-               $options->setOption( 
EntityIdLabelFormatter::OPT_LABEL_FALLBACK, 
EntityIdLabelFormatter::FALLBACK_EMPTY_STRING );
-
-               $argLists[] = array( new EntityIdValue( new ItemId( 'Q42' ) ), 
'', $options );
+               $argLists[] = array( new EntityIdValue( new ItemId( 'Q42' ) ), 
'de', '', $options );
 
                $options = new FormatterOptions();
                $options->setOption( EntityIdLabelFormatter::OPT_LANG, 'en' );
                $options->setOption( EntityIdLabelFormatter::OPT_LOOKUP_LABEL, 
false );
 
-               $argLists[] = array( new EntityIdValue( new ItemId( 'Q42' ) ), 
'Q42', $options );
+               $argLists[] = array( new EntityIdValue( new ItemId( 'Q42' ) ), 
'en', 'Q42', $options );
 
 
                $options = new FormatterOptions();
                $options->setOption( EntityIdLabelFormatter::OPT_LANG, 'en' );
 
-               $argLists[] = array( new EntityIdValue( new ItemId( 'Q9001' ) 
), 'Q9001', $options );
+               $argLists[] = array( new EntityIdValue( new ItemId( 'Q9001' ) 
), 'en', 'Q9001', $options );
 
 
                $options = new FormatterOptions();
                $options->setOption( EntityIdLabelFormatter::OPT_LANG, 'en' );
 
-               $argLists[] = array( new PropertyId( 'P9001' ), 'P9001', 
$options );
+               $argLists[] = array( new PropertyId( 'P9001' ), 'en', 'P9001', 
$options );
 
                $options = new FormatterOptions();
                $options->setOption( EntityIdLabelFormatter::OPT_LANG, 'en' );
 
-               $argLists['unresolved-redirect'] = array( new ItemId( 'Q23' ), 
'Q23', $options );
+               $argLists['unresolved-redirect'] = array( new ItemId( 'Q23' ), 
'en', 'Q23', $options );
 
                return $argLists;
        }
@@ -154,11 +79,15 @@
         * @dataProvider validProvider
         *
         * @param EntityId|EntityIdValue $entityId
+        * @param string $languageCode
         * @param string $expectedString
         * @param FormatterOptions $formatterOptions
         */
-       public function testParseWithValidArguments( $entityId, 
$expectedString, FormatterOptions $formatterOptions ) {
-               $formatter = new EntityIdLabelFormatter( $formatterOptions, 
$this->newEntityLoader() );
+       public function testParseWithValidArguments( $entityId, $languageCode, 
$expectedString,
+               FormatterOptions $formatterOptions
+       ) {
+               $labelLookup = $this->getLabelLookup( $languageCode );
+               $formatter = new EntityIdLabelFormatter( $formatterOptions, 
$labelLookup );
 
                $formattedValue = $formatter->format( $entityId );
 
@@ -166,4 +95,22 @@
                $this->assertEquals( $expectedString, $formattedValue );
        }
 
+       protected function getLabelLookup( $languageCode ) {
+               $labelLookup = $this->getMockBuilder( 
'Wikibase\Lib\Store\LabelLookup' )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $labelLookup->expects( $this->any() )
+                       ->method( 'getLabel' )
+                       ->will( $this->returnCallback( function( EntityId 
$entityId ) use ( $languageCode ) {
+                               if ( $entityId->getSerialization() === 'Q42' && 
$languageCode === 'es' ) {
+                                       return 'foo';
+                               } else {
+                                       throw new \OutOfBoundsException( 'Label 
not found' );
+                               }
+                       } ) );
+
+               return $labelLookup;
+       }
+
 }
diff --git 
a/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php 
b/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
index aa6bd33..fca38bf 100644
--- a/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
+++ b/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
@@ -370,7 +370,7 @@
 
        public function testGetWidgetFormatters() {
                $builders = $this->newWikibaseValueFormatterBuilders( new 
ItemId( 'Q5' ) );
-               $options = new FormatterOptions();
+               $options = $this->newFormatterOptions();
 
                // check for all the required types, that is, the ones 
supported by the fallback format
                $required = array_keys( $builders->getHtmlFormatters( $options 
) );
@@ -389,7 +389,7 @@
 
        public function testGetDiffFormatters() {
                $builders = $this->newWikibaseValueFormatterBuilders( new 
ItemId( 'Q5' ) );
-               $options = new FormatterOptions();
+               $options = $this->newFormatterOptions();
 
                // check for all the required types, that is, the ones 
supported by the fallback format
                $required = array_keys( $builders->getHtmlFormatters( $options 
) );
diff --git a/lib/tests/phpunit/store/EntityRetrievingTermLookupTest.php 
b/lib/tests/phpunit/store/EntityRetrievingTermLookupTest.php
new file mode 100644
index 0000000..4e43cab
--- /dev/null
+++ b/lib/tests/phpunit/store/EntityRetrievingTermLookupTest.php
@@ -0,0 +1,96 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\Lib\Store\EntityRetrievingTermLookup;
+
+class EntityRetrievingTermLookupTest extends \PHPUnit_Framework_TestCase {
+
+       public function testGetLabel() {
+               $termLookup = $this->getEntityTermLookup();
+
+               $label = $termLookup->getLabel( new ItemId( 'Q116' ), 'en' );
+               $this->assertEquals( 'New York City', $label );
+       }
+
+       public function testGetLabel_notFoundThrowsException() {
+               $termLookup = $this->getEntityTermLookup();
+
+               $this->setExpectedException( 'OutOfBoundsException' );
+               $termLookup->getLabel( new ItemId( 'Q116' ), 'fa' );
+       }
+
+       public function testGetLabel_entityNotFound() {
+               $termLookup = $this->getEntityTermLookup();
+
+               $this->setExpectedException( 
'\Wikibase\Lib\Store\StorageException' );
+               $termLookup->getLabel( new ItemId( 'Q120' ), 'en' );
+       }
+
+       public function testGetLabels() {
+               $termLookup = $this->getEntityTermLookup();
+
+               $labels = $termLookup->getLabels( new ItemId( 'Q116' ) );
+
+               $expected = array(
+                       'en' => 'New York City',
+                       'es' => 'Nueva York'
+               );
+
+               $this->assertEquals( $expected, $labels );
+       }
+
+       public function testGetDescription() {
+               $termLookup = $this->getEntityTermLookup();
+
+               $description = $termLookup->getDescription( new ItemId( 'Q116' 
), 'de' );
+               $expected = 'Metropole an der Ostküste der Vereinigten Staaten';
+
+               $this->assertEquals( $expected, $description );
+       }
+
+       public function testGetDescription_notFoundThrowsException() {
+               $termLookup = $this->getEntityTermLookup();
+
+               $this->setExpectedException( 'OutOfBoundsException' );
+               $termLookup->getDescription( new ItemId( 'Q116' ), 'fr' );
+       }
+
+       public function getDescriptions() {
+               $termLookup = $this->getEntityTermLookup();
+
+               $descriptions = $termLookup->getDescriptions( new ItemId( 
'Q116' ) );
+
+               $expected = array(
+                       'de' => 'Metropole an der Ostküste der Vereinigten 
Staaten',
+                       'en' => 'largest city in New York and the United States 
of America',
+               );
+
+               $this->assertEquals( $expected, $descriptions );
+       }
+
+       private function getEntityTermLookup() {
+               return new EntityRetrievingTermLookup( $this->getEntityLookup() 
);
+       }
+
+       private function getEntityLookup() {
+               $mockRepo = new MockRepository();
+
+               $item = Item::newEmpty();
+               $item->setId( new ItemId( 'Q116' ) );
+
+               $item->setLabel( 'en', 'New York City' );
+               $item->setLabel( 'es', 'Nueva York' );
+
+               $item->setDescription( 'de', 'Metropole an der Ostküste der 
Vereinigten Staaten' );
+               $item->setDescription( 'en', 'largest city in New York and the 
United States of America' );
+
+               $mockRepo->putEntity( $item );
+
+               return $mockRepo;
+       }
+
+}
diff --git a/repo/includes/Diff/EntityContentDiffView.php 
b/repo/includes/Diff/EntityContentDiffView.php
index 51245b6..6ba4dfb 100644
--- a/repo/includes/Diff/EntityContentDiffView.php
+++ b/repo/includes/Diff/EntityContentDiffView.php
@@ -20,6 +20,8 @@
 use Wikibase\Lib\EntityIdLabelFormatter;
 use Wikibase\Lib\EscapingValueFormatter;
 use Wikibase\Lib\SnakFormatter;
+use Wikibase\Lib\Store\EntityRetrievingTermLookup;
+use Wikibase\Lib\Store\LanguageLabelLookup;
 use Wikibase\Repo\WikibaseRepo;
 use WikiPage;
 
@@ -75,10 +77,14 @@
                        ValueFormatter::OPT_LANG => $langCode
                ) );
 
-               $labelFormatter = new EntityIdLabelFormatter( $options, 
WikibaseRepo::getDefaultInstance()->getEntityLookup() );
+               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
+
+               $termLookup = new EntityRetrievingTermLookup( 
$wikibaseRepo->getEntityLookup() );
+               $labelLookup = new LanguageLabelLookup( $termLookup, $langCode 
);
+               $labelFormatter = new EntityIdLabelFormatter( $options, 
$labelLookup );
+
                $this->propertyNameFormatter = new EscapingValueFormatter( 
$labelFormatter, 'htmlspecialchars' );
 
-               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
                $formatterFactory = $wikibaseRepo->getSnakFormatterFactory();
                $this->detailedSnakFormatter = 
$formatterFactory->getSnakFormatter( SnakFormatter::FORMAT_HTML_DIFF, $options 
);
                $this->terseSnakFormatter = 
$formatterFactory->getSnakFormatter( SnakFormatter::FORMAT_HTML, $options );
diff --git a/repo/includes/actions/EditEntityAction.php 
b/repo/includes/actions/EditEntityAction.php
index db69ee3..33746a3 100644
--- a/repo/includes/actions/EditEntityAction.php
+++ b/repo/includes/actions/EditEntityAction.php
@@ -17,6 +17,8 @@
 use Wikibase\Lib\EntityIdLabelFormatter;
 use Wikibase\Lib\EscapingValueFormatter;
 use Wikibase\Lib\SnakFormatter;
+use Wikibase\Lib\Store\EntityRetrievingTermLookup;
+use Wikibase\Lib\Store\LanguageLabelLookup;
 use Wikibase\Repo\Content\EntityContentDiff;
 use Wikibase\Repo\Diff\ClaimDiffer;
 use Wikibase\Repo\Diff\ClaimDifferenceVisualizer;
@@ -67,10 +69,14 @@
                        ValueFormatter::OPT_LANG => $langCode
                ) );
 
-               $labelFormatter = new EntityIdLabelFormatter( $options, 
WikibaseRepo::getDefaultInstance()->getEntityLookup() );
+               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
+
+               $termLookup = new EntityRetrievingTermLookup( 
$wikibaseRepo->getEntityLookup() );
+               $labelLookup = new LanguageLabelLookup( $termLookup, $langCode 
);
+               $labelFormatter = new EntityIdLabelFormatter( $options, 
$labelLookup );
+
                $this->propertyNameFormatter = new EscapingValueFormatter( 
$labelFormatter, 'htmlspecialchars' );
 
-               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
                $formatterFactory = $wikibaseRepo->getSnakFormatterFactory();
                $this->detailedSnakFormatter = 
$formatterFactory->getSnakFormatter( SnakFormatter::FORMAT_HTML_DIFF, $options 
);
                $this->terseSnakFormatter = 
$formatterFactory->getSnakFormatter( SnakFormatter::FORMAT_HTML, $options );
diff --git a/repo/includes/specials/SpecialItemDisambiguation.php 
b/repo/includes/specials/SpecialItemDisambiguation.php
index a9418ac..7dc0843 100644
--- a/repo/includes/specials/SpecialItemDisambiguation.php
+++ b/repo/includes/specials/SpecialItemDisambiguation.php
@@ -10,7 +10,9 @@
 use Wikibase\ItemDisambiguation;
 use Wikibase\Lib\EntityIdHtmlLinkFormatter;
 use Wikibase\Lib\Store\EntityLookup;
+use Wikibase\Lib\Store\EntityRetrievingTermLookup;
 use Wikibase\Lib\Store\EntityTitleLookup;
+use Wikibase\Lib\Store\LanguageLabelLookup;
 use Wikibase\Repo\WikibaseRepo;
 use Wikibase\TermIndex;
 
@@ -162,13 +164,22 @@
         * @param string $langCode
         */
        protected function displayDisambiguationPage( array /* of Item */ 
$items, $langCode ) {
+               // @fixme it is confusing to have so many $langCodes here, 
coming from
+               // different places and maybe not necessary to be this way.
+
                $formatterOptions = new FormatterOptions( array(
                        ValueFormatter::OPT_LANG => 
$this->getLanguage()->getCode()
                ) );
 
+               // @fixme inject this!
+               $labelLookup = new LanguageLabelLookup(
+                       new EntityRetrievingTermLookup( $this->entityLookup ),
+                       $this->getLanguage()->getCode()
+               );
+
                $linkFormatter = new EntityIdHtmlLinkFormatter(
                        $formatterOptions,
-                       $this->entityLookup,
+                       $labelLookup,
                        $this->entityTitleLookup
                );
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia6a968d289cefc2e4df917b60dc761652d8354b6
Gerrit-PatchSet: 21
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Hoo man <h...@online.de>
Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to