Cleanup for displaying old versions
[lhc/web/wiklou.git] / includes / MessageCache.php
index a269c62..9bd3321 100644 (file)
@@ -1,8 +1,7 @@
 <?php
 /**
- *
- * @package MediaWiki
- * @subpackage Cache
+ * @file
+ * @ingroup Cache
  */
 
 /**
@@ -16,8 +15,7 @@ define( 'MSG_CACHE_VERSION', 1 );
 /**
  * Message cache
  * Performs various MediaWiki namespace-related functions
- *
- * @package MediaWiki
+ * @ingroup Cache
  */
 class MessageCache {
        var $mCache, $mUseCache, $mDisable, $mExpiry;
@@ -25,6 +23,7 @@ class MessageCache {
        var $mExtensionMessages = array();
        var $mInitialised = false;
        var $mDeferred = true;
+       var $mAllMessagesLoaded;
 
        function __construct( &$memCached, $useDB, $expiry, $memcPrefix) {
                wfProfileIn( __METHOD__ );
@@ -59,9 +58,8 @@ class MessageCache {
         * Try to load the cache from a local file
         */
        function loadFromLocal( $hash ) {
-               global $wgLocalMessageCache;
+               global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
 
-               $this->mCache = false;
                if ( $wgLocalMessageCache === false ) {
                        return;
                }
@@ -75,21 +73,35 @@ class MessageCache {
                        return;
                }
 
-               // Check to see if the file has the hash specified
-               $localHash = fread( $file, 32 );
-               if ( $hash == $localHash ) {
-                       // All good, get the rest of it
-                       $serialized = fread( $file, 10000000 );
-                       $this->setCache( unserialize( $serialized ) );
+               if ( $wgLocalMessageCacheSerialized ) {
+                       // Check to see if the file has the hash specified
+                       $localHash = fread( $file, 32 );
+                       if ( $hash === $localHash ) {
+                               // All good, get the rest of it
+                               $serialized = '';
+                               while ( !feof( $file ) ) {
+                                       $serialized .= fread( $file, 100000 );
+                               }
+                               $this->setCache( unserialize( $serialized ) );
+                       }
+                       fclose( $file );
+               } else {
+                       $localHash=substr(fread($file,40),8);
+                       fclose($file);
+                       if ($hash!=$localHash) {
+                               return;
+                       }
+
+                       require("$wgLocalMessageCache/messages-" . wfWikiID());
+                       $this->setCache( $this->mCache);
                }
-               fclose( $file );
        }
 
        /**
         * Save the cache to a local file
         */
        function saveToLocal( $serialized, $hash ) {
-               global $wgLocalMessageCache;
+               global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
 
                if ( $wgLocalMessageCache === false ) {
                        return;
@@ -112,28 +124,10 @@ class MessageCache {
        }
 
        function loadFromScript( $hash ) {
-               global $wgLocalMessageCache;
-               if ( $wgLocalMessageCache === false ) {
-                       return;
-               }
-               
-               $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
-               
-               wfSuppressWarnings();
-               $file = fopen( $filename, 'r' );
-               wfRestoreWarnings();
-               if ( !$file ) {
-                       return;
-               }
-               $localHash=substr(fread($file,40),8);
-               fclose($file);
-               if ($hash!=$localHash) {
-                       return;
-               }
-               require("$wgLocalMessageCache/messages-" . wfWikiID());
-               $this->setCache( $this->mCache);
+               wfDeprecated( __METHOD__ );
+               $this->loadFromLocal( $hash );
        }
-       
+
        function saveToScript($array, $hash) {
                global $wgLocalMessageCache;
                if ( $wgLocalMessageCache === false ) {
@@ -146,10 +140,10 @@ class MessageCache {
                umask( $oldUmask );
                $file = fopen( $filename.'.tmp', 'w');
                fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
-               
+
                foreach ($array as $key => $message) {
                        fwrite($file, "'". $this->escapeForScript($key).
-                               "' => '" . $this->escapeForScript($message). 
+                               "' => '" . $this->escapeForScript($message).
                                "',\n");
                }
                fwrite($file,");\n?>");
@@ -202,19 +196,17 @@ class MessageCache {
                $this->mCache = false;
 
                # Try local cache
-               wfProfileIn( $fname.'-fromlocal' );
-               $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
-               if ( $hash ) {
-                       if ($wgLocalMessageCacheSerialized) {
+               if ( $wgLocalMessageCache !== false ) {
+                       wfProfileIn( $fname.'-fromlocal' );
+                       $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
+                       if ( $hash ) {
                                $this->loadFromLocal( $hash );
-                       } else {
-                               $this->loadFromScript( $hash );
-                       }
-                       if ( $this->mCache ) {
-                               wfDebug( "MessageCache::load(): got from local cache\n" );
+                               if ( $this->mCache ) {
+                                       wfDebug( "MessageCache::load(): got from local cache\n" );
+                               }
                        }
+                       wfProfileOut( $fname.'-fromlocal' );
                }
-               wfProfileOut( $fname.'-fromlocal' );
 
                # Try memcached
                if ( !$this->mCache ) {
@@ -298,19 +290,19 @@ class MessageCache {
         * Loads all or main part of cacheable messages from the database
         */
        function loadFromDB() {
-               global $wgLang, $wgMaxMsgCacheEntrySize;
+               global $wgMaxMsgCacheEntrySize;
 
                wfProfileIn( __METHOD__ );
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_SLAVE );
                $this->mCache = array();
 
                # Load titles for all oversized pages in the MediaWiki namespace
                $res = $dbr->select( 'page', 'page_title',
-                       array( 
+                       array(
                                'page_len > ' . intval( $wgMaxMsgCacheEntrySize ),
                                'page_is_redirect' => 0,
                                'page_namespace' => NS_MEDIAWIKI,
-                       ), 
+                       ),
                        __METHOD__ );
                while ( $row = $dbr->fetchObject( $res ) ) {
                        $this->mCache[$row->page_title] = '!TOO BIG';
@@ -320,12 +312,12 @@ class MessageCache {
                # Load text for the remaining pages
                $res = $dbr->select( array( 'page', 'revision', 'text' ),
                        array( 'page_title', 'old_text', 'old_flags' ),
-                       array( 
+                       array(
                                'page_is_redirect' => 0,
                                'page_namespace' => NS_MEDIAWIKI,
                                'page_latest=rev_id',
                                'rev_text_id=old_id',
-                               'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ) ), 
+                               'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ) ),
                        __METHOD__ );
 
                for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
@@ -336,22 +328,6 @@ class MessageCache {
                wfProfileOut( __METHOD__ );
        }
 
-       /**
-        * Not really needed anymore
-        */
-       function getKeys() {
-               global $wgContLang;
-               if ( !$this->mKeys ) {
-                       $this->mKeys = array();
-                       $allMessages = Language::getMessagesFor( 'en' );
-                       foreach ( $allMessages as $key => $unused ) {
-                               $title = $wgContLang->ucfirst( $key );
-                               array_push( $this->mKeys, $title );
-                       }
-               }
-               return $this->mKeys;
-       }
-
        function replace( $title, $text ) {
                global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
                global $wgMaxMsgCacheEntrySize;
@@ -359,7 +335,6 @@ class MessageCache {
                wfProfileIn( __METHOD__ );
                $this->lock();
                $this->load();
-               $parserMemc->delete(wfMemcKey('sidebar'));
                if ( is_array( $this->mCache ) ) {
                        if ( $text === false ) {
                                # Article was deleted
@@ -387,6 +362,7 @@ class MessageCache {
                        }
                }
                $this->unlock();
+               $parserMemc->delete(wfMemcKey('sidebar'));
                wfProfileOut( __METHOD__ );
        }
 
@@ -421,18 +397,43 @@ class MessageCache {
         *
         * @param string $key The message cache key
         * @param bool $useDB Get the message from the DB, false to use only the localisation
-        * @param bool $forContent Get the message from the content language rather than the 
-        *                         user language
+        * @param string $langcode Code of the language to get the message for, if
+        *                         it is a valid code create a language for that
+        *                         language, if it is a string but not a valid code
+        *                         then make a basic language object, if it is a
+        *                         false boolean then use the current users
+        *                         language (as a fallback for the old parameter
+        *                         functionality), or if it is a true boolean then
+        *                         use the wikis content language (also as a
+        *                         fallback).
         * @param bool $isFullKey Specifies whether $key is a two part key "lang/msg".
         */
-       function get( $key, $useDB = true, $forContent = true, $isFullKey = false ) {
+       function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
                global $wgContLanguageCode, $wgContLang, $wgLang;
-               if( $forContent ) {
+
+               # Identify which language to get or create a language object for.
+               if( $langcode === $wgContLang->getCode() || $langcode === true ) {
+                       # $langcode is the language code of the wikis content language object.
+                       # or it is a boolean and value is true
                        $lang =& $wgContLang;
-               } else {
+               } elseif( $langcode === $wgLang->getCode() || $langcode === false ) {
+                       # $langcode is the language code of user language object.
+                       # or it was a boolean and value is false
                        $lang =& $wgLang;
+               } else {
+                       $validCodes = array_keys( Language::getLanguageNames() );
+                       if( in_array( $langcode, $validCodes ) ) {
+                               # $langcode corresponds to a valid language.
+                               $lang = Language::factory( $langcode );
+                       } else {
+                               # $langcode is a string, but not a valid language code; use content language.
+                               $lang =& $wgContLang;
+                               wfDebug( 'Invalid language code passed to MessageCache::get, falling back to content language.' );
+                       }
                }
+
                $langcode = $lang->getCode();
+
                # If uninitialised, someone is trying to call this halfway through Setup.php
                if( !$this->mInitialised ) {
                        return '&lt;' . htmlspecialchars($key) . '&gt;';
@@ -476,17 +477,20 @@ class MessageCache {
                }
 
                # Try the array of another language
-               if( $message === false && strpos( $lckey, '/' ) ) {
-                       $message = explode( '/', $lckey );
-                       if ( $message[1] ) {
-                               wfSuppressWarnings();
-                               $message = Language::getMessageFor( $message[0], $message[1] );
-                               wfRestoreWarnings();
-                               if ( is_null( $message ) ) {
-                                       $message = false;
+               $pos = strrpos( $lckey, '/' );
+               if( $message === false && $pos !== false) {
+                       $mkey = substr( $lckey, 0, $pos );
+                       $code = substr( $lckey, $pos+1 );
+                       if ( $code ) {
+                               $validCodes = array_keys( Language::getLanguageNames() );
+                               if ( in_array( $code, $validCodes ) ) {
+                                       $message = Language::getMessageFor( $mkey, $code );
+                                       if ( is_null( $message ) ) {
+                                               $message = false;
+                                       }
+                               } else {
+                                       wfDebug( __METHOD__ . ": Invalid code $code for $mkey/$code, not trying messages array\n" );
                                }
-                       } else {
-                               $message = false;
                        }
                }
 
@@ -501,14 +505,11 @@ class MessageCache {
                if( $message === false ) {
                        return '&lt;' . htmlspecialchars($key) . '&gt;';
                }
-
-               # Replace brace tags
-               $message = $this->transform( $message );
                return $message;
        }
 
        /**
-        * Get a message from the MediaWiki namespace, with caching. The key must 
+        * Get a message from the MediaWiki namespace, with caching. The key must
         * first be converted to two-part lang/msg form if necessary.
         *
         * @param string $title Message cache key with initial uppercase letter
@@ -547,7 +548,7 @@ class MessageCache {
 
                                if ( $type == ' ' ) {
                                        $message = substr( $entry, 1 );
-                                       $this->mCache[$title] = $message;
+                                       $this->mCache[$title] = $entry;
                                        return $message;
                                } elseif ( $entry == '!NONEXISTENT' ) {
                                        return false;
@@ -577,7 +578,7 @@ class MessageCache {
                return $message;
        }
 
-       function transform( $message ) {
+       function transform( $message, $interface = false ) {
                global $wgParser;
                if ( !$this->mParser && isset( $wgParser ) ) {
                        # Do some initialisation so that we don't have to do it twice
@@ -585,9 +586,11 @@ class MessageCache {
                        # Clone it and store it
                        $this->mParser = clone $wgParser;
                }
-               if ( !$this->mDisableTransform && $this->mParser ) {
+               if ( $this->mParser ) {
                        if( strpos( $message, '{{' ) !== false ) {
-                               $message = $this->mParser->transformMsg( $message, $this->getParserOptions() );
+                               $popts = $this->getParserOptions();
+                               $popts->setInterfaceMessage( $interface );
+                               $message = $this->mParser->transformMsg( $message, $popts );
                        }
                }
                return $message;
@@ -595,10 +598,21 @@ class MessageCache {
 
        function disable() { $this->mDisable = true; }
        function enable() { $this->mDisable = false; }
-       function disableTransform() { $this->mDisableTransform = true; }
-       function enableTransform() { $this->mDisableTransform = false; }
-       function setTransform( $x ) { $this->mDisableTransform = $x; }
-       function getTransform() { return $this->mDisableTransform; }
+
+       /** @deprecated */
+       function disableTransform(){
+               wfDeprecated( __METHOD__ );
+       }
+       function enableTransform() {
+               wfDeprecated( __METHOD__ );
+       }
+       function setTransform( $x ) {
+               wfDeprecated( __METHOD__ );
+       }
+       function getTransform() {
+               wfDeprecated( __METHOD__ );
+               return false;
+       }
 
        /**
         * Add a message to the cache
@@ -619,6 +633,9 @@ class MessageCache {
         */
        function addMessages( $messages, $lang = 'en' ) {
                wfProfileIn( __METHOD__ );
+               if ( !is_array( $messages ) ) {
+                       throw new MWException( __METHOD__.': Invalid message array' );
+               }
                if ( isset( $this->mExtensionMessages[$lang] ) ) {
                        $this->mExtensionMessages[$lang] = $messages + $this->mExtensionMessages[$lang];
                } else {
@@ -629,7 +646,6 @@ class MessageCache {
 
        /**
         * Add a 2-D array of messages by lang. Useful for extensions.
-        * Introduced in 1.9. Please do not use it for now, for backwards compatibility.
         *
         * @param array $messages The array to be added
         */
@@ -642,7 +658,8 @@ class MessageCache {
        }
 
        /**
-        * Get the extension messages for a specific language
+        * Get the extension messages for a specific language. Only English, interface
+        * and content language are guaranteed to be loaded.
         *
         * @param string $lang The messages language, English by default
         */
@@ -672,12 +689,76 @@ class MessageCache {
                }
        }
 
-       static function loadAllMessages() {
+       function loadAllMessages() {
+               global $wgExtensionMessagesFiles;
+               if ( $this->mAllMessagesLoaded ) {
+                       return;
+               }
+               $this->mAllMessagesLoaded = true;
+
                # Some extensions will load their messages when you load their class file
                wfLoadAllExtensions();
                # Others will respond to this hook
                wfRunHooks( 'LoadAllMessages' );
+               # Some register their messages in $wgExtensionMessagesFiles
+               foreach ( $wgExtensionMessagesFiles as $name => $file ) {
+                       wfLoadExtensionMessages( $name );
+               }
                # Still others will respond to neither, they are EVIL. We sometimes need to know!
        }
+
+       /**
+        * Load messages from a given file
+        * 
+        * @param string $filename Filename of file to load.
+        * @param string $langcode Language to load messages for, or false for 
+     *                         default behvaiour (en, content language and user
+     *                         language).
+        */
+       function loadMessagesFile( $filename, $langcode = false ) {
+               global $wgLang, $wgContLang;
+               $messages = $magicWords = false;
+               require( $filename );
+
+               $validCodes = Language::getLanguageNames();
+               if( is_string( $langcode ) && array_key_exists( $langcode, $validCodes ) ) {
+                       # Load messages for given language code.
+                       $this->processMessagesArray( $messages, $langcode );
+               } elseif( is_string( $langcode ) && !array_key_exists( $langcode, $validCodes ) ) {
+                       wfDebug( "Invalid language '$langcode' code passed to MessageCache::loadMessagesFile()" );
+               } else {
+                       # Load only languages that are usually used, and merge all
+                       # fallbacks, except English.
+                       $langs = array_unique( array( 'en', $wgContLang->getCode(), $wgLang->getCode() ) );
+                       foreach( $langs as $code ) {
+                               $this->processMessagesArray( $messages, $code );
+                       }
+               }
+
+               if ( $magicWords !== false ) {
+                       global $wgContLang;
+                       $wgContLang->addMagicWordsByLang( $magicWords );
+               }
+       }
+
+       /**
+        * Process an array of messages, loading it into the message cache.
+        *
+        * @param array $messages Messages array.
+        * @param string $langcode Language code to process.
+        */
+       function processMessagesArray( $messages, $langcode ) {
+               $fallbackCode = $langcode;
+               $mergedMessages = array();
+               do {
+                       if ( isset($messages[$fallbackCode]) ) {
+                               $mergedMessages += $messages[$fallbackCode];
+                       }
+                       $fallbackCode = Language::getFallbackfor( $fallbackCode );
+               } while( $fallbackCode && $fallbackCode !== 'en' );
+               
+               if ( !empty($mergedMessages) )
+                       $this->addMessages( $mergedMessages, $langcode );
+       }
+
 }
-?>