New global config setting $wgMaxTocLevel: Maximum indent level of toc.
[lhc/web/wiklou.git] / includes / MessageCache.php
index e3a44e9..8b28aaf 100755 (executable)
@@ -11,7 +11,8 @@ class MessageCache
 {
        var $mCache, $mUseCache, $mDisable, $mExpiry;
        var $mMemcKey, $mKeys, $mParserOptions, $mParser;
-       
+       var $mExtensionMessages;
+
        var $mInitialised = false;
 
        function initialise( &$memCached, $useDB, $expiry, $memcPrefix ) {
@@ -36,6 +37,7 @@ class MessageCache
                global $wgAllMessagesEn;
                
                if ( $this->mDisable ) {
+                       wfDebug( "MessageCache::load(): disabled\n" );
                        return true;
                }
                
@@ -46,21 +48,31 @@ class MessageCache
                        
                        # If there's nothing in memcached, load all the messages from the database
                        if ( !$this->mCache ) {
+                               wfDebug( "MessageCache::load(): loading all messages\n" );
                                $this->lock();
                                # Other threads don't need to load the messages if another thread is doing it.
-                               $this->mMemc->set( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
-                               $this->loadFromDB();
-                               # Save in memcached
-                               if ( !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ) ) {
-                                       # Hack for slabs reassignment problem
-                                       $this->mMemc->set( $this->mMemcKey, "error" );
-                                       wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
+                               $success = $this->mMemc->set( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
+                               if ( $success ) {
+                                       $this->loadFromDB();
+                                       # Save in memcached
+                                       # Keep trying if it fails, this is kind of important
+                                       for ( $i=0; $i<20 && !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ); $i++ ) {
+                                               usleep(mt_rand(500000,1500000));
+                                       }
+                                       if ( $i == 20 ) {
+                                               $this->mMemc->set( $this->mMemcKey, "error", 86400 );
+                                               wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
+                                       }
                                }
                                $this->unlock();
                        }
                        
                        if ( !is_array( $this->mCache ) ) {
+                               wfMsg( "MessageCache::load(): individual message mode\n" );
                                # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
+                               # Causing too much DB load, disabling -- TS 
+                               $this->mDisable = true;
+                               /*
                                if ( $this->mCache == "loading" ) {
                                        $this->mUseCache = false;
                                } elseif ( $this->mCache == "error" ) {
@@ -69,7 +81,7 @@ class MessageCache
                                } else {
                                        $this->mDisable = true;
                                        $success = false;
-                               }
+                               }*/
                                $this->mCache = false;
                        }
                }
@@ -79,16 +91,20 @@ class MessageCache
        # Loads all cacheable messages from the database
        function loadFromDB()
        {
-               $fname = "MessageCache::loadFromDB";
-               $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI;
-               $res = wfQuery( $sql, DB_READ, $fname );
+               $fname = "MessageCache::loadFromDB";
+               $dbr =& wfGetDB( DB_SLAVE );
+               $res = $dbr->select( 'cur', 
+                       array( 'cur_title', 'cur_text' ), 
+                       array( 'cur_is_redirect' => 0, 'cur_namespace' => NS_MEDIAWIKI ),
+                       $fname
+               );
                
                $this->mCache = array();
-               for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
+               for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
                        $this->mCache[$row->cur_title] = $row->cur_text;
                }
 
-               wfFreeResult( $res );
+               $dbr->freeResult( $res );
        }
        
        # Not really needed anymore
@@ -155,26 +171,30 @@ class MessageCache
                        return "&lt;$key&gt;";
                }
                
-               if ( $this->mDisable ) {
-                       return $this->transform( $wgLang->getMessage( $key ) );
-               }
-               $title = $wgLang->ucfirst( $key );
-               
                $message = false;
+               if ( !$this->mDisable ) {
+                       $title = $wgLang->ucfirst( $key );
+                       
 
-               # Try the cache
-               if ( $this->mUseCache && $this->mCache && array_key_exists( $title, $this->mCache ) ) {
-                       $message = $this->mCache[$title];
-               }
-               
-               # If it wasn't in the cache, load each message from the DB individually
-               if ( !$message && $useDB) {
-                       $result = wfGetArray( "cur", array("cur_text"), 
-                         array( "cur_namespace" => NS_MEDIAWIKI, "cur_title" => $title ),
-                         "MessageCache::get" );
-                       if ( $result ) {
-                               $message = $result->cur_text;
+                       # Try the cache
+                       if ( $this->mUseCache && $this->mCache && array_key_exists( $title, $this->mCache ) ) {
+                               $message = $this->mCache[$title];
                        }
+                       
+                       # If it wasn't in the cache, load each message from the DB individually
+                       if ( !$message && $useDB) {
+                               $dbr =& wfGetDB( DB_SLAVE );
+                               $result = $dbr->getArray( "cur", array("cur_text"), 
+                                 array( "cur_namespace" => NS_MEDIAWIKI, "cur_title" => $title ),
+                                 "MessageCache::get" );
+                               if ( $result ) {
+                                       $message = $result->cur_text;
+                               }
+                       }
+               }
+               # Try the extension array
+               if ( !$message ) {
+                       $message = @$this->mExtensionMessages[$key];
                }
 
                # Try the array in $wgLang
@@ -210,5 +230,14 @@ class MessageCache
        function enable() { $this->mDisable = false; }
        function disableTransform() { $this->mDisableTransform = true; }
 
+       function addMessage( $key, $value ) {
+               $this->mExtensionMessages[$key] = $value;
+       }
+
+       function addMessages( $messages ) {
+               foreach ( $messages as $key => $value ) {
+                       $this->mExtensionMessages[$key] = $value;
+               }
+       }
 }
 ?>