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 global $wgFileCacheDirectory;
32 if( !$this->mFileCache
) {
33 $key = $this->mTitle
->getPrefixedDbkey();
35 $key = str_replace( '.', '%2E', urlencode( $key ) );
37 $hash1 = substr( $hash, 0, 1 );
38 $hash2 = substr( $hash, 0, 2 );
39 $this->mFileCache
= "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
41 if( $this->useGzip() )
42 $this->mFileCache
.= '.gz';
44 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
46 return $this->mFileCache
;
49 public function isFileCached() {
50 return file_exists( $this->fileCacheName() );
53 public function fileCacheTime() {
54 return wfTimestamp( TS_MW
, filemtime( $this->fileCacheName() ) );
58 * Check if pages can be cached for this request/user
61 public static function useFileCache() {
62 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
63 if( !$wgUseFileCache )
65 // Get all query values
66 $queryVals = $wgRequest->getValues();
67 foreach( $queryVals as $query => $val ) {
68 // Normal page view in query form can have action=view
69 if( $query !== 'title' && $query !== 'curid' && !($query == 'action' && $val == 'view') ) {
73 // Check for non-standard user language; this covers uselang,
74 // and extensions for auto-detecting user language.
75 $ulang = $wgLang->getCode();
76 $clang = $wgContLang->getCode();
78 return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
82 * Check if up to date cache file exists
83 * @param $timestamp string
85 public function isFileCacheGood( $timestamp = '' ) {
88 if( !$this->isFileCached() ) return false;
89 if( !$timestamp ) return true; // should be invalidated on change
91 $cachetime = $this->fileCacheTime();
92 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
94 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
98 public function useGzip() {
103 /* In handy string packages */
104 public function fetchRawText() {
105 return file_get_contents( $this->fileCacheName() );
108 public function fetchPageText() {
109 if( $this->useGzip() ) {
110 /* Why is there no gzfile_get_contents() or gzdecode()? */
111 return implode( '', gzfile( $this->fileCacheName() ) );
113 return $this->fetchRawText();
117 /* Working directory to/from output */
118 public function loadFromFileCache() {
119 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
120 wfDebug(" loadFromFileCache()\n");
122 $filename = $this->fileCacheName();
123 $wgOut->sendCacheControl();
125 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
126 header( "Content-language: $wgContLanguageCode" );
128 if( $this->useGzip() ) {
129 if( wfClientAcceptsGzip() ) {
130 header( 'Content-Encoding: gzip' );
132 /* Send uncompressed */
133 readgzfile( $filename );
137 readfile( $filename );
140 protected function checkCacheDirs() {
141 $filename = $this->fileCacheName();
142 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
143 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
145 wfMkdirParents( $mydir1 );
146 wfMkdirParents( $mydir2 );
149 public function saveToFileCache( $origtext ) {
150 global $wgUseFileCache;
151 if( !$wgUseFileCache ) {
152 return $origtext; // return to output
155 if( strcmp($text,'') == 0 ) return '';
157 wfDebug(" saveToFileCache()\n", false);
159 $this->checkCacheDirs();
161 $f = fopen( $this->fileCacheName(), 'w' );
163 $now = wfTimestampNow();
164 if( $this->useGzip() ) {
165 $rawtext = str_replace( '</html>',
166 '<!-- Cached/compressed '.$now." -->\n</html>",
168 $text = gzencode( $rawtext );
170 $text = str_replace( '</html>',
171 '<!-- Cached '.$now." -->\n</html>",
176 if( $this->useGzip() ) {
177 if( wfClientAcceptsGzip() ) {
178 header( 'Content-Encoding: gzip' );