Cripple the wiki text stuff for now. It doesn't SEEM dangerous but I haven't tested...
[lhc/web/wiklou.git] / includes / MessageCache.php
old mode 100755 (executable)
new mode 100644 (file)
index a02bd62..ed9e5ec
@@ -5,9 +5,6 @@
  * @subpackage Cache
  */
 
-/** */
-require_once( 'Revision.php' );
-
 /**
  *
  */
@@ -21,8 +18,7 @@ define( 'MSG_WAIT_TIMEOUT', 10);
  *
  * @package MediaWiki
  */
-class MessageCache
-{
+class MessageCache {
        var $mCache, $mUseCache, $mDisable, $mExpiry;
        var $mMemcKey, $mKeys, $mParserOptions, $mParser;
        var $mExtensionMessages = array();
@@ -30,7 +26,6 @@ class MessageCache
        var $mDeferred = true;
 
        function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
-               global $wgLocalMessageCache;
                $fname = 'MessageCache::initialise';
                wfProfileIn( $fname );
 
@@ -59,7 +54,7 @@ class MessageCache
                wfProfileOut( $fname );
        }
 
-       /** 
+       /**
         * Try to load the cache from a local file
         */
        function loadFromLocal( $hash ) {
@@ -69,10 +64,12 @@ class MessageCache
                if ( $wgLocalMessageCache === false ) {
                        return;
                }
-               
+
                $filename = "$wgLocalMessageCache/messages-$wgDBname";
 
+               wfSuppressWarnings();
                $file = fopen( $filename, 'r' );
+               wfRestoreWarnings();
                if ( !$file ) {
                        return;
                }
@@ -96,9 +93,11 @@ class MessageCache
                if ( $wgLocalMessageCache === false ) {
                        return;
                }
-               
+
                $filename = "$wgLocalMessageCache/messages-$wgDBname";
+               $oldUmask = umask( 0 );
                wfMkdirParents( $wgLocalMessageCache, 0777 );
+               umask( $oldUmask );
 
                $file = fopen( $filename, 'w' );
                if ( !$file ) {
@@ -108,8 +107,59 @@ class MessageCache
 
                fwrite( $file, $hash . $serialized );
                fclose( $file );
+               @chmod( $filename, 0666 );
+       }
+
+       function loadFromScript( $hash ) {
+               global $wgLocalMessageCache, $wgDBname;
+               if ( $wgLocalMessageCache === false ) {
+                       return;
+               }
+               
+               $filename = "$wgLocalMessageCache/messages-$wgDBname";
+               
+               wfSuppressWarnings();
+               $file = fopen( $filename, 'r' );
+               wfRestoreWarnings();
+               if ( !$file ) {
+                       return;
+               }
+               $localHash=substr(fread($file,40),8);
+               fclose($file);
+               if ($hash!=$localHash) {
+                       return;
+               }
+               require("$wgLocalMessageCache/messages-$wgDBname");
        }
+       
+       function saveToScript($array, $hash) {
+               global $wgLocalMessageCache, $wgDBname;
+               if ( $wgLocalMessageCache === false ) {
+                       return;
+               }
 
+               $filename = "$wgLocalMessageCache/messages-$wgDBname";
+               $oldUmask = umask( 0 );
+               wfMkdirParents( $wgLocalMessageCache, 0777 );
+               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). 
+                               "',\n");
+               }
+               fwrite($file,");\n?>");
+               fclose($file);
+               rename($filename.'.tmp',$filename);
+       }
+
+       function escapeForScript($string) {
+               $string = str_replace( '\\', '\\\\', $string );
+               $string = str_replace( '\'', '\\\'', $string );
+               return $string;
+       }
 
        /**
         * Loads messages either from memcached or the database, if not disabled
@@ -117,7 +167,7 @@ class MessageCache
         * Returns false for a reportable error, true otherwise
         */
        function load() {
-               global $wgAllMessagesEn, $wgLocalMessageCache;
+               global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
 
                if ( $this->mDisable ) {
                        static $shownDisabled = false;
@@ -138,7 +188,11 @@ class MessageCache
                        wfProfileIn( $fname.'-fromlocal' );
                        $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
                        if ( $hash ) {
-                               $this->loadFromLocal( $hash );
+                               if ($wgLocalMessageCacheSerialized) {
+                                       $this->loadFromLocal( $hash );
+                               } else {
+                                       $this->loadFromScript( $hash );
+                               }
                        }
                        wfProfileOut( $fname.'-fromlocal' );
 
@@ -148,13 +202,17 @@ class MessageCache
                                $this->mCache = $this->mMemc->get( $this->mMemcKey );
 
                                # Save to local cache
-                               if ( $wgLocalMessageCache !== false ) { 
+                               if ( $wgLocalMessageCache !== false ) {
                                        $serialized = serialize( $this->mCache );
                                        if ( !$hash ) {
                                                $hash = md5( $serialized );
                                                $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
                                        }
-                                       $this->saveToLocal( $serialized, $hash );
+                                       if ($wgLocalMessageCacheSerialized) {
+                                               $this->saveToLocal( $serialized,$hash );
+                                       } else {
+                                               $this->saveToScript( $this->mCache, $hash );
+                                       }
                                }
                                wfProfileOut( $fname.'-fromcache' );
                        }
@@ -181,11 +239,15 @@ class MessageCache
                                        }
 
                                        # Save to local cache
-                                       if ( $wgLocalMessageCache !== false ) { 
+                                       if ( $wgLocalMessageCache !== false ) {
                                                $serialized = serialize( $this->mCache );
                                                $hash = md5( $serialized );
                                                $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
-                                               $this->saveToLocal( $serialized, $hash );
+                                               if ($wgLocalMessageCacheSerialized) {
+                                                       $this->saveToLocal( $serialized,$hash );
+                                               } else {
+                                                       $this->saveToScript( $this->mCache, $hash );
+                                               }
                                        }
 
                                        wfProfileOut( $fname.'-save' );
@@ -225,11 +287,11 @@ class MessageCache
         */
        function loadFromDB() {
                global $wgAllMessagesEn, $wgLang;
-               
+
                $fname = 'MessageCache::loadFromDB';
                $dbr =& wfGetDB( DB_SLAVE );
                if ( !$dbr ) {
-                       wfDebugDieBacktrace( 'Invalid database object' );
+                       throw new MWException( 'Invalid database object' );
                }
                $conditions = array( 'page_is_redirect' => 0,
                                        'page_namespace' => NS_MEDIAWIKI);
@@ -245,7 +307,7 @@ class MessageCache
                }
 
                # Negative caching
-               # Go through the language array and the extension array and make a note of 
+               # Go through the language array and the extension array and make a note of
                # any keys missing from the cache
                foreach ( $wgAllMessagesEn as $key => $value ) {
                        $uckey = $wgLang->ucfirst( $key );
@@ -258,7 +320,7 @@ class MessageCache
                        if ( !array_key_exists( $uckey, $this->mCache ) ) {
                                $this->mCache[$uckey] = false;
                        }
-               }       
+               }
 
                $dbr->freeResult( $res );
        }
@@ -286,20 +348,25 @@ class MessageCache
        }
 
        function replace( $title, $text ) {
-               global $wgLocalMessageCache;
+               global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname;
 
                $this->lock();
                $this->load();
+               $parserMemc->delete("$wgDBname:sidebar");
                if ( is_array( $this->mCache ) ) {
                        $this->mCache[$title] = $text;
                        $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
-                       
+
                        # Save to local cache
-                       if ( $wgLocalMessageCache !== false ) { 
+                       if ( $wgLocalMessageCache !== false ) {
                                $serialized = serialize( $this->mCache );
                                $hash = md5( $serialized );
                                $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
-                               $this->saveToLocal( $serialized, $hash );
+                               if ($wgLocalMessageCacheSerialized) {
+                                       $this->saveToLocal( $serialized,$hash );
+                               } else {
+                                       $this->saveToScript( $this->mCache, $hash );
+                               }
                        }
 
 
@@ -387,7 +454,7 @@ class MessageCache
                }
 
                # Is this a custom message? Try the default language in the db...
-               if( $message === false &&
+               if( ($message === false || $message === '-' ) &&
                        !$this->mDisable && $useDB &&
                        !$isfullkey && ($langcode != $wgContLanguageCode) ) {
                        $message = $this->getFromCache( $lang->ucfirst( $key ) );
@@ -424,6 +491,9 @@ class MessageCache
                        }
                }
 
+               # Call message Hooks, in case they are defined
+               wfRunHooks('MessagesPreLoad',array($title,&$message));
+
                # If it wasn't in the cache, load each message from the DB individually
                $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
                if( $revision ) {
@@ -457,6 +527,8 @@ class MessageCache
        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; }
 
        /**
         * Add a message to the cache