* Fixed unclosed <p> tag
[lhc/web/wiklou.git] / includes / MessageCache.php
index f97e73c..24822b5 100755 (executable)
@@ -2,6 +2,7 @@
 /**
  *
  * @package MediaWiki
+ * @subpackage Cache
  */
 
 /** */
@@ -53,7 +54,7 @@ class MessageCache
                # can return a cache hit, this saves
                # some extra milliseconds
                $this->mDeferred = true;
-               
+
                wfProfileOut( $fname );
        }
 
@@ -66,7 +67,11 @@ class MessageCache
                global $wgAllMessagesEn;
 
                if ( $this->mDisable ) {
-                       wfDebug( "MessageCache::load(): disabled\n" );
+                       static $shownDisabled = false;
+                       if ( !$shownDisabled ) {
+                               wfDebug( "MessageCache::load(): disabled\n" );
+                               $shownDisabled = true;
+                       }
                        return true;
                }
                $fname = 'MessageCache::load';
@@ -83,7 +88,7 @@ class MessageCache
                                wfDebug( "MessageCache::load(): loading all messages\n" );
                                $this->lock();
                                # Other threads don't need to load the messages if another thread is doing it.
-                               $success = $this->mMemc->add( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
+                               $success = $this->mMemc->add( $this->mMemcKey.'-status', "loading", MSG_LOAD_TIMEOUT );
                                if ( $success ) {
                                        wfProfileIn( $fname.'-load' );
                                        $this->loadFromDB();
@@ -91,12 +96,14 @@ class MessageCache
                                        # Save in memcached
                                        # Keep trying if it fails, this is kind of important
                                        wfProfileIn( $fname.'-save' );
-                                       for ( $i=0; $i<20 && !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ); $i++ ) {
+                                       for ($i=0; $i<20 &&
+                                                  !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
+                                            $i++ ) {
                                                usleep(mt_rand(500000,1500000));
                                        }
                                        wfProfileOut( $fname.'-save' );
                                        if ( $i == 20 ) {
-                                               $this->mMemc->set( $this->mMemcKey, 'error', 60*5 );
+                                               $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
                                                wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
                                        }
                                }
@@ -130,23 +137,13 @@ class MessageCache
         * Loads all or main part of cacheable messages from the database
         */
        function loadFromDB() {
-               global $wgPartialMessageCache;
                $fname = 'MessageCache::loadFromDB';
                $dbr =& wfGetDB( DB_SLAVE );
-               $conditions = array( 'page_is_redirect' => 0, 
+               $conditions = array( 'page_is_redirect' => 0,
                                        'page_namespace' => NS_MEDIAWIKI);
-               if ($wgPartialMessageCache) {
-                       wfDebugDieBacktrace( "Confused about how this works." );
-                       if (is_array($wgPartialMessageCache)) {
-                               $conditions['page_title']=$wgPartialMessageCache;
-                       } else {
-                               require_once("MessageCacheHints.php");
-                               $conditions['page_title']=MessageCacheHints::get();
-                       }
-               }
-               $res = $dbr->select( array( 'page', 'text' ),
+               $res = $dbr->select( array( 'page', 'revision', 'text' ),
                        array( 'page_title', 'old_text', 'old_flags' ),
-                       'page_is_redirect=0 AND page_namespace = '.NS_MEDIAWIKI.' AND page_latest = old_id',
+                       'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI.' AND page_latest=rev_id AND rev_text_id=old_id',
                        $fname
                );
 
@@ -156,6 +153,20 @@ class MessageCache
                }
 
                $dbr->freeResult( $res );
+               /*
+               # FIXME: This is too slow currently.
+               # We need to bulk-fetch revisions, but in a portable way...
+               $resultSet = Revision::fetchFromConds( $dbr, array(
+                       'page_namespace'   => NS_MEDIAWIKI,
+                       'page_is_redirect' => 0,
+                       'page_id=rev_page' ) );
+               while( $row = $resultSet->fetchObject() ) {
+                       $revision = new Revision( $row );
+                       $title = $revision->getTitle();
+                       $this->mCache[$title->getDBkey()] = $revision->getText();
+               }
+               $resultSet->free();
+               */
        }
 
        /**
@@ -174,7 +185,7 @@ class MessageCache
        }
 
        /**
-        * Obsolete
+        * @deprecated
         */
        function isCacheable( $key ) {
                return true;
@@ -229,7 +240,7 @@ class MessageCache
                }
                # If uninitialised, someone is trying to call this halfway through Setup.php
                if( !$this->mInitialised ) {
-                       return "&lt;$key&gt;";
+                       return '&lt;' . htmlspecialchars($key) . '&gt;';
                }
                # If cache initialization was deferred, start it now.
                if( $this->mDeferred ) {
@@ -269,20 +280,20 @@ class MessageCache
                        !$isfullkey && ($langcode != $wgContLanguageCode) ) {
                        $message = $this->getFromCache( $lang->ucfirst( $key ) );
                }
-               
+
                # Final fallback
                if( !$message ) {
-                       $message = "&lt;$key&gt;";
+                       return '&lt;' . htmlspecialchars($key) . '&gt;';
                }
 
                # Replace brace tags
                $message = $this->transform( $message );
                return $message;
        }
-       
+
        function getFromCache( $title ) {
                $message = false;
-               
+
                # Try the cache
                if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
                        $message = $this->mCache[$title];
@@ -297,25 +308,19 @@ class MessageCache
 
                # If it wasn't in the cache, load each message from the DB individually
                if ( !$message ) {
-                       $dbr =& wfGetDB( DB_SLAVE );
-                       $result = $dbr->selectRow( array( 'page', 'text' ),
-                         array( 'old_flags', 'old_text' ),
-                         'page_namespace=' . NS_MEDIAWIKI .
-                         ' AND page_title=' . $dbr->addQuotes( $title ) .
-                         ' AND page_latest=old_id',
-                         'MessageCache::get' );
-                       if ( $result ) {
-                               $message = Revision::getRevisionText( $result );
+                       $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
+                       if( $revision ) {
+                               $message = $revision->getText();
                                if ($this->mUseCache) {
                                        $this->mCache[$title]=$message;
-                                       /* individual messages may be often 
-                                          recached until proper purge code exists 
+                                       /* individual messages may be often
+                                          recached until proper purge code exists
                                        */
                                        $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
                                }
                        }
                }
-               
+
                return $message;
        }
 
@@ -333,10 +338,21 @@ class MessageCache
        function disableTransform() { $this->mDisableTransform = true; }
        function enableTransform() { $this->mDisableTransform = false; }
 
+       /**
+        * Add a message to the cache
+        *
+        * @param mixed $key
+        * @param mixed $value
+        */
        function addMessage( $key, $value ) {
                $this->mExtensionMessages[$key] = $value;
        }
 
+       /**
+        * Add an associative array of message to the cache
+        *
+        * @param array $messages An associative array of key => values to be added
+        */
        function addMessages( $messages ) {
                foreach ( $messages as $key => $value ) {
                        $this->mExtensionMessages[$key] = $value;