MediaWiki REL1_33
HTMLFileCache.php
Go to the documentation of this file.
1<?php
25
34 const MODE_NORMAL = 0; // normal cache mode
35 const MODE_OUTAGE = 1; // fallback cache for DB outages
36 const MODE_REBUILD = 2; // background cache rebuild mode
37
43 public function __construct( $title, $action ) {
44 parent::__construct();
45
46 $allowedTypes = self::cacheablePageActions();
47 if ( !in_array( $action, $allowedTypes ) ) {
48 throw new MWException( 'Invalid file cache type given.' );
49 }
50 $this->mKey = ( $title instanceof Title )
51 ? $title->getPrefixedDBkey()
52 : (string)$title;
53 $this->mType = (string)$action;
54 $this->mExt = 'html';
55 }
56
61 protected static function cacheablePageActions() {
62 return [ 'view', 'history' ];
63 }
64
69 protected function cacheDirectory() {
70 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
71 }
72
79 protected function typeSubdirectory() {
80 if ( $this->mType === 'view' ) {
81 return ''; // b/c to not skip existing cache
82 } else {
83 return $this->mType . '/';
84 }
85 }
86
93 public static function useFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
94 $config = MediaWikiServices::getInstance()->getMainConfig();
95
96 if ( !$config->get( 'UseFileCache' ) && $mode !== self::MODE_REBUILD ) {
97 return false;
98 } elseif ( $config->get( 'DebugToolbar' ) ) {
99 wfDebug( "HTML file cache skipped. \$wgDebugToolbar on\n" );
100
101 return false;
102 }
103
104 // Get all query values
105 $queryVals = $context->getRequest()->getValues();
106 foreach ( $queryVals as $query => $val ) {
107 if ( $query === 'title' || $query === 'curid' ) {
108 continue; // note: curid sets title
109 // Normal page view in query form can have action=view.
110 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
111 continue;
112 // Below are header setting params
113 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
114 continue;
115 }
116
117 return false;
118 }
119
120 $user = $context->getUser();
121 // Check for non-standard user language; this covers uselang,
122 // and extensions for auto-detecting user language.
123 $ulang = $context->getLanguage();
124
125 // Check that there are no other sources of variation
126 if ( $user->getId() ||
127 !$ulang->equals( MediaWikiServices::getInstance()->getContentLanguage() ) ) {
128 return false;
129 }
130
131 if ( ( $mode === self::MODE_NORMAL ) && $user->getNewtalk() ) {
132 return false;
133 }
134
135 // Allow extensions to disable caching
136 return Hooks::run( 'HTMLFileCache::useFileCache', [ $context ] );
137 }
138
145 public function loadFromFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
146 $config = MediaWikiServices::getInstance()->getMainConfig();
147
148 wfDebug( __METHOD__ . "()\n" );
149 $filename = $this->cachePath();
150
151 if ( $mode === self::MODE_OUTAGE ) {
152 // Avoid DB errors for queries in sendCacheControl()
153 $context->getTitle()->resetArticleID( 0 );
154 }
155
156 $context->getOutput()->sendCacheControl();
157 header( "Content-Type: {$config->get( 'MimeType' )}; charset=UTF-8" );
158 header( 'Content-Language: ' .
159 MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() );
160 if ( $this->useGzip() ) {
161 if ( wfClientAcceptsGzip() ) {
162 header( 'Content-Encoding: gzip' );
163 readfile( $filename );
164 } else {
165 /* Send uncompressed */
166 wfDebug( __METHOD__ . " uncompressing cache file and sending it\n" );
167 readgzfile( $filename );
168 }
169 } else {
170 readfile( $filename );
171 }
172
173 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
174 }
175
188 public function saveToFileCache( $text ) {
189 if ( strlen( $text ) < 512 ) {
190 // Disabled or empty/broken output (OOM and PHP errors)
191 return $text;
192 }
193
194 wfDebug( __METHOD__ . "()\n", 'private' );
195
196 $now = wfTimestampNow();
197 if ( $this->useGzip() ) {
198 $text = str_replace(
199 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
200 } else {
201 $text = str_replace(
202 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
203 }
204
205 // Store text to FS...
206 $compressed = $this->saveText( $text );
207 if ( $compressed === false ) {
208 return $text; // error
209 }
210
211 // gzip output to buffer as needed and set headers...
212 // @todo Ugly wfClientAcceptsGzip() function - use context!
213 if ( $this->useGzip() && wfClientAcceptsGzip() ) {
214 header( 'Content-Encoding: gzip' );
215
216 return $compressed;
217 }
218
219 return $text;
220 }
221
227 public static function clearFileCache( Title $title ) {
228 $config = MediaWikiServices::getInstance()->getMainConfig();
229
230 if ( !$config->get( 'UseFileCache' ) ) {
231 return false;
232 }
233
234 foreach ( self::cacheablePageActions() as $type ) {
235 $fc = new self( $title, $type );
236 $fc->clearCache();
237 }
238
239 return true;
240 }
241}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
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.
loadFromFileCache(IContextSource $context, $mode=self::MODE_NORMAL)
Read from cache to context output.
saveToFileCache( $text)
Save this cache object with the given text.
cacheDirectory()
Get the base file cache directory.
static clearFileCache(Title $title)
Clear the file caches for a page for all actions.
typeSubdirectory()
Get the cache type subdirectory (with the trailing slash) or the empty string Alter the type -> direc...
static cacheablePageActions()
Cacheable actions.
__construct( $title, $action)
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:40
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition hooks.txt:181
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2848
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1617
Interface for objects which can provide a MediaWiki context on request.