From 0e5e02c9858076fe504f8c6ffc156d7cffc72582 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Tue, 23 Sep 2014 15:27:13 +1000 Subject: [PATCH] Add a process cache for fetchLanguageNames Since it is slow and is called many times when articles with lots of interlanguage links are viewed. Change-Id: Ib826ea6f1c105ffd43e456fb1c0c3b8aa6d8e391 --- languages/Language.php | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/languages/Language.php b/languages/Language.php index c889417cf1..bf2e3a399b 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -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 @@ -846,11 +852,36 @@ 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; - wfProfileIn( __METHOD__ ); - if ( $coreLanguageNames === null ) { global $IP; include "$IP/languages/Names.php"; @@ -878,7 +909,6 @@ class Language { } if ( $include === 'all' ) { - wfProfileOut( __METHOD__ ); return $names; } @@ -900,12 +930,10 @@ class Language { } } - wfProfileOut( __METHOD__ ); return $namesMwFile; } # 'mw' option; default if it's not one of the other two options (all/mwfile) - wfProfileOut( __METHOD__ ); return $returnMw; } -- 2.20.1