MediaWiki  1.32.0
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 ) {
132  if ( $user->getNewtalk() ) {
133  return false;
134  }
135  }
136 
137  // Allow extensions to disable caching
138  return Hooks::run( 'HTMLFileCache::useFileCache', [ $context ] );
139  }
140 
147  public function loadFromFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
148  $config = MediaWikiServices::getInstance()->getMainConfig();
149 
150  wfDebug( __METHOD__ . "()\n" );
151  $filename = $this->cachePath();
152 
153  if ( $mode === self::MODE_OUTAGE ) {
154  // Avoid DB errors for queries in sendCacheControl()
155  $context->getTitle()->resetArticleID( 0 );
156  }
157 
158  $context->getOutput()->sendCacheControl();
159  header( "Content-Type: {$config->get( 'MimeType' )}; charset=UTF-8" );
160  header( 'Content-Language: ' .
161  MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() );
162  if ( $this->useGzip() ) {
163  if ( wfClientAcceptsGzip() ) {
164  header( 'Content-Encoding: gzip' );
165  readfile( $filename );
166  } else {
167  /* Send uncompressed */
168  wfDebug( __METHOD__ . " uncompressing cache file and sending it\n" );
169  readgzfile( $filename );
170  }
171  } else {
172  readfile( $filename );
173  }
174 
175  $context->getOutput()->disable(); // tell $wgOut that output is taken care of
176  }
177 
190  public function saveToFileCache( $text ) {
191  if ( strlen( $text ) < 512 ) {
192  // Disabled or empty/broken output (OOM and PHP errors)
193  return $text;
194  }
195 
196  wfDebug( __METHOD__ . "()\n", 'private' );
197 
198  $now = wfTimestampNow();
199  if ( $this->useGzip() ) {
200  $text = str_replace(
201  '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
202  } else {
203  $text = str_replace(
204  '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
205  }
206 
207  // Store text to FS...
208  $compressed = $this->saveText( $text );
209  if ( $compressed === false ) {
210  return $text; // error
211  }
212 
213  // gzip output to buffer as needed and set headers...
214  if ( $this->useGzip() ) {
215  // @todo Ugly wfClientAcceptsGzip() function - use context!
216  if ( wfClientAcceptsGzip() ) {
217  header( 'Content-Encoding: gzip' );
218 
219  return $compressed;
220  } else {
221  return $text;
222  }
223  } else {
224  return $text;
225  }
226  }
227 
233  public static function clearFileCache( Title $title ) {
234  $config = MediaWikiServices::getInstance()->getMainConfig();
235 
236  if ( !$config->get( 'UseFileCache' ) ) {
237  return false;
238  }
239 
240  foreach ( self::cacheablePageActions() as $type ) {
241  $fc = new self( $title, $type );
242  $fc->clearCache();
243  }
244 
245  return true;
246  }
247 }
HTMLFileCache\useFileCache
static useFileCache(IContextSource $context, $mode=self::MODE_NORMAL)
Check if pages can be cached for this request/user.
Definition: HTMLFileCache.php:93
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
FileCacheBase\saveText
saveText( $text)
Save and compress text to the cache.
Definition: FileCacheBase.php:159
$context
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:2675
HTMLFileCache\clearFileCache
static clearFileCache(Title $title)
Clear the file caches for a page for all actions.
Definition: HTMLFileCache.php:233
HTMLFileCache
Page view caching in the file system.
Definition: HTMLFileCache.php:33
HTMLFileCache\MODE_OUTAGE
const MODE_OUTAGE
Definition: HTMLFileCache.php:35
HTMLFileCache\typeSubdirectory
typeSubdirectory()
Get the cache type subdirectory (with the trailing slash) or the empty string Alter the type -> direc...
Definition: HTMLFileCache.php:79
MediaWiki\getTitle
getTitle()
Get the Title object that we'll be acting on, as specified in the WebRequest.
Definition: MediaWiki.php:137
HTMLFileCache\cacheablePageActions
static cacheablePageActions()
Cacheable actions.
Definition: HTMLFileCache.php:61
HTMLFileCache\saveToFileCache
saveToFileCache( $text)
Save this cache object with the given text.
Definition: HTMLFileCache.php:190
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$query
null for the 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:1627
MWException
MediaWiki exception.
Definition: MWException.php:26
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
FileCacheBase\useGzip
useGzip()
Check if the cache is gzipped.
Definition: FileCacheBase.php:136
FileCacheBase\baseCacheDirectory
baseCacheDirectory()
Get the base file cache directory.
Definition: FileCacheBase.php:52
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1983
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:988
string
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:175
HTMLFileCache\__construct
__construct( $title, $action)
Definition: HTMLFileCache.php:43
wfClientAcceptsGzip
wfClientAcceptsGzip( $force=false)
Whether the client accept gzip encoding.
Definition: GlobalFunctions.php:1582
HTMLFileCache\MODE_NORMAL
const MODE_NORMAL
Definition: HTMLFileCache.php:34
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
Title
Represents a title within MediaWiki.
Definition: Title.php:39
HTMLFileCache\loadFromFileCache
loadFromFileCache(IContextSource $context, $mode=self::MODE_NORMAL)
Read from cache to context output.
Definition: HTMLFileCache.php:147
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
HTMLFileCache\MODE_REBUILD
const MODE_REBUILD
Definition: HTMLFileCache.php:36
FileCacheBase\cachePath
cachePath()
Get the path to the cache file.
Definition: FileCacheBase.php:68
HTMLFileCache\cacheDirectory
cacheDirectory()
Get the base file cache directory.
Definition: HTMLFileCache.php:69
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
FileCacheBase
Base class for data storage in the file system.
Definition: FileCacheBase.php:29
$type
$type
Definition: testCompression.php:48