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 for the emergency abort/fallback to cache.
12 * Global options that affect this module:
17 * - $wgFileCacheDirectory
23 var $mTitle, $mFileCache, $mType;
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
31 public function fileCacheName() {
32 if( !$this->mFileCache
) {
33 global $wgCacheDirectory, $wgFileCacheDirectory;
35 if ( $wgFileCacheDirectory ) {
36 $dir = $wgFileCacheDirectory;
37 } elseif ( $wgCacheDirectory ) {
38 $dir = "$wgCacheDirectory/html";
40 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
43 # Store raw pages (like CSS hits) elsewhere
44 $subdir = ($this->mType
=== 'raw') ?
'raw/' : '';
45 $key = $this->mTitle
->getPrefixedDbkey();
47 # Avoid extension confusion
48 $key = str_replace( '.', '%2E', urlencode( $key ) );
50 $hash1 = substr( $hash, 0, 1 );
51 $hash2 = substr( $hash, 0, 2 );
52 $this->mFileCache
= "{$dir}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
54 if( $this->useGzip() ) {
55 $this->mFileCache
.= '.gz';
58 wfDebug( __METHOD__
. ": {$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;
110 $cachetime = $this->fileCacheTime();
111 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
113 wfDebug( __METHOD__
. ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
117 public function useGzip() {
122 /* In handy string packages */
123 public function fetchRawText() {
124 return file_get_contents( $this->fileCacheName() );
127 public function fetchPageText() {
128 if( $this->useGzip() ) {
129 /* Why is there no gzfile_get_contents() or gzdecode()? */
130 return implode( '', gzfile( $this->fileCacheName() ) );
132 return $this->fetchRawText();
136 /* Working directory to/from output */
137 public function loadFromFileCache() {
138 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgLanguageCode;
139 wfDebug( __METHOD__
. "()\n");
140 $filename = $this->fileCacheName();
141 // Raw pages should handle cache control on their own,
142 // even when using file cache. This reduces hits from clients.
143 if( $this->mType
!== 'raw' ) {
144 $wgOut->sendCacheControl();
145 header( "Content-Type: $wgMimeType; charset={$wgOutputEncoding}" );
146 header( "Content-Language: $wgLanguageCode" );
149 if( $this->useGzip() ) {
150 if( wfClientAcceptsGzip() ) {
151 header( 'Content-Encoding: gzip' );
153 /* Send uncompressed */
154 readgzfile( $filename );
158 readfile( $filename );
159 $wgOut->disable(); // tell $wgOut that output is taken care of
162 protected function checkCacheDirs() {
163 $filename = $this->fileCacheName();
164 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
165 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
167 wfMkdirParents( $mydir1 );
168 wfMkdirParents( $mydir2 );
171 public function saveToFileCache( $text ) {
172 global $wgUseFileCache;
173 if( !$wgUseFileCache ||
strlen( $text ) < 512 ) {
174 // Disabled or empty/broken output (OOM and PHP errors)
178 wfDebug( __METHOD__
. "()\n", false);
180 $this->checkCacheDirs();
182 $f = fopen( $this->fileCacheName(), 'w' );
184 $now = wfTimestampNow();
185 if( $this->useGzip() ) {
186 $rawtext = str_replace( '</html>',
187 '<!-- Cached/compressed '.$now." -->\n</html>",
189 $text = gzencode( $rawtext );
191 $text = str_replace( '</html>',
192 '<!-- Cached '.$now." -->\n</html>",
197 if( $this->useGzip() ) {
198 if( wfClientAcceptsGzip() ) {
199 header( 'Content-Encoding: gzip' );
211 public static function clearFileCache( $title ) {
212 global $wgUseFileCache;
214 if ( !$wgUseFileCache ) {
218 wfSuppressWarnings();
220 $fc = new self( $title, 'view' );
221 unlink( $fc->fileCacheName() );
223 $fc = new self( $title, 'raw' );
224 unlink( $fc->fileCacheName() );