Bug 589: make random selection slightly more random. PHP's
[lhc/web/wiklou.git] / includes / CacheManager.php
1 <?php
2 /**
3 * Contain the CacheManager class
4 * @package MediaWiki
5 */
6
7 /**
8 * We need the title class
9 */
10 require_once( 'Title.php' );
11
12 /**
13 * Handles talking to the file cache, putting stuff in and taking it back out.
14 * Mostly called from Article.php, also from DatabaseFunctions.php for the
15 * emergency abort/fallback to cache.
16 *
17 * Global options that affect this module:
18 * $wgCachePages
19 * $wgCacheEpoch
20 * $wgUseFileCache
21 * $wgFileCacheDirectory
22 * $wgUseGzip
23 * @package MediaWiki
24 */
25 class CacheManager {
26 var $mTitle, $mFileCache;
27
28 function CacheManager( &$title ) {
29 $this->mTitle =& $title;
30 $this->mFileCache = '';
31 }
32
33 function fileCacheName() {
34 global $wgFileCacheDirectory, $wgContLang;
35 if( !$this->mFileCache ) {
36 $hash = md5( $key = $this->mTitle->getDbkey() );
37 if( $this->mTitle->getNamespace() )
38 $key = $wgContLang->getNsText( $this->mTitle->getNamespace() ) . ":" . $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 wfUnix2Timestamp( 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;
96 wfDebug(" loadFromFileCache()\n");
97
98 $filename=$this->fileCacheName();
99 $wgOut->sendCacheControl();
100
101 if( $this->useGzip() ) {
102 if( wfClientAcceptsGzip() ) {
103 header( 'Content-Encoding: gzip' );
104 } else {
105 /* Send uncompressed */
106 readgzfile( $filename );
107 return;
108 }
109 }
110 readfile( $filename );
111 }
112
113 function checkCacheDirs() {
114 $filename = $this->fileCacheName();
115 $mydir2=substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
116 $mydir1=substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
117
118 if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
119 if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
120 }
121
122 function saveToFileCache( $origtext ) {
123 $text = $origtext;
124 if(strcmp($text,'') == 0) return '';
125
126 wfDebug(" saveToFileCache()\n", false);
127
128 $this->checkCacheDirs();
129
130 $f = fopen( $this->fileCacheName(), 'w' );
131 if($f) {
132 $now = wfTimestampNow();
133 if( $this->useGzip() ) {
134 $rawtext = str_replace( '</html>',
135 '<!-- Cached/compressed '.$now." -->\n</html>",
136 $text );
137 $text = gzencode( $rawtext );
138 } else {
139 $text = str_replace( '</html>',
140 '<!-- Cached '.$now." -->\n</html>",
141 $text );
142 }
143 fwrite( $f, $text );
144 fclose( $f );
145 if( $this->useGzip() ) {
146 if( wfClientAcceptsGzip() ) {
147 header( 'Content-Encoding: gzip' );
148 return $text;
149 } else {
150 return $rawtext;
151 }
152 } else {
153 return $text;
154 }
155 }
156 return $text;
157 }
158
159 }
160
161 ?>