Merge "Fix wrong @return type hints in Language::tsTo… methods"
[lhc/web/wiklou.git] / languages / Language.php
index 56ecee9..190f2bf 100644 (file)
@@ -30,9 +30,7 @@ if ( !defined( 'MEDIAWIKI' ) ) {
        exit( 1 );
 }
 
-if ( function_exists( 'mb_strtoupper' ) ) {
-       mb_internal_encoding( 'UTF-8' );
-}
+mb_internal_encoding( 'UTF-8' );
 
 use CLDRPluralRuleParser\Evaluator;
 
@@ -443,7 +441,7 @@ class Language {
         * @return array
         * @since 1.19
         */
-       function getFallbackLanguages() {
+       public function getFallbackLanguages() {
                return self::getFallbacksFor( $this->mCode );
        }
 
@@ -451,7 +449,7 @@ class Language {
         * Exports $wgBookstoreListEn
         * @return array
         */
-       function getBookstoreList() {
+       public function getBookstoreList() {
                return self::$dataCache->getItem( $this->mCode, 'bookstoreList' );
        }
 
@@ -520,7 +518,7 @@ class Language {
         *
         * @return array
         */
-       function getFormattedNamespaces() {
+       public function getFormattedNamespaces() {
                $ns = $this->getNamespaces();
                foreach ( $ns as $k => $v ) {
                        $ns[$k] = strtr( $v, '_', ' ' );
@@ -539,7 +537,7 @@ class Language {
         * @param int $index The array key of the namespace to return
         * @return string|bool String if the namespace value exists, otherwise false
         */
-       function getNsText( $index ) {
+       public function getNsText( $index ) {
                $ns = $this->getNamespaces();
                return isset( $ns[$index] ) ? $ns[$index] : false;
        }
@@ -557,7 +555,7 @@ class Language {
         * @param int $index The array key of the namespace to return
         * @return string Namespace name without underscores (empty string if namespace does not exist)
         */
-       function getFormattedNsText( $index ) {
+       public function getFormattedNsText( $index ) {
                $ns = $this->getNsText( $index );
                return strtr( $ns, '_', ' ' );
        }
@@ -570,7 +568,7 @@ class Language {
         * @return string
         * @since 1.18
         */
-       function getGenderNsText( $index, $gender ) {
+       public function getGenderNsText( $index, $gender ) {
                global $wgExtraGenderNamespaces;
 
                $ns = $wgExtraGenderNamespaces +
@@ -585,7 +583,7 @@ class Language {
         * @return bool
         * @since 1.18
         */
-       function needsGenderDistinction() {
+       public function needsGenderDistinction() {
                global $wgExtraGenderNamespaces, $wgExtraNamespaces;
                if ( count( $wgExtraGenderNamespaces ) > 0 ) {
                        // $wgExtraGenderNamespaces overrides everything
@@ -618,7 +616,7 @@ class Language {
        /**
         * @return array
         */
-       function getNamespaceAliases() {
+       public function getNamespaceAliases() {
                if ( is_null( $this->namespaceAliases ) ) {
                        $aliases = self::$dataCache->getItem( $this->mCode, 'namespaceAliases' );
                        if ( !$aliases ) {
@@ -662,7 +660,7 @@ class Language {
        /**
         * @return array
         */
-       function getNamespaceIds() {
+       public function getNamespaceIds() {
                if ( is_null( $this->mNamespaceIds ) ) {
                        global $wgNamespaceAliases;
                        # Put namespace names and aliases into a hashtable.
@@ -692,7 +690,7 @@ class Language {
         * @param string $text
         * @return int|bool An integer if $text is a valid value otherwise false
         */
-       function getNsIndex( $text ) {
+       public function getNsIndex( $text ) {
                $lctext = $this->lc( $text );
                $ns = MWNamespace::getCanonicalIndex( $lctext );
                if ( $ns !== null ) {
@@ -709,7 +707,7 @@ class Language {
         * @param bool $usemsg Use the "variantname-xyz" message if it exists
         * @return string
         */
-       function getVariantname( $code, $usemsg = true ) {
+       public function getVariantname( $code, $usemsg = true ) {
                $msg = "variantname-$code";
                if ( $usemsg && wfMessage( $msg )->exists() ) {
                        return $this->getMessageFromDB( $msg );
@@ -726,7 +724,7 @@ class Language {
        /**
         * @return array
         */
-       function getDatePreferences() {
+       public function getDatePreferences() {
                return self::$dataCache->getItem( $this->mCode, 'datePreferences' );
        }
 
@@ -740,7 +738,7 @@ class Language {
        /**
         * @return array|string
         */
-       function getDefaultDateFormat() {
+       public function getDefaultDateFormat() {
                $df = self::$dataCache->getItem( $this->mCode, 'defaultDateFormat' );
                if ( $df === 'dmy or mdy' ) {
                        global $wgAmericanDates;
@@ -753,7 +751,7 @@ class Language {
        /**
         * @return array
         */
-       function getDatePreferenceMigrationMap() {
+       public function getDatePreferenceMigrationMap() {
                return self::$dataCache->getItem( $this->mCode, 'datePreferenceMigrationMap' );
        }
 
@@ -769,14 +767,14 @@ class Language {
         * @return array
         * @since 1.24
         */
-       function getImageFiles() {
+       public function getImageFiles() {
                return self::$dataCache->getItem( $this->mCode, 'imageFiles' );
        }
 
        /**
         * @return array
         */
-       function getExtraUserToggles() {
+       public function getExtraUserToggles() {
                return (array)self::$dataCache->getItem( $this->mCode, 'extraUserToggles' );
        }
 
@@ -899,7 +897,7 @@ class Language {
         * @param string $msg Message name
         * @return string
         */
-       function getMessageFromDB( $msg ) {
+       public function getMessageFromDB( $msg ) {
                return $this->msg( $msg )->text();
        }
 
@@ -917,14 +915,14 @@ class Language {
         * @param string $key
         * @return string
         */
-       function getMonthName( $key ) {
+       public function getMonthName( $key ) {
                return $this->getMessageFromDB( self::$mMonthMsgs[$key - 1] );
        }
 
        /**
         * @return array
         */
-       function getMonthNamesArray() {
+       public function getMonthNamesArray() {
                $monthNames = [ '' ];
                for ( $i = 1; $i < 13; $i++ ) {
                        $monthNames[] = $this->getMonthName( $i );
@@ -936,7 +934,7 @@ class Language {
         * @param string $key
         * @return string
         */
-       function getMonthNameGen( $key ) {
+       public function getMonthNameGen( $key ) {
                return $this->getMessageFromDB( self::$mMonthGenMsgs[$key - 1] );
        }
 
@@ -2592,7 +2590,7 @@ class Language {
                return $text;
        }
 
-       // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
+       // callback functions for ucwords(), ucwordbreaks()
 
        /**
         * @param array $matches
@@ -2610,24 +2608,6 @@ class Language {
                return mb_strtoupper( $matches[0] );
        }
 
-       /**
-        * @param array $matches
-        * @return string
-        */
-       function ucCallback( $matches ) {
-               list( $wikiUpperChars ) = self::getCaseMaps();
-               return strtr( $matches[1], $wikiUpperChars );
-       }
-
-       /**
-        * @param array $matches
-        * @return string
-        */
-       function lcCallback( $matches ) {
-               list( , $wikiLowerChars ) = self::getCaseMaps();
-               return strtr( $matches[1], $wikiLowerChars );
-       }
-
        /**
         * @param array $matches
         * @return string
@@ -2636,15 +2616,6 @@ class Language {
                return mb_strtoupper( $matches[0] );
        }
 
-       /**
-        * @param array $matches
-        * @return string
-        */
-       function ucwordsCallbackWiki( $matches ) {
-               list( $wikiUpperChars ) = self::getCaseMaps();
-               return strtr( $matches[0], $wikiUpperChars );
-       }
-
        /**
         * Make a string's first character uppercase
         *
@@ -2673,27 +2644,14 @@ class Language {
         * @return string
         */
        function uc( $str, $first = false ) {
-               if ( function_exists( 'mb_strtoupper' ) ) {
-                       if ( $first ) {
-                               if ( $this->isMultibyte( $str ) ) {
-                                       return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
-                               } else {
-                                       return ucfirst( $str );
-                               }
-                       } else {
-                               return $this->isMultibyte( $str ) ? mb_strtoupper( $str ) : strtoupper( $str );
-                       }
-               } else {
+               if ( $first ) {
                        if ( $this->isMultibyte( $str ) ) {
-                               $x = $first ? '^' : '';
-                               return preg_replace_callback(
-                                       "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
-                                       [ $this, 'ucCallback' ],
-                                       $str
-                               );
+                               return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
                        } else {
-                               return $first ? ucfirst( $str ) : strtoupper( $str );
+                               return ucfirst( $str );
                        }
+               } else {
+                       return $this->isMultibyte( $str ) ? mb_strtoupper( $str ) : strtoupper( $str );
                }
        }
 
@@ -2721,27 +2679,14 @@ class Language {
         * @return mixed|string
         */
        function lc( $str, $first = false ) {
-               if ( function_exists( 'mb_strtolower' ) ) {
-                       if ( $first ) {
-                               if ( $this->isMultibyte( $str ) ) {
-                                       return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
-                               } else {
-                                       return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
-                               }
-                       } else {
-                               return $this->isMultibyte( $str ) ? mb_strtolower( $str ) : strtolower( $str );
-                       }
-               } else {
+               if ( $first ) {
                        if ( $this->isMultibyte( $str ) ) {
-                               $x = $first ? '^' : '';
-                               return preg_replace_callback(
-                                       "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
-                                       [ $this, 'lcCallback' ],
-                                       $str
-                               );
+                               return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
                        } else {
-                               return $first ? strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
+                               return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
                        }
+               } else {
+                       return $this->isMultibyte( $str ) ? mb_strtolower( $str ) : strtolower( $str );
                }
        }
 
@@ -2765,19 +2710,11 @@ class Language {
                        $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
 
                        // function to use to capitalize a single char
-                       if ( function_exists( 'mb_strtoupper' ) ) {
-                               return preg_replace_callback(
-                                       $replaceRegexp,
-                                       [ $this, 'ucwordsCallbackMB' ],
-                                       $str
-                               );
-                       } else {
-                               return preg_replace_callback(
-                                       $replaceRegexp,
-                                       [ $this, 'ucwordsCallbackWiki' ],
-                                       $str
-                               );
-                       }
+                       return preg_replace_callback(
+                               $replaceRegexp,
+                               [ $this, 'ucwordsCallbackMB' ],
+                               $str
+                       );
                } else {
                        return ucwords( strtolower( $str ) );
                }
@@ -2800,19 +2737,11 @@ class Language {
                        $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|" .
                                "$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
 
-                       if ( function_exists( 'mb_strtoupper' ) ) {
-                               return preg_replace_callback(
-                                       $replaceRegexp,
-                                       [ $this, 'ucwordbreaksCallbackMB' ],
-                                       $str
-                               );
-                       } else {
-                               return preg_replace_callback(
-                                       $replaceRegexp,
-                                       [ $this, 'ucwordsCallbackWiki' ],
-                                       $str
-                               );
-                       }
+                       return preg_replace_callback(
+                               $replaceRegexp,
+                               [ $this, 'ucwordbreaksCallbackMB' ],
+                               $str
+                       );
                } else {
                        return preg_replace_callback(
                                '/\b([\w\x80-\xff]+)\b/',
@@ -3966,10 +3895,11 @@ class Language {
         * match up with it.
         *
         * @param string $str The validated block duration in English
+        * @param User $user User object to use timezone from or null for $wgUser
         * @return string Somehow translated block duration
         * @see LanguageFi.php for example implementation
         */
-       function translateBlockExpiry( $str ) {
+       function translateBlockExpiry( $str, User $user = null ) {
                $duration = SpecialBlock::getSuggestedDurations( $this );
                foreach ( $duration as $show => $value ) {
                        if ( strcmp( $str, $value ) == 0 ) {
@@ -3995,10 +3925,12 @@ class Language {
                } else { // It's an absolute timestamp.
                        if ( $time === 0 ) {
                                // wfTimestamp() handles 0 as current time instead of epoch.
-                               return $this->timeanddate( '19700101000000' );
-                       } else {
-                               return $this->timeanddate( $time );
+                               $time = '19700101000000';
+                       }
+                       if ( $user ) {
+                               return $this->userTimeAndDate( $time, $user );
                        }
+                       return $this->timeanddate( $time );
                }
        }
 
@@ -4464,7 +4396,7 @@ class Language {
         * @return array Array of message keys (strings)
         */
        public static function getMessageKeysFor( $code ) {
-               return self::getLocalisationCache()->getSubItemList( $code, 'messages' );
+               return self::getLocalisationCache()->getSubitemList( $code, 'messages' );
        }
 
        /**
@@ -4496,26 +4428,6 @@ class Language {
                return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
        }
 
-       /**
-        * @throws MWException
-        * @return array
-        */
-       static function getCaseMaps() {
-               static $wikiUpperChars, $wikiLowerChars;
-               if ( isset( $wikiUpperChars ) ) {
-                       return [ $wikiUpperChars, $wikiLowerChars ];
-               }
-
-               $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
-               if ( $arr === false ) {
-                       throw new MWException(
-                               "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
-               }
-               $wikiUpperChars = $arr['wikiUpperChars'];
-               $wikiLowerChars = $arr['wikiLowerChars'];
-               return [ $wikiUpperChars, $wikiLowerChars ];
-       }
-
        /**
         * Decode an expiry (block, protection, etc) which has come from the DB
         *