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