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