Liangent has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/72225


Change subject: Label serialization now accepts LanguageFallbackChain as the 
language option
......................................................................

Label serialization now accepts LanguageFallbackChain as the language option

Labels in all serialized formats will get a new field 'source-language',
to indicate the language where this label was fetched.

Change-Id: Iec62d6817e3007e1982db65b5031ec09ecda3374
---
M lib/includes/serializers/EntitySerializer.php
M lib/includes/serializers/LabelSerializer.php
M lib/tests/phpunit/serializers/EntitySerializerBaseTest.php
M lib/tests/phpunit/serializers/LabelSerializerTest.php
4 files changed, 87 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/25/72225/1

diff --git a/lib/includes/serializers/EntitySerializer.php 
b/lib/includes/serializers/EntitySerializer.php
index f41e5f6..7ad4045 100644
--- a/lib/includes/serializers/EntitySerializer.php
+++ b/lib/includes/serializers/EntitySerializer.php
@@ -89,7 +89,19 @@
                                        break;
                                case 'labels':
                                        $labelSerializer = new LabelSerializer( 
$this->options );
-                                       $labels = $entity->getLabels( 
$this->options->getLanguages() );
+                                       $allLabels = $entity->getLabels();
+                                       $labels = array();
+                                       $languageFallbackChains = 
$this->options->getLanguageFallbackChains();
+                                       if ( $languageFallbackChains ) {
+                                               foreach ( 
$languageFallbackChains as $languageCode => $languageFallbackChain ) {
+                                                       $data = 
$languageFallbackChain->extractPreferredValue( $allLabels );
+                                                       if ( $data !== null ) {
+                                                               
$labels[$languageCode] = $data;
+                                                       }
+                                               }
+                                       } else {
+                                               $labels = $allLabels;
+                                       }
                                        $serialization['labels'] = 
$labelSerializer->getSerialized( $labels );
                                        break;
                                case 'claims':
diff --git a/lib/includes/serializers/LabelSerializer.php 
b/lib/includes/serializers/LabelSerializer.php
index 13094f8..0789c27 100644
--- a/lib/includes/serializers/LabelSerializer.php
+++ b/lib/includes/serializers/LabelSerializer.php
@@ -73,12 +73,23 @@
                $value = array();
                $idx = 0;
 
-               foreach ( $labels as $languageCode => $label ) {
+               foreach ( $labels as $languageCode => $labelData ) {
                        $key = $this->options->shouldUseKeys() ? $languageCode 
: $idx++;
+                       if ( is_array( $labelData ) ) {
+                               $label = $labelData['value'];
+                               $labelLanguageCode = $labelData['language'];
+                               $labelSourceLanguageCode = $labelData['source'];
+                       } else {
+                               // back-compat
+                               $label = $labelData;
+                               $labelLanguageCode = $languageCode;
+                               $labelSourceLanguageCode = $languageCode;
+                       }
                        $valueKey = ( $label === '' ) ? 'removed' : 'value';
                        $value[$key] = array(
-                               'language' => $languageCode,
-                               $valueKey => $label
+                               'language' => $labelLanguageCode,
+                               'source-language' => $labelSourceLanguageCode,
+                               $valueKey => $label,
                        );
                }
 
diff --git a/lib/tests/phpunit/serializers/EntitySerializerBaseTest.php 
b/lib/tests/phpunit/serializers/EntitySerializerBaseTest.php
index 0abafa3..6c7d069 100644
--- a/lib/tests/phpunit/serializers/EntitySerializerBaseTest.php
+++ b/lib/tests/phpunit/serializers/EntitySerializerBaseTest.php
@@ -122,10 +122,12 @@
                                        'en' => array(
                                                'value' => 'foo',
                                                'language' => 'en',
+                                               'source-language' => 'en',
                                        ),
                                        'de' => array(
                                                'value' => 'bar',
                                                'language' => 'de',
+                                               'source-language' => 'de',
                                        ),
                                ),
                                'descriptions' => array(
diff --git a/lib/tests/phpunit/serializers/LabelSerializerTest.php 
b/lib/tests/phpunit/serializers/LabelSerializerTest.php
index 6f49cf0..4b3ed13 100644
--- a/lib/tests/phpunit/serializers/LabelSerializerTest.php
+++ b/lib/tests/phpunit/serializers/LabelSerializerTest.php
@@ -53,18 +53,22 @@
                $expectedSerialization = array(
                        "en" => array(
                                "language" => "en",
+                               "source-language" => "en",
                                "value" => "Rome"
                        ),
                        "de" => array(
                                "language" => "de",
+                               "source-language" => "de",
                                "value" => "Rom"
                        ),
                        "it" => array(
                                "language" => "it",
+                               "source-language" => "it",
                                "removed" => ""
                        ),
                        "fi" => array(
                                "language" => "fi",
+                               "source-language" => "fi",
                                "value" => "Rooma"
                        ),
                );
@@ -81,24 +85,78 @@
                $expectedSerialization = array(
                        array(
                                "language" => "en",
+                               "source-language" => "en",
                                "value" => "Rome"
                        ),
                        array(
                                "language" => "de",
+                               "source-language" => "de",
                                "value" => "Rom"
                        ),
                        array(
                                "language" => "it",
+                               "source-language" => "it",
                                "value" => "Roma"
                        ),
                        array(
                                "language" => "fi",
+                               "source-language" => "fi",
                                "value" => "Rooma"
                        ),
                        "_element" => "label",
                );
                $validArgs[] = array( $labels, $options, $expectedSerialization 
);
 
+               $options = new MultiLangSerializationOptions();
+               $options->setUseKeys( true );
+               $labels = array(
+                       "en" => "Rome",
+                       "de-formal" => array(
+                               "value" => "Rom",
+                               "language" => "de",
+                               "source" => "de",
+                       ),
+                       "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",
+                               "source-language" => "en",
+                               "value" => "Rome"
+                       ),
+                       "de-formal" => array(
+                               "language" => "de",
+                               "source-language" => "de",
+                               "value" => "Rom"
+                       ),
+                       "it" => array(
+                               "language" => "it",
+                               "source-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 
);
+
                return $validArgs;
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iec62d6817e3007e1982db65b5031ec09ecda3374
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Liangent <liang...@gmail.com>

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

Reply via email to