* Remove unneeded code
[lhc/web/wiklou.git] / includes / HTMLFileCache.php
1 <?php
2 /**
3 * Contain the HTMLFileCache class
4 * @file
5 * @ingroup 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 *
20 * @ingroup Cache
21 */
22 class HTMLFileCache {
23 var $mTitle, $mFileCache;
24
25 function HTMLFileCache( &$title ) {
26 $this->mTitle =& $title;
27 $this->mFileCache = '';
28 }
29
30 function fileCacheName() {
31 global $wgFileCacheDirectory;
32 if( !$this->mFileCache ) {
33 $key = $this->mTitle->getPrefixedDbkey();
34 $hash = md5( $key );
35 $key = str_replace( '.', '%2E', urlencode( $key ) );
36
37 $hash1 = substr( $hash, 0, 1 );
38 $hash2 = substr( $hash, 0, 2 );
39 $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
40
41 if($this->useGzip())
42 $this->mFileCache .= '.gz';
43
44 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
45 }
46 return $this->mFileCache;
47 }
48
49 function isFileCached() {
50 return file_exists( $this->fileCacheName() );
51 }
52
53 function fileCacheTime() {
54 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
55 }
56
57 function isFileCacheGood( $timestamp ) {
58 global $wgCacheEpoch;
59
60 if( !$this->isFileCached() ) return false;
61
62 $cachetime = $this->fileCacheTime();
63 $good = (( $timestamp <= $cachetime ) &&
64 ( $wgCacheEpoch <= $cachetime ));
65
66 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
67 return $good;
68 }
69
70 function useGzip() {
71 global $wgUseGzip;
72 return $wgUseGzip;
73 }
74
75 /* In handy string packages */
76 function fetchRawText() {
77 return file_get_contents( $this->fileCacheName() );
78 }
79
80 function fetchPageText() {
81 if( $this->useGzip() ) {
82 /* Why is there no gzfile_get_contents() or gzdecode()? */
83 return implode( '', gzfile( $this->fileCacheName() ) );
84 } else {
85 return $this->fetchRawText();
86 }
87 }
88
89 /* Working directory to/from output */
90 function loadFromFileCache() {
91 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
92 wfDebug(" loadFromFileCache()\n");
93
94 $filename=$this->fileCacheName();
95 $wgOut->sendCacheControl();
96
97 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
98 header( "Content-language: $wgContLanguageCode" );
99
100 if( $this->useGzip() ) {
101 if( wfClientAcceptsGzip() ) {
102 header( 'Content-Encoding: gzip' );
103 } else {
104 /* Send uncompressed */
105 readgzfile( $filename );
106 return;
107 }
108 }
109 readfile( $filename );
110 }
111
112 function checkCacheDirs() {
113 $filename = $this->fileCacheName();
114 $mydir2=substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
115 $mydir1=substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
116
117 wfMkdirParents( $mydir1 );
118 wfMkdirParents( $mydir2 );
119 }
120
121 function saveToFileCache( $origtext ) {
122 $text = $origtext;
123 if(strcmp($text,'') == 0) return '';
124
125 wfDebug(" saveToFileCache()\n", false);
126
127 $this->checkCacheDirs();
128
129 $f = fopen( $this->fileCacheName(), 'w' );
130 if($f) {
131 $now = wfTimestampNow();
132 if( $this->useGzip() ) {
133 $rawtext = str_replace( '</html>',
134 '<!-- Cached/compressed '.$now." -->\n</html>",
135 $text );
136 $text = gzencode( $rawtext );
137 } else {
138 $text = str_replace( '</html>',
139 '<!-- Cached '.$now." -->\n</html>",
140 $text );
141 }
142 fwrite( $f, $text );
143 fclose( $f );
144 if( $this->useGzip() ) {
145 if( wfClientAcceptsGzip() ) {
146 header( 'Content-Encoding: gzip' );
147 return $text;
148 } else {
149 return $rawtext;
150 }
151 } else {
152 return $text;
153 }
154 }
155 return $text;
156 }
157
158 }