FileCache cleanuo:
[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 $wgOut->sendCacheControl();
134
135 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
136 header( "Content-language: $wgContLanguageCode" );
137
138 if( $this->useGzip() ) {
139 if( wfClientAcceptsGzip() ) {
140 header( 'Content-Encoding: gzip' );
141 } else {
142 /* Send uncompressed */
143 readgzfile( $filename );
144 return;
145 }
146 }
147 readfile( $filename );
148 }
149
150 protected function checkCacheDirs() {
151 $filename = $this->fileCacheName();
152 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
153 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
154
155 wfMkdirParents( $mydir1 );
156 wfMkdirParents( $mydir2 );
157 }
158
159 public function saveToFileCache( $origtext ) {
160 global $wgUseFileCache;
161 if( !$wgUseFileCache ) {
162 return $origtext; // return to output
163 }
164 $text = $origtext;
165 if( strcmp($text,'') == 0 ) return '';
166
167 wfDebug(" saveToFileCache()\n", false);
168
169 $this->checkCacheDirs();
170
171 $f = fopen( $this->fileCacheName(), 'w' );
172 if($f) {
173 $now = wfTimestampNow();
174 if( $this->useGzip() ) {
175 $rawtext = str_replace( '</html>',
176 '<!-- Cached/compressed '.$now." -->\n</html>",
177 $text );
178 $text = gzencode( $rawtext );
179 } else {
180 $text = str_replace( '</html>',
181 '<!-- Cached '.$now." -->\n</html>",
182 $text );
183 }
184 fwrite( $f, $text );
185 fclose( $f );
186 if( $this->useGzip() ) {
187 if( wfClientAcceptsGzip() ) {
188 header( 'Content-Encoding: gzip' );
189 return $text;
190 } else {
191 return $rawtext;
192 }
193 } else {
194 return $text;
195 }
196 }
197 return $text;
198 }
199
200 public static function clearFileCache( $title ) {
201 global $wgUseFileCache;
202 if( !$wgUseFileCache ) return false;
203 $fc = new self( $title, '' );
204 @unlink( $fc->fileCacheName() );
205 $fc = new self( $title, 'raw' );
206 @unlink( $fc->fileCacheName() );
207 return true;
208 }
209 }