Big attack on unused variables...
[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 var $mTitle, $mFileCache, $mType;
24
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
29 }
30
31 public function fileCacheName() {
32 if( !$this->mFileCache ) {
33 global $wgCacheDirectory, $wgFileCacheDirectory;
34
35 if ( !$wgFileCacheDirectory && !$wgCacheDirectory ) {
36 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
37 }
38
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 ) );
45
46 $hash1 = substr( $hash, 0, 1 );
47 $hash2 = substr( $hash, 0, 2 );
48 $this->mFileCache = "{$wgFileCacheDirectory}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
49
50 if( $this->useGzip() ) {
51 $this->mFileCache .= '.gz';
52 }
53
54 wfDebug( __METHOD__ . ": {$this->mFileCache}\n" );
55 }
56 return $this->mFileCache;
57 }
58
59 public function isFileCached() {
60 if( $this->mType === false ) return false;
61 return file_exists( $this->fileCacheName() );
62 }
63
64 public function fileCacheTime() {
65 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
66 }
67
68 /**
69 * Check if pages can be cached for this request/user
70 * @return bool
71 */
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;
88 }
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;
95 }
96
97 /*
98 * Check if up to date cache file exists
99 * @param $timestamp string
100 */
101 public function isFileCacheGood( $timestamp = '' ) {
102 global $wgCacheEpoch;
103
104 if( !$this->isFileCached() ) return false;
105
106 $cachetime = $this->fileCacheTime();
107 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
108
109 wfDebug( __METHOD__ . ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
110 return $good;
111 }
112
113 public function useGzip() {
114 global $wgUseGzip;
115 return $wgUseGzip;
116 }
117
118 /* In handy string packages */
119 public function fetchRawText() {
120 return file_get_contents( $this->fileCacheName() );
121 }
122
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();
129 }
130 }
131
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" );
143 }
144
145 if( $this->useGzip() ) {
146 if( wfClientAcceptsGzip() ) {
147 header( 'Content-Encoding: gzip' );
148 } else {
149 /* Send uncompressed */
150 readgzfile( $filename );
151 return;
152 }
153 }
154 readfile( $filename );
155 $wgOut->disable(); // tell $wgOut that output is taken care of
156 }
157
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
162
163 wfMkdirParents( $mydir1 );
164 wfMkdirParents( $mydir2 );
165 }
166
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;
172 }
173
174 wfDebug( __METHOD__ . "()\n", false);
175
176 $this->checkCacheDirs();
177
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 );
190 }
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;
199 }
200 } else {
201 return $text;
202 }
203 }
204 return $text;
205 }
206
207 public static function clearFileCache( $title ) {
208 global $wgUseFileCache;
209
210 if ( !$wgUseFileCache ) {
211 return false;
212 }
213
214 wfSuppressWarnings();
215
216 $fc = new self( $title, 'view' );
217 unlink( $fc->fileCacheName() );
218
219 $fc = new self( $title, 'raw' );
220 unlink( $fc->fileCacheName() );
221
222 wfRestoreWarnings();
223
224 return true;
225 }
226 }