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