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
= '';
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";
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 ) &&
64 ( $wgCacheEpoch <= $cachetime ));
66 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
75 /* In handy string packages */
76 function fetchRawText() {
77 return file_get_contents( $this->fileCacheName() );
80 function fetchPageText() {
81 if( $this->useGzip() ) {
82 /* Why is there no gzfile_get_contents() or gzdecode()? */
83 return implode( '', gzfile( $this->fileCacheName() ) );
85 return $this->fetchRawText();
89 /* Working directory to/from output */
90 function loadFromFileCache() {
91 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
92 wfDebug(" loadFromFileCache()\n");
94 $filename=$this->fileCacheName();
95 $wgOut->sendCacheControl();
97 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
98 header( "Content-language: $wgContLanguageCode" );
100 if( $this->useGzip() ) {
101 if( wfClientAcceptsGzip() ) {
102 header( 'Content-Encoding: gzip' );
104 /* Send uncompressed */
105 readgzfile( $filename );
109 readfile( $filename );
112 function checkCacheDirs() {
113 $filename = $this->fileCacheName();
114 $mydir2=substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
115 $mydir1=substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
117 wfMkdirParents( $mydir1 );
118 wfMkdirParents( $mydir2 );
121 function saveToFileCache( $origtext ) {
123 if(strcmp($text,'') == 0) return '';
125 wfDebug(" saveToFileCache()\n", false);
127 $this->checkCacheDirs();
129 $f = fopen( $this->fileCacheName(), 'w' );
131 $now = wfTimestampNow();
132 if( $this->useGzip() ) {
133 $rawtext = str_replace( '</html>',
134 '<!-- Cached/compressed '.$now." -->\n</html>",
136 $text = gzencode( $rawtext );
138 $text = str_replace( '</html>',
139 '<!-- Cached '.$now." -->\n</html>",
144 if( $this->useGzip() ) {
145 if( wfClientAcceptsGzip() ) {
146 header( 'Content-Encoding: gzip' );