split prefs-help-userdata to prefs-help-realname & prefs-help-email. Nds still need...
[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 var $mTitle, $mFileCache;
28
29 function CacheManager( &$title ) {
30 $this->mTitle =& $title;
31 $this->mFileCache = '';
32 }
33
34 function fileCacheName() {
35 global $wgFileCacheDirectory, $wgContLang;
36 if( !$this->mFileCache ) {
37 $hash = md5( $key = $this->mTitle->getDbkey() );
38 if( $this->mTitle->getNamespace() )
39 $key = $wgContLang->getNsText( $this->mTitle->getNamespace() ) . ":" . $key;
40 $key = str_replace( '.', '%2E', urlencode( $key ) );
41
42 $hash1 = substr( $hash, 0, 1 );
43 $hash2 = substr( $hash, 0, 2 );
44 $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
45
46 if($this->useGzip())
47 $this->mFileCache .= '.gz';
48
49 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
50 }
51 return $this->mFileCache;
52 }
53
54 function isFileCached() {
55 return file_exists( $this->fileCacheName() );
56 }
57
58 function fileCacheTime() {
59 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
60 }
61
62 function isFileCacheGood( $timestamp ) {
63 global $wgCacheEpoch;
64
65 if( !$this->isFileCached() ) return false;
66
67 $cachetime = $this->fileCacheTime();
68 $good = (( $timestamp <= $cachetime ) &&
69 ( $wgCacheEpoch <= $cachetime ));
70
71 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
72 return $good;
73 }
74
75 function useGzip() {
76 global $wgUseGzip;
77 return $wgUseGzip;
78 }
79
80 /* In handy string packages */
81 function fetchRawText() {
82 return file_get_contents( $this->fileCacheName() );
83 }
84
85 function fetchPageText() {
86 if( $this->useGzip() ) {
87 /* Why is there no gzfile_get_contents() or gzdecode()? */
88 return implode( '', gzfile( $this->fileCacheName() ) );
89 } else {
90 return $this->fetchRawText();
91 }
92 }
93
94 /* Working directory to/from output */
95 function loadFromFileCache() {
96 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
97 wfDebug(" loadFromFileCache()\n");
98
99 $filename=$this->fileCacheName();
100 $wgOut->sendCacheControl();
101
102 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
103 header( "Content-language: $wgContLanguageCode" );
104
105 if( $this->useGzip() ) {
106 if( wfClientAcceptsGzip() ) {
107 header( 'Content-Encoding: gzip' );
108 } else {
109 /* Send uncompressed */
110 readgzfile( $filename );
111 return;
112 }
113 }
114 readfile( $filename );
115 }
116
117 function checkCacheDirs() {
118 $filename = $this->fileCacheName();
119 $mydir2=substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
120 $mydir1=substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
121
122 if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
123 if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
124 }
125
126 function saveToFileCache( $origtext ) {
127 $text = $origtext;
128 if(strcmp($text,'') == 0) return '';
129
130 wfDebug(" saveToFileCache()\n", false);
131
132 $this->checkCacheDirs();
133
134 $f = fopen( $this->fileCacheName(), 'w' );
135 if($f) {
136 $now = wfTimestampNow();
137 if( $this->useGzip() ) {
138 $rawtext = str_replace( '</html>',
139 '<!-- Cached/compressed '.$now." -->\n</html>",
140 $text );
141 $text = gzencode( $rawtext );
142 } else {
143 $text = str_replace( '</html>',
144 '<!-- Cached '.$now." -->\n</html>",
145 $text );
146 }
147 fwrite( $f, $text );
148 fclose( $f );
149 if( $this->useGzip() ) {
150 if( wfClientAcceptsGzip() ) {
151 header( 'Content-Encoding: gzip' );
152 return $text;
153 } else {
154 return $rawtext;
155 }
156 } else {
157 return $text;
158 }
159 }
160 return $text;
161 }
162
163 }
164
165 ?>