3 include_once( "Title.php" );
6 var $mTitle, $mFileCache;
8 function CacheManager( &$title ) {
9 $this->mTitle
=& $title;
10 $this->mFileCache
= "";
13 function fileCacheName() {
14 global $wgFileCacheDirectory, $wgLang;
15 if( !$this->mFileCache
) {
16 $hash = md5( $key = $this->mTitle
->getDbkey() );
17 if( $this->mTitle
->getNamespace() )
18 $key = $wgLang->getNsText( $this->mTitle
->getNamespace() ) . ":" . $key;
19 $key = str_replace( ".", "%2E", urlencode( $key ) );
21 $hash1 = substr( $hash, 0, 1 );
22 $hash2 = substr( $hash, 0, 2 );
23 $this->mFileCache
= "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
26 $this->mFileCache
.= ".gz";
28 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
30 return $this->mFileCache
;
33 function isFileCached() {
34 return file_exists( $this->fileCacheName() );
37 function fileCacheTime() {
38 return wfUnix2Timestamp( filemtime( $this->fileCacheName() ) );
41 function isFileCacheGood( $timestamp ) {
44 if( !$this->isFileCached() ) return false;
46 $cachetime = $this->fileCacheTime();
47 $good = (( $timestamp <= $cachetime ) &&
48 ( $wgCacheEpoch <= $cachetime ));
50 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
59 /* In handy string packages */
60 function fetchRawText() {
61 return file_get_contents( $this->fileCacheName() );
64 function fetchPageText() {
65 if( $this->useGzip() ) {
66 /* Why is there no gzfile_get_contents() or gzdecode()? */
67 return implode( "", gzfile( $this->fileCacheName() ) );
69 return $this->fetchRawText();
73 /* Working directory to/from output */
74 function loadFromFileCache() {
76 wfDebug(" loadFromFileCache()\n");
78 $filename=$this->fileCacheName();
79 $wgOut->sendCacheControl();
81 if( $this->useGzip() ) {
82 if( wfClientAcceptsGzip() ) {
83 header( "Content-Encoding: gzip" );
85 /* Send uncompressed */
86 readgzfile( $filename );
90 readfile( $filename );
93 function checkCacheDirs() {
94 $filename = $this->fileCacheName();
95 $mydir2=substr($filename,0,strrpos($filename,"/")); # subdirectory level 2
96 $mydir1=substr($mydir2,0,strrpos($mydir2,"/")); # subdirectory level 1
98 if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
99 if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
102 function saveToFileCache( $text ) {
103 if(strcmp($text,"") == 0) return "";
105 wfDebug(" saveToFileCache()\n", false);
107 $this->checkCacheDirs();
109 $f = fopen( $this->fileCacheName(), "w" );
111 $now = wfTimestampNow();
112 if( $this->useGzip() ) {
113 $rawtext = str_replace( "</html>",
114 "<!-- Cached/compressed $now -->\n</html>",
116 $text = gzencode( $rawtext );
118 $text = str_replace( "</html>",
119 "<!-- Cached $now -->\n</html>",
124 if( $this->useGzip() ) {
125 if( wfClientAcceptsGzip() ) {
126 header( "Content-Encoding: gzip" );