Reduce queries for pagesize thingy
[mediawiki.git] / includes / HTMLFileCache.php
blob1ff8e1d6a34f12e02c0b9b5a377fe4ca4c83f00e
1 <?php
2 /**
3 * Contain the HTMLFileCache class
4 * @file
5 * @ingroup Cache
6 */
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.
13 * Global options that affect this module:
14 * - $wgCachePages
15 * - $wgCacheEpoch
16 * - $wgUseFileCache
17 * - $wgFileCacheDirectory
18 * - $wgUseGzip
20 * @ingroup Cache
22 class HTMLFileCache {
23 var $mTitle, $mFileCache;
25 function HTMLFileCache( &$title ) {
26 $this->mTitle = $title;
27 $this->mFileCache = $this->fileCacheName();
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 ) );
37 $hash1 = substr( $hash, 0, 1 );
38 $hash2 = substr( $hash, 0, 2 );
39 $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
41 if( $this->useGzip() )
42 $this->mFileCache .= '.gz';
44 wfDebug( " fileCacheName() - {$this->mFileCache}\n" );
46 return $this->mFileCache;
49 function isFileCached() {
50 return file_exists( $this->fileCacheName() );
53 function fileCacheTime() {
54 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
57 function isFileCacheGood( $timestamp ) {
58 global $wgCacheEpoch;
60 if( !$this->isFileCached() ) return false;
62 $cachetime = $this->fileCacheTime();
63 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
65 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$timestamp} epoch {$wgCacheEpoch}, good $good\n");
66 return $good;
69 function useGzip() {
70 global $wgUseGzip;
71 return $wgUseGzip;
74 /* In handy string packages */
75 function fetchRawText() {
76 return file_get_contents( $this->fileCacheName() );
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();
88 /* Working directory to/from output */
89 function loadFromFileCache() {
90 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
91 wfDebug(" loadFromFileCache()\n");
93 $filename = $this->fileCacheName();
94 $wgOut->sendCacheControl();
96 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
97 header( "Content-language: $wgContLanguageCode" );
99 if( $this->useGzip() ) {
100 if( wfClientAcceptsGzip() ) {
101 header( 'Content-Encoding: gzip' );
102 } else {
103 /* Send uncompressed */
104 readgzfile( $filename );
105 return;
108 readfile( $filename );
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
116 wfMkdirParents( $mydir1 );
117 wfMkdirParents( $mydir2 );
120 function saveToFileCache( $origtext ) {
121 global $wgUseFileCache;
122 if( !$wgUseFileCache ) {
123 return $origtext; // return to output
125 $text = $origtext;
126 if( strcmp($text,'') == 0 ) return '';
128 wfDebug(" saveToFileCache()\n", false);
130 $this->checkCacheDirs();
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 );
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;
154 } else {
155 return $text;
158 return $text;