MediaWiki REL1_41
HTMLFileCache.php
Go to the documentation of this file.
1<?php
29
38 public const MODE_NORMAL = 0; // normal cache mode
39 public const MODE_OUTAGE = 1; // fallback cache for DB outages
40 public const MODE_REBUILD = 2; // background cache rebuild mode
41
42 private const CACHEABLE_ACTIONS = [
43 'view',
44 'history',
45 ];
46
51 public function __construct( $page, $action ) {
52 parent::__construct();
53
54 if ( !in_array( $action, self::CACHEABLE_ACTIONS ) ) {
55 throw new InvalidArgumentException( 'Invalid file cache type given.' );
56 }
57
58 $this->mKey = CacheKeyHelper::getKeyForPage( $page );
59 $this->mType = (string)$action;
60 $this->mExt = 'html';
61 }
62
67 protected function cacheDirectory() {
68 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
69 }
70
77 protected function typeSubdirectory() {
78 if ( $this->mType === 'view' ) {
79 return ''; // b/c to not skip existing cache
80 } else {
81 return $this->mType . '/';
82 }
83 }
84
91 public static function useFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
92 $services = MediaWikiServices::getInstance();
93 $config = $services->getMainConfig();
94
95 if ( !$config->get( MainConfigNames::UseFileCache ) && $mode !== self::MODE_REBUILD ) {
96 return false;
97 }
98
99 // Get all query values
100 $queryVals = $context->getRequest()->getValues();
101 foreach ( $queryVals as $query => $val ) {
102 if ( $query === 'title' || $query === 'curid' ) {
103 continue; // note: curid sets title
104 // Normal page view in query form can have action=view.
105 } elseif ( $query === 'action' && in_array( $val, self::CACHEABLE_ACTIONS ) ) {
106 continue;
107 // Below are header setting params
108 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
109 continue;
110 // Uselang value is checked below
111 } elseif ( $query === 'uselang' ) {
112 continue;
113 }
114
115 return false;
116 }
117
118 $user = $context->getUser();
119 // Check for non-standard user language; this covers uselang,
120 // and extensions for auto-detecting user language.
121 $ulang = $context->getLanguage();
122
123 // Check that there are no other sources of variation
124 if ( $user->isRegistered() ||
125 !$ulang->equals( $services->getContentLanguage() ) ) {
126 return false;
127 }
128
129 $userHasNewMessages = $services->getTalkPageNotificationManager()->userHasNewMessages( $user );
130 if ( ( $mode === self::MODE_NORMAL ) && $userHasNewMessages ) {
131 return false;
132 }
133
134 // Allow extensions to disable caching
135 return ( new HookRunner( $services->getHookContainer() ) )->onHTMLFileCache__useFileCache( $context );
136 }
137
144 public function loadFromFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
145 wfDebug( __METHOD__ . "()" );
146 $filename = $this->cachePath();
147
148 if ( $mode === self::MODE_OUTAGE ) {
149 // Avoid DB errors for queries in sendCacheControl()
150 $context->getTitle()->resetArticleID( 0 );
151 }
152
153 $context->getOutput()->sendCacheControl();
154 header( "Content-Type: {$this->options->get( MainConfigNames::MimeType )}; charset=UTF-8" );
155 header( 'Content-Language: ' .
156 MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() );
157 if ( $this->useGzip() ) {
158 if ( wfClientAcceptsGzip() ) {
159 header( 'Content-Encoding: gzip' );
160 readfile( $filename );
161 } else {
162 /* Send uncompressed */
163 wfDebug( __METHOD__ . " uncompressing cache file and sending it" );
164 readgzfile( $filename );
165 }
166 } else {
167 readfile( $filename );
168 }
169
170 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
171 }
172
185 public function saveToFileCache( $text ) {
186 if ( strlen( $text ) < 512 ) {
187 // Disabled or empty/broken output (OOM and PHP errors)
188 return $text;
189 }
190
191 wfDebug( __METHOD__ . "()\n", 'private' );
192
193 $now = wfTimestampNow();
194 if ( $this->useGzip() ) {
195 $text = str_replace(
196 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
197 } else {
198 $text = str_replace(
199 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
200 }
201
202 // Store text to FS...
203 $compressed = $this->saveText( $text );
204 if ( $compressed === false ) {
205 return $text; // error
206 }
207
208 // gzip output to buffer as needed and set headers...
209 // @todo Ugly wfClientAcceptsGzip() function - use context!
210 if ( $this->useGzip() && wfClientAcceptsGzip() ) {
211 header( 'Content-Encoding: gzip' );
212
213 return $compressed;
214 }
215
216 return $text;
217 }
218
225 public static function clearFileCache( $page ) {
226 $config = MediaWikiServices::getInstance()->getMainConfig();
227 if ( !$config->get( MainConfigNames::UseFileCache ) ) {
228 return false;
229 }
230
231 foreach ( self::CACHEABLE_ACTIONS as $type ) {
232 $fc = new self( $page, $type );
233 $fc->clearCache();
234 }
235
236 return true;
237 }
238}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfClientAcceptsGzip( $force=false)
Whether the client accept gzip encoding.
Base class for data storage in the file system.
useGzip()
Check if the cache is gzipped.
cachePath()
Get the path to the cache file.
saveText( $text)
Save and compress text to the cache.
baseCacheDirectory()
Get the base file cache directory.
Page view caching in the file system.
static useFileCache(IContextSource $context, $mode=self::MODE_NORMAL)
Check if pages can be cached for this request/user.
__construct( $page, $action)
loadFromFileCache(IContextSource $context, $mode=self::MODE_NORMAL)
Read from cache to context output.
static clearFileCache( $page)
Clear the file caches for a page for all actions.
saveToFileCache( $text)
Save this cache object with the given text.
cacheDirectory()
Get the base file cache directory.
typeSubdirectory()
Get the cache type subdirectory (with the trailing slash) or the empty string Alter the type -> direc...
Helper class for mapping value objects representing basic entities to cache keys.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Interface for objects which can provide a MediaWiki context on request.
Interface for objects (potentially) representing an editable wiki page.