Follow-up r84814: revert redundant summary message addition.
[lhc/web/wiklou.git] / includes / 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 ) return false;
73 return file_exists( $this->fileCacheName() );
74 }
75
76 public function fileCacheTime() {
77 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
78 }
79
80 /**
81 * Check if pages can be cached for this request/user
82 * @return bool
83 */
84 public static function useFileCache() {
85 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
86 if( !$wgUseFileCache ) return false;
87 // Get all query values
88 $queryVals = $wgRequest->getValues();
89 foreach( $queryVals as $query => $val ) {
90 if( $query == 'title' || $query == 'curid' ) continue;
91 // Normal page view in query form can have action=view.
92 // Raw hits for pages also stored, like .css pages for example.
93 else if( $query == 'action' && ($val == 'view' || $val == 'raw') ) continue;
94 else if( $query == 'usemsgcache' && $val == 'yes' ) continue;
95 // Below are header setting params
96 else if( $query == 'maxage' || $query == 'smaxage' || $query == 'ctype' || $query == 'gen' )
97 continue;
98 else
99 return false;
100 }
101 // Check for non-standard user language; this covers uselang,
102 // and extensions for auto-detecting user language.
103 $ulang = $wgLang->getCode();
104 $clang = $wgContLang->getCode();
105 // Check that there are no other sources of variation
106 return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
107 }
108
109 /*
110 * Check if up to date cache file exists
111 * @param $timestamp string
112 */
113 public function isFileCacheGood( $timestamp = '' ) {
114 global $wgCacheEpoch;
115
116 if( !$this->isFileCached() ) return false;
117
118 $cachetime = $this->fileCacheTime();
119 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
120
121 wfDebug( __METHOD__ . ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
122 return $good;
123 }
124
125 public function useGzip() {
126 global $wgUseGzip;
127 return $wgUseGzip;
128 }
129
130 /* In handy string packages */
131 public function fetchRawText() {
132 return file_get_contents( $this->fileCacheName() );
133 }
134
135 public function fetchPageText() {
136 if( $this->useGzip() ) {
137 /* Why is there no gzfile_get_contents() or gzdecode()? */
138 return implode( '', gzfile( $this->fileCacheName() ) );
139 } else {
140 return $this->fetchRawText();
141 }
142 }
143
144 /* Working directory to/from output */
145 public function loadFromFileCache() {
146 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgLanguageCode;
147 wfDebug( __METHOD__ . "()\n");
148 $filename = $this->fileCacheName();
149 // Raw pages should handle cache control on their own,
150 // even when using file cache. This reduces hits from clients.
151 if( $this->mType !== 'raw' ) {
152 $wgOut->sendCacheControl();
153 header( "Content-Type: $wgMimeType; charset={$wgOutputEncoding}" );
154 header( "Content-Language: $wgLanguageCode" );
155 }
156
157 if( $this->useGzip() ) {
158 if( wfClientAcceptsGzip() ) {
159 header( 'Content-Encoding: gzip' );
160 } else {
161 /* Send uncompressed */
162 readgzfile( $filename );
163 return;
164 }
165 }
166 readfile( $filename );
167 $wgOut->disable(); // tell $wgOut that output is taken care of
168 }
169
170 protected function checkCacheDirs() {
171 $filename = $this->fileCacheName();
172 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
173 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
174
175 wfMkdirParents( $mydir1 );
176 wfMkdirParents( $mydir2 );
177 }
178
179 public function saveToFileCache( $text ) {
180 global $wgUseFileCache;
181 if( !$wgUseFileCache || strlen( $text ) < 512 ) {
182 // Disabled or empty/broken output (OOM and PHP errors)
183 return $text;
184 }
185
186 wfDebug( __METHOD__ . "()\n", false);
187
188 $this->checkCacheDirs();
189
190 $f = fopen( $this->fileCacheName(), 'w' );
191 if($f) {
192 $now = wfTimestampNow();
193 if( $this->useGzip() ) {
194 $rawtext = str_replace( '</html>',
195 '<!-- Cached/compressed '.$now." -->\n</html>",
196 $text );
197 $text = gzencode( $rawtext );
198 } else {
199 $text = str_replace( '</html>',
200 '<!-- Cached '.$now." -->\n</html>",
201 $text );
202 }
203 fwrite( $f, $text );
204 fclose( $f );
205 if( $this->useGzip() ) {
206 if( wfClientAcceptsGzip() ) {
207 header( 'Content-Encoding: gzip' );
208 return $text;
209 } else {
210 return $rawtext;
211 }
212 } else {
213 return $text;
214 }
215 }
216 return $text;
217 }
218
219 public static function clearFileCache( $title ) {
220 global $wgUseFileCache;
221
222 if ( !$wgUseFileCache ) {
223 return false;
224 }
225
226 wfSuppressWarnings();
227
228 $fc = new self( $title, 'view' );
229 unlink( $fc->fileCacheName() );
230
231 $fc = new self( $title, 'raw' );
232 unlink( $fc->fileCacheName() );
233
234 wfRestoreWarnings();
235
236 return true;
237 }
238 }