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