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