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, $wgFileCacheDepth;
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/' : '';
46 $key = $this->mTitle
->getPrefixedDbkey();
47 if ( $wgFileCacheDepth > 0 ) {
49 for ( $i = 1; $i <= $wgFileCacheDepth; $i++
) {
50 $subdir .= substr( $hash, 0, $i ) . '/';
53 # Avoid extension confusion
54 $key = str_replace( '.', '%2E', urlencode( $key ) );
55 $this->mFileCache
= "{$dir}/{$subdir}{$key}.html";
57 if( $this->useGzip() ) {
58 $this->mFileCache
.= '.gz';
61 wfDebug( __METHOD__
. ": {$this->mFileCache}\n" );
63 return $this->mFileCache
;
66 public function isFileCached() {
67 if( $this->mType
=== false ) return false;
68 return file_exists( $this->fileCacheName() );
71 public function fileCacheTime() {
72 return wfTimestamp( TS_MW
, filemtime( $this->fileCacheName() ) );
76 * Check if pages can be cached for this request/user
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' )
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;
105 * Check if up to date cache file exists
106 * @param $timestamp string
108 public function isFileCacheGood( $timestamp = '' ) {
109 global $wgCacheEpoch;
111 if( !$this->isFileCached() ) return false;
113 $cachetime = $this->fileCacheTime();
114 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
116 wfDebug( __METHOD__
. ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
120 public function useGzip() {
125 /* In handy string packages */
126 public function fetchRawText() {
127 return file_get_contents( $this->fileCacheName() );
130 public function fetchPageText() {
131 if( $this->useGzip() ) {
132 /* Why is there no gzfile_get_contents() or gzdecode()? */
133 return implode( '', gzfile( $this->fileCacheName() ) );
135 return $this->fetchRawText();
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" );
152 if( $this->useGzip() ) {
153 if( wfClientAcceptsGzip() ) {
154 header( 'Content-Encoding: gzip' );
156 /* Send uncompressed */
157 readgzfile( $filename );
161 readfile( $filename );
162 $wgOut->disable(); // tell $wgOut that output is taken care of
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
170 wfMkdirParents( $mydir1 );
171 wfMkdirParents( $mydir2 );
174 public function saveToFileCache( $text ) {
175 global $wgUseFileCache;
176 if( !$wgUseFileCache ||
strlen( $text ) < 512 ) {
177 // Disabled or empty/broken output (OOM and PHP errors)
181 wfDebug( __METHOD__
. "()\n", false);
183 $this->checkCacheDirs();
185 $f = fopen( $this->fileCacheName(), 'w' );
187 $now = wfTimestampNow();
188 if( $this->useGzip() ) {
189 $rawtext = str_replace( '</html>',
190 '<!-- Cached/compressed '.$now." -->\n</html>",
192 $text = gzencode( $rawtext );
194 $text = str_replace( '</html>',
195 '<!-- Cached '.$now." -->\n</html>",
200 if( $this->useGzip() ) {
201 if( wfClientAcceptsGzip() ) {
202 header( 'Content-Encoding: gzip' );
214 public static function clearFileCache( $title ) {
215 global $wgUseFileCache;
217 if ( !$wgUseFileCache ) {
221 wfSuppressWarnings();
223 $fc = new self( $title, 'view' );
224 unlink( $fc->fileCacheName() );
226 $fc = new self( $title, 'raw' );
227 unlink( $fc->fileCacheName() );