Liangent has uploaded a new change for review.

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


Change subject: New function Utils::getLanguageFallbackChainFromContext()
......................................................................

New function Utils::getLanguageFallbackChainFromContext()

Change-Id: Id5c2131cd072506ffeed1d53d7d0dfdba0c8e782
---
M lib/includes/Utils.php
M lib/tests/phpunit/UtilsTest.php
2 files changed, 83 insertions(+), 1 deletion(-)


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

diff --git a/lib/includes/Utils.php b/lib/includes/Utils.php
index 1eff1f6..6ce8af5 100644
--- a/lib/includes/Utils.php
+++ b/lib/includes/Utils.php
@@ -1,7 +1,7 @@
 <?php
 
 namespace Wikibase;
-use Sanitizer, UtfNormal, Language, SiteList, SiteSQLStore;
+use Sanitizer, UtfNormal, Language, SiteList, SiteSQLStore, IContextSource;
 
 /**
  * Utility functions for Wikibase.
@@ -126,6 +126,80 @@
        }
 
        /**
+        * Returns the fallback chain for a context, currently based on data 
provided by Extension:Babel.
+        *
+        * @param IContextSource $context
+        *
+        * @return array of LanguageWithConversion objects
+        */
+       public static function getLanguageFallbackChainFromContext( 
IContextSource $context ) {
+               static $cache = array();
+               static $levels = null;
+               $user = $context->getUser();
+
+               if ( !class_exists( 'Babel' ) || $user->isAnon() ) {
+                       return self::getLanguageFallbackChain( 
$context->getLanguage() );
+               }
+
+               if ( isset( $cache[$user->getName()] ) ) {
+                       return $cache[$user->getName()];
+               }
+
+               if ( $levels === null ) {
+                       global $wgBabelCategoryNames;
+                       $levels = array_keys( $wgBabelCategoryNames );
+                       rsort( $levels );
+               }
+
+               $fetched = array();
+               $chain = array();
+               $babels = array();
+               $contextLanguage = array( $context->getLanguage()->getCode() );
+
+               if ( count( $levels ) ) {
+                       // A little redundant but it's the only way to get 
required information with current Babel API.
+                       $previousLevelBabel = array();
+                       foreach ( $levels as $level ) {
+                               // Make the current language at the top of the 
chain.
+                               $levelBabel = array_unique( array_merge(
+                                       $contextLanguage, 
\Babel::getUserLanguages( $user, $level )
+                               ) );
+                               $babels[$level] = array_diff( $levelBabel, 
$previousLevelBabel );
+                               $previousLevelBabel = $levelBabel;
+                       }
+               } else {
+                       // Just in case
+                       $babels['N'] = $contextLanguage;
+               }
+
+               // First pass to get "compatible" languages (self and variants)
+               foreach ( $babels as $languageCodes ) { // Already sorted when 
added
+                       foreach ( array( self::LANGUAGE_FALLBACK_SELF, 
self::LANGUAGE_FALLBACK_VARIANTS ) as $mode ) {
+                               foreach ( $languageCodes as $languageCode ) {
+                                       $chain = array_merge( $chain, 
self::getLanguageFallbackChainInternal(
+                                               Language::factory( 
$languageCode ), $mode, $fetched
+                                       ) );
+                               }
+                       }
+               }
+
+               // Second pass to get other languages from system fallback chain
+               foreach ( $babels as $languageCodes ) {
+                       foreach ( $languageCodes as $languageCode ) {
+                               $chain = array_merge( $chain, 
self::getLanguageFallbackChainInternal(
+                                       Language::factory( $languageCode ),
+                                       self::LANGUAGE_FALLBACK_OTHERS | 
self::LANGUAGE_FALLBACK_VARIANTS,
+                                       $fetched
+                               ) );
+                       }
+               }
+
+               $cache[$user->getName()] = $chain;
+
+               return $chain;
+       }
+
+       /**
         * Returns a list of language codes that Wikibase supports,
         * ie the languages that a label or description can be in.
         *
diff --git a/lib/tests/phpunit/UtilsTest.php b/lib/tests/phpunit/UtilsTest.php
index 5f89cd3..b6a8fb0 100644
--- a/lib/tests/phpunit/UtilsTest.php
+++ b/lib/tests/phpunit/UtilsTest.php
@@ -150,6 +150,14 @@
 
        /**
         * @group WikibaseUtils
+        */
+       public function testGetLanguageFallbackChainFromContext() {
+               $chain = Utils::getLanguageFallbackChainFromContext( 
\RequestContext::getMain() );
+               $this->assertTrue( is_array( $chain ) );
+       }
+
+       /**
+        * @group WikibaseUtils
         * @dataProvider providerGetLanguageCodes
         */
        public function testGetLanguageCodes( $lang ) {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id5c2131cd072506ffeed1d53d7d0dfdba0c8e782
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Liangent <liang...@gmail.com>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Denny Vrandecic <denny.vrande...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to