jenkins-bot has submitted this change and it was merged.
Change subject: Extract FormatterLabelLookupFactory
......................................................................
Extract FormatterLabelLookupFactory
Change-Id: I9fa6c6baa7eb5d4893fb2dd8554d27dd7047eb6f
---
M client/includes/WikibaseClient.php
A lib/includes/formatters/FormatterLabelLookupFactory.php
M lib/includes/formatters/WikibaseValueFormatterBuilders.php
M lib/includes/store/TermLookup.php
A lib/tests/phpunit/formatters/FormatterLabelLookupFactoryTest.php
M lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
M lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
M repo/includes/WikibaseRepo.php
8 files changed, 247 insertions(+), 55 deletions(-)
Approvals:
Aude: Looks good to me, but someone else must approve
Thiemo Mättig (WMDE): Looks good to me, approved
jenkins-bot: Verified
diff --git a/client/includes/WikibaseClient.php
b/client/includes/WikibaseClient.php
index 7ed0f92..e922929 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -13,6 +13,7 @@
use Site;
use SiteSQLStore;
use SiteStore;
+use StubObject;
use Wikibase\Client\Changes\AffectedPagesFinder;
use Wikibase\Client\Changes\ChangeHandler;
use Wikibase\Client\Changes\ChangeRunCoalescer;
@@ -39,6 +40,7 @@
use Wikibase\LanguageFallbackChainFactory;
use Wikibase\Lib\Changes\EntityChangeFactory;
use Wikibase\Lib\EntityRetrievingDataTypeLookup;
+use Wikibase\Lib\FormatterLabelLookupFactory;
use Wikibase\Lib\OutputFormatSnakFormatterFactory;
use Wikibase\Lib\OutputFormatValueFormatterFactory;
use Wikibase\Lib\PropertyInfoDataTypeLookup;
@@ -46,6 +48,7 @@
use Wikibase\Lib\Store\EntityContentDataCodec;
use Wikibase\Lib\Store\EntityLookup;
use Wikibase\Lib\Store\EntityRetrievingTermLookup;
+use Wikibase\Lib\Store\TermLookup;
use Wikibase\Lib\WikibaseDataTypeBuilders;
use Wikibase\Lib\WikibaseSnakFormatterBuilders;
use Wikibase\Lib\WikibaseValueFormatterBuilders;
@@ -473,8 +476,8 @@
*/
private function newSnakFormatterFactory() {
$valueFormatterBuilders = new WikibaseValueFormatterBuilders(
- $this->getTermLookup(),
- $this->contentLanguage
+ $this->contentLanguage,
+ new FormatterLabelLookupFactory( $this->getTermLookup()
)
);
$builders = new WikibaseSnakFormatterBuilders(
@@ -505,8 +508,8 @@
*/
private function newValueFormatterFactory() {
$builders = new WikibaseValueFormatterBuilders(
- $this->getTermLookup(),
- $this->contentLanguage
+ $this->contentLanguage,
+ new FormatterLabelLookupFactory( $this->getTermLookup()
)
);
return new OutputFormatValueFormatterFactory(
$builders->getValueFormatterBuildersForFormats() );
@@ -551,6 +554,7 @@
*/
public function getLanguageLinkBadgeDisplay() {
global $wgLang;
+ StubObject::unstub( $wgLang );
$badgeClassNames = $this->settings->getSetting(
'badgeClassNames' );
diff --git a/lib/includes/formatters/FormatterLabelLookupFactory.php
b/lib/includes/formatters/FormatterLabelLookupFactory.php
new file mode 100644
index 0000000..f855a68
--- /dev/null
+++ b/lib/includes/formatters/FormatterLabelLookupFactory.php
@@ -0,0 +1,81 @@
+<?php
+
+namespace Wikibase\Lib;
+
+use InvalidArgumentException;
+use ValueFormatters\FormatterOptions;
+use ValueFormatters\ValueFormatter;
+use Wikibase\LanguageFallbackChain;
+use Wikibase\Lib\Store\TermLookup;
+use Wikibase\Lib\Store\LabelLookup;
+use Wikibase\Lib\Store\LanguageFallbackLabelLookup;
+use Wikibase\Lib\Store\LanguageLabelLookup;
+
+/**
+ * Factory for LabelLookup objects based on FormatterOptions.
+ *
+ * The LabelLookup is created based on the values of the options
+ * 'LabelLookup', 'languages', and ValueFormatter::OPT_LANG:
+ *
+ * * 'LabelLookup' can be used to provide a custom LabelLookup instance
directly
+ * * If 'languages' is set, a LanguageFallbackLabelLookup will be created byed
on
+ * the LanguageFallbackChain contained in that option.
+ * * If ValueFormatter::OPT_LANG is set, a LanguageLabelLookup is created
+ * * If none of these options is set, an InvalidArgumentException is thrown.
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class FormatterLabelLookupFactory {
+
+ /**
+ * @var TermLookup
+ */
+ private $termLookup;
+
+ public function __construct( TermLookup $termLookup ) {
+ $this->termLookup = $termLookup;
+ }
+
+ private function getOption( FormatterOptions $options, $key, $type ) {
+ $value = $options->getOption( $key );
+
+ if ( !( $value instanceof $type ) && gettype( $value ) !==
$type ) {
+ throw new InvalidArgumentException( 'Option ' . $key .
' must be used ' .
+ 'with an instance of ' . $type . '.' );
+ }
+
+ return $value;
+ }
+
+ /**
+ * @param FormatterOptions $options
+ *
+ * @throws InvalidArgumentException
+ * @return LabelLookup
+ */
+ public function getLabelLookup( FormatterOptions $options ) {
+ if ( $options->hasOption( 'LabelLookup' ) ) {
+ $labelLookup = $this->getOption( $options,
'LabelLookup', 'Wikibase\Lib\Store\LabelLookup' );
+
+ } elseif ( $options->hasOption( 'languages' ) ) {
+ $fallbackChain = $this->getOption( $options,
'languages', 'Wikibase\LanguageFallbackChain' );
+ $labelLookup = new LanguageFallbackLabelLookup(
$this->termLookup, $fallbackChain );
+
+ } elseif ( $options->hasOption( ValueFormatter::OPT_LANG ) ) {
+ $language = $this->getOption( $options,
ValueFormatter::OPT_LANG, 'string' );
+ $labelLookup = new LanguageLabelLookup(
$this->termLookup, $language );
+
+ } else {
+ throw new InvalidArgumentException(
+ 'OPT_LANG, languages (fallback chain), or
LabelLookup ' .
+ 'must be set in FormatterOptions.'
+ );
+ }
+
+ return $labelLookup;
+ }
+
+}
diff --git a/lib/includes/formatters/WikibaseValueFormatterBuilders.php
b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
index 4c93676..a21bf50 100644
--- a/lib/includes/formatters/WikibaseValueFormatterBuilders.php
+++ b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
@@ -14,11 +14,7 @@
use ValueFormatters\ValueFormatter;
use Wikibase\LanguageFallbackChain;
use Wikibase\LanguageFallbackChainFactory;
-use Wikibase\Lib\Store\TermLookup;
use Wikibase\Lib\Store\EntityTitleLookup;
-use Wikibase\Lib\Store\LabelLookup;
-use Wikibase\Lib\Store\LanguageFallbackLabelLookup;
-use Wikibase\Lib\Store\LanguageLabelLookup;
/**
* Defines the formatters for DataValues supported by Wikibase.
@@ -31,14 +27,14 @@
class WikibaseValueFormatterBuilders {
/**
- * @var TermLookup
- */
- private $termLookup;
-
- /**
* @var Language
*/
private $defaultLanguage;
+
+ /**
+ * @var FormatterLabelLookupFactory
+ */
+ private $labelLookupFactory;
/**
* @var EntityTitleLookup|null
@@ -113,12 +109,12 @@
);
public function __construct(
- TermLookup $termLookup,
Language $defaultLanguage,
+ FormatterLabelLookupFactory $labelLookupFactory,
EntityTitleLookup $entityTitleLookup = null
) {
- $this->termLookup = $termLookup;
$this->defaultLanguage = $defaultLanguage;
+ $this->labelLookupFactory = $labelLookupFactory;
$this->entityTitleLookup = $entityTitleLookup;
}
@@ -506,40 +502,6 @@
}
/**
- * @param FormatterOptions $options
- *
- * @throws InvalidArgumentException
- * @return LabelLookup
- */
- private function newLabelLookup( FormatterOptions $options ) {
- $termLookup = $this->termLookup;
-
- if ( $options->hasOption( 'LabelLookup' ) ) {
- $labelLookup = $options->getOption( 'LabelLookup' );
-
- if ( !( $labelLookup instanceof LabelLookup ) ) {
- throw new InvalidArgumentException( 'Option
LabelLookup must be used ' .
- 'with an instance of LabelLookup.' );
- }
- } elseif ( $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.
*
@@ -548,7 +510,7 @@
* @return EntityIdLabelFormatter
*/
private function newEntityIdFormatter( FormatterOptions $options ) {
- $labelLookup = $this->newLabelLookup( $options );
+ $labelLookup = $this->labelLookupFactory->getLabelLookup(
$options );
return new EntityIdLabelFormatter( $options, $labelLookup );
}
@@ -561,7 +523,7 @@
* @return ValueFormatter
*/
private function newEntityIdHtmlFormatter( FormatterOptions $options ) {
- $labelLookup = $this->newLabelLookup( $options );
+ $labelLookup = $this->labelLookupFactory->getLabelLookup(
$options );
if ( !$this->entityTitleLookup ) {
return new EscapingValueFormatter(
diff --git a/lib/includes/store/TermLookup.php
b/lib/includes/store/TermLookup.php
index 0a2ed8d..7c07055 100644
--- a/lib/includes/store/TermLookup.php
+++ b/lib/includes/store/TermLookup.php
@@ -21,6 +21,7 @@
*
* @throws OutOfBoundsException for label not found
* @throws StorageException for Entity not found
+ *
* @return string
* @throws OutOfBoundsException if no such label was found
*/
diff --git a/lib/tests/phpunit/formatters/FormatterLabelLookupFactoryTest.php
b/lib/tests/phpunit/formatters/FormatterLabelLookupFactoryTest.php
new file mode 100644
index 0000000..ce77a42
--- /dev/null
+++ b/lib/tests/phpunit/formatters/FormatterLabelLookupFactoryTest.php
@@ -0,0 +1,133 @@
+<?php
+
+namespace Wikibase\Lib\Test;
+
+use Language;
+use OutOfBoundsException;
+use ValueFormatters\FormatterOptions;
+use ValueFormatters\StringFormatter;
+use ValueFormatters\ValueFormatter;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\LanguageFallbackChain;
+use Wikibase\LanguageWithConversion;
+use Wikibase\Lib\FormatterLabelLookupFactory;
+use Wikibase\Lib\Store\LanguageLabelLookup;
+use Wikibase\Lib\Store\TermLookup;
+
+/**
+ * @covers Wikibase\Lib\FormatterLabelLookupFactory
+ *
+ * @group ValueFormatters
+ * @group DataValueExtensions
+ * @group WikibaseLib
+ * @group Wikibase
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class FormatterLabelLookupFactoryTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @dataProvider provideGetLabelLookup
+ */
+ public function testGetLabelLookup( TermLookup $termLookup,
FormatterOptions $options, $expectedLabel ) {
+ $factory = new FormatterLabelLookupFactory( $termLookup );
+ $labelLookup = $factory->getLabelLookup( $options );
+
+ $this->assertInstanceOf( 'Wikibase\Lib\Store\LabelLookup',
$labelLookup );
+
+ $label = $labelLookup->getLabel( new ItemId( 'Q1' ) );
+ $this->assertEquals( $expectedLabel, $label );
+ }
+
+ public function provideGetLabelLookup() {
+ $termLookup = $this->getMock( 'Wikibase\Lib\Store\TermLookup' );
+
+ $termLookup->expects( $this->any() )
+ ->method( 'getLabel' )
+ ->will( $this->returnCallback( function ( $item,
$language ) {
+ if ( $language === 'de' ) {
+ return 'Kätzchen';
+ }
+
+ throw new OutOfBoundsException( 'no bananas' );
+ } ) );
+
+ $termLookup->expects( $this->any() )
+ ->method( 'getLabels' )
+ ->will( $this->returnValue( array( 'de' => 'Kätzchen' )
) );
+
+ $labelLookup = new LanguageLabelLookup( $termLookup, 'de' );
+
+ $deChChain = new LanguageFallbackChain( array(
+ LanguageWithConversion::factory( 'de-ch' ),
+ LanguageWithConversion::factory( 'de' ),
+ ) );
+
+ $frChain = new LanguageFallbackChain( array(
+ LanguageWithConversion::factory( 'fr' ),
+ ) );
+
+ return array(
+ 'language' => array(
+ $termLookup,
+ new FormatterOptions( array(
+ ValueFormatter::OPT_LANG => 'de',
+ ) ),
+ 'Kätzchen'
+ ),
+ 'language and fallback chain' => array(
+ $termLookup,
+ new FormatterOptions( array(
+ ValueFormatter::OPT_LANG => 'fr',
+ 'languages' => $deChChain,
+ ) ),
+ 'Kätzchen'
+ ),
+ 'language and fallback chain and LabelLookup' => array(
+ $termLookup,
+ new FormatterOptions( array(
+ ValueFormatter::OPT_LANG => 'fr',
+ 'languages' => $frChain,
+ 'LabelLookup' => $labelLookup
+ ) ),
+ 'Kätzchen'
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider provideGetLabelLookup_failure
+ */
+ public function testGetLabelLookup_failure( FormatterOptions $options )
{
+ $termLookup = $this->getMock( 'Wikibase\Lib\Store\TermLookup' );
+ $factory = new FormatterLabelLookupFactory( $termLookup );
+
+ $this->setExpectedException( 'InvalidArgumentException' );
+ $factory->getLabelLookup( $options );
+ }
+
+ public function provideGetLabelLookup_failure() {
+ return array(
+ 'bad language' => array(
+ new FormatterOptions( array(
+ ValueFormatter::OPT_LANG =>
Language::factory( 'en' ),
+ ) ),
+ ),
+ 'bad fallback chain' => array(
+ new FormatterOptions( array(
+ 'languages' => array( 'x', 'y', 'z' ),
+ ) ),
+ ),
+ 'bad LabelLookup' => array(
+ new FormatterOptions( array(
+ 'LabelLookup' => new
LanguageFallbackChain( array() )
+ ) ),
+ ),
+ 'no options' => array(
+ new FormatterOptions( array( ) ),
+ ),
+ );
+ }
+
+}
diff --git a/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
b/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
index 7b0c837..0751796 100644
--- a/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
+++ b/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
@@ -12,6 +12,7 @@
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
use Wikibase\DataModel\Snak\PropertyValueSnak;
+use Wikibase\Lib\FormatterLabelLookupFactory;
use Wikibase\Lib\OutputFormatSnakFormatterFactory;
use Wikibase\Lib\SnakFormatter;
use Wikibase\Lib\WikibaseSnakFormatterBuilders;
@@ -63,7 +64,11 @@
$lang = Language::factory( 'en' );
- $valueFormatterBuilders = new WikibaseValueFormatterBuilders(
$termLookup, $lang );
+ $valueFormatterBuilders = new WikibaseValueFormatterBuilders(
+ $lang,
+ new FormatterLabelLookupFactory( $termLookup )
+ );
+
return new WikibaseSnakFormatterBuilders(
$valueFormatterBuilders, $typeLookup, $typeFactory );
}
diff --git
a/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
b/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
index 20eb09b..bebc3ea 100644
--- a/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
+++ b/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
@@ -17,6 +17,7 @@
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\LanguageFallbackChainFactory;
use Wikibase\Lib\EntityIdFormatter;
+use Wikibase\Lib\FormatterLabelLookupFactory;
use Wikibase\Lib\OutputFormatValueFormatterFactory;
use Wikibase\Lib\SnakFormatter;
use Wikibase\Lib\WikibaseValueFormatterBuilders;
@@ -68,7 +69,11 @@
);
} ) );
- return new WikibaseValueFormatterBuilders( $termLookup,
Language::factory( 'en' ), $entityTitleLookup );
+ return new WikibaseValueFormatterBuilders(
+ Language::factory( 'en' ),
+ new FormatterLabelLookupFactory( $termLookup ),
+ $entityTitleLookup
+ );
}
private function newFormatterOptions( $lang = 'en' ) {
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index cdfcf3c..d155e75 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -22,6 +22,7 @@
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Entity\Item;
use Wikibase\DataModel\Entity\Property;
+use Wikibase\DataModel\Entity\PropertyDataTypeLookup;
use Wikibase\EntityFactory;
use Wikibase\EntityParserOutputGeneratorFactory;
use Wikibase\InternalSerialization\DeserializerFactory;
@@ -34,6 +35,7 @@
use Wikibase\Lib\DispatchingValueFormatter;
use Wikibase\Lib\EntityIdLinkFormatter;
use Wikibase\Lib\EntityRetrievingDataTypeLookup;
+use Wikibase\Lib\FormatterLabelLookupFactory;
use Wikibase\Lib\Localizer\DispatchingExceptionLocalizer;
use Wikibase\Lib\Localizer\ExceptionLocalizer;
use Wikibase\Lib\Localizer\GenericExceptionLocalizer;
@@ -41,7 +43,6 @@
use Wikibase\Lib\Localizer\ParseExceptionLocalizer;
use Wikibase\Lib\OutputFormatSnakFormatterFactory;
use Wikibase\Lib\OutputFormatValueFormatterFactory;
-use Wikibase\DataModel\Entity\PropertyDataTypeLookup;
use Wikibase\Lib\PropertyInfoDataTypeLookup;
use Wikibase\Lib\SnakConstructionService;
use Wikibase\Lib\SnakFormatter;
@@ -515,8 +516,8 @@
global $wgContLang;
return new WikibaseValueFormatterBuilders(
- $termLookup,
$wgContLang,
+ new FormatterLabelLookupFactory( $termLookup ),
$this->getEntityTitleLookup()
);
}
--
To view, visit https://gerrit.wikimedia.org/r/174198
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I9fa6c6baa7eb5d4893fb2dd8554d27dd7047eb6f
Gerrit-PatchSet: 10
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Adrian Lang <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: JanZerebecki <[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