MediaWiki  master
HTMLFileCache.php
Go to the documentation of this file.
1 <?php
41  public static function newFromTitle( $title, $action ) {
42  return new self( $title, $action );
43  }
44 
50  public function __construct( $title, $action ) {
51  parent::__construct();
52  $allowedTypes = self::cacheablePageActions();
53  if ( !in_array( $action, $allowedTypes ) ) {
54  throw new MWException( 'Invalid file cache type given.' );
55  }
56  $this->mKey = ( $title instanceof Title )
57  ? $title->getPrefixedDBkey()
58  : (string)$title;
59  $this->mType = (string)$action;
60  $this->mExt = 'html';
61  }
62 
67  protected static function cacheablePageActions() {
68  return [ 'view', 'history' ];
69  }
70 
75  protected function cacheDirectory() {
76  return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
77  }
78 
85  protected function typeSubdirectory() {
86  if ( $this->mType === 'view' ) {
87  return ''; // b/c to not skip existing cache
88  } else {
89  return $this->mType . '/';
90  }
91  }
92 
98  public static function useFileCache( IContextSource $context ) {
100  if ( !$wgUseFileCache ) {
101  return false;
102  }
103  if ( $wgDebugToolbar ) {
104  wfDebug( "HTML file cache skipped. \$wgDebugToolbar on\n" );
105 
106  return false;
107  }
108 
109  // Get all query values
110  $queryVals = $context->getRequest()->getValues();
111  foreach ( $queryVals as $query => $val ) {
112  if ( $query === 'title' || $query === 'curid' ) {
113  continue; // note: curid sets title
114  // Normal page view in query form can have action=view.
115  } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
116  continue;
117  // Below are header setting params
118  } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
119  continue;
120  }
121 
122  return false;
123  }
124  $user = $context->getUser();
125  // Check for non-standard user language; this covers uselang,
126  // and extensions for auto-detecting user language.
127  $ulang = $context->getLanguage();
128 
129  // Check that there are no other sources of variation
130  if ( $user->getId() || $user->getNewtalk() || !$ulang->equals( $wgContLang ) ) {
131  return false;
132  }
133  // Allow extensions to disable caching
134  return Hooks::run( 'HTMLFileCache::useFileCache', [ $context ] );
135  }
136 
144 
145  wfDebug( __METHOD__ . "()\n" );
146  $filename = $this->cachePath();
147 
148  $context->getOutput()->sendCacheControl();
149  header( "Content-Type: $wgMimeType; charset=UTF-8" );
150  header( "Content-Language: $wgLanguageCode" );
151  if ( $this->useGzip() ) {
152  if ( wfClientAcceptsGzip() ) {
153  header( 'Content-Encoding: gzip' );
154  readfile( $filename );
155  } else {
156  /* Send uncompressed */
157  wfDebug( __METHOD__ . " uncompressing cache file and sending it\n" );
158  readgzfile( $filename );
159  }
160  } else {
161  readfile( $filename );
162  }
163  $context->getOutput()->disable(); // tell $wgOut that output is taken care of
164  }
165 
172  public function saveToFileCache( $text ) {
174 
175  if ( !$wgUseFileCache || strlen( $text ) < 512 ) {
176  // Disabled or empty/broken output (OOM and PHP errors)
177  return $text;
178  }
179 
180  wfDebug( __METHOD__ . "()\n", 'private' );
181 
182  $now = wfTimestampNow();
183  if ( $this->useGzip() ) {
184  $text = str_replace(
185  '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
186  } else {
187  $text = str_replace(
188  '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
189  }
190 
191  // Store text to FS...
192  $compressed = $this->saveText( $text );
193  if ( $compressed === false ) {
194  return $text; // error
195  }
196 
197  // gzip output to buffer as needed and set headers...
198  if ( $this->useGzip() ) {
199  // @todo Ugly wfClientAcceptsGzip() function - use context!
200  if ( wfClientAcceptsGzip() ) {
201  header( 'Content-Encoding: gzip' );
202 
203  return $compressed;
204  } else {
205  return $text;
206  }
207  } else {
208  return $text;
209  }
210  }
211 
217  public static function clearFileCache( Title $title ) {
219 
220  if ( !$wgUseFileCache ) {
221  return false;
222  }
223 
224  foreach ( self::cacheablePageActions() as $type ) {
225  $fc = new self( $title, $type );
226  $fc->clearCache();
227  }
228 
229  return true;
230  }
231 }
saveText($text)
Save and compress text to the cache.
Interface for objects which can provide a MediaWiki context on request.
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:1435
$context
Definition: load.php:43
$wgMimeType
The default Content-Type header.
$wgDebugToolbar
Display the new debugging toolbar.
static newFromTitle($title, $action)
Construct an ObjectFileCache from a Title and an action.
baseCacheDirectory()
Get the base file cache directory.
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:177
Page view caching in the file system.
loadFromFileCache(IContextSource $context)
Read from cache to context output.
Represents a title within MediaWiki.
Definition: Title.php:36
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static useFileCache(IContextSource $context)
Check if pages can be cached for this request/user.
static cacheablePageActions()
Cacheable actions.
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
$wgLanguageCode
Site language code.
getUser()
Get the User object.
typeSubdirectory()
Get the cache type subdirectory (with the trailing slash) or the empty string Alter the type -> direc...
$wgUseFileCache
This will cache static pages for non-logged-in users to reduce database traffic on public sites...
static clearFileCache(Title $title)
Clear the file caches for a page for all actions.
wfClientAcceptsGzip($force=false)
Base class for data storage in the file system.
MediaWiki exception.
Definition: MWException.php:26
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Allows to change the fields on the form that will be generated are created Can be used to omit specific feeds from being outputted You must not use this hook to add use OutputPage::addFeedLink() instead.&$feedLinks conditions will AND in the final query as a Content object as a Content object $title
Definition: hooks.txt:312
cachePath()
Get the path to the cache file.
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
getLanguage()
Get the Language object.
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
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 local account $user
Definition: hooks.txt:242
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
saveToFileCache($text)
Save this cache object with the given text.
useGzip()
Check if the cache is gzipped.
__construct($title, $action)
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition: design.txt:56
cacheDirectory()
Get the base file cache directory.
getOutput()
Get the OutputPage object.
getRequest()
Get the WebRequest object.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2376