jenkins-bot has submitted this change and it was merged.
Change subject: Introduce FormatterLabelLookupFactory::OPT_ constants
......................................................................
Introduce FormatterLabelLookupFactory::OPT_ constants
These two options used to be strings before.
Note that there are two options with the value 'languages' in our code
base, the second is in SerializationOptions and not used outside of
that class.
Change-Id: If82c20e2698ded4540829f5ee87f3fdfb21709ca
---
M
client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php
M lib/includes/formatters/FormatterLabelDescriptionLookupFactory.php
M lib/includes/formatters/WikibaseValueFormatterBuilders.php
M lib/tests/phpunit/formatters/FormatterLabelDescriptionLookupFactoryTest.php
M lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
M repo/includes/WikibaseHtmlSnakFormatterFactory.php
M repo/tests/phpunit/includes/WikibaseHtmlSnakFormatterFactoryTest.php
7 files changed, 52 insertions(+), 36 deletions(-)
Approvals:
Hoo man: Looks good to me, approved
Legoktm: Looks good to me, approved
jenkins-bot: Verified
diff --git
a/client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php
b/client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php
index b726219..0c625cd 100644
---
a/client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php
+++
b/client/includes/DataAccess/PropertyParserFunction/PropertyClaimsRendererFactory.php
@@ -12,6 +12,7 @@
use Wikibase\DataAccess\StatementTransclusionInteractor;
use Wikibase\DataAccess\SnaksFinder;
use Wikibase\LanguageFallbackChainFactory;
+use Wikibase\Lib\FormatterLabelDescriptionLookupFactory;
use Wikibase\Lib\OutputFormatSnakFormatterFactory;
use Wikibase\Lib\SnakFormatter;
use Wikibase\Lib\Store\EntityLookup;
@@ -198,7 +199,7 @@
);
$options = new FormatterOptions( array(
- 'languages' => $languageFallbackChain,
+
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN =>
$languageFallbackChain,
// ...more options... (?)
) );
diff --git a/lib/includes/formatters/FormatterLabelDescriptionLookupFactory.php
b/lib/includes/formatters/FormatterLabelDescriptionLookupFactory.php
index a62f4bf..d539852 100644
--- a/lib/includes/formatters/FormatterLabelDescriptionLookupFactory.php
+++ b/lib/includes/formatters/FormatterLabelDescriptionLookupFactory.php
@@ -6,19 +6,19 @@
use ValueFormatters\FormatterOptions;
use ValueFormatters\ValueFormatter;
use Wikibase\LanguageFallbackChain;
-use Wikibase\Lib\Store\TermLookup;
use Wikibase\Lib\Store\LabelDescriptionLookup;
use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookup;
use Wikibase\Lib\Store\LanguageLabelDescriptionLookup;
+use Wikibase\Lib\Store\TermLookup;
/**
* Factory for LabelDescriptionLookup objects based on FormatterOptions.
*
* The LabelDescriptionLookup is created based on the values of the options
- * 'LabelDescriptionLookup', 'languages', and ValueFormatter::OPT_LANG:
+ * OPT_LABEL_DESCRIPTION_LOOKUP, OPT_LANGUAGE_FALLBACK_CHAIN, and
ValueFormatter::OPT_LANG:
*
- * * 'LabelDescriptionLookup' can be used to provide a custom
LabelDescriptionLookup instance directly
- * * If 'languages' is set, a LanguageFallbackLabelDescriptionLookup will be
created byed on
+ * * OPT_LABEL_DESCRIPTION_LOOKUP can be used to provide a custom
LabelDescriptionLookup instance directly
+ * * If OPT_LANGUAGE_FALLBACK_CHAIN is set, a
LanguageFallbackLabelDescriptionLookup will be created byed on
* the LanguageFallbackChain contained in that option.
* * If ValueFormatter::OPT_LANG is set, a LanguageLabelDescriptionLookup is
created
* * If none of these options is set, an InvalidArgumentException is thrown.
@@ -29,6 +29,9 @@
* @author Daniel Kinzler
*/
class FormatterLabelDescriptionLookupFactory {
+
+ const OPT_LABEL_DESCRIPTION_LOOKUP = 'LabelDescriptionLookup';
+ const OPT_LANGUAGE_FALLBACK_CHAIN = 'languages';
/**
* @var TermLookup
@@ -46,23 +49,23 @@
* @return LabelDescriptionLookup
*/
public function getLabelDescriptionLookup( FormatterOptions $options ) {
- if ( $options->hasOption( 'LabelDescriptionLookup' ) ) {
+ if ( $options->hasOption( self::OPT_LABEL_DESCRIPTION_LOOKUP )
) {
return $this->getLabelDescriptionLookupFromOptions(
$options );
- } elseif ( $options->hasOption( 'languages' ) ) {
+ } elseif ( $options->hasOption(
self::OPT_LANGUAGE_FALLBACK_CHAIN ) ) {
return
$this->newLanguageFallbackLabelDescriptionLookup( $options );
} elseif ( $options->hasOption( ValueFormatter::OPT_LANG ) ) {
return $this->newLanguageLabelDescriptionLookup(
$options );
} else {
- throw new InvalidArgumentException( 'OPT_LANG,
languages (fallback chain), '
- . 'or LabelDescriptionLookup must be set in
FormatterOptions.' );
+ throw new InvalidArgumentException( 'OPT_LANG,
OPT_LANGUAGE_FALLBACK_CHAIN, '
+ . 'or OPT_LABEL_DESCRIPTION_LOOKUP must be set
in FormatterOptions.' );
}
}
private function getLabelDescriptionLookupFromOptions( FormatterOptions
$options ) {
- $labelDescriptionLookup = $options->getOption(
'LabelDescriptionLookup' );
+ $labelDescriptionLookup = $options->getOption(
self::OPT_LABEL_DESCRIPTION_LOOKUP );
if ( !( $labelDescriptionLookup instanceof
LabelDescriptionLookup ) ) {
- throw new InvalidArgumentException( 'Option
LabelDescriptionLookup must be used ' .
+ throw new InvalidArgumentException(
'OPT_LABEL_DESCRIPTION_LOOKUP must be used ' .
'with an instance of LabelDescriptionLookup.' );
}
@@ -70,10 +73,10 @@
}
private function newLanguageFallbackLabelDescriptionLookup(
FormatterOptions $options ) {
- $fallbackChain = $options->getOption( 'languages' );
+ $fallbackChain = $options->getOption(
self::OPT_LANGUAGE_FALLBACK_CHAIN );
if ( !( $fallbackChain instanceof LanguageFallbackChain ) ) {
- throw new InvalidArgumentException( 'Option `languages`
must be used ' .
+ throw new InvalidArgumentException(
'OPT_LANGUAGE_FALLBACK_CHAIN must be used ' .
'with an instance of LanguageFallbackChain.' );
}
diff --git a/lib/includes/formatters/WikibaseValueFormatterBuilders.php
b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
index 84b2916..7e1ce1d 100644
--- a/lib/includes/formatters/WikibaseValueFormatterBuilders.php
+++ b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
@@ -261,8 +261,8 @@
}
/**
- * Initializes the options keys ValueFormatter::OPT_LANG and
'languages' if
- * they are not yet set.
+ * Initializes the options keys ValueFormatter::OPT_LANG and
+ * FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN
if they are not yet set.
*
* @param FormatterOptions $options The options to modify.
*
@@ -279,20 +279,25 @@
$lang = $options->getOption( ValueFormatter::OPT_LANG );
if ( !is_string( $lang ) ) {
- throw new InvalidArgumentException( 'The value of
OPT_LANG must be a language code. For a fallback chain, use the `languages`
option.' );
+ throw new InvalidArgumentException(
+ 'The value of OPT_LANG must be a language code.
For a fallback chain, use OPT_LANGUAGE_FALLBACK_CHAIN.'
+ );
}
- if ( !$options->hasOption( 'languages' ) ) {
+ if ( !$options->hasOption(
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN ) ) {
$fallbackMode = (
LanguageFallbackChainFactory::FALLBACK_VARIANTS
| LanguageFallbackChainFactory::FALLBACK_OTHERS
| LanguageFallbackChainFactory::FALLBACK_SELF );
- $options->setOption( 'languages',
$languageFallbackChainFactory->newFromLanguageCode( $lang, $fallbackMode ) );
+ $options->setOption(
+
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN,
+
$languageFallbackChainFactory->newFromLanguageCode( $lang, $fallbackMode )
+ );
}
- if ( !( $options->getOption( 'languages' ) instanceof
LanguageFallbackChain ) ) {
- throw new InvalidArgumentException( 'The value of the
`languages` option must be an instance of LanguageFallbackChain.' );
+ if ( !( $options->getOption(
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN )
instanceof LanguageFallbackChain ) ) {
+ throw new InvalidArgumentException( 'The value of
OPT_LANGUAGE_FALLBACK_CHAIN must be an instance of LanguageFallbackChain.' );
}
}
diff --git
a/lib/tests/phpunit/formatters/FormatterLabelDescriptionLookupFactoryTest.php
b/lib/tests/phpunit/formatters/FormatterLabelDescriptionLookupFactoryTest.php
index eb63c01..b61cff5 100644
---
a/lib/tests/phpunit/formatters/FormatterLabelDescriptionLookupFactoryTest.php
+++
b/lib/tests/phpunit/formatters/FormatterLabelDescriptionLookupFactoryTest.php
@@ -79,7 +79,7 @@
$termLookup,
new FormatterOptions( array(
ValueFormatter::OPT_LANG => 'fr',
- 'languages' => $deChChain,
+
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN =>
$deChChain,
) ),
'Kätzchen'
),
@@ -87,8 +87,8 @@
$termLookup,
new FormatterOptions( array(
ValueFormatter::OPT_LANG => 'fr',
- 'languages' => $frChain,
- 'LabelDescriptionLookup' =>
$labelDescriptionLookup
+
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN => $frChain,
+
FormatterLabelDescriptionLookupFactory::OPT_LABEL_DESCRIPTION_LOOKUP =>
$labelDescriptionLookup
) ),
'Kätzchen'
),
@@ -115,12 +115,12 @@
),
'bad fallback chain' => array(
new FormatterOptions( array(
- 'languages' => array( 'x', 'y', 'z' ),
+
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN => array(
'x', 'y', 'z' ),
) ),
),
'bad LabelDescriptionLookup' => array(
new FormatterOptions( array(
- 'LabelDescriptionLookup' => new
LanguageFallbackChain( array() )
+
FormatterLabelDescriptionLookupFactory::OPT_LABEL_DESCRIPTION_LOOKUP => new
LanguageFallbackChain( array() )
) ),
),
'no options' => array(
diff --git
a/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
b/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
index de059b7..fa7dc73 100644
--- a/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
+++ b/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
@@ -327,14 +327,14 @@
),
'fallback option' => array(
new FormatterOptions( array(
- 'languages' => $fallbackChain,
+
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN =>
$fallbackChain,
) ),
new ItemId( 'Q5' ),
'@>Name für Q5<@'
),
'LabelDescriptionLookup option' => array(
new FormatterOptions( array(
- 'LabelDescriptionLookup' =>
$labelDescriptionLookup,
+
FormatterLabelDescriptionLookupFactory::OPT_LABEL_DESCRIPTION_LOOKUP =>
$labelDescriptionLookup,
) ),
new ItemId( 'Q5' ),
'@>Custom LabelDescriptionLookup<@'
@@ -611,16 +611,16 @@
if ( $expectedLanguage !== null ) {
$lang = $options->getOption( ValueFormatter::OPT_LANG );
- $this->assertEquals( $expectedLanguage, $lang, 'option:
' . ValueFormatter::OPT_LANG );
+ $this->assertEquals( $expectedLanguage, $lang,
'OPT_LANG' );
}
if ( $expectedFallback !== null ) {
/** @var LanguageFallbackChain $languageFallback */
- $languageFallback = $options->getOption( 'languages' );
+ $languageFallback = $options->getOption(
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN );
$languages = $languageFallback->getFallbackChain();
$lang = $languages[0]->getLanguage()->getCode();
- $this->assertEquals( $expectedFallback, $lang, 'option:
languages' );
+ $this->assertEquals( $expectedFallback, $lang,
'OPT_LANGUAGE_FALLBACK_CHAIN' );
}
}
@@ -640,12 +640,17 @@
'de' // derived from language code
),
'language fallback set' => array(
- new FormatterOptions( array( 'languages' =>
$languageFallback ) ),
+ new FormatterOptions( array(
+
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN =>
$languageFallback
+ ) ),
'en', // default code is taken from the
constructor, not the fallback chain
'fr' // as given
),
'language code and fallback set' => array(
- new FormatterOptions( array(
ValueFormatter::OPT_LANG => 'de', 'languages' => $languageFallback ) ),
+ new FormatterOptions( array(
+ ValueFormatter::OPT_LANG => 'de',
+
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN =>
$languageFallback
+ ) ),
'de', // as given
'fr' // as given
),
diff --git a/repo/includes/WikibaseHtmlSnakFormatterFactory.php
b/repo/includes/WikibaseHtmlSnakFormatterFactory.php
index a8b796a..8520b30 100644
--- a/repo/includes/WikibaseHtmlSnakFormatterFactory.php
+++ b/repo/includes/WikibaseHtmlSnakFormatterFactory.php
@@ -5,6 +5,7 @@
use ValueFormatters\FormatterOptions;
use ValueFormatters\ValueFormatter;
use Wikibase\LanguageFallbackChain;
+use Wikibase\Lib\FormatterLabelDescriptionLookupFactory;
use Wikibase\Lib\OutputFormatSnakFormatterFactory;
use Wikibase\Lib\SnakFormatter;
use Wikibase\Lib\Store\LabelDescriptionLookup;
@@ -43,8 +44,8 @@
) {
$formatterOptions = new FormatterOptions( array(
ValueFormatter::OPT_LANG => $languageCode,
- 'languages' => $languageFallbackChain,
- 'LabelDescriptionLookup' => $labelDescriptionLookup,
+
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN =>
$languageFallbackChain,
+
FormatterLabelDescriptionLookupFactory::OPT_LABEL_DESCRIPTION_LOOKUP =>
$labelDescriptionLookup,
) );
return $formatterOptions;
}
diff --git
a/repo/tests/phpunit/includes/WikibaseHtmlSnakFormatterFactoryTest.php
b/repo/tests/phpunit/includes/WikibaseHtmlSnakFormatterFactoryTest.php
index c99804f..4774884 100644
--- a/repo/tests/phpunit/includes/WikibaseHtmlSnakFormatterFactoryTest.php
+++ b/repo/tests/phpunit/includes/WikibaseHtmlSnakFormatterFactoryTest.php
@@ -6,6 +6,7 @@
use ValueFormatters\FormatterOptions;
use ValueFormatters\ValueFormatter;
use Wikibase\LanguageFallbackChain;
+use Wikibase\Lib\FormatterLabelDescriptionLookupFactory;
use Wikibase\Lib\SnakFormatter;
use Wikibase\Repo\WikibaseHtmlSnakFormatterFactory;
@@ -35,8 +36,8 @@
SnakFormatter::FORMAT_HTML_WIDGET,
new FormatterOptions( array(
ValueFormatter::OPT_LANG => 'en',
- 'languages' => $languageFallbackChain,
- 'LabelDescriptionLookup' =>
$labelDescriptionLookup
+
FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN =>
$languageFallbackChain,
+
FormatterLabelDescriptionLookupFactory::OPT_LABEL_DESCRIPTION_LOOKUP =>
$labelDescriptionLookup
) )
)
->will( $this->returnValue( $snakFormatter ) );
--
To view, visit https://gerrit.wikimedia.org/r/197011
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: If82c20e2698ded4540829f5ee87f3fdfb21709ca
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: Adrian Lang <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Legoktm <[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