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 && !$wgCacheDirectory ) {
36 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
39 # Store raw pages (like CSS hits) elsewhere
40 $subdir = ($this->mType
=== 'raw') ?
'raw/' : '';
41 $key = $this->mTitle
->getPrefixedDbkey();
43 # Avoid extension confusion
44 $key = str_replace( '.', '%2E', urlencode( $key ) );
46 $hash1 = substr( $hash, 0, 1 );
47 $hash2 = substr( $hash, 0, 2 );
48 $this->mFileCache
= "{$wgFileCacheDirectory}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
50 if( $this->useGzip() ) {
51 $this->mFileCache
.= '.gz';
54 wfDebug( __METHOD__
. ": {$this->mFileCache}\n" );
56 return $this->mFileCache
;
59 public function isFileCached() {
60 if( $this->mType
=== false ) return false;
61 return file_exists( $this->fileCacheName() );
64 public function fileCacheTime() {
65 return wfTimestamp( TS_MW
, filemtime( $this->fileCacheName() ) );
69 * Check if pages can be cached for this request/user
72 public static function useFileCache() {
73 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
74 if( !$wgUseFileCache ) return false;
75 // Get all query values
76 $queryVals = $wgRequest->getValues();
77 foreach( $queryVals as $query => $val ) {
78 if( $query == 'title' ||
$query == 'curid' ) continue;
79 // Normal page view in query form can have action=view.
80 // Raw hits for pages also stored, like .css pages for example.
81 else if( $query == 'action' && ($val == 'view' ||
$val == 'raw') ) continue;
82 else if( $query == 'usemsgcache' && $val == 'yes' ) continue;
83 // Below are header setting params
84 else if( $query == 'maxage' ||
$query == 'smaxage' ||
$query == 'ctype' ||
$query == 'gen' )
89 // Check for non-standard user language; this covers uselang,
90 // and extensions for auto-detecting user language.
91 $ulang = $wgLang->getCode();
92 $clang = $wgContLang->getCode();
93 // Check that there are no other sources of variation
94 return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
98 * Check if up to date cache file exists
99 * @param $timestamp string
101 public function isFileCacheGood( $timestamp = '' ) {
102 global $wgCacheEpoch;
104 if( !$this->isFileCached() ) return false;
106 $cachetime = $this->fileCacheTime();
107 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
109 wfDebug( __METHOD__
. ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
113 public function useGzip() {
118 /* In handy string packages */
119 public function fetchRawText() {
120 return file_get_contents( $this->fileCacheName() );
123 public function fetchPageText() {
124 if( $this->useGzip() ) {
125 /* Why is there no gzfile_get_contents() or gzdecode()? */
126 return implode( '', gzfile( $this->fileCacheName() ) );
128 return $this->fetchRawText();
132 /* Working directory to/from output */
133 public function loadFromFileCache() {
134 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgLanguageCode;
135 wfDebug( __METHOD__
. "()\n");
136 $filename = $this->fileCacheName();
137 // Raw pages should handle cache control on their own,
138 // even when using file cache. This reduces hits from clients.
139 if( $this->mType
!== 'raw' ) {
140 $wgOut->sendCacheControl();
141 header( "Content-Type: $wgMimeType; charset={$wgOutputEncoding}" );
142 header( "Content-Language: $wgLanguageCode" );
145 if( $this->useGzip() ) {
146 if( wfClientAcceptsGzip() ) {
147 header( 'Content-Encoding: gzip' );
149 /* Send uncompressed */
150 readgzfile( $filename );
154 readfile( $filename );
155 $wgOut->disable(); // tell $wgOut that output is taken care of
158 protected function checkCacheDirs() {
159 $filename = $this->fileCacheName();
160 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
161 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
163 wfMkdirParents( $mydir1 );
164 wfMkdirParents( $mydir2 );
167 public function saveToFileCache( $text ) {
168 global $wgUseFileCache;
169 if( !$wgUseFileCache ||
strlen( $text ) < 512 ) {
170 // Disabled or empty/broken output (OOM and PHP errors)
174 wfDebug( __METHOD__
. "()\n", false);
176 $this->checkCacheDirs();
178 $f = fopen( $this->fileCacheName(), 'w' );
180 $now = wfTimestampNow();
181 if( $this->useGzip() ) {
182 $rawtext = str_replace( '</html>',
183 '<!-- Cached/compressed '.$now." -->\n</html>",
185 $text = gzencode( $rawtext );
187 $text = str_replace( '</html>',
188 '<!-- Cached '.$now." -->\n</html>",
193 if( $this->useGzip() ) {
194 if( wfClientAcceptsGzip() ) {
195 header( 'Content-Encoding: gzip' );
207 public static function clearFileCache( $title ) {
208 global $wgUseFileCache;
210 if ( !$wgUseFileCache ) {
214 wfSuppressWarnings();
216 $fc = new self( $title, 'view' );
217 unlink( $fc->fileCacheName() );
219 $fc = new self( $title, 'raw' );
220 unlink( $fc->fileCacheName() );