Fixed a couple of E_DEPRECATED in DatabaseMssql
[lhc/web/wiklou.git] / includes / HTMLFileCache.php
index 7eca798..fd7d048 100644 (file)
@@ -7,13 +7,13 @@
 
 /**
  * Handles talking to the file cache, putting stuff in and taking it back out.
- * Mostly called from Article.php, also from DatabaseFunctions.php for the
- * emergency abort/fallback to cache.
+ * Mostly called from Article.php for the emergency abort/fallback to cache.
  *
  * Global options that affect this module:
  * - $wgCachePages
  * - $wgCacheEpoch
  * - $wgUseFileCache
+ * - $wgCacheDirectory
  * - $wgFileCacheDirectory
  * - $wgUseGzip
  *
@@ -24,14 +24,22 @@ class HTMLFileCache {
 
        public function __construct( &$title, $type = 'view' ) {
                $this->mTitle = $title;
-               $type = $type ? $type : 'view';
                $this->mType = ($type == 'raw' || $type == 'view' ) ? $type : false;
                $this->fileCacheName(); // init name
        }
 
        public function fileCacheName() {
                if( !$this->mFileCache ) {
-                       global $wgFileCacheDirectory, $wgRequest;
+                       global $wgCacheDirectory, $wgFileCacheDirectory, $wgRequest;
+
+                       if ( $wgFileCacheDirectory ) {
+                               $dir = $wgFileCacheDirectory;
+                       } elseif ( $wgCacheDirectory ) {
+                               $dir = "$wgCacheDirectory/html";
+                       } else {
+                               throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
+                       }
+
                        # Store raw pages (like CSS hits) elsewhere
                        $subdir = ($this->mType === 'raw') ? 'raw/' : '';
                        $key = $this->mTitle->getPrefixedDbkey();
@@ -46,7 +54,7 @@ class HTMLFileCache {
                        if( $this->useGzip() )
                                $this->mFileCache .= '.gz';
 
-                       wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
+                       wfDebug( __METHOD__ . ": {$this->mFileCache}\n" );
                }
                return $this->mFileCache;
        }
@@ -97,12 +105,11 @@ class HTMLFileCache {
                global $wgCacheEpoch;
 
                if( !$this->isFileCached() ) return false;
-               if( !$timestamp ) return true; // should be invalidated on change
 
                $cachetime = $this->fileCacheTime();
                $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
 
-               wfDebug(" isFileCacheGood() - cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
+               wfDebug( __METHOD__ . ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
                return $good;
        }
 
@@ -128,13 +135,15 @@ class HTMLFileCache {
        /* Working directory to/from output */
        public function loadFromFileCache() {
                global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
-               wfDebug(" loadFromFileCache()\n");
-
+               wfDebug( __METHOD__ . "()\n");
                $filename = $this->fileCacheName();
-               $wgOut->sendCacheControl();
-
-               header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
-               header( "Content-language: $wgContLanguageCode" );
+               // Raw pages should handle cache control on their own,
+               // even when using file cache. This reduces hits from clients.
+               if( $this->mType !== 'raw' ) {
+                       $wgOut->sendCacheControl();
+                       header( "Content-Type: $wgMimeType; charset={$wgOutputEncoding}" );
+                       header( "Content-Language: $wgContLanguageCode" );
+               }
 
                if( $this->useGzip() ) {
                        if( wfClientAcceptsGzip() ) {
@@ -146,6 +155,7 @@ class HTMLFileCache {
                        }
                }
                readfile( $filename );
+               $wgOut->disable(); // tell $wgOut that output is taken care of
        }
 
        protected function checkCacheDirs() {
@@ -157,15 +167,14 @@ class HTMLFileCache {
                wfMkdirParents( $mydir2 );
        }
 
-       public function saveToFileCache( $origtext ) {
+       public function saveToFileCache( $text ) {
                global $wgUseFileCache;
-               if( !$wgUseFileCache ) {
-                       return $origtext; // return to output
+               if( !$wgUseFileCache || strlen( $text ) < 512 ) {
+                       // Disabled or empty/broken output (OOM and PHP errors)
+                       return $text;
                }
-               $text = $origtext;
-               if( strcmp($text,'') == 0 ) return '';
 
-               wfDebug(" saveToFileCache()\n", false);
+               wfDebug( __METHOD__ . "()\n", false);
 
                $this->checkCacheDirs();
 
@@ -201,7 +210,7 @@ class HTMLFileCache {
        public static function clearFileCache( $title ) {
                global $wgUseFileCache;
                if( !$wgUseFileCache ) return false;
-               $fc = new self( $title, '' );
+               $fc = new self( $title, 'view' );
                @unlink( $fc->fileCacheName() );
                $fc = new self( $title, 'raw' );
                @unlink( $fc->fileCacheName() );