-
- static function getLocalisationArray( $code, $disableCache = false ) {
- self::loadLocalisation( $code, $disableCache );
- return self::$mLocalisationCache[$code];
- }
-
- /**
- * Load localisation data for a given code into the static cache
- *
- * @return array Dependencies, map of filenames to mtimes
- */
- static function loadLocalisation( $code, $disableCache = false ) {
- static $recursionGuard = array();
- global $wgMemc, $wgCheckSerialized;
-
- if ( !$code ) {
- throw new MWException( "Invalid language code requested" );
- }
-
- if ( !$disableCache ) {
- # Try the per-process cache
- if ( isset( self::$mLocalisationCache[$code] ) ) {
- return self::$mLocalisationCache[$code]['deps'];
- }
-
- wfProfileIn( __METHOD__ );
-
- # Try the serialized directory
- $cache = wfGetPrecompiledData( self::getFileName( "Messages", $code, '.ser' ) );
- if ( $cache ) {
- if ( $wgCheckSerialized && self::isLocalisationOutOfDate( $cache ) ) {
- $cache = false;
- wfDebug( "Language::loadLocalisation(): precompiled data file for $code is out of date\n" );
- } else {
- self::$mLocalisationCache[$code] = $cache;
- wfDebug( "Language::loadLocalisation(): got localisation for $code from precompiled data file\n" );
- wfProfileOut( __METHOD__ );
- return self::$mLocalisationCache[$code]['deps'];
- }
- }
-
- # Try the global cache
- $memcKey = wfMemcKey('localisation', $code );
- $fbMemcKey = wfMemcKey('fallback', $cache['fallback'] );
- $cache = $wgMemc->get( $memcKey );
- if ( $cache ) {
- if ( self::isLocalisationOutOfDate( $cache ) ) {
- $wgMemc->delete( $memcKey );
- $wgMemc->delete( $fbMemcKey );
- $cache = false;
- wfDebug( "Language::loadLocalisation(): localisation cache for $code had expired\n" );
- } else {
- self::$mLocalisationCache[$code] = $cache;
- wfDebug( "Language::loadLocalisation(): got localisation for $code from cache\n" );
- wfProfileOut( __METHOD__ );
- return $cache['deps'];
- }
- }
- } else {
- wfProfileIn( __METHOD__ );
- }
-
- # Default fallback, may be overridden when the messages file is included
- if ( $code != 'en' ) {
- $fallback = 'en';
- } else {
- $fallback = false;
- }
-
- # Load the primary localisation from the source file
- $filename = self::getMessagesFileName( $code );
- if ( !file_exists( $filename ) ) {
- wfDebug( "Language::loadLocalisation(): no localisation file for $code, using implicit fallback to en\n" );
- $cache = compact( self::$mLocalisationKeys ); // Set correct fallback
- $deps = array();
- } else {
- $deps = array( $filename => filemtime( $filename ) );
- require( $filename );
- $cache = compact( self::$mLocalisationKeys );
- wfDebug( "Language::loadLocalisation(): got localisation for $code from source\n" );
- }
-
- if ( !empty( $fallback ) ) {
- # Load the fallback localisation, with a circular reference guard
- if ( isset( $recursionGuard[$code] ) ) {
- throw new MWException( "Error: Circular fallback reference in language code $code" );
- }
- $recursionGuard[$code] = true;
- $newDeps = self::loadLocalisation( $fallback, $disableCache );
- unset( $recursionGuard[$code] );
-
- $secondary = self::$mLocalisationCache[$fallback];
- $deps = array_merge( $deps, $newDeps );
-
- # Merge the fallback localisation with the current localisation
- foreach ( self::$mLocalisationKeys as $key ) {
- if ( isset( $cache[$key] ) ) {
- if ( isset( $secondary[$key] ) ) {
- if ( in_array( $key, self::$mMergeableMapKeys ) ) {
- $cache[$key] = $cache[$key] + $secondary[$key];
- } elseif ( in_array( $key, self::$mMergeableListKeys ) ) {
- $cache[$key] = array_merge( $secondary[$key], $cache[$key] );
- } elseif ( in_array( $key, self::$mMergeableAliasListKeys ) ) {
- $cache[$key] = array_merge_recursive( $cache[$key], $secondary[$key] );
- }
- }
- } else {
- $cache[$key] = $secondary[$key];
- }
- }
-
- # Merge bookstore lists if requested
- if ( !empty( $cache['bookstoreList']['inherit'] ) ) {
- $cache['bookstoreList'] = array_merge( $cache['bookstoreList'], $secondary['bookstoreList'] );
- }
- if ( isset( $cache['bookstoreList']['inherit'] ) ) {
- unset( $cache['bookstoreList']['inherit'] );
- }
- }
-
- # Add dependencies to the cache entry
- $cache['deps'] = $deps;
-
- # Replace spaces with underscores in namespace names
- $cache['namespaceNames'] = str_replace( ' ', '_', $cache['namespaceNames'] );
-
- # And do the same for specialpage aliases. $page is an array.
- foreach ( $cache['specialPageAliases'] as &$page ) {
- $page = str_replace( ' ', '_', $page );
- }
- # Decouple the reference to prevent accidental damage
- unset($page);
-
- # Save to both caches
- self::$mLocalisationCache[$code] = $cache;
- if ( !$disableCache ) {
- $wgMemc->set( $memcKey, $cache );
- $wgMemc->set( $fbMemcKey, (string) $cache['fallback'] );
- }
-
- wfProfileOut( __METHOD__ );
- return $deps;
- }