New function Language::getParentLanguage().
authorLiangent <liangent@gmail.com>
Fri, 7 Jun 2013 08:50:10 +0000 (08:50 +0000)
committerLiangent <liangent@gmail.com>
Mon, 10 Jun 2013 18:07:49 +0000 (18:07 +0000)
Change-Id: Ib2109176b7dfc7ec2d0ee827c804cf93ea83b9e5

languages/Language.php
tests/phpunit/languages/LanguageTest.php

index 92ea75c..ead72c6 100644 (file)
@@ -81,7 +81,7 @@ class Language {
 
        public $mVariants, $mCode, $mLoaded = false;
        public $mMagicExtensions = array(), $mMagicHookDone = false;
-       private $mHtmlCode = null;
+       private $mHtmlCode = null, $mParentLanguage = false;
 
        public $dateFormatStrings = array();
        public $mExtendedSpecialPageAliases;
@@ -3933,6 +3933,34 @@ class Language {
                return $this;
        }
 
+       /**
+        * Get the "parent" language which has a converter to convert a "compatible" language
+        * (in another variant) to this language (eg. zh for zh-cn, but not en for en-gb).
+        *
+        * @return Language|null
+        * @since 1.22
+        */
+       public function getParentLanguage() {
+               if ( $this->mParentLanguage !== false ) {
+                       return $this->mParentLanguage;
+               }
+
+               $pieces = explode( '-', $this->getCode() );
+               $code = $pieces[0];
+               if ( !in_array( $code, LanguageConverter::$languagesWithVariants ) ) {
+                       $this->mParentLanguage = null;
+                       return null;
+               }
+               $lang = Language::factory( $code );
+               if ( !$lang->hasVariant( $this->getCode() ) ) {
+                       $this->mParentLanguage = null;
+                       return null;
+               }
+
+               $this->mParentLanguage = $lang;
+               return $lang;
+       }
+
        /**
         * Get the RFC 3066 code for this language object
         *
@@ -3967,8 +3995,9 @@ class Language {
         */
        public function setCode( $code ) {
                $this->mCode = $code;
-               // Ensure we don't leave an incorrect html code lying around
+               // Ensure we don't leave incorrect cached data lying around
                $this->mHtmlCode = null;
+               $this->mParentLanguage = false;
        }
 
        /**
index d687dbb..4d67f45 100644 (file)
@@ -1481,4 +1481,26 @@ class LanguageTest extends LanguageClassesTestCase {
                        array( 'FI', false, 'is not supported language, input should be in lower case' ),
                );
        }
+
+       /**
+        * @dataProvider provideGetParentLanguage
+        */
+       function testGetParentLanguage( $code, $expected, $comment ) {
+               $lang = Language::factory( $code );
+               if ( is_null( $expected ) ) {
+                       $this->assertNull( $lang->getParentLanguage(), $comment );
+               } else {
+                       $this->assertEquals( $expected, $lang->getParentLanguage()->getCode(), $comment );
+               }
+       }
+
+       public static function provideGetParentLanguage() {
+               return array(
+                       array( 'zh-cn', 'zh', 'zh is the parent language of zh-cn' ),
+                       array( 'zh', 'zh', 'zh is defined as the parent language of zh, because zh converter can convert zh-cn to zh' ),
+                       array( 'zh-invalid', null, 'do not be fooled by arbitrarily composed language codes' ),
+                       array( 'en-gb', null, 'en does not have converter' ),
+                       array( 'en', null, 'en does not have converter. Although FakeConverter handles en -> en conversion but it is useless' ),
+               );
+       }
 }