3 * Contain the CacheManager class
7 * We need the title class
9 require_once( 'Title.php' );
12 * Handles talking to the file cache, putting stuff in and taking it back out.
13 * Mostly called from Article.php, also from DatabaseFunctions.php for the
14 * emergency abort/fallback to cache.
16 * Global options that affect this module:
20 * $wgFileCacheDirectory
24 var $mTitle, $mFileCache;
26 function CacheManager( &$title ) {
27 $this->mTitle
=& $title;
28 $this->mFileCache
= '';
31 function fileCacheName() {
32 global $wgFileCacheDirectory, $wgLang;
33 if( !$this->mFileCache
) {
34 $hash = md5( $key = $this->mTitle
->getDbkey() );
35 if( $this->mTitle
->getNamespace() )
36 $key = $wgLang->getNsText( $this->mTitle
->getNamespace() ) . ":" . $key;
37 $key = str_replace( '.', '%2E', urlencode( $key ) );
39 $hash1 = substr( $hash, 0, 1 );
40 $hash2 = substr( $hash, 0, 2 );
41 $this->mFileCache
= "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
44 $this->mFileCache
.= '.gz';
46 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
48 return $this->mFileCache
;
51 function isFileCached() {
52 return file_exists( $this->fileCacheName() );
55 function fileCacheTime() {
56 return wfUnix2Timestamp( filemtime( $this->fileCacheName() ) );
59 function isFileCacheGood( $timestamp ) {
62 if( !$this->isFileCached() ) return false;
64 $cachetime = $this->fileCacheTime();
65 $good = (( $timestamp <= $cachetime ) &&
66 ( $wgCacheEpoch <= $cachetime ));
68 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
77 /* In handy string packages */
78 function fetchRawText() {
79 return file_get_contents( $this->fileCacheName() );
82 function fetchPageText() {
83 if( $this->useGzip() ) {
84 /* Why is there no gzfile_get_contents() or gzdecode()? */
85 return implode( '', gzfile( $this->fileCacheName() ) );
87 return $this->fetchRawText();
91 /* Working directory to/from output */
92 function loadFromFileCache() {
94 wfDebug(" loadFromFileCache()\n");
96 $filename=$this->fileCacheName();
97 $wgOut->sendCacheControl();
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' );