Relax Language::isValidCode() to avoid breaking uselang hacks
[lhc/web/wiklou.git] / languages / Language.php
index 965be76..3b87db7 100644 (file)
@@ -44,13 +44,15 @@ class FakeConverter {
        function convertTitle( $t ) { return $t->getPrefixedText(); }
        function getVariants() { return array( $this->mLang->getCode() ); }
        function getPreferredVariant() { return $this->mLang->getCode(); }
+       function getDefaultVariant() { return $this->mLang->getCode(); }
+       function getURLVariant() { return ''; }
        function getConvRuleTitle() { return false; }
        function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
        function getExtraHashOptions() { return ''; }
        function getParsedTitle() { return ''; }
        function markNoConversion( $text, $noParse = false ) { return $text; }
        function convertCategoryKey( $key ) { return $key; }
-       function convertLinkToAllVariants( $text ) { return autoConvertToAllVariants( $text ); }
+       function convertLinkToAllVariants( $text ) { return $this->autoConvertToAllVariants( $text ); }
        function armourMath( $text ) { return $text; }
 }
 
@@ -130,6 +132,8 @@ class Language {
 
        /**
         * Get a cached language object for a given language code
+        * @param $code String
+        * @return Language
         */
        static function factory( $code ) {
                if ( !isset( self::$mLangObjCache[$code] ) ) {
@@ -144,10 +148,20 @@ class Language {
 
        /**
         * Create a language object for a given language code
+        * @param $code String
+        * @return Language
         */
        protected static function newFromCode( $code ) {
                global $IP;
                static $recursionLevel = 0;
+
+               // Protect against path traversal below
+               if ( !Language::isValidCode( $code ) 
+                       || strcspn( $code, "/\\\000" ) !== strlen( $code ) ) 
+               {
+                       throw new MWException( "Invalid language code \"$code\"" );
+               }
+
                if ( $code == 'en' ) {
                        $class = 'Language';
                } else {
@@ -177,6 +191,14 @@ class Language {
                return $lang;
        }
 
+       /**
+        * Returns true if a language code string is of a valid form, whether or 
+        * not it exists.
+        */
+       public static function isValidCode( $code ) {
+               return strcspn( $code, "/\\\000" ) === strlen( $code );
+       }
+
        /**
         * Get the LocalisationCache instance
         */
@@ -260,7 +282,7 @@ class Language {
                                $this->namespaceNames[NS_PROJECT_TALK] =
                                        $this->fixVariableInNamespace( $talk );
                        }
-                       
+
                        # Sometimes a language will be localised but not actually exist on this wiki.
                        foreach( $this->namespaceNames as $key => $text ) {
                                if ( !isset( $validNamespaces[$key] ) ) {
@@ -485,6 +507,25 @@ class Language {
                return $names;
        }
 
+       /**
+        * Get translated language names. This is done on best effort and
+        * by default this is exactly the same as Language::getLanguageNames.
+        * The CLDR extension provides translated names.
+        * @param $code String Language code.
+        * @return Array language code => language name
+        * @since 1.18.0
+        */
+       public static function getTranslatedLanguageNames( $code ) {
+               $names = array();
+               wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $code ) );
+
+               foreach ( self::getLanguageNames() as $code => $name ) {
+                       if ( !isset( $names[$code] ) ) $names[$code] = $name;
+               }
+
+               return $names;
+       }
+
        /**
         * Get a message from the MediaWiki namespace.
         *
@@ -975,7 +1016,6 @@ class Language {
                                } else {
                                        $s .= $this->formatNum( $num, true );
                                }
-                               $num = false;
                        }
                }
                return $s;
@@ -1366,7 +1406,7 @@ class Language {
                return $s;
        }
 
-       /**
+       /**
         * Hebrew Gematria number formatting up to 9999
         */
        static function hebrewNumeral( $num ) {
@@ -1544,11 +1584,21 @@ class Language {
        }
 
        function getMessage( $key ) {
-               return self::$dataCache->getSubitem( $this->mCode, 'messages', $key );
+               // Don't change getPreferredVariant() to getCode() / mCode, because:
+
+               // 1. Some language like Chinese has multiple variant languages. Only
+               //    getPreferredVariant() (in LanguageConverter) could return a
+               //    sub-language which would be more suitable for the user.
+               // 2. To languages without multiple variants, getPreferredVariant()
+               //    (in FakeConverter) functions exactly same as getCode() / mCode,
+               //    it won't break anything.
+
+               // The same below.
+               return self::$dataCache->getSubitem( $this->getPreferredVariant(), 'messages', $key );
        }
 
        function getAllMessages() {
-               return self::$dataCache->getItem( $this->mCode, 'messages' );
+               return self::$dataCache->getItem( $this->getPreferredVariant(), 'messages' );
        }
 
        function iconv( $in, $out, $string ) {
@@ -1764,7 +1814,7 @@ class Language {
                }
 
                $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
-                '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
+                               '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
                if ( $isutf8 ) {
                        return $s;
                }
@@ -2331,8 +2381,8 @@ class Language {
                        # We got the first byte only of a multibyte char; remove it.
                        $string = substr( $string, 0, -1 );
                } elseif ( $char >= 0x80 &&
-                     preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
-                                 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
+                         preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
+                                                 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
                {
                        # We chopped in the middle of a character; remove it
                        $string = $m[1];
@@ -2664,7 +2714,7 @@ class Language {
        }
 
        /**
-        * Get the list of variants supported by this langauge
+        * Get the list of variants supported by this language
         * see sample implementation in LanguageZh.php
         *
         * @return array an array of language codes
@@ -2673,8 +2723,16 @@ class Language {
                return $this->mConverter->getVariants();
        }
 
-       function getPreferredVariant( $fromUser = true, $fromHeader = false ) {
-               return $this->mConverter->getPreferredVariant( $fromUser, $fromHeader );
+       function getPreferredVariant() {
+               return $this->mConverter->getPreferredVariant();
+       }
+       
+       function getDefaultVariant() {
+               return $this->mConverter->getDefaultVariant();
+       }
+       
+       function getURLVariant() {
+               return $this->mConverter->getURLVariant();
        }
 
        /**
@@ -2697,6 +2755,8 @@ class Language {
         * If a language supports multiple variants, converts text
         * into an array of all possible variants of the text:
         *  'variant' => text in that variant
+        *
+        * @deprecated Use autoConvertToAllVariants()
         */
        function convertLinkToAllVariants( $text ) {
                return $this->mConverter->convertLinkToAllVariants( $text );
@@ -2768,6 +2828,13 @@ class Language {
         * @return string $prefix . $mangledCode . $suffix
         */
        static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
+               // Protect against path traversal
+               if ( !Language::isValidCode( $code ) 
+                       || strcspn( $code, "/\\\000" ) !== strlen( $code ) ) 
+               {
+                       throw new MWException( "Invalid language code \"$code\"" );
+               }
+               
                return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
        }
 
@@ -2858,17 +2925,18 @@ class Language {
                        throw new MWException(
                                "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
                }
-               extract( $arr );
+               $wikiUpperChars = $arr['wikiUpperChars'];
+               $wikiLowerChars = $arr['wikiLowerChars'];
                wfProfileOut( __METHOD__ );
                return array( $wikiUpperChars, $wikiLowerChars );
        }
 
        function formatTimePeriod( $seconds ) {
-               if ( $seconds < 10 ) {
-                       return $this->formatNum( sprintf( "%.1f", $seconds ) ) . $this->getMessageFromDB( 'seconds-abbrev' );
-               } elseif ( $seconds < 60 ) {
+               if ( round( $seconds * 10 ) < 100 ) {
+                       return $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) ) . $this->getMessageFromDB( 'seconds-abbrev' );
+               } elseif ( round( $seconds ) < 60 ) {
                        return $this->formatNum( round( $seconds ) ) . $this->getMessageFromDB( 'seconds-abbrev' );
-               } elseif ( $seconds < 3600 ) {
+               } elseif ( round( $seconds ) < 3600 ) {
                        $minutes = floor( $seconds / 60 );
                        $secondsPart = round( fmod( $seconds, 60 ) );
                        if ( $secondsPart == 60 ) {
@@ -2949,50 +3017,4 @@ class Language {
        function getConvRuleTitle() {
                return $this->mConverter->getConvRuleTitle();
        }
-
-       /**
-        * Given a string, convert it to a (hopefully short) key that can be used
-        * for efficient sorting.  A binary sort according to the sortkeys
-        * corresponds to a logical sort of the corresponding strings.  Current
-        * code expects that a null character should sort before all others, but
-        * has no other particular expectations (and that one can be changed if
-        * necessary).
-        *
-        * @param string $string UTF-8 string
-        * @return string Binary sortkey
-        */
-       public function convertToSortkey( $string ) {
-               # Fake function for now
-               return strtoupper( $string );
-       }
-
-       /**
-        * Given a string, return the logical "first letter" to be used for
-        * grouping on category pages and so on.  This has to be coordinated
-        * carefully with convertToSortkey(), or else the sorted list might jump
-        * back and forth between the same "initial letters" or other pathological
-        * behavior.  For instance, if you just return the first character, but "a"
-        * sorts the same as "A" based on convertToSortkey(), then you might get a
-        * list like
-        *
-        * == A ==
-        * * [[Aardvark]]
-        *
-        * == a ==
-        * * [[antelope]]
-        *
-        * == A ==
-        * * [[Ape]]
-        *
-        * etc., assuming for the sake of argument that $wgCapitalLinks is false.
-        *
-        * @param string $string UTF-8 string
-        * @return string UTF-8 string corresponding to the first letter of input
-        */
-       public function firstLetterForLists( $string ) {
-               if ( $string[0] == "\0" ) {
-                       $string = substr( $string, 1 );
-               }
-               return strtoupper( $this->firstChar( $string ) );
-       }
 }