jenkins-bot has submitted this change and it was merged.
Change subject: Introduce ContentLanguages concept
......................................................................
Introduce ContentLanguages concept
Bug: T87762
Change-Id: Ic61612828ee0fde4c0108f86a7ed310ceabed1e7
---
M client/includes/WikibaseClient.php
A lib/includes/ContentLanguages.php
A lib/includes/WikibaseContentLanguages.php
M lib/includes/formatters/MonolingualHtmlFormatter.php
M lib/includes/formatters/WikibaseValueFormatterBuilders.php
M lib/tests/phpunit/formatters/MonolingualHtmlFormatterTest.php
M lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
M lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
M repo/includes/ValidatorBuilders.php
M repo/includes/WikibaseRepo.php
M repo/tests/phpunit/includes/ValidatorBuildersTest.php
11 files changed, 171 insertions(+), 23 deletions(-)
Approvals:
Hoo man: Looks good to me, approved
jenkins-bot: Verified
diff --git a/client/includes/WikibaseClient.php
b/client/includes/WikibaseClient.php
index 4c8ce45..c2a9ea8 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -49,6 +49,7 @@
use Wikibase\Lib\Store\EntityLookup;
use Wikibase\Lib\Store\EntityRetrievingTermLookup;
use Wikibase\Lib\Store\TermLookup;
+use Wikibase\Lib\WikibaseContentLanguages;
use Wikibase\Lib\WikibaseDataTypeBuilders;
use Wikibase\Lib\WikibaseSnakFormatterBuilders;
use Wikibase\Lib\WikibaseValueFormatterBuilders;
@@ -474,10 +475,7 @@
* @return OutputFormatSnakFormatterFactory
*/
private function newSnakFormatterFactory() {
- $valueFormatterBuilders = new WikibaseValueFormatterBuilders(
- $this->contentLanguage,
- new FormatterLabelLookupFactory( $this->getTermLookup()
)
- );
+ $valueFormatterBuilders =
$this->newWikibaseValueFormatterBuilders();
$builders = new WikibaseSnakFormatterBuilders(
$valueFormatterBuilders,
@@ -506,14 +504,18 @@
* @return OutputFormatValueFormatterFactory
*/
private function newValueFormatterFactory() {
- $builders = new WikibaseValueFormatterBuilders(
- $this->contentLanguage,
- new FormatterLabelLookupFactory( $this->getTermLookup()
)
- );
-
+ $builders = $this->newWikibaseValueFormatterBuilders();
return new OutputFormatValueFormatterFactory(
$builders->getValueFormatterBuildersForFormats() );
}
+ private function newWikibaseValueFormatterBuilders() {
+ return new WikibaseValueFormatterBuilders(
+ $this->contentLanguage,
+ new FormatterLabelLookupFactory( $this->getTermLookup()
),
+ new WikibaseContentLanguages()
+ );
+ }
+
/**
* @return NamespaceChecker
*/
diff --git a/lib/includes/ContentLanguages.php
b/lib/includes/ContentLanguages.php
new file mode 100644
index 0000000..478eafd
--- /dev/null
+++ b/lib/includes/ContentLanguages.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Wikibase\Lib;
+
+/**
+ * A list of languages supported as content language
+ *
+ * @author Adrian Heine < [email protected] >
+ */
+interface ContentLanguages {
+
+ /**
+ * @return string[] Array of language codes supported as content
language
+ */
+ public function getLanguages();
+
+ /**
+ * Get the name of the language specified by $languageCode. The name
should be in the language
+ * specified by $inLanguage, but it might be in any other language. If
null is given as $inLanguage,
+ * $languageCode is used, i. e. the service tries to give the autonym
of the language.
+ *
+ * @param string $languageCode
+ * @param string|null $inLanguage
+ *
+ * @return string
+ */
+ public function getName( $languageCode, $inLanguage = null );
+
+}
diff --git a/lib/includes/WikibaseContentLanguages.php
b/lib/includes/WikibaseContentLanguages.php
new file mode 100644
index 0000000..e6dc85e
--- /dev/null
+++ b/lib/includes/WikibaseContentLanguages.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Wikibase\Lib;
+
+use Wikibase\Utils;
+
+/**
+ * Provide languages supported as content language based on Wikibase\Utils
+ *
+ * @author Adrian Heine < [email protected] >
+ */
+class WikibaseContentLanguages implements ContentLanguages {
+
+ /**
+ * @return string[] Array of language codes supported as content
language
+ */
+ public function getLanguages() {
+ return Utils::getLanguageCodes();
+ }
+
+ /**
+ * Get the name of the language specified by $languageCode. The name
should be in the language
+ * specified by $inLanguage, but it might be in any other language. If
null is given as $inLanguage,
+ * $languageCode is used, i. e. the service tries to give the autonym
of the language.
+ *
+ * @param string $languageCode
+ * @param string|null $inLanguage
+ *
+ * @return string
+ */
+ public function getName( $languageCode, $inLanguage = null ) {
+ return Utils::fetchLanguageName( $languageCode, $inLanguage );
+ }
+
+}
diff --git a/lib/includes/formatters/MonolingualHtmlFormatter.php
b/lib/includes/formatters/MonolingualHtmlFormatter.php
index 04bb644..808e180 100644
--- a/lib/includes/formatters/MonolingualHtmlFormatter.php
+++ b/lib/includes/formatters/MonolingualHtmlFormatter.php
@@ -4,9 +4,10 @@
use DataValues\MonolingualTextValue;
use InvalidArgumentException;
+use ValueFormatters\FormatterOptions;
use ValueFormatters\ValueFormatter;
use ValueFormatters\ValueFormatterBase;
-use Wikibase\Utils;
+use Wikibase\Lib\ContentLanguages;
/**
* @since 0.5
@@ -15,6 +16,20 @@
* @author Daniel Kinzler
*/
class MonolingualHtmlFormatter extends ValueFormatterBase {
+
+ /**
+ * @var ContentLanguages $contentLanguages
+ */
+ private $contentLanguages;
+
+ /**
+ * @param FormatterOptions $options
+ * @param ContentLanguages $contentLanguages
+ */
+ public function __construct( FormatterOptions $options,
ContentLanguages $contentLanguages ) {
+ parent::__construct( $options );
+ $this->contentLanguages = $contentLanguages;
+ }
/**
* @see ValueFormatter::format
@@ -28,7 +43,7 @@
$text = $value->getText();
$languageCode = $value->getLanguageCode();
- $languageName = Utils::fetchLanguageName( $languageCode,
$userLanguage );
+ $languageName = $this->contentLanguages->getName(
$languageCode, $userLanguage );
$msg = wfMessage( 'wikibase-monolingualtext' )->params(
wfEscapeWikiText( $text ),
diff --git a/lib/includes/formatters/WikibaseValueFormatterBuilders.php
b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
index 2fceb4a..13bda9e 100644
--- a/lib/includes/formatters/WikibaseValueFormatterBuilders.php
+++ b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
@@ -12,6 +12,7 @@
use ValueFormatters\FormatterOptions;
use ValueFormatters\QuantityFormatter;
use ValueFormatters\ValueFormatter;
+use Wikibase\Formatters\MonolingualHtmlFormatter;
use Wikibase\LanguageFallbackChain;
use Wikibase\LanguageFallbackChainFactory;
use Wikibase\Lib\Store\EntityTitleLookup;
@@ -40,6 +41,13 @@
* @var EntityTitleLookup|null
*/
private $entityTitleLookup;
+
+ /**
+ * The languages available for MonolingualTextValues
+ *
+ * @var ContentLanguages
+ */
+ private $monolingualTextLanguages;
/**
* This determines which value is formatted how by providing a
formatter mapping
@@ -92,7 +100,7 @@
'PT:wikibase-item' => array( 'this',
'newEntityIdHtmlFormatter' ),
'PT:wikibase-property' => array( 'this',
'newEntityIdHtmlFormatter' ),
'VT:time' => array( 'this', 'newHtmlTimeFormatter' ),
- 'VT:monolingualtext' =>
'Wikibase\Formatters\MonolingualHtmlFormatter',
+ 'VT:monolingualtext' => array( 'this',
'newMonolingualHtmlFormatter' ),
),
// Formatters to use for HTML widgets.
@@ -112,10 +120,12 @@
public function __construct(
Language $defaultLanguage,
FormatterLabelLookupFactory $labelLookupFactory,
+ ContentLanguages $monolingualTextLanguages,
EntityTitleLookup $entityTitleLookup = null
) {
$this->defaultLanguage = $defaultLanguage;
$this->labelLookupFactory = $labelLookupFactory;
+ $this->monolingualTextLanguages = $monolingualTextLanguages;
$this->entityTitleLookup = $entityTitleLookup;
}
@@ -582,6 +592,18 @@
}
/**
+ * Builder callback for use in
WikibaseValueFormatterBuilders::$valueFormatterSpecs.
+ * Used to compose the MonolingualHtmlFormatter.
+ *
+ * @param FormatterOptions $options
+ *
+ * @return MonolingualHtmlFormatter
+ */
+ private function newMonolingualHtmlFormatter( FormatterOptions $options
) {
+ return new MonolingualHtmlFormatter( $options,
$this->monolingualTextLanguages );
+ }
+
+ /**
* Wrap each entry in a list of formatters in an EscapingValueFormatter.
* This is useful to apply escaping to the output of a set of
formatters,
* allowing them to be used for a different format.
diff --git a/lib/tests/phpunit/formatters/MonolingualHtmlFormatterTest.php
b/lib/tests/phpunit/formatters/MonolingualHtmlFormatterTest.php
index e5996d8..ea67480 100644
--- a/lib/tests/phpunit/formatters/MonolingualHtmlFormatterTest.php
+++ b/lib/tests/phpunit/formatters/MonolingualHtmlFormatterTest.php
@@ -24,7 +24,12 @@
* @dataProvider monolingualHtmlFormatProvider
*/
public function testFormat( $value, $options, $pattern, $not = '' ) {
- $formatter = new MonolingualHtmlFormatter( $options );
+ $contentLanguages = $this->getMock(
'Wikibase\Lib\ContentLanguages' );
+ $contentLanguages->expects( $this->any() )
+ ->method( 'getName' )
+ ->will( $this->returnValue( 'Deutsch' ));
+
+ $formatter = new MonolingualHtmlFormatter( $options,
$contentLanguages );
$text = $formatter->format( $value );
@@ -43,7 +48,7 @@
'formatting' => array(
new MonolingualTextValue( 'de', 'Hallo Welt' ),
$options,
- '@^<span lang="de".*?>Hallo
Welt<\/span>.*\((German|Deutsch)\).*$@'
+ '@^<span lang="de".*?>Hallo
Welt<\/span>.*\Deutsch.*$@'
),
'html/wikitext escaping' => array(
new MonolingualTextValue( 'de',
'[[Hallo&Welt]]' ),
diff --git a/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
b/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
index 0751796..b149cdd 100644
--- a/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
+++ b/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
@@ -66,7 +66,8 @@
$valueFormatterBuilders = new WikibaseValueFormatterBuilders(
$lang,
- new FormatterLabelLookupFactory( $termLookup )
+ new FormatterLabelLookupFactory( $termLookup ),
+ $this->getMock( 'Wikibase\Lib\ContentLanguages' )
);
return new WikibaseSnakFormatterBuilders(
$valueFormatterBuilders, $typeLookup, $typeFactory );
diff --git
a/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
b/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
index 217a8f8..1177cb2 100644
--- a/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
+++ b/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
@@ -73,9 +73,15 @@
);
} ) );
+ $contentLanguages = $this->getMock(
'Wikibase\Lib\ContentLanguages' );
+ $contentLanguages->expects( $this->any() )
+ ->method( 'getName' )
+ ->will( $this->returnValue( 'Deutsch' ));
+
return new WikibaseValueFormatterBuilders(
Language::factory( 'en' ),
new FormatterLabelLookupFactory( $termLookup ),
+ $contentLanguages,
$entityTitleLookup
);
}
@@ -216,7 +222,7 @@
SnakFormatter::FORMAT_HTML,
$this->newFormatterOptions( 'en' ),
new MonolingualTextValue( 'de', 'Hallo Welt' ),
- '/^<span lang="de".*?>Hallo
Welt<\/span>.*\((German|Deutsch)\).*$/'
+ '/^<span lang="de".*?>Hallo
Welt<\/span>.*\Deutsch.*$/'
),
'text in spanish' => array(
SnakFormatter::FORMAT_WIKI,
diff --git a/repo/includes/ValidatorBuilders.php
b/repo/includes/ValidatorBuilders.php
index 821f102..a74a092 100644
--- a/repo/includes/ValidatorBuilders.php
+++ b/repo/includes/ValidatorBuilders.php
@@ -7,8 +7,8 @@
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Entity\Item;
use Wikibase\DataModel\Entity\Property;
+use Wikibase\Lib\ContentLanguages;
use Wikibase\Lib\Store\EntityLookup;
-use Wikibase\Utils;
use Wikibase\Validators\AlternativeValidator;
use Wikibase\Validators\CompositeValidator;
use Wikibase\Validators\DataFieldValidator;
@@ -49,15 +49,24 @@
private $urlSchemes;
/**
+ *
+ * @var ContentLanguages
+ */
+ private $contentLanguages;
+
+ /**
* @param EntityLookup $lookup
* @param EntityIdParser $idParser
* @param string[] $urlSchemes
+ * @param ContentLanguages $contentLanguages
*/
public function __construct(
EntityLookup $lookup,
EntityIdParser $idParser,
- array $urlSchemes
+ array $urlSchemes,
+ ContentLanguages $contentLanguages
) {
+ $this->contentLanguages = $contentLanguages;
$this->entityIdParser = $idParser;
$this->entityLookup = $lookup;
$this->urlSchemes = $urlSchemes;
@@ -182,7 +191,7 @@
$validators[] = new DataFieldValidator(
'language',
- new MembershipValidator( Utils::getLanguageCodes() )
+ new MembershipValidator(
$this->contentLanguages->getLanguages() )
);
$topValidator = new DataValueValidator(
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index af4f18f..93c7b4e 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -29,6 +29,7 @@
use Wikibase\InternalSerialization\SerializerFactory;
use Wikibase\LabelDescriptionDuplicateDetector;
use Wikibase\LanguageFallbackChainFactory;
+use Wikibase\Lib\WikibaseContentLanguages;
use Wikibase\Lib\Changes\EntityChangeFactory;
use Wikibase\Lib\ClaimGuidGenerator;
use Wikibase\Lib\ClaimGuidValidator;
@@ -176,6 +177,11 @@
* @var TermLookup
*/
private $termLookup;
+
+ /**
+ * @var ContentLanguages
+ */
+ private $monolingualTextLanguages = null;
/**
* Returns the default instance constructed using newInstance().
@@ -540,6 +546,7 @@
return new WikibaseValueFormatterBuilders(
$wgContLang,
new FormatterLabelLookupFactory( $termLookup ),
+ $this->getMonolingualTextLanguages(),
$this->getEntityTitleLookup()
);
}
@@ -1033,9 +1040,16 @@
new ValidatorBuilders(
$this->getEntityLookup(),
$this->getEntityIdParser(),
- $urlSchemes
+ $urlSchemes,
+ $this->getMonolingualTextLanguages()
)
);
}
+ private function getMonolingualTextLanguages() {
+ if( $this->monolingualTextLanguages === null ) {
+ $this->monolingualTextLanguages = new
WikibaseContentLanguages();
+ }
+ return $this->monolingualTextLanguages;
+ }
}
diff --git a/repo/tests/phpunit/includes/ValidatorBuildersTest.php
b/repo/tests/phpunit/includes/ValidatorBuildersTest.php
index c5c98b8..629faca 100644
--- a/repo/tests/phpunit/includes/ValidatorBuildersTest.php
+++ b/repo/tests/phpunit/includes/ValidatorBuildersTest.php
@@ -46,7 +46,17 @@
$urlSchemes = array( 'http', 'https', 'ftp', 'mailto' );
- $builders = new ValidatorBuilders( $entityLookup,
$entityIdParser, $urlSchemes );
+ $contentLanguages = $this->getMock(
'Wikibase\Lib\ContentLanguages' );
+ $contentLanguages->expects( $this->any() )
+ ->method( 'getLanguages' )
+ ->will( $this->returnValue( array( 'contentlanguage' )
) );
+
+ $builders = new ValidatorBuilders(
+ $entityLookup,
+ $entityIdParser,
+ $urlSchemes,
+ $contentLanguages
+ );
$validatorFactory = new BuilderBasedDataTypeValidatorFactory(
$builders );
return $validatorFactory;
@@ -178,8 +188,8 @@
array( 'quantity', QuantityValue::newFromNumber(
'-11.234', '1', '-10', '-12' ), true, 'decimal strings' ),
//monolingual text
- array( 'monolingualtext', new MonolingualTextValue(
'en', 'text' ), true, 'Simple value' ),
- array( 'monolingualtext', new MonolingualTextValue(
'grrr', 'text' ), false, 'Not a valid language' ),
+ array( 'monolingualtext', new MonolingualTextValue(
'contentlanguage', 'text' ), true, 'Simple value' ),
+ array( 'monolingualtext', new MonolingualTextValue(
'en', 'text' ), false, 'Not a valid language' ),
);
if ( defined( 'WB_EXPERIMENTAL_FEATURES' ) &&
WB_EXPERIMENTAL_FEATURES ) {
--
To view, visit https://gerrit.wikimedia.org/r/189937
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ic61612828ee0fde4c0108f86a7ed310ceabed1e7
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Adrian Lang <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits