Removed action=raw support from HTMLFileCache. Obsolete due to RL.
[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 == '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 other views of aspects of pages elsewhere
49 $subdir = ($this->mType === 'view') ? '' : "{$this->mType}/";
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; // 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' ) {
99 continue;
100 // Below are header setting params
101 } elseif( $query == 'maxage' || $query == 'smaxage' ) {
102 continue;
103 } else {
104 return false;
105 }
106 }
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;
113 }
114
115 /**
116 * Check if up to date cache file exists
117 * @param $timestamp string
118 *
119 * @return bool
120 */
121 public function isFileCacheGood( $timestamp = '' ) {
122 global $wgCacheEpoch;
123
124 if( !$this->isFileCached() ) {
125 return false;
126 }
127
128 $cachetime = $this->fileCacheTime();
129 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
130
131 wfDebug( __METHOD__ . ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
132 return $good;
133 }
134
135 public function useGzip() {
136 global $wgUseGzip;
137 return $wgUseGzip;
138 }
139
140 /* In handy string packages */
141 public function fetchRawText() {
142 return file_get_contents( $this->fileCacheName() );
143 }
144
145 public function fetchPageText() {
146 if( $this->useGzip() ) {
147 /* Why is there no gzfile_get_contents() or gzdecode()? */
148 return implode( '', gzfile( $this->fileCacheName() ) );
149 } else {
150 return $this->fetchRawText();
151 }
152 }
153
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' );
165 } else {
166 /* Send uncompressed */
167 readgzfile( $filename );
168 return;
169 }
170 }
171 readfile( $filename );
172 $wgOut->disable(); // tell $wgOut that output is taken care of
173 }
174
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
179
180 wfMkdirParents( $mydir1, null, __METHOD__ );
181 wfMkdirParents( $mydir2, null, __METHOD__ );
182 }
183
184 public function saveToFileCache( $text ) {
185 global $wgUseFileCache;
186 if( !$wgUseFileCache || strlen( $text ) < 512 ) {
187 // Disabled or empty/broken output (OOM and PHP errors)
188 return $text;
189 }
190
191 wfDebug( __METHOD__ . "()\n", false);
192
193 $this->checkCacheDirs();
194
195 $f = fopen( $this->fileCacheName(), 'w' );
196 if($f) {
197 $now = wfTimestampNow();
198 if( $this->useGzip() ) {
199 $rawtext = str_replace( '</html>',
200 '<!-- Cached/compressed '.$now." -->\n</html>",
201 $text );
202 $text = gzencode( $rawtext );
203 } else {
204 $text = str_replace( '</html>',
205 '<!-- Cached '.$now." -->\n</html>",
206 $text );
207 }
208 fwrite( $f, $text );
209 fclose( $f );
210 if( $this->useGzip() ) {
211 if( wfClientAcceptsGzip() ) {
212 header( 'Content-Encoding: gzip' );
213 return $text;
214 } else {
215 return $rawtext;
216 }
217 } else {
218 return $text;
219 }
220 }
221 return $text;
222 }
223
224 public static function clearFileCache( $title ) {
225 global $wgUseFileCache;
226
227 if ( !$wgUseFileCache ) {
228 return false;
229 }
230
231 wfSuppressWarnings();
232
233 $fc = new self( $title, 'view' );
234 unlink( $fc->fileCacheName() );
235
236 wfRestoreWarnings();
237
238 return true;
239 }
240 }