Followup r74035, add GAID_FOR_UPDATE to the defines for back-compat
[mediawiki.git] / includes / HTMLFileCache.php
blobedae81b6aa0a810cdafc56449a9d0acf6b768f38
1 <?php
2 /**
3 * Contain the HTMLFileCache class
4 * @file
5 * @ingroup Cache
6 */
8 /**
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:
13 * - $wgCachePages
14 * - $wgCacheEpoch
15 * - $wgUseFileCache
16 * - $wgCacheDirectory
17 * - $wgFileCacheDirectory
18 * - $wgUseGzip
20 * @ingroup Cache
22 class HTMLFileCache {
23 var $mTitle, $mFileCache, $mType;
25 public function __construct( &$title, $type = 'view' ) {
26 $this->mTitle = $title;
27 $this->mType = ($type == 'raw' || $type == 'view' ) ? $type : false;
28 $this->fileCacheName(); // init name
31 public function fileCacheName() {
32 if( !$this->mFileCache ) {
33 global $wgCacheDirectory, $wgFileCacheDirectory;
35 if ( !$wgFileCacheDirectory && !$wgCacheDirectory ) {
36 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
39 # Store raw pages (like CSS hits) elsewhere
40 $subdir = ($this->mType === 'raw') ? 'raw/' : '';
41 $key = $this->mTitle->getPrefixedDbkey();
42 $hash = md5( $key );
43 # Avoid extension confusion
44 $key = str_replace( '.', '%2E', urlencode( $key ) );
46 $hash1 = substr( $hash, 0, 1 );
47 $hash2 = substr( $hash, 0, 2 );
48 $this->mFileCache = "{$wgFileCacheDirectory}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
50 if( $this->useGzip() ) {
51 $this->mFileCache .= '.gz';
54 wfDebug( __METHOD__ . ": {$this->mFileCache}\n" );
56 return $this->mFileCache;
59 public function isFileCached() {
60 if( $this->mType === false ) return false;
61 return file_exists( $this->fileCacheName() );
64 public function fileCacheTime() {
65 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
68 /**
69 * Check if pages can be cached for this request/user
70 * @return bool
72 public static function useFileCache() {
73 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
74 if( !$wgUseFileCache ) return false;
75 // Get all query values
76 $queryVals = $wgRequest->getValues();
77 foreach( $queryVals as $query => $val ) {
78 if( $query == 'title' || $query == 'curid' ) continue;
79 // Normal page view in query form can have action=view.
80 // Raw hits for pages also stored, like .css pages for example.
81 else if( $query == 'action' && ($val == 'view' || $val == 'raw') ) continue;
82 else if( $query == 'usemsgcache' && $val == 'yes' ) continue;
83 // Below are header setting params
84 else if( $query == 'maxage' || $query == 'smaxage' || $query == 'ctype' || $query == 'gen' )
85 continue;
86 else
87 return false;
89 // Check for non-standard user language; this covers uselang,
90 // and extensions for auto-detecting user language.
91 $ulang = $wgLang->getCode();
92 $clang = $wgContLang->getCode();
93 // Check that there are no other sources of variation
94 return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
97 /*
98 * Check if up to date cache file exists
99 * @param $timestamp string
101 public function isFileCacheGood( $timestamp = '' ) {
102 global $wgCacheEpoch;
104 if( !$this->isFileCached() ) return false;
106 $cachetime = $this->fileCacheTime();
107 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
109 wfDebug( __METHOD__ . ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
110 return $good;
113 public function useGzip() {
114 global $wgUseGzip;
115 return $wgUseGzip;
118 /* In handy string packages */
119 public function fetchRawText() {
120 return file_get_contents( $this->fileCacheName() );
123 public function fetchPageText() {
124 if( $this->useGzip() ) {
125 /* Why is there no gzfile_get_contents() or gzdecode()? */
126 return implode( '', gzfile( $this->fileCacheName() ) );
127 } else {
128 return $this->fetchRawText();
132 /* Working directory to/from output */
133 public function loadFromFileCache() {
134 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgLanguageCode;
135 wfDebug( __METHOD__ . "()\n");
136 $filename = $this->fileCacheName();
137 // Raw pages should handle cache control on their own,
138 // even when using file cache. This reduces hits from clients.
139 if( $this->mType !== 'raw' ) {
140 $wgOut->sendCacheControl();
141 header( "Content-Type: $wgMimeType; charset={$wgOutputEncoding}" );
142 header( "Content-Language: $wgLanguageCode" );
145 if( $this->useGzip() ) {
146 if( wfClientAcceptsGzip() ) {
147 header( 'Content-Encoding: gzip' );
148 } else {
149 /* Send uncompressed */
150 readgzfile( $filename );
151 return;
154 readfile( $filename );
155 $wgOut->disable(); // tell $wgOut that output is taken care of
158 protected function checkCacheDirs() {
159 $filename = $this->fileCacheName();
160 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
161 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
163 wfMkdirParents( $mydir1 );
164 wfMkdirParents( $mydir2 );
167 public function saveToFileCache( $text ) {
168 global $wgUseFileCache;
169 if( !$wgUseFileCache || strlen( $text ) < 512 ) {
170 // Disabled or empty/broken output (OOM and PHP errors)
171 return $text;
174 wfDebug( __METHOD__ . "()\n", false);
176 $this->checkCacheDirs();
178 $f = fopen( $this->fileCacheName(), 'w' );
179 if($f) {
180 $now = wfTimestampNow();
181 if( $this->useGzip() ) {
182 $rawtext = str_replace( '</html>',
183 '<!-- Cached/compressed '.$now." -->\n</html>",
184 $text );
185 $text = gzencode( $rawtext );
186 } else {
187 $text = str_replace( '</html>',
188 '<!-- Cached '.$now." -->\n</html>",
189 $text );
191 fwrite( $f, $text );
192 fclose( $f );
193 if( $this->useGzip() ) {
194 if( wfClientAcceptsGzip() ) {
195 header( 'Content-Encoding: gzip' );
196 return $text;
197 } else {
198 return $rawtext;
200 } else {
201 return $text;
204 return $text;
207 public static function clearFileCache( $title ) {
208 global $wgUseFileCache;
210 if ( !$wgUseFileCache ) {
211 return false;
214 wfSuppressWarnings();
216 $fc = new self( $title, 'view' );
217 unlink( $fc->fileCacheName() );
219 $fc = new self( $title, 'raw' );
220 unlink( $fc->fileCacheName() );
222 wfRestoreWarnings();
224 return true;