Merge "Optimize HashRing to avoid hashing for the common single-location case"
[lhc/web/wiklou.git] / languages / Language.php
index 643d0eb..7ee6a65 100644 (file)
@@ -61,7 +61,9 @@ class Language {
 
        public $mVariants, $mCode, $mLoaded = false;
        public $mMagicExtensions = [];
-       private $mHtmlCode = null, $mParentLanguage = false;
+       private $mHtmlCode = null;
+       /** @var Language|false */
+       private $mParentLanguage = false;
 
        public $dateFormatStrings = [];
        public $mExtendedSpecialPageAliases;
@@ -75,8 +77,10 @@ class Language {
         */
        public $transformData = [];
 
-       /** @var LocalisationCache */
-       private $localisationCache;
+       /**
+        * @var LocalisationCache
+        */
+       public static $dataCache;
 
        public static $mLangObjCache = [];
 
@@ -281,12 +285,12 @@ class Language {
         * @since 1.32
         */
        public static function clearCaches() {
-               if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MEDIAWIKI_INSTALL' ) ) {
-                       throw new MWException( __METHOD__ . ' must not be used outside tests/installer' );
-               }
-               if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
-                       MediaWikiServices::getInstance()->resetServiceForTesting( 'LocalisationCache' );
+               if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
+                       throw new MWException( __METHOD__ . ' must not be used outside tests' );
                }
+               self::$dataCache = null;
+               // Reinitialize $dataCache, since it's expected to always be available
+               self::getLocalisationCache();
                self::$mLangObjCache = [];
                self::$fallbackLanguageCache = [];
                self::$grammarTransformations = null;
@@ -441,14 +445,19 @@ class Language {
        /**
         * Get the LocalisationCache instance
         *
-        * @deprecated since 1.34, use MediaWikiServices
         * @return LocalisationCache
         */
        public static function getLocalisationCache() {
-               return MediaWikiServices::getInstance()->getLocalisationCache();
+               if ( is_null( self::$dataCache ) ) {
+                       global $wgLocalisationCacheConf;
+                       $class = $wgLocalisationCacheConf['class'];
+                       self::$dataCache = new $class( $wgLocalisationCacheConf );
+               }
+               return self::$dataCache;
        }
 
        function __construct() {
+               // @phan-suppress-next-line PhanTypeMismatchProperty
                $this->mConverter = new FakeConverter( $this );
                // Set the code to the name of the descendant
                if ( static::class === 'Language' ) {
@@ -456,11 +465,12 @@ class Language {
                } else {
                        $this->mCode = str_replace( '_', '-', strtolower( substr( static::class, 8 ) ) );
                }
-               $this->localisationCache = MediaWikiServices::getInstance()->getLocalisationCache();
+               self::getLocalisationCache();
        }
 
        /**
         * Reduce memory usage
+        * @suppress PhanTypeSuspiciousNonTraversableForeach
         */
        function __destruct() {
                foreach ( $this as $name => $value ) {
@@ -488,7 +498,7 @@ class Language {
         * @return array
         */
        public function getBookstoreList() {
-               return $this->localisationCache->getItem( $this->mCode, 'bookstoreList' );
+               return self::$dataCache->getItem( $this->mCode, 'bookstoreList' );
        }
 
        /**
@@ -505,7 +515,7 @@ class Language {
                                getCanonicalNamespaces();
 
                        $this->namespaceNames = $wgExtraNamespaces +
-                               $this->localisationCache->getItem( $this->mCode, 'namespaceNames' );
+                               self::$dataCache->getItem( $this->mCode, 'namespaceNames' );
                        $this->namespaceNames += $validNamespaces;
 
                        $this->namespaceNames[NS_PROJECT] = $wgMetaNamespace;
@@ -518,6 +528,7 @@ class Language {
                        }
 
                        # Sometimes a language will be localised but not actually exist on this wiki.
+                       // @phan-suppress-next-line PhanTypeMismatchForeach
                        foreach ( $this->namespaceNames as $key => $text ) {
                                if ( !isset( $validNamespaces[$key] ) ) {
                                        unset( $this->namespaceNames[$key] );
@@ -526,6 +537,7 @@ class Language {
 
                        # The above mixing may leave namespaces out of canonical order.
                        # Re-order by namespace ID number...
+                       // @phan-suppress-next-line PhanTypeMismatchArgumentInternal
                        ksort( $this->namespaceNames );
 
                        Hooks::run( 'LanguageGetNamespaces', [ &$this->namespaceNames ] );
@@ -612,7 +624,7 @@ class Language {
                global $wgExtraGenderNamespaces;
 
                $ns = $wgExtraGenderNamespaces +
-                       (array)$this->localisationCache->getItem( $this->mCode, 'namespaceGenderAliases' );
+                       (array)self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
 
                return $ns[$index][$gender] ?? $this->getNsText( $index );
        }
@@ -634,7 +646,7 @@ class Language {
                        return false;
                } else {
                        // Check what is in i18n files
-                       $aliases = $this->localisationCache->getItem( $this->mCode, 'namespaceGenderAliases' );
+                       $aliases = self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
                        return count( $aliases ) > 0;
                }
        }
@@ -658,7 +670,7 @@ class Language {
         */
        public function getNamespaceAliases() {
                if ( is_null( $this->namespaceAliases ) ) {
-                       $aliases = $this->localisationCache->getItem( $this->mCode, 'namespaceAliases' );
+                       $aliases = self::$dataCache->getItem( $this->mCode, 'namespaceAliases' );
                        if ( !$aliases ) {
                                $aliases = [];
                        } else {
@@ -672,8 +684,8 @@ class Language {
                        }
 
                        global $wgExtraGenderNamespaces;
-                       $genders = $wgExtraGenderNamespaces + (array)$this->localisationCache
-                               ->getItem( $this->mCode, 'namespaceGenderAliases' );
+                       $genders = $wgExtraGenderNamespaces +
+                               (array)self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
                        foreach ( $genders as $index => $forms ) {
                                foreach ( $forms as $alias ) {
                                        $aliases[$alias] = $index;
@@ -774,21 +786,21 @@ class Language {
         * @return string[]|bool List of date format preference keys, or false if disabled.
         */
        public function getDatePreferences() {
-               return $this->localisationCache->getItem( $this->mCode, 'datePreferences' );
+               return self::$dataCache->getItem( $this->mCode, 'datePreferences' );
        }
 
        /**
         * @return array
         */
        function getDateFormats() {
-               return $this->localisationCache->getItem( $this->mCode, 'dateFormats' );
+               return self::$dataCache->getItem( $this->mCode, 'dateFormats' );
        }
 
        /**
         * @return array|string
         */
        public function getDefaultDateFormat() {
-               $df = $this->localisationCache->getItem( $this->mCode, 'defaultDateFormat' );
+               $df = self::$dataCache->getItem( $this->mCode, 'defaultDateFormat' );
                if ( $df === 'dmy or mdy' ) {
                        global $wgAmericanDates;
                        return $wgAmericanDates ? 'mdy' : 'dmy';
@@ -801,7 +813,7 @@ class Language {
         * @return array
         */
        public function getDatePreferenceMigrationMap() {
-               return $this->localisationCache->getItem( $this->mCode, 'datePreferenceMigrationMap' );
+               return self::$dataCache->getItem( $this->mCode, 'datePreferenceMigrationMap' );
        }
 
        /**
@@ -2268,8 +2280,7 @@ class Language {
                }
 
                if ( !isset( $this->dateFormatStrings[$type][$pref] ) ) {
-                       $df =
-                               $this->localisationCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
+                       $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
 
                        if ( $type === 'pretty' && $df === null ) {
                                $df = $this->getDateFormatString( 'date', $pref );
@@ -2277,8 +2288,7 @@ class Language {
 
                        if ( !$wasDefault && $df === null ) {
                                $pref = $this->getDefaultDateFormat();
-                               $df = $this->getLocalisationCache()
-                                       ->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
+                               $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
                        }
 
                        $this->dateFormatStrings[$type][$pref] = $df;
@@ -2642,14 +2652,14 @@ class Language {
         * @return string|null
         */
        public function getMessage( $key ) {
-               return $this->localisationCache->getSubitem( $this->mCode, 'messages', $key );
+               return self::$dataCache->getSubitem( $this->mCode, 'messages', $key );
        }
 
        /**
         * @return array
         */
        function getAllMessages() {
-               return $this->localisationCache->getItem( $this->mCode, 'messages' );
+               return self::$dataCache->getItem( $this->mCode, 'messages' );
        }
 
        /**
@@ -2891,7 +2901,7 @@ class Language {
         * @return string
         */
        function fallback8bitEncoding() {
-               return $this->localisationCache->getItem( $this->mCode, 'fallback8bitEncoding' );
+               return self::$dataCache->getItem( $this->mCode, 'fallback8bitEncoding' );
        }
 
        /**
@@ -3081,7 +3091,7 @@ class Language {
         * @return bool
         */
        function isRTL() {
-               return $this->localisationCache->getItem( $this->mCode, 'rtl' );
+               return self::$dataCache->getItem( $this->mCode, 'rtl' );
        }
 
        /**
@@ -3157,7 +3167,7 @@ class Language {
         * @return array
         */
        function capitalizeAllNouns() {
-               return $this->localisationCache->getItem( $this->mCode, 'capitalizeAllNouns' );
+               return self::$dataCache->getItem( $this->mCode, 'capitalizeAllNouns' );
        }
 
        /**
@@ -3190,7 +3200,7 @@ class Language {
         * @return bool
         */
        function linkPrefixExtension() {
-               return $this->localisationCache->getItem( $this->mCode, 'linkPrefixExtension' );
+               return self::$dataCache->getItem( $this->mCode, 'linkPrefixExtension' );
        }
 
        /**
@@ -3198,7 +3208,7 @@ class Language {
         * @return array
         */
        function getMagicWords() {
-               return $this->localisationCache->getItem( $this->mCode, 'magicWords' );
+               return self::$dataCache->getItem( $this->mCode, 'magicWords' );
        }
 
        /**
@@ -3208,7 +3218,7 @@ class Language {
         */
        function getMagic( $mw ) {
                $rawEntry = $this->mMagicExtensions[$mw->mId] ??
-                       $this->localisationCache->getSubitem( $this->mCode, 'magicWords', $mw->mId );
+                       self::$dataCache->getSubitem( $this->mCode, 'magicWords', $mw->mId );
 
                if ( !is_array( $rawEntry ) ) {
                        wfWarn( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
@@ -3228,6 +3238,7 @@ class Language {
                $fallbackChain = array_reverse( $fallbackChain );
                foreach ( $fallbackChain as $code ) {
                        if ( isset( $newWords[$code] ) ) {
+                               // @phan-suppress-next-line PhanTypeMismatchProperty
                                $this->mMagicExtensions = $newWords[$code] + $this->mMagicExtensions;
                        }
                }
@@ -3243,7 +3254,7 @@ class Language {
                if ( is_null( $this->mExtendedSpecialPageAliases ) ) {
                        // Initialise array
                        $this->mExtendedSpecialPageAliases =
-                               $this->localisationCache->getItem( $this->mCode, 'specialPageAliases' );
+                               self::$dataCache->getItem( $this->mCode, 'specialPageAliases' );
                }
 
                return $this->mExtendedSpecialPageAliases;
@@ -3408,28 +3419,28 @@ class Language {
         * @return string
         */
        function digitGroupingPattern() {
-               return $this->localisationCache->getItem( $this->mCode, 'digitGroupingPattern' );
+               return self::$dataCache->getItem( $this->mCode, 'digitGroupingPattern' );
        }
 
        /**
         * @return array
         */
        function digitTransformTable() {
-               return $this->localisationCache->getItem( $this->mCode, 'digitTransformTable' );
+               return self::$dataCache->getItem( $this->mCode, 'digitTransformTable' );
        }
 
        /**
         * @return array
         */
        function separatorTransformTable() {
-               return $this->localisationCache->getItem( $this->mCode, 'separatorTransformTable' );
+               return self::$dataCache->getItem( $this->mCode, 'separatorTransformTable' );
        }
 
        /**
         * @return int|null
         */
        function minimumGroupingDigits() {
-               return $this->localisationCache->getItem( $this->mCode, 'minimumGroupingDigits' );
+               return self::$dataCache->getItem( $this->mCode, 'minimumGroupingDigits' );
        }
 
        /**
@@ -4329,7 +4340,7 @@ class Language {
         * @return string
         */
        public function linkTrail() {
-               return $this->localisationCache->getItem( $this->mCode, 'linkTrail' );
+               return self::$dataCache->getItem( $this->mCode, 'linkTrail' );
        }
 
        /**
@@ -4339,7 +4350,7 @@ class Language {
         * @return string
         */
        public function linkPrefixCharset() {
-               return $this->localisationCache->getItem( $this->mCode, 'linkPrefixCharset' );
+               return self::$dataCache->getItem( $this->mCode, 'linkPrefixCharset' );
        }
 
        /**
@@ -4929,13 +4940,11 @@ class Language {
         * @return array Associative array with plural form, and plural rule as key-value pairs
         */
        public function getCompiledPluralRules() {
-               $pluralRules =
-                       $this->localisationCache->getItem( strtolower( $this->mCode ), 'compiledPluralRules' );
+               $pluralRules = self::$dataCache->getItem( strtolower( $this->mCode ), 'compiledPluralRules' );
                $fallbacks = self::getFallbacksFor( $this->mCode );
                if ( !$pluralRules ) {
                        foreach ( $fallbacks as $fallbackCode ) {
-                               $pluralRules = $this->localisationCache
-                                       ->getItem( strtolower( $fallbackCode ), 'compiledPluralRules' );
+                               $pluralRules = self::$dataCache->getItem( strtolower( $fallbackCode ), 'compiledPluralRules' );
                                if ( $pluralRules ) {
                                        break;
                                }
@@ -4950,13 +4959,11 @@ class Language {
         * @return array Associative array with plural form number and plural rule as key-value pairs
         */
        public function getPluralRules() {
-               $pluralRules =
-                       $this->localisationCache->getItem( strtolower( $this->mCode ), 'pluralRules' );
+               $pluralRules = self::$dataCache->getItem( strtolower( $this->mCode ), 'pluralRules' );
                $fallbacks = self::getFallbacksFor( $this->mCode );
                if ( !$pluralRules ) {
                        foreach ( $fallbacks as $fallbackCode ) {
-                               $pluralRules = $this->localisationCache
-                                       ->getItem( strtolower( $fallbackCode ), 'pluralRules' );
+                               $pluralRules = self::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRules' );
                                if ( $pluralRules ) {
                                        break;
                                }
@@ -4971,13 +4978,11 @@ class Language {
         * @return array Associative array with plural form number and plural rule type as key-value pairs
         */
        public function getPluralRuleTypes() {
-               $pluralRuleTypes =
-                       $this->localisationCache->getItem( strtolower( $this->mCode ), 'pluralRuleTypes' );
+               $pluralRuleTypes = self::$dataCache->getItem( strtolower( $this->mCode ), 'pluralRuleTypes' );
                $fallbacks = self::getFallbacksFor( $this->mCode );
                if ( !$pluralRuleTypes ) {
                        foreach ( $fallbacks as $fallbackCode ) {
-                               $pluralRuleTypes = $this->localisationCache
-                                       ->getItem( strtolower( $fallbackCode ), 'pluralRuleTypes' );
+                               $pluralRuleTypes = self::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRuleTypes' );
                                if ( $pluralRuleTypes ) {
                                        break;
                                }