jenkins-bot has submitted this change and it was merged.

Change subject: api.php?action=wbgetentities gets a new parameter 
'languagefallback'
......................................................................


api.php?action=wbgetentities gets a new parameter 'languagefallback'

Change-Id: Icb9b4fc0f8ebfe74b7895ed5f775fc6940ac19e9
---
M repo/includes/api/GetEntities.php
M repo/tests/phpunit/includes/api/EntityTestHelper.php
M repo/tests/phpunit/includes/api/GetEntitiesTest.php
3 files changed, 119 insertions(+), 3 deletions(-)

Approvals:
  Daniel Kinzler: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/includes/api/GetEntities.php 
b/repo/includes/api/GetEntities.php
index 552f80e..04ef779 100644
--- a/repo/includes/api/GetEntities.php
+++ b/repo/includes/api/GetEntities.php
@@ -12,6 +12,7 @@
 use Wikibase\StoreFactory;
 use Wikibase\Item;
 use Wikibase\EntityContentFactory;
+use Wikibase\LanguageFallbackChainFactory;
 
 /**
  * API module to get the data for one or more Wikibase entities.
@@ -40,11 +41,17 @@
         */
        protected $entityIdParser;
 
+       /**
+        * @var \Wikibase\LanguageFallbackChainFactory
+        */
+       protected $languageFallbackChainFactory;
+
        public function __construct( \ApiMain $main, $name, $prefix = '' ) {
                parent::__construct( $main, $name, $prefix );
 
                $this->stringNormalizer = 
WikibaseRepo::getDefaultInstance()->getStringNormalizer();
                $this->entityIdParser = 
WikibaseRepo::getDefaultInstance()->getEntityIdParser();
+               $this->languageFallbackChainFactory = 
WikibaseRepo::getDefaultInstance()->getLanguageFallbackChainFactory();
        }
 
        /**
@@ -221,7 +228,17 @@
 
                                // TODO: inject id formatter
                                $options = new EntitySerializationOptions( 
WikibaseRepo::getDefaultInstance()->getIdFormatter() );
-                               $options->setLanguages( $params['languages'] );
+                               if ( $params['languagefallback'] ) {
+                                       $languages = array();
+                                       foreach ( $params['languages'] as 
$languageCode ) {
+                                               // $languageCode is already 
filtered as valid ones
+                                               $languages[$languageCode] = 
$this->languageFallbackChainFactory
+                                                       
->newFromContextAndLanguageCode( $this->getContext(), $languageCode );
+                                       }
+                               } else {
+                                       $languages = $params['languages'];
+                               }
+                               $options->setLanguages( $languages );
                                $options->setSortDirection( $params['dir'] );
                                $options->setProps( $props );
                                $options->setIndexTags( 
$this->getResult()->getIsRawMode() );
@@ -290,6 +307,10 @@
                                ApiBase::PARAM_TYPE => 
Utils::getLanguageCodes(),
                                ApiBase::PARAM_ISMULTI => true,
                        ),
+                       'languagefallback' => array(
+                               ApiBase::PARAM_TYPE => 'boolean',
+                               ApiBase::PARAM_DFLT => false
+                       ),
                        'normalize' => array(
                                ApiBase::PARAM_TYPE => 'boolean',
                                ApiBase::PARAM_DFLT => false
@@ -322,6 +343,9 @@
                        ),
                        'languages' => array( 'By default the internationalized 
values are returned in all available languages.',
                                'This parameter allows filtering these down to 
one or more languages by providing one or more language codes.'
+                       ),
+                       'languagefallback' => array( 'Apply language fallback 
for languages defined in the "languages" parameter,',
+                               'with the current context of API call.'
                        ),
                        'normalize' => array( 'Try to normalize the page title 
against the client site.',
                                'This only works if exactly one site and one 
page have been given.'
@@ -365,6 +389,8 @@
                        => "Get entities with IDs Q42 and p2 with all available 
attributes in all available languages",
                        "api.php?action=wbgetentities&ids=Q42&languages=en"
                        => "Get entity with ID Q42 with all available 
attributes in English language",
+                       
"api.php?action=wbgetentities&ids=Q42&languages=ii&languagefallback="
+                       => "Get entity with ID q42 with all available 
attributes in any possible fallback language for the ii language",
                        "api.php?action=wbgetentities&ids=Q42&props=labels"
                        => "Get entity with ID Q42 showing all labels in all 
available languages",
                        "api.php?action=wbgetentities&ids=P2|P3&props=datatype"
diff --git a/repo/tests/phpunit/includes/api/EntityTestHelper.php 
b/repo/tests/phpunit/includes/api/EntityTestHelper.php
index f917ff4..90d9d25 100644
--- a/repo/tests/phpunit/includes/api/EntityTestHelper.php
+++ b/repo/tests/phpunit/includes/api/EntityTestHelper.php
@@ -188,6 +188,20 @@
                                ),
                        )
                ),
+               'Guangzhou' => array(
+                       "new" => "item",
+                       "data" => array(
+                               "labels" => array(
+                                       array( "language" => "de", "value" => 
"Guangzhou" ),
+                                       array( "language" => "yue", "value" => 
"廣州" ),
+                                       array( "language" => "zh-cn", "value" 
=> "广州市" ),
+                               ),
+                               "descriptions" => array(
+                                       array( "language" => "en", "value" => 
"Capital of Guangdong." ),
+                                       array( "language" => "zh-hk", "value" 
=> "廣東的省會。" ),
+                               ),
+                       )
+               ),
 
        );
 
@@ -298,4 +312,4 @@
                return self::$activeHandles[ $handle ];
        }
 
-}
\ No newline at end of file
+}
diff --git a/repo/tests/phpunit/includes/api/GetEntitiesTest.php 
b/repo/tests/phpunit/includes/api/GetEntitiesTest.php
index de189fb..74fecdb 100644
--- a/repo/tests/phpunit/includes/api/GetEntitiesTest.php
+++ b/repo/tests/phpunit/includes/api/GetEntitiesTest.php
@@ -50,7 +50,7 @@
 class GetEntitiesTest extends WikibaseApiTestCase {
 
        private static $hasSetup;
-       private static $usedHandles = array( 'Berlin', 'London', 'Oslo', 
'Leipzig', 'Empty' );
+       private static $usedHandles = array( 'Berlin', 'London', 'Oslo', 
'Leipzig', 'Guangzhou', 'Empty' );
 
        public function setup() {
                parent::setup();
@@ -498,6 +498,82 @@
                }
        }
 
+       function provideLanguageFallback() {
+               return array(
+                       array(
+                               'Guangzhou',
+                               array( 'de-formal', 'en', 'fr', 'yue', 'zh-cn', 
'zh-hk' ),
+                               array(
+                                       'de-formal' => array(
+                                               'language' => 'de',
+                                               'value' => 'Guangzhou',
+                                       ),
+                                       'yue' => array(
+                                               'language' => 'yue',
+                                               'value' => '廣州',
+                                       ),
+                                       'zh-cn' => array(
+                                               'language' => 'zh-cn',
+                                               'value' => '广州市',
+                                       ),
+                                       'zh-hk' => array(
+                                               'language' => 'zh-hk',
+                                               'source-language' => 'zh-cn',
+                                               'value' => '廣州市',
+                                       ),
+                               ),
+                               array(
+                                       'de-formal' => array(
+                                               'language' => 'en',
+                                               'value' => 'Capital of 
Guangdong.',
+                                       ),
+                                       'en' => array(
+                                               'language' => 'en',
+                                               'value' => 'Capital of 
Guangdong.',
+                                       ),
+                                       'fr' => array(
+                                               'language' => 'en',
+                                               'value' => 'Capital of 
Guangdong.',
+                                       ),
+                                       'yue' => array(
+                                               'language' => 'en',
+                                               'value' => 'Capital of 
Guangdong.',
+                                       ),
+                                       'zh-cn' => array(
+                                               'language' => 'zh-cn',
+                                               'source-language' => 'zh-hk',
+                                               'value' => '广东的省会。',
+                                       ),
+                                       'zh-hk' => array(
+                                               'language' => 'zh-hk',
+                                               'value' => '廣東的省會。',
+                                       ),
+
+                               ),
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider provideLanguageFallback
+        */
+       function testLanguageFallback( $handle, $languages, $expectedLabels, 
$expectedDescriptions ) {
+               $id = EntityTestHelper::getId( $handle );
+
+               list($res,,) = $this->doApiRequest(
+                       array(
+                               'action' => 'wbgetentities',
+                               'languages' => join( '|', $languages ),
+                               'languagefallback' => '',
+                               'format' => 'json', // make sure IDs are used 
as keys
+                               'ids' => $id,
+                       )
+               );
+
+               $this->assertEquals( $expectedLabels, 
$res['entities'][$id]['labels'] );
+               $this->assertEquals( $expectedDescriptions, 
$res['entities'][$id]['descriptions'] );
+       }
+
        function provideProps() {
                return array(
                        array( 'Berlin', '', array( 'id', 'type' ) ),

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Icb9b4fc0f8ebfe74b7895ed5f775fc6940ac19e9
Gerrit-PatchSet: 16
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Liangent <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Denny Vrandecic <[email protected]>
Gerrit-Reviewer: Liangent <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to