3 * Contain the HTMLFileCache class
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:
18 * - $wgFileCacheDirectory
24 var $mTitle, $mFileCache, $mType;
26 public function __construct( &$title, $type = 'view' ) {
27 $this->mTitle
= $title;
28 $this->mType
= ($type == 'raw' ||
$type == 'view' ) ?
$type : false;
29 $this->fileCacheName(); // init name
32 public function fileCacheName() {
33 if( !$this->mFileCache
) {
34 global $wgCacheDirectory, $wgFileCacheDirectory, $wgRequest;
36 if ( $wgFileCacheDirectory ) {
37 $dir = $wgFileCacheDirectory;
38 } elseif ( $wgCacheDirectory ) {
39 $dir = "$wgCacheDirectory/html";
41 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
44 # Store raw pages (like CSS hits) elsewhere
45 $subdir = ($this->mType
=== 'raw') ?
'raw/' : '';
46 $key = $this->mTitle
->getPrefixedDbkey();
48 # Avoid extension confusion
49 $key = str_replace( '.', '%2E', urlencode( $key ) );
51 $hash1 = substr( $hash, 0, 1 );
52 $hash2 = substr( $hash, 0, 2 );
53 $this->mFileCache
= "{$wgFileCacheDirectory}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
55 if( $this->useGzip() )
56 $this->mFileCache
.= '.gz';
58 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
60 return $this->mFileCache
;
63 public function isFileCached() {
64 if( $this->mType
=== false ) return false;
65 return file_exists( $this->fileCacheName() );
68 public function fileCacheTime() {
69 return wfTimestamp( TS_MW
, filemtime( $this->fileCacheName() ) );
73 * Check if pages can be cached for this request/user
76 public static function useFileCache() {
77 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
78 if( !$wgUseFileCache ) return false;
79 // Get all query values
80 $queryVals = $wgRequest->getValues();
81 foreach( $queryVals as $query => $val ) {
82 if( $query == 'title' ||
$query == 'curid' ) continue;
83 // Normal page view in query form can have action=view.
84 // Raw hits for pages also stored, like .css pages for example.
85 else if( $query == 'action' && ($val == 'view' ||
$val == 'raw') ) continue;
86 else if( $query == 'usemsgcache' && $val == 'yes' ) continue;
87 // Below are header setting params
88 else if( $query == 'maxage' ||
$query == 'smaxage' ||
$query == 'ctype' ||
$query == 'gen' )
93 // Check for non-standard user language; this covers uselang,
94 // and extensions for auto-detecting user language.
95 $ulang = $wgLang->getCode();
96 $clang = $wgContLang->getCode();
97 // Check that there are no other sources of variation
98 return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
102 * Check if up to date cache file exists
103 * @param $timestamp string
105 public function isFileCacheGood( $timestamp = '' ) {
106 global $wgCacheEpoch;
108 if( !$this->isFileCached() ) return false;
109 if( !$timestamp ) return true; // should be invalidated on change
111 $cachetime = $this->fileCacheTime();
112 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
114 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
118 public function useGzip() {
123 /* In handy string packages */
124 public function fetchRawText() {
125 return file_get_contents( $this->fileCacheName() );
128 public function fetchPageText() {
129 if( $this->useGzip() ) {
130 /* Why is there no gzfile_get_contents() or gzdecode()? */
131 return implode( '', gzfile( $this->fileCacheName() ) );
133 return $this->fetchRawText();
137 /* Working directory to/from output */
138 public function loadFromFileCache() {
139 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
140 wfDebug(" loadFromFileCache()\n");
141 $filename = $this->fileCacheName();
142 // Raw pages should handle cache control on their own,
143 // even when using file cache. This reduces hits from clients.
144 if( $this->mType
!== 'raw' ) {
145 $wgOut->sendCacheControl();
146 header( "Content-Type: $wgMimeType; charset={$wgOutputEncoding}" );
147 header( "Content-Language: $wgContLanguageCode" );
150 if( $this->useGzip() ) {
151 if( wfClientAcceptsGzip() ) {
152 header( 'Content-Encoding: gzip' );
154 /* Send uncompressed */
155 readgzfile( $filename );
159 readfile( $filename );
160 $wgOut->disable(); // tell $wgOut that output is taken care of
163 protected function checkCacheDirs() {
164 $filename = $this->fileCacheName();
165 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
166 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
168 wfMkdirParents( $mydir1 );
169 wfMkdirParents( $mydir2 );
172 public function saveToFileCache( $text ) {
173 global $wgUseFileCache;
174 if( !$wgUseFileCache ||
strlen( $text ) < 512 ) {
175 // Disabled or empty/broken output (OOM and PHP errors)
179 wfDebug(" saveToFileCache()\n", false);
181 $this->checkCacheDirs();
183 $f = fopen( $this->fileCacheName(), 'w' );
185 $now = wfTimestampNow();
186 if( $this->useGzip() ) {
187 $rawtext = str_replace( '</html>',
188 '<!-- Cached/compressed '.$now." -->\n</html>",
190 $text = gzencode( $rawtext );
192 $text = str_replace( '</html>',
193 '<!-- Cached '.$now." -->\n</html>",
198 if( $this->useGzip() ) {
199 if( wfClientAcceptsGzip() ) {
200 header( 'Content-Encoding: gzip' );
212 public static function clearFileCache( $title ) {
213 global $wgUseFileCache;
214 if( !$wgUseFileCache ) return false;
215 $fc = new self( $title, 'view' );
216 @unlink
( $fc->fileCacheName() );
217 $fc = new self( $title, 'raw' );
218 @unlink
( $fc->fileCacheName() );