Localisation updates for core messages from Betawiki (2008-12-18 00:40 CET)
[mediawiki.git] / includes / HTMLFileCache.php
blob299235aeb815d3c28c6df3b5d6b961ac3792d0a6
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 public function __construct( &$title ) {
26 $this->mTitle = $title;
27 $this->mFileCache = $this->fileCacheName();
30 public 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 public function isFileCached() {
50 return file_exists( $this->fileCacheName() );
53 public function fileCacheTime() {
54 return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
57 /**
58 * Check if pages can be cached for this request/user
59 * @return bool
61 public static function useFileCache() {
62 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
63 if( !$wgUseFileCache )
64 return false;
65 // Get all query values
66 $queryVals = $wgRequest->getValues();
67 foreach( $queryVals as $query => $val ) {
68 // Normal page view in query form can have action=view
69 if( $query !== 'title' && $query !== 'curid' && !($query == 'action' && $val == 'view') ) {
70 return false;
73 // Check for non-standard user language; this covers uselang,
74 // and extensions for auto-detecting user language.
75 $ulang = $wgLang->getCode();
76 $clang = $wgContLang->getCode();
78 return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
81 /*
82 * Check if up to date cache file exists
83 * @param $timestamp string
85 public function isFileCacheGood( $timestamp = '' ) {
86 global $wgCacheEpoch;
88 if( !$this->isFileCached() ) return false;
89 if( !$timestamp ) return true; // should be invalidated on change
91 $cachetime = $this->fileCacheTime();
92 $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
94 wfDebug(" isFileCacheGood() - cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
95 return $good;
98 public function useGzip() {
99 global $wgUseGzip;
100 return $wgUseGzip;
103 /* In handy string packages */
104 public function fetchRawText() {
105 return file_get_contents( $this->fileCacheName() );
108 public function fetchPageText() {
109 if( $this->useGzip() ) {
110 /* Why is there no gzfile_get_contents() or gzdecode()? */
111 return implode( '', gzfile( $this->fileCacheName() ) );
112 } else {
113 return $this->fetchRawText();
117 /* Working directory to/from output */
118 public function loadFromFileCache() {
119 global $wgOut, $wgMimeType, $wgOutputEncoding, $wgContLanguageCode;
120 wfDebug(" loadFromFileCache()\n");
122 $filename = $this->fileCacheName();
123 $wgOut->sendCacheControl();
125 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
126 header( "Content-language: $wgContLanguageCode" );
128 if( $this->useGzip() ) {
129 if( wfClientAcceptsGzip() ) {
130 header( 'Content-Encoding: gzip' );
131 } else {
132 /* Send uncompressed */
133 readgzfile( $filename );
134 return;
137 readfile( $filename );
140 protected function checkCacheDirs() {
141 $filename = $this->fileCacheName();
142 $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
143 $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
145 wfMkdirParents( $mydir1 );
146 wfMkdirParents( $mydir2 );
149 public function saveToFileCache( $origtext ) {
150 global $wgUseFileCache;
151 if( !$wgUseFileCache ) {
152 return $origtext; // return to output
154 $text = $origtext;
155 if( strcmp($text,'') == 0 ) return '';
157 wfDebug(" saveToFileCache()\n", false);
159 $this->checkCacheDirs();
161 $f = fopen( $this->fileCacheName(), 'w' );
162 if($f) {
163 $now = wfTimestampNow();
164 if( $this->useGzip() ) {
165 $rawtext = str_replace( '</html>',
166 '<!-- Cached/compressed '.$now." -->\n</html>",
167 $text );
168 $text = gzencode( $rawtext );
169 } else {
170 $text = str_replace( '</html>',
171 '<!-- Cached '.$now." -->\n</html>",
172 $text );
174 fwrite( $f, $text );
175 fclose( $f );
176 if( $this->useGzip() ) {
177 if( wfClientAcceptsGzip() ) {
178 header( 'Content-Encoding: gzip' );
179 return $text;
180 } else {
181 return $rawtext;
183 } else {
184 return $text;
187 return $text;