3 * Contain the CacheManager 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
22 var $mTitle, $mFileCache;
24 function CacheManager( &$title ) {
25 $this->mTitle
=& $title;
26 $this->mFileCache
= '';
29 function fileCacheName() {
30 global $wgFileCacheDirectory;
31 if( !$this->mFileCache
) {
32 $key = $this->mTitle
->getPrefixedDbkey();
34 $key = str_replace( '.', '%2E', urlencode( $key ) );
36 $hash1 = substr( $hash, 0, 1 );
37 $hash2 = substr( $hash, 0, 2 );
38 $this->mFileCache
= "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
41 $this->mFileCache
.= '.gz';
43 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
45 return $this->mFileCache
;
48 function isFileCached() {
49 return file_exists( $this->fileCacheName() );
52 function fileCacheTime() {
53 return wfTimestamp( TS_MW
, filemtime( $this->fileCacheName() ) );
56 function isFileCacheGood( $timestamp ) {
59 if( !$this->isFileCached() ) return false;
61 $cachetime = $this->fileCacheTime();
62 $good = (( $timestamp <= $cachetime ) &&
63 ( $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 if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
117 if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
120 function saveToFileCache( $origtext ) {
122 if(strcmp($text,'') == 0) return '';
124 wfDebug(" saveToFileCache()\n", false);
126 $this->checkCacheDirs();
128 $f = fopen( $this->fileCacheName(), 'w' );
130 $now = wfTimestampNow();
131 if( $this->useGzip() ) {
132 $rawtext = str_replace( '</html>',
133 '<!-- Cached/compressed '.$now." -->\n</html>",
135 $text = gzencode( $rawtext );
137 $text = str_replace( '</html>',
138 '<!-- Cached '.$now." -->\n</html>",
143 if( $this->useGzip() ) {
144 if( wfClientAcceptsGzip() ) {
145 header( 'Content-Encoding: gzip' );