Move file cache functions into CacheManager class (in CacheManager.php)
[lhc/web/wiklou.git] / includes / CacheManager.php
1 <?
2
3 include_once( "Title.php" );
4
5 class CacheManager {
6 var $mTitle, $mFileCache;
7
8 function CacheManager( &$title ) {
9 $this->mTitle =& $title;
10 $this->mFileCache = "";
11 }
12
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 ) );
20
21 $hash1 = substr( $hash, 0, 1 );
22 $hash2 = substr( $hash, 0, 2 );
23 $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
24
25 if($this->useGzip())
26 $this->mFileCache .= ".gz";
27
28 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
29 }
30 return $this->mFileCache;
31 }
32
33 function isFileCached() {
34 return file_exists( $this->fileCacheName() );
35 }
36
37 function fileCacheTime() {
38 return wfUnix2Timestamp( filemtime( $this->fileCacheName() ) );
39 }
40
41 function isFileCacheGood( $timestamp ) {
42 global $wgUser, $wgCacheEpoch;
43
44 if( !$this->isFileCached() ) return false;
45
46 $cachetime = $this->fileCacheTime();
47 $good = (( $timestamp <= $cachetime ) &&
48 ( $wgCacheEpoch <= $cachetime ));
49
50 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$this->mTouched} epoch {$wgCacheEpoch}, good $good\n");
51 return $good;
52 }
53
54 function useGzip() {
55 global $wgUseGzip;
56 return $wgUseGzip;
57 }
58
59 /* In handy string packages */
60 function fetchRawText() {
61 return file_get_contents( $this->fileCacheName() );
62 }
63
64 function fetchPageText() {
65 if( $this->useGzip() ) {
66 /* Why is there no gzfile_get_contents() or gzdecode()? */
67 return implode( "", gzfile( $this->fileCacheName() ) );
68 } else {
69 return $this->fetchRawText();
70 }
71 }
72
73 /* Working directory to/from output */
74 function loadFromFileCache() {
75 global $wgOut;
76 wfDebug(" loadFromFileCache()\n");
77
78 $filename=$this->fileCacheName();
79 $wgOut->sendCacheControl();
80
81 if( $this->useGzip() ) {
82 if( wfClientAcceptsGzip() ) {
83 header( "Content-Encoding: gzip" );
84 readfile( $filename );
85 } else {
86 /* Send uncompressed */
87 readgzfile( $filename );
88 }
89 } else {
90 readfile( $filename );
91 }
92 }
93
94 function checkCacheDirs() {
95 $filename = $this->fileCacheName();
96 $mydir2=substr($filename,0,strrpos($filename,"/")); # subdirectory level 2
97 $mydir1=substr($mydir2,0,strrpos($mydir2,"/")); # subdirectory level 1
98
99 if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
100 if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
101 }
102
103 function saveToFileCache( $text ) {
104 if(strcmp($text,"") == 0) return "";
105
106 wfDebug(" saveToFileCache()\n", false);
107
108 $this->checkCacheDirs();
109
110 $f = fopen( $this->fileCacheName(), "w" );
111 if($f) {
112 $now = wfTimestampNow();
113 if( $this->useGzip() ) {
114 $rawtext = str_replace( "</html>",
115 "<!-- Cached/compressed $now -->\n</html>",
116 $text );
117 $text = gzencode( $rawtext );
118 } else {
119 $text = str_replace( "</html>",
120 "<!-- Cached $now -->\n</html>",
121 $text );
122 }
123 fwrite( $f, $text );
124 fclose( $f );
125 if( $this->useGzip() ) {
126 if( wfClientAcceptsGzip() ) {
127 header( "Content-Encoding: gzip" );
128 header( "Vary: Accept-Encoding" );
129 return $text;
130 } else {
131 return $rawtext;
132 }
133 } else {
134 return $text;
135 }
136 }
137 return $text;
138 }
139
140 }
141
142 ?>