Code style tweaks
[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 = $this->fileCacheName();
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 && $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 wfMkdirParents( $mydir1 );
117 wfMkdirParents( $mydir2 );
118 }
119
120 function saveToFileCache( $origtext ) {
121 global $wgUseFileCache;
122 if( !$wgUseFileCache ) {
123 return $origtext; // return to output
124 }
125 $text = $origtext;
126 if( strcmp($text,'') == 0 ) return '';
127
128 wfDebug(" saveToFileCache()\n", false);
129
130 $this->checkCacheDirs();
131
132 $f = fopen( $this->fileCacheName(), 'w' );
133 if($f) {
134 $now = wfTimestampNow();
135 if( $this->useGzip() ) {
136 $rawtext = str_replace( '</html>',
137 '<!-- Cached/compressed '.$now." -->\n</html>",
138 $text );
139 $text = gzencode( $rawtext );
140 } else {
141 $text = str_replace( '</html>',
142 '<!-- Cached '.$now." -->\n</html>",
143 $text );
144 }
145 fwrite( $f, $text );
146 fclose( $f );
147 if( $this->useGzip() ) {
148 if( wfClientAcceptsGzip() ) {
149 header( 'Content-Encoding: gzip' );
150 return $text;
151 } else {
152 return $rawtext;
153 }
154 } else {
155 return $text;
156 }
157 }
158 return $text;
159 }
160
161 }