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