]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/HTMLFileCache.php
MediaWiki 1.17.0
[autoinstallsdev/mediawiki.git] / includes / HTMLFileCache.php
index 68cafa242c1832c3ec9fde92650e3f7efed2a610..26cb147dd9d7317ccd78e1dd5ad8110f0166db2d 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
  *
@@ -30,22 +30,35 @@ class HTMLFileCache {
 
        public function fileCacheName() {
                if( !$this->mFileCache ) {
-                       global $wgFileCacheDirectory, $wgRequest;
+                       global $wgCacheDirectory, $wgFileCacheDirectory, $wgFileCacheDepth;
+
+                       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();
-                       $hash = md5( $key );
+                       if ( $wgFileCacheDepth > 0 ) {
+                               $hash = md5( $key );
+                               for ( $i = 1; $i <= $wgFileCacheDepth; $i++ ) {
+                                       $subdir .= substr( $hash, 0, $i ) . '/';
+                               }
+                       }
                        # Avoid extension confusion
                        $key = str_replace( '.', '%2E', urlencode( $key ) );
-       
-                       $hash1 = substr( $hash, 0, 1 );
-                       $hash2 = substr( $hash, 0, 2 );
-                       $this->mFileCache = "{$wgFileCacheDirectory}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
+                       $this->mFileCache = "{$dir}/{$subdir}{$key}.html";
 
-                       if( $this->useGzip() )
+                       if( $this->useGzip() ) {
                                $this->mFileCache .= '.gz';
+                       }
 
-                       wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
+                       wfDebug( __METHOD__ . ": {$this->mFileCache}\n" );
                }
                return $this->mFileCache;
        }
@@ -96,12 +109,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;
        }
 
@@ -126,15 +138,15 @@ class HTMLFileCache {
 
        /* Working directory to/from output */
        public function loadFromFileCache() {
-               global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
-               wfDebug(" loadFromFileCache()\n");
+               global $wgOut, $wgMimeType, $wgOutputEncoding, $wgLanguageCode;
+               wfDebug( __METHOD__ . "()\n");
                $filename = $this->fileCacheName();
                // 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" );
+                       header( "Content-Language: $wgLanguageCode" );
                }
 
                if( $this->useGzip() ) {
@@ -166,7 +178,7 @@ class HTMLFileCache {
                        return $text;
                }
 
-               wfDebug(" saveToFileCache()\n", false);
+               wfDebug( __METHOD__ . "()\n", false);
 
                $this->checkCacheDirs();
 
@@ -201,11 +213,21 @@ class HTMLFileCache {
 
        public static function clearFileCache( $title ) {
                global $wgUseFileCache;
-               if( !$wgUseFileCache ) return false;
+
+               if ( !$wgUseFileCache ) {
+                       return false;
+               }
+
+               wfSuppressWarnings();
+
                $fc = new self( $title, 'view' );
-               @unlink( $fc->fileCacheName() );
+               unlink( $fc->fileCacheName() );
+
                $fc = new self( $title, 'raw' );
-               @unlink( $fc->fileCacheName() );
+               unlink( $fc->fileCacheName() );
+
+               wfRestoreWarnings();
+
                return true;
        }
 }