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