language: Use item 'fallbackSequence' instead of duplicating logic
authorTimo Tijhof <krinklemail@gmail.com>
Mon, 1 Jun 2015 23:04:26 +0000 (00:04 +0100)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 4 Jun 2015 17:45:12 +0000 (17:45 +0000)
The 'fallbackSequence' is exactly generated for this purpose. It
is equal to the value of 'fallback' after splitting, trimming
and ensuring 'en' is present. See LocalisationCache::recache().

Also simplify returning of the first array index by returning it
directly instead of modifying the array first.

Due to an inconsistency between how LocalisationCache and Language classes
treat the fallback sequence differently, we have to manually fallback
to 'en' in case of unknown language codes. This is because otherwise
Language::factory() will throw an exception causing tests to fail.
We should investigate whether this is desirable or not, but keeping
existing behaviour for now and documenting it.

Change-Id: I9c1d51b59aabebf5a31f38205304bb8cc22dcd8c

languages/Language.php

index d19dc25..0634d9f 100644 (file)
@@ -4396,8 +4396,7 @@ class Language {
                        return false;
                } else {
                        $fallbacks = self::getFallbacksFor( $code );
-                       $first = array_shift( $fallbacks );
-                       return $first;
+                       return $fallbacks[0];
                }
        }
 
@@ -4406,19 +4405,15 @@ class Language {
         *
         * @since 1.19
         * @param string $code Language code
-        * @return array
+        * @return array Non-empty array, ending in "en"
         */
        public static function getFallbacksFor( $code ) {
                if ( $code === 'en' || !Language::isValidBuiltInCode( $code ) ) {
                        return array();
-               } else {
-                       $v = self::getLocalisationCache()->getItem( $code, 'fallback' );
-                       $v = array_map( 'trim', explode( ',', $v ) );
-                       if ( $v[count( $v ) - 1] !== 'en' ) {
-                               $v[] = 'en';
-                       }
-                       return $v;
                }
+               // For unknown languages, fallbackSequence returns an empty array,
+               // hardcode fallback to 'en' in that case.
+               return self::getLocalisationCache()->getItem( $code, 'fallbackSequence' ) ?: array( 'en' );
        }
 
        /**