00001 <?php
00023 class HTMLFileCache {
00024 var $mTitle, $mFileCache, $mType;
00025
00026 public function __construct( &$title, $type = 'view' ) {
00027 $this->mTitle = $title;
00028 $this->mType = ($type == 'raw' || $type == 'view' ) ? $type : false;
00029 $this->fileCacheName();
00030 }
00031
00032 public function fileCacheName() {
00033 if( !$this->mFileCache ) {
00034 global $wgCacheDirectory, $wgFileCacheDirectory, $wgRequest;
00035
00036 if ( $wgFileCacheDirectory ) {
00037 $dir = $wgFileCacheDirectory;
00038 } elseif ( $wgCacheDirectory ) {
00039 $dir = "$wgCacheDirectory/html";
00040 } else {
00041 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
00042 }
00043
00044 # Store raw pages (like CSS hits) elsewhere
00045 $subdir = ($this->mType === 'raw') ? 'raw/' : '';
00046 $key = $this->mTitle->getPrefixedDbkey();
00047 $hash = md5( $key );
00048 # Avoid extension confusion
00049 $key = str_replace( '.', '%2E', urlencode( $key ) );
00050
00051 $hash1 = substr( $hash, 0, 1 );
00052 $hash2 = substr( $hash, 0, 2 );
00053 $this->mFileCache = "{$wgFileCacheDirectory}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
00054
00055 if( $this->useGzip() )
00056 $this->mFileCache .= '.gz';
00057
00058 wfDebug( __METHOD__ . ": {$this->mFileCache}\n" );
00059 }
00060 return $this->mFileCache;
00061 }
00062
00063 public function isFileCached() {
00064 if( $this->mType === false ) return false;
00065 return file_exists( $this->fileCacheName() );
00066 }
00067
00068 public function fileCacheTime() {
00069 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
00070 }
00071
00076 public static function useFileCache() {
00077 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
00078 if( !$wgUseFileCache ) return false;
00079
00080 $queryVals = $wgRequest->getValues();
00081 foreach( $queryVals as $query => $val ) {
00082 if( $query == 'title' || $query == 'curid' ) continue;
00083
00084
00085 else if( $query == 'action' && ($val == 'view' || $val == 'raw') ) continue;
00086 else if( $query == 'usemsgcache' && $val == 'yes' ) continue;
00087
00088 else if( $query == 'maxage' || $query == 'smaxage' || $query == 'ctype' || $query == 'gen' )
00089 continue;
00090 else
00091 return false;
00092 }
00093
00094
00095 $ulang = $wgLang->getCode();
00096 $clang = $wgContLang->getCode();
00097
00098 return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
00099 }
00100
00101
00102
00103
00104
00105 public function isFileCacheGood( $timestamp = '' ) {
00106 global $wgCacheEpoch;
00107
00108 if( !$this->isFileCached() ) return false;
00109
00110 $cachetime = $this->fileCacheTime();
00111 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
00112
00113 wfDebug( __METHOD__ . ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
00114 return $good;
00115 }
00116
00117 public function useGzip() {
00118 global $wgUseGzip;
00119 return $wgUseGzip;
00120 }
00121
00122
00123 public function fetchRawText() {
00124 return file_get_contents( $this->fileCacheName() );
00125 }
00126
00127 public function fetchPageText() {
00128 if( $this->useGzip() ) {
00129
00130 return implode( '', gzfile( $this->fileCacheName() ) );
00131 } else {
00132 return $this->fetchRawText();
00133 }
00134 }
00135
00136
00137 public function loadFromFileCache() {
00138 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
00139 wfDebug( __METHOD__ . "()\n");
00140 $filename = $this->fileCacheName();
00141
00142
00143 if( $this->mType !== 'raw' ) {
00144 $wgOut->sendCacheControl();
00145 header( "Content-Type: $wgMimeType; charset={$wgOutputEncoding}" );
00146 header( "Content-Language: $wgContLanguageCode" );
00147 }
00148
00149 if( $this->useGzip() ) {
00150 if( wfClientAcceptsGzip() ) {
00151 header( 'Content-Encoding: gzip' );
00152 } else {
00153
00154 readgzfile( $filename );
00155 return;
00156 }
00157 }
00158 readfile( $filename );
00159 $wgOut->disable();
00160 }
00161
00162 protected function checkCacheDirs() {
00163 $filename = $this->fileCacheName();
00164 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
00165 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
00166
00167 wfMkdirParents( $mydir1 );
00168 wfMkdirParents( $mydir2 );
00169 }
00170
00171 public function saveToFileCache( $text ) {
00172 global $wgUseFileCache;
00173 if( !$wgUseFileCache || strlen( $text ) < 512 ) {
00174
00175 return $text;
00176 }
00177
00178 wfDebug( __METHOD__ . "()\n", false);
00179
00180 $this->checkCacheDirs();
00181
00182 $f = fopen( $this->fileCacheName(), 'w' );
00183 if($f) {
00184 $now = wfTimestampNow();
00185 if( $this->useGzip() ) {
00186 $rawtext = str_replace( '</html>',
00187 '<!-- Cached/compressed '.$now." -->\n</html>",
00188 $text );
00189 $text = gzencode( $rawtext );
00190 } else {
00191 $text = str_replace( '</html>',
00192 '<!-- Cached '.$now." -->\n</html>",
00193 $text );
00194 }
00195 fwrite( $f, $text );
00196 fclose( $f );
00197 if( $this->useGzip() ) {
00198 if( wfClientAcceptsGzip() ) {
00199 header( 'Content-Encoding: gzip' );
00200 return $text;
00201 } else {
00202 return $rawtext;
00203 }
00204 } else {
00205 return $text;
00206 }
00207 }
00208 return $text;
00209 }
00210
00211 public static function clearFileCache( $title ) {
00212 global $wgUseFileCache;
00213 if( !$wgUseFileCache ) return false;
00214 $fc = new self( $title, 'view' );
00215 @unlink( $fc->fileCacheName() );
00216 $fc = new self( $title, 'raw' );
00217 @unlink( $fc->fileCacheName() );
00218 return true;
00219 }
00220 }