Fixed missing quotes in r46840
[mediawiki.git] / includes / HTMLFileCache.php
blobab3c964540e14dabbc57d4b7dc0c3c477e3bdd42
1 <?php
2 /**
3 * Contain the HTMLFileCache class
4 * @file
5 * @ingroup Cache
6 */
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.
13 * Global options that affect this module:
14 * - $wgCachePages
15 * - $wgCacheEpoch
16 * - $wgUseFileCache
17 * - $wgFileCacheDirectory
18 * - $wgUseGzip
20 * @ingroup Cache
22 class HTMLFileCache {
23 var $mTitle, $mFileCache, $mType;
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
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 ) );
41 $hash1 = substr( $hash, 0, 1 );
42 $hash2 = substr( $hash, 0, 2 );
43 $this->mFileCache = "{$wgFileCacheDirectory}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
45 if( $this->useGzip() )
46 $this->mFileCache .= '.gz';
48 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
50 return $this->mFileCache;
53 public function isFileCached() {
54 if( $this->mType === false ) return false;
55 return file_exists( $this->fileCacheName() );
58 public function fileCacheTime() {
59 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
62 /**
63 * Check if pages can be cached for this request/user
64 * @return bool
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;
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;
91 /*
92 * Check if up to date cache file exists
93 * @param $timestamp string
95 public function isFileCacheGood( $timestamp = '' ) {
96 global $wgCacheEpoch;
98 if( !$this->isFileCached() ) return false;
99 if( !$timestamp ) return true; // should be invalidated on change
101 $cachetime = $this->fileCacheTime();
102 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
104 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
105 return $good;
108 public function useGzip() {
109 global $wgUseGzip;
110 return $wgUseGzip;
113 /* In handy string packages */
114 public function fetchRawText() {
115 return file_get_contents( $this->fileCacheName() );
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();
127 /* Working directory to/from output */
128 public function loadFromFileCache() {
129 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
130 wfDebug(" loadFromFileCache()\n");
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();
138 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
139 header( "Content-language: $wgContLanguageCode" );
141 if( $this->useGzip() ) {
142 if( wfClientAcceptsGzip() ) {
143 header( 'Content-Encoding: gzip' );
144 } else {
145 /* Send uncompressed */
146 readgzfile( $filename );
147 return;
150 readfile( $filename );
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
158 wfMkdirParents( $mydir1 );
159 wfMkdirParents( $mydir2 );
162 public function saveToFileCache( $origtext ) {
163 global $wgUseFileCache;
164 if( !$wgUseFileCache ) {
165 return $origtext; // return to output
167 $text = $origtext;
168 if( strcmp($text,'') == 0 ) return '';
170 wfDebug(" saveToFileCache()\n", false);
172 $this->checkCacheDirs();
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 );
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;
196 } else {
197 return $text;
200 return $text;
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;