Add a process cache for fetchLanguageNames
[lhc/web/wiklou.git] / languages / Language.php
index cd8df5d..bf2e3a3 100644 (file)
@@ -143,6 +143,12 @@ class Language {
         */
        static private $fallbackLanguageCache = array();
 
+       /**
+        * Cache for language names
+        * @var MapCacheLRU|null
+        */
+       static private $languageNameCache;
+
        /**
         * Get a cached or new language object for a given language code
         * @param string $code
@@ -784,6 +790,14 @@ class Language {
                return self::$dataCache->getSubitem( $this->mCode, 'imageFiles', $image );
        }
 
+       /**
+        * @return array
+        * @since 1.24
+        */
+       function getImageFiles() {
+               return self::$dataCache->getItem( $this->mCode, 'imageFiles' );
+       }
+
        /**
         * @return array
         */
@@ -838,6 +852,33 @@ class Language {
         * @since 1.20
         */
        public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) {
+               wfProfileIn( __METHOD__ );
+               $cacheKey = $inLanguage === null ? 'null' : $inLanguage;
+               $cacheKey .= ":$include";
+               if ( self::$languageNameCache === null ) {
+                       self::$languageNameCache = new MapCacheLRU( 20 );
+               }
+               if ( self::$languageNameCache->has( $cacheKey ) ) {
+                       $ret = self::$languageNameCache->get( $cacheKey );
+               } else {
+                       $ret = self::fetchLanguageNamesUncached( $inLanguage, $include );
+                       self::$languageNameCache->set( $cacheKey, $ret );
+               }
+               wfProfileOut( __METHOD__ );
+               return $ret;
+       }
+
+       /**
+        * Uncached helper for fetchLanguageNames
+        * @param null|string $inLanguage Code of language in which to return the names
+        *              Use null for autonyms (native names)
+        * @param string $include One of:
+        *              'all' all available languages
+        *              'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
+        *              'mwfile' only if the language is in 'mw' *and* has a message file
+        * @return array Language code => language name
+        */
+       private static function fetchLanguageNamesUncached( $inLanguage = null, $include = 'mw' ) {
                global $wgExtraLanguageNames;
                static $coreLanguageNames;
 
@@ -1026,6 +1067,11 @@ class Language {
 
        /**
         * Pass through result from $dateTimeObj->format()
+        * @param DateTime|bool|null &$dateTimeObj
+        * @param string $ts
+        * @param DateTimeZone|bool|null $zone
+        * @param string $code
+        * @return string
         */
        private static function dateTimeObjFormat( &$dateTimeObj, $ts, $zone, $code ) {
                if ( !$dateTimeObj ) {
@@ -3137,7 +3183,7 @@ class Language {
                }
 
                if ( !is_array( $rawEntry ) ) {
-                       error_log( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
+                       wfWarn( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
                } else {
                        $mw->mCaseSensitive = $rawEntry[0];
                        $mw->mSynonyms = array_slice( $rawEntry, 1 );
@@ -3992,7 +4038,7 @@ class Language {
         * Get the list of variants supported by this language
         * see sample implementation in LanguageZh.php
         *
-        * @return array an array of language codes
+        * @return array An array of language codes
         */
        public function getVariants() {
                return $this->mConverter->getVariants();
@@ -4284,7 +4330,7 @@ class Language {
         *
         * @since 1.22
         * @param string $code Language code
-        * @return array array( fallbacks, site fallbacks )
+        * @return array Array( fallbacks, site fallbacks )
         */
        public static function getFallbacksIncludingSiteLanguage( $code ) {
                global $wgLanguageCode;
@@ -4339,7 +4385,7 @@ class Language {
         *
         * @since 1.19
         * @param string $code Language code
-        * @return array of message keys (strings)
+        * @return array Array of message keys (strings)
         */
        public static function getMessageKeysFor( $code ) {
                return self::getLocalisationCache()->getSubItemList( $code, 'messages' );
@@ -4611,7 +4657,7 @@ class Language {
         * @param Title $title Title object to link
         * @param int $offset
         * @param int $limit
-        * @param array|string $query Optional URL query parameter string
+        * @param array $query Optional URL query parameter string
         * @param bool $atend Optional param for specified if this is the last page
         * @return string
         */