(bug 17420) Send the correct content type from action=raw when the HTML file cache...
[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, also from DatabaseFunctions.php for the
11 * emergency abort/fallback to cache.
12 *
13 * Global options that affect this module:
14 * - $wgCachePages
15 * - $wgCacheEpoch
16 * - $wgUseFileCache
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 $wgFileCacheDirectory, $wgRequest;
34 # Store raw pages (like CSS hits) elsewhere
35 $subdir = ($this->mType === 'raw') ? 'raw/' : '';
36 $key = $this->mTitle->getPrefixedDbkey();
37 $hash = md5( $key );
38 # Avoid extension confusion
39 $key = str_replace( '.', '%2E', urlencode( $key ) );
40
41 $hash1 = substr( $hash, 0, 1 );
42 $hash2 = substr( $hash, 0, 2 );
43 $this->mFileCache = "{$wgFileCacheDirectory}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
44
45 if( $this->useGzip() )
46 $this->mFileCache .= '.gz';
47
48 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
49 }
50 return $this->mFileCache;
51 }
52
53 public function isFileCached() {
54 if( $this->mType === false ) return false;
55 return file_exists( $this->fileCacheName() );
56 }
57
58 public function fileCacheTime() {
59 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
60 }
61
62 /**
63 * Check if pages can be cached for this request/user
64 * @return bool
65 */
66 public static function useFileCache() {
67 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
68 if( !$wgUseFileCache ) return false;
69 // Get all query values
70 $queryVals = $wgRequest->getValues();
71 foreach( $queryVals as $query => $val ) {
72 if( $query == 'title' || $query == 'curid' ) continue;
73 // Normal page view in query form can have action=view.
74 // Raw hits for pages also stored, like .css pages for example.
75 else if( $query == 'action' && ($val == 'view' || $val == 'raw') ) continue;
76 else if( $query == 'usemsgcache' && $val == 'yes' ) continue;
77 // Below are header setting params
78 else if( $query == 'maxage' || $query == 'smaxage' || $query == 'ctype' || $query == 'gen' )
79 continue;
80 else
81 return false;
82 }
83 // Check for non-standard user language; this covers uselang,
84 // and extensions for auto-detecting user language.
85 $ulang = $wgLang->getCode();
86 $clang = $wgContLang->getCode();
87 // Check that there are no other sources of variation
88 return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
89 }
90
91 /*
92 * Check if up to date cache file exists
93 * @param $timestamp string
94 */
95 public function isFileCacheGood( $timestamp = '' ) {
96 global $wgCacheEpoch;
97
98 if( !$this->isFileCached() ) return false;
99 if( !$timestamp ) return true; // should be invalidated on change
100
101 $cachetime = $this->fileCacheTime();
102 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
103
104 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
105 return $good;
106 }
107
108 public function useGzip() {
109 global $wgUseGzip;
110 return $wgUseGzip;
111 }
112
113 /* In handy string packages */
114 public function fetchRawText() {
115 return file_get_contents( $this->fileCacheName() );
116 }
117
118 public function fetchPageText() {
119 if( $this->useGzip() ) {
120 /* Why is there no gzfile_get_contents() or gzdecode()? */
121 return implode( '', gzfile( $this->fileCacheName() ) );
122 } else {
123 return $this->fetchRawText();
124 }
125 }
126
127 /* Working directory to/from output */
128 public function loadFromFileCache() {
129 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
130 wfDebug(" loadFromFileCache()\n");
131
132 $filename = $this->fileCacheName();
133 // Raw pages should handle cache control on their own,
134 // even when using file cache. This reduces hits from clients.
135 if( $this->mType !== 'raw' ) {
136 $wgOut->sendCacheControl();
137 header( "Content-Type: $wgMimeType; charset={$wgOutputEncoding}" );
138 header( "Content-Language: $wgContLanguageCode" );
139 }
140
141 if( $this->useGzip() ) {
142 if( wfClientAcceptsGzip() ) {
143 header( 'Content-Encoding: gzip' );
144 } else {
145 /* Send uncompressed */
146 readgzfile( $filename );
147 return;
148 }
149 }
150 readfile( $filename );
151 }
152
153 protected function checkCacheDirs() {
154 $filename = $this->fileCacheName();
155 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
156 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
157
158 wfMkdirParents( $mydir1 );
159 wfMkdirParents( $mydir2 );
160 }
161
162 public function saveToFileCache( $origtext ) {
163 global $wgUseFileCache;
164 if( !$wgUseFileCache ) {
165 return $origtext; // return to output
166 }
167 $text = $origtext;
168 if( strcmp($text,'') == 0 ) return '';
169
170 wfDebug(" saveToFileCache()\n", false);
171
172 $this->checkCacheDirs();
173
174 $f = fopen( $this->fileCacheName(), 'w' );
175 if($f) {
176 $now = wfTimestampNow();
177 if( $this->useGzip() ) {
178 $rawtext = str_replace( '</html>',
179 '<!-- Cached/compressed '.$now." -->\n</html>",
180 $text );
181 $text = gzencode( $rawtext );
182 } else {
183 $text = str_replace( '</html>',
184 '<!-- Cached '.$now." -->\n</html>",
185 $text );
186 }
187 fwrite( $f, $text );
188 fclose( $f );
189 if( $this->useGzip() ) {
190 if( wfClientAcceptsGzip() ) {
191 header( 'Content-Encoding: gzip' );
192 return $text;
193 } else {
194 return $rawtext;
195 }
196 } else {
197 return $text;
198 }
199 }
200 return $text;
201 }
202
203 public static function clearFileCache( $title ) {
204 global $wgUseFileCache;
205 if( !$wgUseFileCache ) return false;
206 $fc = new self( $title, 'view' );
207 @unlink( $fc->fileCacheName() );
208 $fc = new self( $title, 'raw' );
209 @unlink( $fc->fileCacheName() );
210 return true;
211 }
212 }