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 function HTMLFileCache( &$title ) {
26 $this->mTitle
= $title;
27 $this->mFileCache
= $this->fileCacheName();
30 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 function isFileCached() {
50 return file_exists( $this->fileCacheName() );
53 function fileCacheTime() {
54 return wfTimestamp( TS_MW
, filemtime( $this->fileCacheName() ) );
57 function isFileCacheGood( $timestamp ) {
60 if( !$this->isFileCached() ) return false;
62 $cachetime = $this->fileCacheTime();
63 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
65 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
74 /* In handy string packages */
75 function fetchRawText() {
76 return file_get_contents( $this->fileCacheName() );
79 function fetchPageText() {
80 if( $this->useGzip() ) {
81 /* Why is there no gzfile_get_contents() or gzdecode()? */
82 return implode( '', gzfile( $this->fileCacheName() ) );
84 return $this->fetchRawText();
88 /* Working directory to/from output */
89 function loadFromFileCache() {
90 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
91 wfDebug(" loadFromFileCache()\n");
93 $filename = $this->fileCacheName();
94 $wgOut->sendCacheControl();
96 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
97 header( "Content-language: $wgContLanguageCode" );
99 if( $this->useGzip() ) {
100 if( wfClientAcceptsGzip() ) {
101 header( 'Content-Encoding: gzip' );
103 /* Send uncompressed */
104 readgzfile( $filename );
108 readfile( $filename );
111 function checkCacheDirs() {
112 $filename = $this->fileCacheName();
113 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
114 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
116 wfMkdirParents( $mydir1 );
117 wfMkdirParents( $mydir2 );
120 function saveToFileCache( $origtext ) {
121 global $wgUseFileCache;
122 if( !$wgUseFileCache ) {
123 return $origtext; // return to output
126 if( strcmp($text,'') == 0 ) return '';
128 wfDebug(" saveToFileCache()\n", false);
130 $this->checkCacheDirs();
132 $f = fopen( $this->fileCacheName(), 'w' );
134 $now = wfTimestampNow();
135 if( $this->useGzip() ) {
136 $rawtext = str_replace( '</html>',
137 '<!-- Cached/compressed '.$now." -->\n</html>",
139 $text = gzencode( $rawtext );
141 $text = str_replace( '</html>',
142 '<!-- Cached '.$now." -->\n</html>",
147 if( $this->useGzip() ) {
148 if( wfClientAcceptsGzip() ) {
149 header( 'Content-Encoding: gzip' );