]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/HTMLFileCache.php
MediaWiki 1.15.0
[autoinstalls/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, 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                 $filename = $this->fileCacheName();
132                 // Raw pages should handle cache control on their own,
133                 // even when using file cache. This reduces hits from clients.
134                 if( $this->mType !== 'raw' ) {
135                         $wgOut->sendCacheControl();
136                         header( "Content-Type: $wgMimeType; charset={$wgOutputEncoding}" );
137                         header( "Content-Language: $wgContLanguageCode" );
138                 }
139
140                 if( $this->useGzip() ) {
141                         if( wfClientAcceptsGzip() ) {
142                                 header( 'Content-Encoding: gzip' );
143                         } else {
144                                 /* Send uncompressed */
145                                 readgzfile( $filename );
146                                 return;
147                         }
148                 }
149                 readfile( $filename );
150                 $wgOut->disable(); // tell $wgOut that output is taken care of
151         }
152
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
157
158                 wfMkdirParents( $mydir1 );
159                 wfMkdirParents( $mydir2 );
160         }
161
162         public function saveToFileCache( $text ) {
163                 global $wgUseFileCache;
164                 if( !$wgUseFileCache || strlen( $text ) < 512 ) {
165                         // Disabled or empty/broken output (OOM and PHP errors)
166                         return $text;
167                 }
168
169                 wfDebug(" saveToFileCache()\n", false);
170
171                 $this->checkCacheDirs();
172
173                 $f = fopen( $this->fileCacheName(), 'w' );
174                 if($f) {
175                         $now = wfTimestampNow();
176                         if( $this->useGzip() ) {
177                                 $rawtext = str_replace( '</html>',
178                                         '<!-- Cached/compressed '.$now." -->\n</html>",
179                                         $text );
180                                 $text = gzencode( $rawtext );
181                         } else {
182                                 $text = str_replace( '</html>',
183                                         '<!-- Cached '.$now." -->\n</html>",
184                                         $text );
185                         }
186                         fwrite( $f, $text );
187                         fclose( $f );
188                         if( $this->useGzip() ) {
189                                 if( wfClientAcceptsGzip() ) {
190                                         header( 'Content-Encoding: gzip' );
191                                         return $text;
192                                 } else {
193                                         return $rawtext;
194                                 }
195                         } else {
196                                 return $text;
197                         }
198                 }
199                 return $text;
200         }
201
202         public static function clearFileCache( $title ) {
203                 global $wgUseFileCache;
204                 if( !$wgUseFileCache ) return false;
205                 $fc = new self( $title, 'view' );
206                 @unlink( $fc->fileCacheName() );
207                 $fc = new self( $title, 'raw' );
208                 @unlink( $fc->fileCacheName() );
209                 return true;
210         }
211 }