Liangent has submitted this change and it was merged.

Change subject: Reduce NPath complexity and improve readability
......................................................................


Reduce NPath complexity and improve readability

Change-Id: I7961b2c507da3661ce4c0a8bbaf994e80001b672
---
M lib/includes/LanguageFallbackChainFactory.php
1 file changed, 42 insertions(+), 23 deletions(-)

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



diff --git a/lib/includes/LanguageFallbackChainFactory.php 
b/lib/includes/LanguageFallbackChainFactory.php
index 5ce24a8..b2106f6 100644
--- a/lib/includes/LanguageFallbackChainFactory.php
+++ b/lib/includes/LanguageFallbackChainFactory.php
@@ -216,7 +216,6 @@
         * @return LanguageFallbackChain
         */
        public function newFromUserAndLanguageCode( User $user, $languageCode ) 
{
-               global $wgBabelCategoryNames;
                wfProfileIn( __METHOD__ );
 
                if ( !class_exists( 'Babel' ) || $user->isAnon() ) {
@@ -233,28 +232,7 @@
                        return $cached;
                }
 
-               $babel = array();
-               $contextLanguage = array( $languageCode );
-
-               if ( count( $wgBabelCategoryNames ) ) {
-                       // A little redundant but it's the only way to get 
required information with current Babel API.
-                       $previousLevelBabel = array();
-                       $babelCategoryNames = array_filter( 
$wgBabelCategoryNames, function( $category ) {
-                               return $category !== false;
-                       } );
-                       krsort( $babelCategoryNames );
-                       foreach ( $babelCategoryNames as $level => $_ ) {
-                               // Make the current language at the top of the 
chain.
-                               $levelBabel = array_unique( array_merge(
-                                       $contextLanguage, 
\Babel::getUserLanguages( $user, $level )
-                               ) );
-                               $babel[$level] = array_diff( $levelBabel, 
$previousLevelBabel );
-                               $previousLevelBabel = $levelBabel;
-                       }
-               } else {
-                       // Just in case
-                       $babel['N'] = $contextLanguage;
-               }
+               $babel = $this->getBabel( $languageCode, $user );
 
                $chain = $this->buildFromBabel( $babel );
                $languageFallbackChain = new LanguageFallbackChain( $chain );
@@ -265,6 +243,47 @@
                return $languageFallbackChain;
        }
 
+       protected function getBabel( $languageCode, $user ) {
+               $babel = array();
+
+               $babelCategoryNames = $this->getBabelCategoryNames();
+
+               if ( count( $babelCategoryNames ) ) {
+                       // A little redundant but it's the only way to get 
required information with current Babel API.
+                       $previousLevelBabel = array();
+
+                       foreach ( $babelCategoryNames as $level => $_ ) {
+                               // Make the current language at the top of the 
chain.
+                               $levelBabel = array_unique( array_merge(
+                                       array( $languageCode ),
+                                       \Babel::getUserLanguages( $user, $level 
)
+                               ) );
+
+                               $babel[$level] = array_diff( $levelBabel, 
$previousLevelBabel );
+                               $previousLevelBabel = $levelBabel;
+                       }
+               } else {
+                       $babel['N'] = array( $languageCode );
+               }
+
+               return $babel;
+       }
+
+       protected function getBabelCategoryNames() {
+               global $wgBabelCategoryNames;
+
+               $babelCategoryNames = array_filter(
+                       $wgBabelCategoryNames,
+                       function( $category ) {
+                               return $category !== false;
+                       }
+               );
+
+               krsort( $babelCategoryNames );
+
+               return $babelCategoryNames;
+       }
+
        /**
         * Build fallback chain array for a given babel array.
         *

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I7961b2c507da3661ce4c0a8bbaf994e80001b672
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Denny Vrandecic <denny.vrande...@wikimedia.de>
Gerrit-Reviewer: Liangent <liang...@gmail.com>
Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@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