Replacing var keyword with private / public as we now require PHP5.
[lhc/web/wiklou.git] / includes / CacheManager.php
1 <?php
2 /**
3 * Contain the CacheManager class
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /**
9 * We need the title class
10 */
11 require_once( 'Title.php' );
12
13 /**
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.
17 *
18 * Global options that affect this module:
19 * $wgCachePages
20 * $wgCacheEpoch
21 * $wgUseFileCache
22 * $wgFileCacheDirectory
23 * $wgUseGzip
24 * @package MediaWiki
25 */
26 class CacheManager {
27 private
28 $mTitle,
29 $mFileCache;
30
31 function CacheManager( &$title ) {
32 $this->mTitle =& $title;
33 $this->mFileCache = '';
34 }
35
36 function fileCacheName() {
37 global $wgFileCacheDirectory;
38 if( !$this->mFileCache ) {
39 $key = $this->mTitle->getPrefixedDbkey();
40 $hash = md5( $key );
41 $key = str_replace( '.', '%2E', urlencode( $key ) );
42
43 $hash1 = substr( $hash, 0, 1 );
44 $hash2 = substr( $hash, 0, 2 );
45 $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
46
47 if($this->useGzip())
48 $this->mFileCache .= '.gz';
49
50 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
51 }
52 return $this->mFileCache;
53 }
54
55 function isFileCached() {
56 return file_exists( $this->fileCacheName() );
57 }
58
59 function fileCacheTime() {
60 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
61 }
62
63 function isFileCacheGood( $timestamp ) {
64 global $wgCacheEpoch;
65
66 if( !$this->isFileCached() ) return false;
67
68 $cachetime = $this->fileCacheTime();
69 $good = (( $timestamp <= $cachetime ) &&
70 ( $wgCacheEpoch <= $cachetime ));
71
72 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
73 return $good;
74 }
75
76 function useGzip() {
77 global $wgUseGzip;
78 return $wgUseGzip;
79 }
80
81 /* In handy string packages */
82 function fetchRawText() {
83 return file_get_contents( $this->fileCacheName() );
84 }
85
86 function fetchPageText() {
87 if( $this->useGzip() ) {
88 /* Why is there no gzfile_get_contents() or gzdecode()? */
89 return implode( '', gzfile( $this->fileCacheName() ) );
90 } else {
91 return $this->fetchRawText();
92 }
93 }
94
95 /* Working directory to/from output */
96 function loadFromFileCache() {
97 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
98 wfDebug(" loadFromFileCache()\n");
99
100 $filename=$this->fileCacheName();
101 $wgOut->sendCacheControl();
102
103 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
104 header( "Content-language: $wgContLanguageCode" );
105
106 if( $this->useGzip() ) {
107 if( wfClientAcceptsGzip() ) {
108 header( 'Content-Encoding: gzip' );
109 } else {
110 /* Send uncompressed */
111 readgzfile( $filename );
112 return;
113 }
114 }
115 readfile( $filename );
116 }
117
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
122
123 if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
124 if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
125 }
126
127 function saveToFileCache( $origtext ) {
128 $text = $origtext;
129 if(strcmp($text,'') == 0) return '';
130
131 wfDebug(" saveToFileCache()\n", false);
132
133 $this->checkCacheDirs();
134
135 $f = fopen( $this->fileCacheName(), 'w' );
136 if($f) {
137 $now = wfTimestampNow();
138 if( $this->useGzip() ) {
139 $rawtext = str_replace( '</html>',
140 '<!-- Cached/compressed '.$now." -->\n</html>",
141 $text );
142 $text = gzencode( $rawtext );
143 } else {
144 $text = str_replace( '</html>',
145 '<!-- Cached '.$now." -->\n</html>",
146 $text );
147 }
148 fwrite( $f, $text );
149 fclose( $f );
150 if( $this->useGzip() ) {
151 if( wfClientAcceptsGzip() ) {
152 header( 'Content-Encoding: gzip' );
153 return $text;
154 } else {
155 return $rawtext;
156 }
157 } else {
158 return $text;
159 }
160 }
161 return $text;
162 }
163
164 }
165
166 ?>