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:
17 * - $wgFileCacheDirectory
23 var $mTitle, $mFileCache;
25 public function __construct( &$title ) {
26 $this->mTitle
= $title;
27 $this->mFileCache
= $this->fileCacheName();
30 public function fileCacheName() {
31 if( !$this->mFileCache
) {
32 global $wgFileCacheDirectory, $wgRequest;
33 $key = $this->mTitle
->getPrefixedDbkey();
35 # Avoid extension confusion
36 $key = str_replace( '.', '%2E', urlencode( $key ) );
37 # Store raw pages (like CSS hits) elsewhere
38 $subdir = $wgRequest->getVal('action') == 'raw' ?
'raw/' : '';
40 $hash1 = substr( $hash, 0, 1 );
41 $hash2 = substr( $hash, 0, 2 );
42 $this->mFileCache
= "{$wgFileCacheDirectory}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
44 if( $this->useGzip() )
45 $this->mFileCache
.= '.gz';
47 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
49 return $this->mFileCache
;
52 public function isFileCached() {
53 return file_exists( $this->fileCacheName() );
56 public function fileCacheTime() {
57 return wfTimestamp( TS_MW
, filemtime( $this->fileCacheName() ) );
61 * Check if pages can be cached for this request/user
64 public static function useFileCache() {
65 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
66 if( !$wgUseFileCache ) return false;
67 // Get all query values
68 $queryVals = $wgRequest->getValues();
69 foreach( $queryVals as $query => $val ) {
70 if( $query == 'title' ||
$query == 'curid' ) continue;
71 // Normal page view in query form can have action=view.
72 // Raw hits for pages also stored, like .css pages for example.
73 if( $query == 'action' && ($val == 'view' ||
$val == 'raw') ) continue;
74 if( $query == 'usemsgcache' && $val == 'yes' ) continue;
75 // Below are header setting params
76 if( $query == 'maxage' ||
$query == 'smaxage' ||
$query == 'ctype' ||
$query == 'gen' )
79 // Check for non-standard user language; this covers uselang,
80 // and extensions for auto-detecting user language.
81 $ulang = $wgLang->getCode();
82 $clang = $wgContLang->getCode();
83 // Check that there are no other sources of variation
84 return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
88 * Check if up to date cache file exists
89 * @param $timestamp string
91 public function isFileCacheGood( $timestamp = '' ) {
94 if( !$this->isFileCached() ) return false;
95 if( !$timestamp ) return true; // should be invalidated on change
97 $cachetime = $this->fileCacheTime();
98 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
100 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
104 public function useGzip() {
109 /* In handy string packages */
110 public function fetchRawText() {
111 return file_get_contents( $this->fileCacheName() );
114 public function fetchPageText() {
115 if( $this->useGzip() ) {
116 /* Why is there no gzfile_get_contents() or gzdecode()? */
117 return implode( '', gzfile( $this->fileCacheName() ) );
119 return $this->fetchRawText();
123 /* Working directory to/from output */
124 public function loadFromFileCache() {
125 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
126 wfDebug(" loadFromFileCache()\n");
128 $filename = $this->fileCacheName();
129 $wgOut->sendCacheControl();
131 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
132 header( "Content-language: $wgContLanguageCode" );
134 if( $this->useGzip() ) {
135 if( wfClientAcceptsGzip() ) {
136 header( 'Content-Encoding: gzip' );
138 /* Send uncompressed */
139 readgzfile( $filename );
143 readfile( $filename );
146 protected function checkCacheDirs() {
147 $filename = $this->fileCacheName();
148 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
149 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
151 wfMkdirParents( $mydir1 );
152 wfMkdirParents( $mydir2 );
155 public function saveToFileCache( $origtext ) {
156 global $wgUseFileCache;
157 if( !$wgUseFileCache ) {
158 return $origtext; // return to output
161 if( strcmp($text,'') == 0 ) return '';
163 wfDebug(" saveToFileCache()\n", false);
165 $this->checkCacheDirs();
167 $f = fopen( $this->fileCacheName(), 'w' );
169 $now = wfTimestampNow();
170 if( $this->useGzip() ) {
171 $rawtext = str_replace( '</html>',
172 '<!-- Cached/compressed '.$now." -->\n</html>",
174 $text = gzencode( $rawtext );
176 $text = str_replace( '</html>',
177 '<!-- Cached '.$now." -->\n</html>",
182 if( $this->useGzip() ) {
183 if( wfClientAcceptsGzip() ) {
184 header( 'Content-Encoding: gzip' );