(Bug 19725) Do not include suppressed edits in the "View X deleted edits" message...
[lhc/web/wiklou.git] / includes / cache / HTMLFileCache.php
1 <?php
2 /**
3 * Contain the HTMLFileCache class
4 * @file
5 * @ingroup Cache
6 */
7
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.
11 *
12 * Global options that affect this module:
13 * - $wgCachePages
14 * - $wgCacheEpoch
15 * - $wgUseFileCache
16 * - $wgCacheDirectory
17 * - $wgFileCacheDirectory
18 * - $wgUseGzip
19 *
20 * @ingroup Cache
21 */
22 class HTMLFileCache {
23
24 /**
25 * @var Title
26 */
27 var $mTitle;
28 var $mFileCache, $mType;
29
30 public function __construct( $title, $type = 'view' ) {
31 $this->mTitle = $title;
32 $this->mType = ($type == 'raw' || $type == 'view' ) ? $type : false;
33 $this->fileCacheName(); // init name
34 }
35
36 public function fileCacheName() {
37 if( !$this->mFileCache ) {
38 global $wgCacheDirectory, $wgFileCacheDirectory, $wgFileCacheDepth;
39
40 if ( $wgFileCacheDirectory ) {
41 $dir = $wgFileCacheDirectory;
42 } elseif ( $wgCacheDirectory ) {
43 $dir = "$wgCacheDirectory/html";
44 } else {
45 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
46 }
47
48 # Store raw pages (like CSS hits) elsewhere
49 $subdir = ($this->mType === 'raw') ? 'raw/' : '';
50
51 $key = $this->mTitle->getPrefixedDbkey();
52 if ( $wgFileCacheDepth > 0 ) {
53 $hash = md5( $key );
54 for ( $i = 1; $i <= $wgFileCacheDepth; $i++ ) {
55 $subdir .= substr( $hash, 0, $i ) . '/';
56 }
57 }
58 # Avoid extension confusion
59 $key = str_replace( '.', '%2E', urlencode( $key ) );
60 $this->mFileCache = "{$dir}/{$subdir}{$key}.html";
61
62 if( $this->useGzip() ) {
63 $this->mFileCache .= '.gz';
64 }
65
66 wfDebug( __METHOD__ . ": {$this->mFileCache}\n" );
67 }
68 return $this->mFileCache;
69 }
70
71 public function isFileCached() {
72 if( $this->mType === false ) {
73 return false;
74 }
75 return file_exists( $this->fileCacheName() );
76 }
77
78 public function fileCacheTime() {
79 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
80 }
81
82 /**
83 * Check if pages can be cached for this request/user
84 * @return bool
85 */
86 public static function useFileCache() {
87 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
88 if( !$wgUseFileCache ) {
89 return false;
90 }
91 // Get all query values
92 $queryVals = $wgRequest->getValues();
93 foreach( $queryVals as $query => $val ) {
94 if( $query == 'title' || $query == 'curid' ) {
95 continue;
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' ) {
99 continue;
100 } elseif( $query == 'usemsgcache' && $val == 'yes' ) {
101 continue;
102 // Below are header setting params
103 } elseif( $query == 'maxage' || $query == 'smaxage' || $query == 'ctype' || $query == 'gen' ) {
104 continue;
105 } else {
106 return false;
107 }
108 }
109 // Check for non-standard user language; this covers uselang,
110 // and extensions for auto-detecting user language.
111 $ulang = $wgLang->getCode();
112 $clang = $wgContLang->getCode();
113 // Check that there are no other sources of variation
114 return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
115 }
116
117 /**
118 * Check if up to date cache file exists
119 * @param $timestamp string
120 *
121 * @return bool
122 */
123 public function isFileCacheGood( $timestamp = '' ) {
124 global $wgCacheEpoch;
125
126 if( !$this->isFileCached() ) {
127 return false;
128 }
129
130 $cachetime = $this->fileCacheTime();
131 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
132
133 wfDebug( __METHOD__ . ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
134 return $good;
135 }
136
137 public function useGzip() {
138 global $wgUseGzip;
139 return $wgUseGzip;
140 }
141
142 /* In handy string packages */
143 public function fetchRawText() {
144 return file_get_contents( $this->fileCacheName() );
145 }
146
147 public function fetchPageText() {
148 if( $this->useGzip() ) {
149 /* Why is there no gzfile_get_contents() or gzdecode()? */
150 return implode( '', gzfile( $this->fileCacheName() ) );
151 } else {
152 return $this->fetchRawText();
153 }
154 }
155
156 /* Working directory to/from output */
157 public function loadFromFileCache() {
158 global $wgOut, $wgMimeType, $wgLanguageCode;
159 wfDebug( __METHOD__ . "()\n");
160 $filename = $this->fileCacheName();
161 // Raw pages should handle cache control on their own,
162 // even when using file cache. This reduces hits from clients.
163 if( $this->mType !== 'raw' ) {
164 $wgOut->sendCacheControl();
165 header( "Content-Type: $wgMimeType; charset=UTF-8" );
166 header( "Content-Language: $wgLanguageCode" );
167 }
168
169 if( $this->useGzip() ) {
170 if( wfClientAcceptsGzip() ) {
171 header( 'Content-Encoding: gzip' );
172 } else {
173 /* Send uncompressed */
174 readgzfile( $filename );
175 return;
176 }
177 }
178 readfile( $filename );
179 $wgOut->disable(); // tell $wgOut that output is taken care of
180 }
181
182 protected function checkCacheDirs() {
183 $filename = $this->fileCacheName();
184 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
185 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
186
187 wfMkdirParents( $mydir1 );
188 wfMkdirParents( $mydir2 );
189 }
190
191 public function saveToFileCache( $text ) {
192 global $wgUseFileCache;
193 if( !$wgUseFileCache || strlen( $text ) < 512 ) {
194 // Disabled or empty/broken output (OOM and PHP errors)
195 return $text;
196 }
197
198 wfDebug( __METHOD__ . "()\n", false);
199
200 $this->checkCacheDirs();
201
202 $f = fopen( $this->fileCacheName(), 'w' );
203 if($f) {
204 $now = wfTimestampNow();
205 if( $this->useGzip() ) {
206 $rawtext = str_replace( '</html>',
207 '<!-- Cached/compressed '.$now." -->\n</html>",
208 $text );
209 $text = gzencode( $rawtext );
210 } else {
211 $text = str_replace( '</html>',
212 '<!-- Cached '.$now." -->\n</html>",
213 $text );
214 }
215 fwrite( $f, $text );
216 fclose( $f );
217 if( $this->useGzip() ) {
218 if( wfClientAcceptsGzip() ) {
219 header( 'Content-Encoding: gzip' );
220 return $text;
221 } else {
222 return $rawtext;
223 }
224 } else {
225 return $text;
226 }
227 }
228 return $text;
229 }
230
231 public static function clearFileCache( $title ) {
232 global $wgUseFileCache;
233
234 if ( !$wgUseFileCache ) {
235 return false;
236 }
237
238 wfSuppressWarnings();
239
240 $fc = new self( $title, 'view' );
241 unlink( $fc->fileCacheName() );
242
243 $fc = new self( $title, 'raw' );
244 unlink( $fc->fileCacheName() );
245
246 wfRestoreWarnings();
247
248 return true;
249 }
250 }