MediaWiki REL1_35
HTMLFileCache.php
Go to the documentation of this file.
1<?php
25
34 public const MODE_NORMAL = 0; // normal cache mode
35 public const MODE_OUTAGE = 1; // fallback cache for DB outages
36 public const MODE_REBUILD = 2; // background cache rebuild mode
37
43 public function __construct( $title, $action ) {
44 parent::__construct();
45
46 if ( !in_array( $action, self::cacheablePageActions() ) ) {
47 throw new InvalidArgumentException( 'Invalid file cache type given.' );
48 }
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 }
99
100 // Get all query values
101 $queryVals = $context->getRequest()->getValues();
102 foreach ( $queryVals as $query => $val ) {
103 if ( $query === 'title' || $query === 'curid' ) {
104 continue; // note: curid sets title
105 // Normal page view in query form can have action=view.
106 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
107 continue;
108 // Below are header setting params
109 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
110 continue;
111 }
112
113 return false;
114 }
115
116 $user = $context->getUser();
117 // Check for non-standard user language; this covers uselang,
118 // and extensions for auto-detecting user language.
119 $ulang = $context->getLanguage();
120
121 // Check that there are no other sources of variation
122 if ( $user->getId() ||
123 !$ulang->equals( MediaWikiServices::getInstance()->getContentLanguage() ) ) {
124 return false;
125 }
126
127 $userHasNewMessages = MediaWikiServices::getInstance()
128 ->getTalkPageNotificationManager()->userHasNewMessages( $user );
129 if ( ( $mode === self::MODE_NORMAL ) && $userHasNewMessages ) {
130 return false;
131 }
132
133 // Allow extensions to disable caching
134 return Hooks::runner()->onHTMLFileCache__useFileCache( $context );
135 }
136
143 public function loadFromFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
144 $config = MediaWikiServices::getInstance()->getMainConfig();
145
146 wfDebug( __METHOD__ . "()" );
147 $filename = $this->cachePath();
148
149 if ( $mode === self::MODE_OUTAGE ) {
150 // Avoid DB errors for queries in sendCacheControl()
151 $context->getTitle()->resetArticleID( 0 );
152 }
153
154 $context->getOutput()->sendCacheControl();
155 header( "Content-Type: {$config->get( 'MimeType' )}; charset=UTF-8" );
156 header( 'Content-Language: ' .
157 MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() );
158 if ( $this->useGzip() ) {
159 if ( wfClientAcceptsGzip() ) {
160 header( 'Content-Encoding: gzip' );
161 readfile( $filename );
162 } else {
163 /* Send uncompressed */
164 wfDebug( __METHOD__ . " uncompressing cache file and sending it" );
165 readgzfile( $filename );
166 }
167 } else {
168 readfile( $filename );
169 }
170
171 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
172 }
173
186 public function saveToFileCache( $text ) {
187 if ( strlen( $text ) < 512 ) {
188 // Disabled or empty/broken output (OOM and PHP errors)
189 return $text;
190 }
191
192 wfDebug( __METHOD__ . "()\n", 'private' );
193
194 $now = wfTimestampNow();
195 if ( $this->useGzip() ) {
196 $text = str_replace(
197 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
198 } else {
199 $text = str_replace(
200 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
201 }
202
203 // Store text to FS...
204 $compressed = $this->saveText( $text );
205 if ( $compressed === false ) {
206 return $text; // error
207 }
208
209 // gzip output to buffer as needed and set headers...
210 // @todo Ugly wfClientAcceptsGzip() function - use context!
211 if ( $this->useGzip() && wfClientAcceptsGzip() ) {
212 header( 'Content-Encoding: gzip' );
213
214 return $compressed;
215 }
216
217 return $text;
218 }
219
226 public static function clearFileCache( $title ) {
227 $config = MediaWikiServices::getInstance()->getMainConfig();
228 if ( !$config->get( 'UseFileCache' ) ) {
229 return false;
230 }
231
232 foreach ( self::cacheablePageActions() as $type ) {
233 $fc = new self( $title, $type );
234 $fc->clearCache();
235 }
236
237 return true;
238 }
239}
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.
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)
static clearFileCache( $title)
Clear the file caches for a page for all actions.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:42
Interface for objects which can provide a MediaWiki context on request.