3 * Contain the CacheManager class
9 * We need the title class
11 require_once( 'Title.php' );
14 * Handles talking to the file cache, putting stuff in and taking it back out.
15 * Mostly called from Article.php, also from DatabaseFunctions.php for the
16 * emergency abort/fallback to cache.
18 * Global options that affect this module:
22 * $wgFileCacheDirectory
31 function CacheManager( &$title ) {
32 $this->mTitle
=& $title;
33 $this->mFileCache
= '';
36 function fileCacheName() {
37 global $wgFileCacheDirectory;
38 if( !$this->mFileCache
) {
39 $key = $this->mTitle
->getPrefixedDbkey();
41 $key = str_replace( '.', '%2E', urlencode( $key ) );
43 $hash1 = substr( $hash, 0, 1 );
44 $hash2 = substr( $hash, 0, 2 );
45 $this->mFileCache
= "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
48 $this->mFileCache
.= '.gz';
50 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
52 return $this->mFileCache
;
55 function isFileCached() {
56 return file_exists( $this->fileCacheName() );
59 function fileCacheTime() {
60 return wfTimestamp( TS_MW
, filemtime( $this->fileCacheName() ) );
63 function isFileCacheGood( $timestamp ) {
66 if( !$this->isFileCached() ) return false;
68 $cachetime = $this->fileCacheTime();
69 $good = (( $timestamp <= $cachetime ) &&
70 ( $wgCacheEpoch <= $cachetime ));
72 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
81 /* In handy string packages */
82 function fetchRawText() {
83 return file_get_contents( $this->fileCacheName() );
86 function fetchPageText() {
87 if( $this->useGzip() ) {
88 /* Why is there no gzfile_get_contents() or gzdecode()? */
89 return implode( '', gzfile( $this->fileCacheName() ) );
91 return $this->fetchRawText();
95 /* Working directory to/from output */
96 function loadFromFileCache() {
97 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
98 wfDebug(" loadFromFileCache()\n");
100 $filename=$this->fileCacheName();
101 $wgOut->sendCacheControl();
103 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
104 header( "Content-language: $wgContLanguageCode" );
106 if( $this->useGzip() ) {
107 if( wfClientAcceptsGzip() ) {
108 header( 'Content-Encoding: gzip' );
110 /* Send uncompressed */
111 readgzfile( $filename );
115 readfile( $filename );
118 function checkCacheDirs() {
119 $filename = $this->fileCacheName();
120 $mydir2=substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
121 $mydir1=substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
123 if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
124 if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
127 function saveToFileCache( $origtext ) {
129 if(strcmp($text,'') == 0) return '';
131 wfDebug(" saveToFileCache()\n", false);
133 $this->checkCacheDirs();
135 $f = fopen( $this->fileCacheName(), 'w' );
137 $now = wfTimestampNow();
138 if( $this->useGzip() ) {
139 $rawtext = str_replace( '</html>',
140 '<!-- Cached/compressed '.$now." -->\n</html>",
142 $text = gzencode( $rawtext );
144 $text = str_replace( '</html>',
145 '<!-- Cached '.$now." -->\n</html>",
150 if( $this->useGzip() ) {
151 if( wfClientAcceptsGzip() ) {
152 header( 'Content-Encoding: gzip' );