Slight cache tweaks; fixes gzip encoding header for newly recached pages
[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 $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 {$timestamp} 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 } else {
85 /* Send uncompressed */
86 readgzfile( $filename );
87 return;
88 }
89 }
90 readfile( $filename );
91 }
92
93 function checkCacheDirs() {
94 $filename = $this->fileCacheName();
95 $mydir2=substr($filename,0,strrpos($filename,"/")); # subdirectory level 2
96 $mydir1=substr($mydir2,0,strrpos($mydir2,"/")); # subdirectory level 1
97
98 if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
99 if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
100 }
101
102 function saveToFileCache( $text ) {
103 if(strcmp($text,"") == 0) return "";
104
105 wfDebug(" saveToFileCache()\n", false);
106
107 $this->checkCacheDirs();
108
109 $f = fopen( $this->fileCacheName(), "w" );
110 if($f) {
111 $now = wfTimestampNow();
112 if( $this->useGzip() ) {
113 $rawtext = str_replace( "</html>",
114 "<!-- Cached/compressed $now -->\n</html>",
115 $text );
116 $text = gzencode( $rawtext );
117 } else {
118 $text = str_replace( "</html>",
119 "<!-- Cached $now -->\n</html>",
120 $text );
121 }
122 fwrite( $f, $text );
123 fclose( $f );
124 if( $this->useGzip() ) {
125 if( wfClientAcceptsGzip() ) {
126 header( "Content-Encoding: gzip" );
127 return $text;
128 } else {
129 return $rawtext;
130 }
131 } else {
132 return $text;
133 }
134 }
135 return $text;
136 }
137
138 }
139
140 ?>