d85a441106140a339a854cb282b15c1469868d90
[lhc/web/wiklou.git] / includes / HTMLFileCache.php
1 <?php
2 /**
3 * Contain the HTMLFileCache class
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /**
9 * Handles talking to the file cache, putting stuff in and taking it back out.
10 * Mostly called from Article.php, also from DatabaseFunctions.php for the
11 * emergency abort/fallback to cache.
12 *
13 * Global options that affect this module:
14 * $wgCachePages
15 * $wgCacheEpoch
16 * $wgUseFileCache
17 * $wgFileCacheDirectory
18 * $wgUseGzip
19 * @package MediaWiki
20 */
21 class HTMLFileCache {
22 var $mTitle, $mFileCache;
23
24 function HTMLFileCache( &$title ) {
25 $this->mTitle =& $title;
26 $this->mFileCache = '';
27 }
28
29 function fileCacheName() {
30 global $wgFileCacheDirectory;
31 if( !$this->mFileCache ) {
32 $key = $this->mTitle->getPrefixedDbkey();
33 $hash = md5( $key );
34 $key = str_replace( '.', '%2E', urlencode( $key ) );
35
36 $hash1 = substr( $hash, 0, 1 );
37 $hash2 = substr( $hash, 0, 2 );
38 $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
39
40 if($this->useGzip())
41 $this->mFileCache .= '.gz';
42
43 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
44 }
45 return $this->mFileCache;
46 }
47
48 function isFileCached() {
49 return file_exists( $this->fileCacheName() );
50 }
51
52 function fileCacheTime() {
53 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
54 }
55
56 function isFileCacheGood( $timestamp ) {
57 global $wgCacheEpoch;
58
59 if( !$this->isFileCached() ) return false;
60
61 $cachetime = $this->fileCacheTime();
62 $good = (( $timestamp <= $cachetime ) &&
63 ( $wgCacheEpoch <= $cachetime ));
64
65 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
66 return $good;
67 }
68
69 function useGzip() {
70 global $wgUseGzip;
71 return $wgUseGzip;
72 }
73
74 /* In handy string packages */
75 function fetchRawText() {
76 return file_get_contents( $this->fileCacheName() );
77 }
78
79 function fetchPageText() {
80 if( $this->useGzip() ) {
81 /* Why is there no gzfile_get_contents() or gzdecode()? */
82 return implode( '', gzfile( $this->fileCacheName() ) );
83 } else {
84 return $this->fetchRawText();
85 }
86 }
87
88 /* Working directory to/from output */
89 function loadFromFileCache() {
90 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
91 wfDebug(" loadFromFileCache()\n");
92
93 $filename=$this->fileCacheName();
94 $wgOut->sendCacheControl();
95
96 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
97 header( "Content-language: $wgContLanguageCode" );
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 ?>