3 * Contain the HTMLFileCache class
9 * Handles talking to the file cache, putting stuff in and taking it back out.
10 * Mostly called from Article.php for the emergency abort/fallback to cache.
12 * Global options that affect this module:
17 * - $wgFileCacheDirectory
28 var $mFileCache, $mType;
30 public function __construct( $title, $type = 'view' ) {
31 $this->mTitle
= $title;
32 $this->mType
= ( $type == 'view' ) ?
$type : false;
33 $this->fileCacheName(); // init name
36 public function fileCacheName() {
37 if( !$this->mFileCache
) {
38 global $wgCacheDirectory, $wgFileCacheDirectory, $wgFileCacheDepth;
40 if ( $wgFileCacheDirectory ) {
41 $dir = $wgFileCacheDirectory;
42 } elseif ( $wgCacheDirectory ) {
43 $dir = "$wgCacheDirectory/html";
45 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
48 # Store other views of aspects of pages elsewhere
49 $subdir = ($this->mType
=== 'view') ?
'' : "{$this->mType}/";
51 $key = $this->mTitle
->getPrefixedDbkey();
52 if ( $wgFileCacheDepth > 0 ) {
54 for ( $i = 1; $i <= $wgFileCacheDepth; $i++
) {
55 $subdir .= substr( $hash, 0, $i ) . '/';
58 # Avoid extension confusion
59 $key = str_replace( '.', '%2E', urlencode( $key ) );
60 $this->mFileCache
= "{$dir}/{$subdir}{$key}.html";
62 if( $this->useGzip() ) {
63 $this->mFileCache
.= '.gz';
66 wfDebug( __METHOD__
. ": {$this->mFileCache}\n" );
68 return $this->mFileCache
;
71 public function isFileCached() {
72 if( $this->mType
=== false ) {
75 return file_exists( $this->fileCacheName() );
78 public function fileCacheTime() {
79 return wfTimestamp( TS_MW
, filemtime( $this->fileCacheName() ) );
83 * Check if pages can be cached for this request/user
86 public static function useFileCache() {
87 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
88 if( !$wgUseFileCache ) {
91 // Get all query values
92 $queryVals = $wgRequest->getValues();
93 foreach( $queryVals as $query => $val ) {
94 if( $query == 'title' ||
$query == 'curid' ) {
95 continue; // note: curid sets title
96 // Normal page view in query form can have action=view.
97 // Raw hits for pages also stored, like .css pages for example.
98 } elseif( $query == 'action' && $val == 'view' ) {
100 // Below are header setting params
101 } elseif( $query == 'maxage' ||
$query == 'smaxage' ) {
107 // Check for non-standard user language; this covers uselang,
108 // and extensions for auto-detecting user language.
109 $ulang = $wgLang->getCode();
110 $clang = $wgContLang->getCode();
111 // Check that there are no other sources of variation
112 return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
116 * Check if up to date cache file exists
117 * @param $timestamp string
121 public function isFileCacheGood( $timestamp = '' ) {
122 global $wgCacheEpoch;
124 if( !$this->isFileCached() ) {
128 $cachetime = $this->fileCacheTime();
129 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
131 wfDebug( __METHOD__
. ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
135 public function useGzip() {
140 /* In handy string packages */
141 public function fetchRawText() {
142 return file_get_contents( $this->fileCacheName() );
145 public function fetchPageText() {
146 if( $this->useGzip() ) {
147 /* Why is there no gzfile_get_contents() or gzdecode()? */
148 return implode( '', gzfile( $this->fileCacheName() ) );
150 return $this->fetchRawText();
154 /* Working directory to/from output */
155 public function loadFromFileCache() {
156 global $wgOut, $wgMimeType, $wgLanguageCode;
157 wfDebug( __METHOD__
. "()\n");
158 $filename = $this->fileCacheName();
159 $wgOut->sendCacheControl();
160 header( "Content-Type: $wgMimeType; charset=UTF-8" );
161 header( "Content-Language: $wgLanguageCode" );
162 if( $this->useGzip() ) {
163 if( wfClientAcceptsGzip() ) {
164 header( 'Content-Encoding: gzip' );
166 /* Send uncompressed */
167 readgzfile( $filename );
171 readfile( $filename );
172 $wgOut->disable(); // tell $wgOut that output is taken care of
175 protected function checkCacheDirs() {
176 $filename = $this->fileCacheName();
177 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
178 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
180 wfMkdirParents( $mydir1, null, __METHOD__
);
181 wfMkdirParents( $mydir2, null, __METHOD__
);
184 public function saveToFileCache( $text ) {
185 global $wgUseFileCache;
186 if( !$wgUseFileCache ||
strlen( $text ) < 512 ) {
187 // Disabled or empty/broken output (OOM and PHP errors)
191 wfDebug( __METHOD__
. "()\n", false);
193 $this->checkCacheDirs();
195 $f = fopen( $this->fileCacheName(), 'w' );
197 $now = wfTimestampNow();
198 if( $this->useGzip() ) {
199 $rawtext = str_replace( '</html>',
200 '<!-- Cached/compressed '.$now." -->\n</html>",
202 $text = gzencode( $rawtext );
204 $text = str_replace( '</html>',
205 '<!-- Cached '.$now." -->\n</html>",
210 if( $this->useGzip() ) {
211 if( wfClientAcceptsGzip() ) {
212 header( 'Content-Encoding: gzip' );
224 public static function clearFileCache( $title ) {
225 global $wgUseFileCache;
227 if ( !$wgUseFileCache ) {
231 wfSuppressWarnings();
233 $fc = new self( $title, 'view' );
234 unlink( $fc->fileCacheName() );