jenkins-bot has submitted this change and it was merged. Change subject: Label and description serialization now accepts LanguageFallbackChain as the language option ......................................................................
Label and description serialization now accepts LanguageFallbackChain as the language option Labels and descriptions in all serialized formats will get a new field 'source-language', to indicate the language where this label was fetched. If the target format doesn't have keys, one new extra field 'for-language' is added, to hold the language which is used as keys "normally". Change-Id: Iec62d6817e3007e1982db65b5031ec09ecda3374 --- M lib/WikibaseLib.classes.php M lib/includes/serializers/DescriptionSerializer.php M lib/includes/serializers/EntitySerializer.php M lib/includes/serializers/LabelSerializer.php A lib/includes/serializers/MultilingualSerializer.php M lib/tests/phpunit/serializers/DescriptionSerializerTest.php M lib/tests/phpunit/serializers/LabelSerializerTest.php A lib/tests/phpunit/serializers/MultilingualSerializerTest.php 8 files changed, 787 insertions(+), 33 deletions(-) Approvals: Daniel Kinzler: Looks good to me, approved jenkins-bot: Verified diff --git a/lib/WikibaseLib.classes.php b/lib/WikibaseLib.classes.php index 7fa82fc..f63ede2 100644 --- a/lib/WikibaseLib.classes.php +++ b/lib/WikibaseLib.classes.php @@ -118,6 +118,7 @@ 'Wikibase\Lib\Serializers\ReferenceSerializer' => 'includes/serializers/ReferenceSerializer.php', 'Wikibase\Lib\Serializers\SerializationOptions' => 'includes/serializers/SerializationOptions.php', 'Wikibase\Lib\Serializers\MultiLangSerializationOptions' => 'includes/serializers/SerializationOptions.php', + 'Wikibase\Lib\Serializers\MultilingualSerializer' => 'includes/serializers/MultilingualSerializer.php', 'Wikibase\Lib\Serializers\EntitySerializationOptions' => 'includes/serializers/SerializationOptions.php', 'Wikibase\Lib\Serializers\Serializer' => 'includes/serializers/Serializer.php', 'Wikibase\Lib\Serializers\SerializerFactory' => 'includes/serializers/SerializerFactory.php', diff --git a/lib/includes/serializers/DescriptionSerializer.php b/lib/includes/serializers/DescriptionSerializer.php index 9cb8dd4..6ed669a 100644 --- a/lib/includes/serializers/DescriptionSerializer.php +++ b/lib/includes/serializers/DescriptionSerializer.php @@ -42,21 +42,33 @@ protected $options; /** + * @var MultilingualSerializer + */ + protected $multilingualSerializer; + + /** * Constructor. * * @since 0.4 * * @param MultiLangSerializationOptions $options */ - public function __construct( MultiLangSerializationOptions $options = null ) { + public function __construct( MultiLangSerializationOptions $options = null, + MultilingualSerializer $multilingualSerializer = null + ) { if ( $options === null ) { $this->options = new MultiLangSerializationOptions(); + } + if ( $multilingualSerializer === null ) { + $this->multilingualSerializer = new MultilingualSerializer( $options ); + } else { + $this->multilingualSerializer = $multilingualSerializer; } parent::__construct( $options ); } /** - * Returns a serialized array of descriptions. + * Returns a serialized array of descriptions for all data given. * * @since 0.4 * @@ -70,17 +82,7 @@ throw new InvalidArgumentException( 'DescriptionSerializer can only serialize an array of descriptions' ); } - $value = array(); - $idx = 0; - - foreach ( $descriptions as $languageCode => $description ) { - $key = $this->options->shouldUseKeys() ? $languageCode : $idx++; - $valueKey = ( $description === '' ) ? 'removed' : 'value'; - $value[$key] = array( - 'language' => $languageCode, - $valueKey => $description - ); - } + $value = $this->multilingualSerializer->serializeMultilingualValues( $descriptions ); if ( !$this->options->shouldUseKeys() ) { $this->setIndexedTagName( $value, 'description' ); @@ -88,4 +90,21 @@ return $value; } + + /** + * Returns a serialized array of descriptions from raw description data array. + * + * Unlike getSerialized(), $descriptions is filtered first for requested languages then gets serialized with getSerialized(). + * + * @since 0.4 + * + * @param array $descriptions + * + * @return array + * @throws InvalidArgumentException + */ + public final function getSerializedMultilingualValues( $descriptions ) { + $descriptions = $this->multilingualSerializer->filterPreferredMultilingualValues( $descriptions ); + return $this->getSerialized( $descriptions ); + } } diff --git a/lib/includes/serializers/EntitySerializer.php b/lib/includes/serializers/EntitySerializer.php index f41e5f6..82c69e8 100644 --- a/lib/includes/serializers/EntitySerializer.php +++ b/lib/includes/serializers/EntitySerializer.php @@ -84,13 +84,13 @@ break; case 'descriptions': $descriptionSerializer = new DescriptionSerializer( $this->options ); - $descriptions = $entity->getDescriptions( $this->options->getLanguages() ); - $serialization['descriptions'] = $descriptionSerializer->getSerialized( $descriptions ); + $serialization['descriptions'] = $descriptionSerializer-> + getSerializedMultilingualValues( $entity->getDescriptions() ); break; case 'labels': $labelSerializer = new LabelSerializer( $this->options ); - $labels = $entity->getLabels( $this->options->getLanguages() ); - $serialization['labels'] = $labelSerializer->getSerialized( $labels ); + $serialization['labels'] = $labelSerializer-> + getSerializedMultilingualValues( $entity->getLabels() ); break; case 'claims': $claimsSerializer = new ClaimsSerializer( $this->options ); diff --git a/lib/includes/serializers/LabelSerializer.php b/lib/includes/serializers/LabelSerializer.php index 13094f8..84cf213 100644 --- a/lib/includes/serializers/LabelSerializer.php +++ b/lib/includes/serializers/LabelSerializer.php @@ -42,21 +42,33 @@ protected $options; /** + * @var MultilingualSerializer + */ + protected $multilingualSerializer; + + /** * Constructor. * * @since 0.4 * * @param MultiLangSerializationOptions $options */ - public function __construct( MultiLangSerializationOptions $options = null ) { + public function __construct( MultiLangSerializationOptions $options = null, + MultilingualSerializer $multilingualSerializer = null + ) { if ( $options === null ) { $this->options = new MultiLangSerializationOptions(); + } + if ( $multilingualSerializer === null ) { + $this->multilingualSerializer = new MultilingualSerializer( $options ); + } else { + $this->multilingualSerializer = $multilingualSerializer; } parent::__construct( $options ); } /** - * Returns a serialized array of labels. + * Returns a serialized array of labels for all data given. * * @since 0.4 * @@ -70,17 +82,7 @@ throw new InvalidArgumentException( 'LabelSerializer can only serialize an array of labels' ); } - $value = array(); - $idx = 0; - - foreach ( $labels as $languageCode => $label ) { - $key = $this->options->shouldUseKeys() ? $languageCode : $idx++; - $valueKey = ( $label === '' ) ? 'removed' : 'value'; - $value[$key] = array( - 'language' => $languageCode, - $valueKey => $label - ); - } + $value = $this->multilingualSerializer->serializeMultilingualValues( $labels ); if ( !$this->options->shouldUseKeys() ) { $this->setIndexedTagName( $value, 'label' ); @@ -88,4 +90,21 @@ return $value; } + + /** + * Returns a serialized array of labels from raw label data array. + * + * Unlike getSerialized(), $labels is filtered first for requested languages then gets serialized with getSerialized(). + * + * @since 0.4 + * + * @param array $labels + * + * @return array + * @throws InvalidArgumentException + */ + public final function getSerializedMultilingualValues( $labels ) { + $labels = $this->multilingualSerializer->filterPreferredMultilingualValues( $labels ); + return $this->getSerialized( $labels ); + } } diff --git a/lib/includes/serializers/MultilingualSerializer.php b/lib/includes/serializers/MultilingualSerializer.php new file mode 100644 index 0000000..95fbdd9 --- /dev/null +++ b/lib/includes/serializers/MultilingualSerializer.php @@ -0,0 +1,122 @@ +<?php + +namespace Wikibase\Lib\Serializers; + +/** + * Multilingual serializer, for serializer of labels and descriptions. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @since 0.4 + * + * @file + * @ingroup WikibaseLib + * + * @licence GNU GPL v2+ + */ +class MultilingualSerializer { + + /** + * @since 0.4 + * + * @var MultiLangSerializationOptions + */ + protected $options; + + /** + * Constructor. + * + * @since 0.4 + * + * @param MultiLangSerializationOptions $options + */ + public function __construct( MultiLangSerializationOptions $options = null ) { + if ( $options === null ) { + $this->options = new MultiLangSerializationOptions(); + } else { + $this->options = $options; + } + } + + /** + * Handle multilingual arrays. + * + * @since 0.4 + * + * @param array $data + * + * @return array + */ + public function serializeMultilingualValues( array $data ) { + + $values = array(); + $idx = 0; + + foreach ( $data as $languageCode => $valueData ) { + $key = $this->options->shouldUseKeys() ? $languageCode : $idx++; + if ( is_array( $valueData ) ) { + $value = $valueData['value']; + $valueLanguageCode = $valueData['language']; + $valueSourceLanguageCode = $valueData['source']; + } else { + // back-compat + $value = $valueData; + $valueLanguageCode = $languageCode; + $valueSourceLanguageCode = null; + } + $valueKey = ( $value === '' ) ? 'removed' : 'value'; + $values[$key] = array( + 'language' => $valueLanguageCode, + $valueKey => $value, + ); + if ( $valueSourceLanguageCode !== null ) { + $values[$key]['source-language'] = $valueSourceLanguageCode; + } + if ( !$this->options->shouldUseKeys() && $languageCode !== $valueLanguageCode ) { + // To have $languageCode kept somewhere + $values[$key]['for-language'] = $languageCode; + } + } + + return $values; + } + + /** + * Used in (Label|Description)Serializer::getSerializedMultilingualValues(), to filter multilingual labels and descriptions + * + * @param array $allData + * + * @return array + */ + public function filterPreferredMultilingualValues( array $allData ) { + $values = array(); + + $languageFallbackChains = $this->options->getLanguageFallbackChains(); + + if ( $languageFallbackChains ) { + foreach ( $languageFallbackChains as $languageCode => $languageFallbackChain ) { + $data = $languageFallbackChain->extractPreferredValue( $allData ); + if ( $data !== null ) { + $values[$languageCode] = $data; + } + } + } else { + $values = $allData; + } + + return $values; + } +} diff --git a/lib/tests/phpunit/serializers/DescriptionSerializerTest.php b/lib/tests/phpunit/serializers/DescriptionSerializerTest.php index 280d89c..acb39e4 100644 --- a/lib/tests/phpunit/serializers/DescriptionSerializerTest.php +++ b/lib/tests/phpunit/serializers/DescriptionSerializerTest.php @@ -2,11 +2,10 @@ namespace Wikibase\Test; -use Wikibase\EntityFactory; - +use Wikibase\LanguageFallbackChainFactory; use Wikibase\Lib\Serializers\MultiLangSerializationOptions; use Wikibase\Lib\Serializers\DescriptionSerializer; -use InvalidArgumentException; +use Wikibase\Lib\Serializers\MultilingualSerializer; /** * @covers Wikibase\Lib\Serializers\DescriptionSerializer @@ -102,6 +101,103 @@ ); $validArgs[] = array( $descriptions, $options, $expectedSerialization ); + $options = new MultiLangSerializationOptions(); + $options->setUseKeys( true ); + $descriptions = array( + "en" => "Rome", + "de-formal" => array( + "value" => "Rom", + "language" => "de", + "source" => null, + ), + "it" => "", + "zh-tw" => array( + "value" => "羅馬", + "language" => "zh-tw", + "source" => "zh-cn", + ), + "sr-ec" => array( + "value" => "Rome", + "language" => "en", + "source" => "en", + ), + ); + $expectedSerialization = array( + "en" => array( + "language" => "en", + "value" => "Rome" + ), + "de-formal" => array( + "language" => "de", + "value" => "Rom" + ), + "it" => array( + "language" => "it", + "removed" => "" + ), + "zh-tw" => array( + "language" => "zh-tw", + "source-language" => "zh-cn", + "value" => "羅馬" + ), + "sr-ec" => array( + "language" => "en", + "source-language" => "en", + "value" => "Rome" + ), + ); + $validArgs[] = array( $descriptions, $options, $expectedSerialization ); + + $options = new MultiLangSerializationOptions(); + $options->setUseKeys( false ); + $descriptions = array( + "en" => "Rome", + "de-formal" => array( + "value" => "Rom", + "language" => "de", + "source" => null, + ), + "it" => "", + "zh-tw" => array( + "value" => "羅馬", + "language" => "zh-tw", + "source" => "zh-cn", + ), + "sr-ec" => array( + "value" => "Rome", + "language" => "en", + "source" => "en", + ), + ); + $expectedSerialization = array( + array( + "language" => "en", + "value" => "Rome" + ), + array( + "language" => "de", + "for-language" => "de-formal", + "value" => "Rom" + ), + array( + "language" => "it", + "removed" => "" + ), + array( + "language" => "zh-tw", + "source-language" => "zh-cn", + "value" => "羅馬" + ), + array( + "language" => "en", + "source-language" => "en", + "for-language" => "sr-ec", + "value" => "Rome" + ), + "_element" => "description", + ); + $validArgs[] = array( $descriptions, $options, $expectedSerialization ); + return $validArgs; } @@ -133,4 +229,51 @@ $serializedDescriptions = $descriptionSerializer->getSerialized( $descriptions ); } + /** + * @dataProvider provideGetSerializedMultilingualValues + */ + public function testGetSerializedMultilingualValues( $values, $options ) { + $multilingualSerializer = new MultilingualSerializer( $options ); + $descriptionSerializer = new DescriptionSerializer( $options, $multilingualSerializer ); + $filtered = $multilingualSerializer->filterPreferredMultilingualValues( $values ); + $expected = $descriptionSerializer->getSerialized( $filtered ); + + $this->assertEquals( $expected, $descriptionSerializer->getSerializedMultilingualValues( $values ) ); + } + + public function provideGetSerializedMultilingualValues() { + $validArgs = array(); + + $options = new MultiLangSerializationOptions(); + $options->setUseKeys( true ); + $options->setLanguages( array( 'en', 'it', 'de', 'fr' ) ); + $values = array( + "en" => "capital city of Italy", + "de" => "Hauptstadt von Italien", + "it" => "", + "fi" => "kunta Italiassa", + ); + $validArgs[] = array( $values, $options ); + + $options = new MultiLangSerializationOptions(); + $languageFallbackChainFactory = new LanguageFallbackChainFactory(); + $options->setUseKeys( true ); + $options->setLanguages( array( + 'de-formal' => $languageFallbackChainFactory->newFromLanguageCode( 'de-formal' ), + 'zh-cn' => $languageFallbackChainFactory->newFromLanguageCode( 'zh-cn' ), + 'key-fr' => $languageFallbackChainFactory->newFromLanguageCode( 'fr' ), + 'sr-ec' => $languageFallbackChainFactory->newFromLanguageCode( 'zh-cn', LanguageFallbackChainFactory::FALLBACK_SELF ), + 'gan-hant' => $languageFallbackChainFactory->newFromLanguageCode( 'gan-hant' ), + ) ); + $values = array( + "en" => "capital city of Italy", + "de" => "Hauptstadt von Italien", + "fi" => "kunta Italiassa", + "zh-tw" => "羅馬", + "gan-hant" => "羅馬G", + ); + $validArgs[] = array( $values, $options ); + + return $validArgs; + } } diff --git a/lib/tests/phpunit/serializers/LabelSerializerTest.php b/lib/tests/phpunit/serializers/LabelSerializerTest.php index 6f49cf0..676ebbb 100644 --- a/lib/tests/phpunit/serializers/LabelSerializerTest.php +++ b/lib/tests/phpunit/serializers/LabelSerializerTest.php @@ -2,8 +2,10 @@ namespace Wikibase\Test; +use Wikibase\LanguageFallbackChainFactory; use Wikibase\Lib\Serializers\MultiLangSerializationOptions; use Wikibase\Lib\Serializers\LabelSerializer; +use Wikibase\Lib\Serializers\MultilingualSerializer; /** * @covers Wikibase\Lib\Serializers\LabelSerializer @@ -99,6 +101,103 @@ ); $validArgs[] = array( $labels, $options, $expectedSerialization ); + $options = new MultiLangSerializationOptions(); + $options->setUseKeys( true ); + $labels = array( + "en" => "Rome", + "de-formal" => array( + "value" => "Rom", + "language" => "de", + "source" => null, + ), + "it" => "", + "zh-tw" => array( + "value" => "羅馬", + "language" => "zh-tw", + "source" => "zh-cn", + ), + "sr-ec" => array( + "value" => "Rome", + "language" => "en", + "source" => "en", + ), + ); + $expectedSerialization = array( + "en" => array( + "language" => "en", + "value" => "Rome" + ), + "de-formal" => array( + "language" => "de", + "value" => "Rom" + ), + "it" => array( + "language" => "it", + "removed" => "" + ), + "zh-tw" => array( + "language" => "zh-tw", + "source-language" => "zh-cn", + "value" => "羅馬" + ), + "sr-ec" => array( + "language" => "en", + "source-language" => "en", + "value" => "Rome" + ), + ); + $validArgs[] = array( $labels, $options, $expectedSerialization ); + + $options = new MultiLangSerializationOptions(); + $options->setUseKeys( false ); + $descriptions = array( + "en" => "Rome", + "de-formal" => array( + "value" => "Rom", + "language" => "de", + "source" => null, + ), + "it" => "", + "zh-tw" => array( + "value" => "羅馬", + "language" => "zh-tw", + "source" => "zh-cn", + ), + "sr-ec" => array( + "value" => "Rome", + "language" => "en", + "source" => "en", + ), + ); + $expectedSerialization = array( + array( + "language" => "en", + "value" => "Rome" + ), + array( + "language" => "de", + "for-language" => "de-formal", + "value" => "Rom" + ), + array( + "language" => "it", + "removed" => "" + ), + array( + "language" => "zh-tw", + "source-language" => "zh-cn", + "value" => "羅馬" + ), + array( + "language" => "en", + "source-language" => "en", + "for-language" => "sr-ec", + "value" => "Rome" + ), + "_element" => "label", + ); + $validArgs[] = array( $descriptions, $options, $expectedSerialization ); + return $validArgs; } @@ -129,4 +228,52 @@ $labelSerializer = new LabelSerializer(); $serializedLabels = $labelSerializer->getSerialized( $labels ); } + + /** + * @dataProvider provideGetSerializedMultilingualValues + */ + public function testGetSerializedMultilingualValues( $values, $options ) { + $multilingualSerializer = new MultilingualSerializer( $options ); + $labelSerializer = new LabelSerializer( $options, $multilingualSerializer ); + $filtered = $multilingualSerializer->filterPreferredMultilingualValues( $values ); + $expected = $labelSerializer->getSerialized( $filtered ); + + $this->assertEquals( $expected, $labelSerializer->getSerializedMultilingualValues( $values ) ); + } + + public function provideGetSerializedMultilingualValues() { + $validArgs = array(); + + $options = new MultiLangSerializationOptions(); + $options->setUseKeys( true ); + $options->setLanguages( array( 'en', 'it', 'de', 'fr' ) ); + $values = array( + "en" => "capital city of Italy", + "de" => "Hauptstadt von Italien", + "it" => "", + "fi" => "kunta Italiassa", + ); + $validArgs[] = array( $values, $options ); + + $options = new MultiLangSerializationOptions(); + $languageFallbackChainFactory = new LanguageFallbackChainFactory(); + $options->setUseKeys( true ); + $options->setLanguages( array( + 'de-formal' => $languageFallbackChainFactory->newFromLanguageCode( 'de-formal' ), + 'zh-cn' => $languageFallbackChainFactory->newFromLanguageCode( 'zh-cn' ), + 'key-fr' => $languageFallbackChainFactory->newFromLanguageCode( 'fr' ), + 'sr-ec' => $languageFallbackChainFactory->newFromLanguageCode( 'zh-cn', LanguageFallbackChainFactory::FALLBACK_SELF ), + 'gan-hant' => $languageFallbackChainFactory->newFromLanguageCode( 'gan-hant' ), + ) ); + $values = array( + "en" => "capital city of Italy", + "de" => "Hauptstadt von Italien", + "fi" => "kunta Italiassa", + "zh-tw" => "羅馬", + "gan-hant" => "羅馬G", + ); + $validArgs[] = array( $values, $options ); + + return $validArgs; + } } diff --git a/lib/tests/phpunit/serializers/MultilingualSerializerTest.php b/lib/tests/phpunit/serializers/MultilingualSerializerTest.php new file mode 100644 index 0000000..5388417 --- /dev/null +++ b/lib/tests/phpunit/serializers/MultilingualSerializerTest.php @@ -0,0 +1,303 @@ +<?php + +namespace Wikibase\Test; + +use Wikibase\LanguageFallbackChainFactory; +use Wikibase\Lib\Serializers\MultiLangSerializationOptions; +use Wikibase\Lib\Serializers\MultilingualSerializer; + +/** + * @covers Wikibase\Lib\Serializers\MultilingualSerializer + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + * @since 0.4 + * + * @ingroup WikibaseLib + * @ingroup Test + * + * @group WikibaseLib + * @group Wikibase + * @group WikibaseSerialization + * + * @licence GNU GPL v2+ + */ +class MultilingualSerializerTest extends \PHPUnit_Framework_TestCase { + + public function provideSerialize() { + $validArgs = array(); + + $options = new MultiLangSerializationOptions(); + $options->setUseKeys( true ); + $values = array( + "en" => "capital city of Italy", + "de" => "Hauptstadt von Italien", + "it" => "", + "fi" => "kunta Italiassa", + ); + $expectedSerialization = array( + "en" => array( + "language" => "en", + "value" => "capital city of Italy" + ), + "de" => array( + "language" => "de", + "value" => "Hauptstadt von Italien" + ), + "it" => array( + "language" => "it", + "removed" => "" + ), + "fi" => array( + "language" => "fi", + "value" => "kunta Italiassa" + ), + ); + $validArgs[] = array( $values, $options, $expectedSerialization ); + + $options = new MultiLangSerializationOptions(); + $options->setUseKeys( false ); + $values = array( + "en" => "capital city of Italy", + "de" => "Hauptstadt von Italien", + "it" => "capitale della Repubblica Italiana", + "fi" => "kunta Italiassa", + ); + $expectedSerialization = array( + array( + "language" => "en", + "value" => "capital city of Italy" + ), + array( + "language" => "de", + "value" => "Hauptstadt von Italien" + ), + array( + "language" => "it", + "value" => "capitale della Repubblica Italiana" + ), + array( + "language" => "fi", + "value" => "kunta Italiassa" + ), + ); + $validArgs[] = array( $values, $options, $expectedSerialization ); + + $options = new MultiLangSerializationOptions(); + $options->setUseKeys( true ); + $values = array( + "en" => "Rome", + "de-formal" => array( + "value" => "Rom", + "language" => "de", + "source" => null, + ), + "it" => "", + "zh-tw" => array( + "value" => "羅馬", + "language" => "zh-tw", + "source" => "zh-cn", + ), + "sr-ec" => array( + "value" => "Rome", + "language" => "en", + "source" => "en", + ), + ); + $expectedSerialization = array( + "en" => array( + "language" => "en", + "value" => "Rome" + ), + "de-formal" => array( + "language" => "de", + "value" => "Rom" + ), + "it" => array( + "language" => "it", + "removed" => "" + ), + "zh-tw" => array( + "language" => "zh-tw", + "source-language" => "zh-cn", + "value" => "羅馬" + ), + "sr-ec" => array( + "language" => "en", + "source-language" => "en", + "value" => "Rome" + ), + ); + $validArgs[] = array( $values, $options, $expectedSerialization ); + + $options = new MultiLangSerializationOptions(); + $options->setUseKeys( false ); + $values = array( + "en" => "Rome", + "de-formal" => array( + "value" => "Rom", + "language" => "de", + "source" => null, + ), + "it" => "", + "fr" => array( + "value" => "", + "language" => "fr", + "source" => null, + ), + "zh-tw" => array( + "value" => "羅馬", + "language" => "zh-tw", + "source" => "zh-cn", + ), + "sr-ec" => array( + "value" => "Rome", + "language" => "en", + "source" => "en", + ), + ); + $expectedSerialization = array( + array( + "language" => "en", + "value" => "Rome" + ), + array( + "language" => "de", + "for-language" => "de-formal", + "value" => "Rom" + ), + array( + "language" => "it", + "removed" => "" + ), + array( + "language" => "fr", + "removed" => "" + ), + array( + "language" => "zh-tw", + "source-language" => "zh-cn", + "value" => "羅馬" + ), + array( + "language" => "en", + "source-language" => "en", + "for-language" => "sr-ec", + "value" => "Rome" + ), + ); + $validArgs[] = array( $values, $options, $expectedSerialization ); + + return $validArgs; + } + + /** + * @dataProvider provideSerialize + */ + public function testSerialize( $values, $options, $expectedSerialization ) { + $serializer = new MultilingualSerializer( $options ); + $serialized = $serializer->serializeMultilingualValues( $values ); + + $this->assertEquals( $expectedSerialization, $serialized ); + } + + public function provideFilter() { + $validArgs = array(); + + $options = new MultiLangSerializationOptions(); + $options->setUseKeys( true ); + $options->setLanguages( array( 'en', 'it', 'de', 'fr' ) ); + $values = array( + "en" => "capital city of Italy", + "de" => "Hauptstadt von Italien", + "it" => "", + "fi" => "kunta Italiassa", + ); + $expectedOutput = array( + "en" => array( + "value" => "capital city of Italy", + "language" => "en", + "source" => null, + ), + "de" => array( + "value" => "Hauptstadt von Italien", + "language" => "de", + "source" => null, + ), + "it" => array( + "value" => "", + "language" => "it", + "source" => null, + ), + ); + $validArgs[] = array( $values, $options, $expectedOutput ); + + $options = new MultiLangSerializationOptions(); + $languageFallbackChainFactory = new LanguageFallbackChainFactory(); + $options->setUseKeys( true ); + $options->setLanguages( array( + 'de-formal' => $languageFallbackChainFactory->newFromLanguageCode( 'de-formal' ), + 'zh-cn' => $languageFallbackChainFactory->newFromLanguageCode( 'zh-cn' ), + 'key-fr' => $languageFallbackChainFactory->newFromLanguageCode( 'fr' ), + 'sr-ec' => $languageFallbackChainFactory->newFromLanguageCode( 'zh-cn', LanguageFallbackChainFactory::FALLBACK_SELF ), + 'gan-hant' => $languageFallbackChainFactory->newFromLanguageCode( 'gan-hant' ), + ) ); + $values = array( + "en" => "capital city of Italy", + "de" => "Hauptstadt von Italien", + "fi" => "kunta Italiassa", + "zh-tw" => "羅馬", + "gan-hant" => "羅馬G", + ); + $expectedOutput = array( + "de-formal" => array( + "value" => "Hauptstadt von Italien", + "language" => "de", + "source" => null, + ), + "zh-cn" => array( + "value" => "罗马", + "language" => "zh-cn", + "source" => "zh-tw", + ), + "key-fr" => array( + "value" => "capital city of Italy", + "language" => "en", + "source" => null, + ), + "gan-hant" => array( + "value" => "羅馬G", + "language" => "gan-hant", + "source" => null, + ), + ); + $validArgs[] = array( $values, $options, $expectedOutput ); + + return $validArgs; + } + + /** + * @dataProvider provideFilter + */ + public function testFilter( $values, $options, $expectedOutput ) { + $serializer = new MultilingualSerializer( $options ); + $filtered = $serializer->filterPreferredMultilingualValues( $values ); + + $this->assertEquals( $expectedOutput, $filtered ); + } + +} -- To view, visit https://gerrit.wikimedia.org/r/72225 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Iec62d6817e3007e1982db65b5031ec09ecda3374 Gerrit-PatchSet: 28 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Liangent <liang...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Denny Vrandecic <denny.vrande...@wikimedia.de> Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com> Gerrit-Reviewer: Liangent <liang...@gmail.com> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits