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