c0c4a577f8c4822f0350131c3ae389490a478e3d
3 * Contain the HTMLFileCache class
7 class HTMLFileCache
extends FileCacheBase
{
9 * Construct an ObjectFileCache from a Title and an action
11 * @param $action string
12 * @return HTMLFileCache
14 public static function newFromTitle( Title
$title, $action ) {
17 $allowedTypes = self
::cacheablePageActions();
18 if ( !in_array( $action, $allowedTypes ) ) {
19 throw new MWException( "Invalid filecache type given." );
21 $cache->mKey
= $title->getPrefixedDBkey();
22 $cache->mType
= (string)$action;
23 $cache->mExt
= 'html';
32 protected static function cacheablePageActions() {
33 return array( 'view', 'history' );
37 * Get the base file cache directory
40 protected function cacheDirectory() {
41 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
45 * Get the cache type subdirectory (with the trailing slash) or the empty string
48 protected function typeSubdirectory() {
49 if ( $this->mType
=== 'view' ) {
50 return ''; // b/c to not skip existing cache
52 return $this->mType
. '/';
57 * Check if pages can be cached for this request/user
58 * @param $context IContextSource
61 public static function useFileCache( IContextSource
$context ) {
62 global $wgUseFileCache, $wgShowIPinHeader, $wgContLang;
63 if ( !$wgUseFileCache ) {
66 // Get all query values
67 $queryVals = $context->getRequest()->getValues();
68 foreach ( $queryVals as $query => $val ) {
69 if ( $query === 'title' ||
$query === 'curid' ) {
70 continue; // note: curid sets title
71 // Normal page view in query form can have action=view.
72 // Raw hits for pages also stored, like .css pages for example.
73 } elseif ( $query === 'action' && in_array( $val, self
::cacheablePageActions() ) ) {
75 // Below are header setting params
76 } elseif ( $query === 'maxage' ||
$query === 'smaxage' ) {
81 $user = $context->getUser();
82 // Check for non-standard user language; this covers uselang,
83 // and extensions for auto-detecting user language.
84 $ulang = $context->getLang()->getCode();
85 $clang = $wgContLang->getCode();
86 // Check that there are no other sources of variation
87 return !$wgShowIPinHeader && !$user->getId() && !$user->getNewtalk() && $ulang == $clang;
91 * Read from cache to context output
92 * @param $context IContextSource
95 public function loadFromFileCache( IContextSource
$context ) {
96 global $wgMimeType, $wgLanguageCode;
98 wfDebug( __METHOD__
. "()\n");
99 $filename = $this->cachePath();
100 $context->getOutput()->sendCacheControl();
101 header( "Content-Type: $wgMimeType; charset=UTF-8" );
102 header( "Content-Language: $wgLanguageCode" );
103 if ( $this->useGzip() ) {
104 if ( wfClientAcceptsGzip() ) {
105 header( 'Content-Encoding: gzip' );
107 /* Send uncompressed */
108 readgzfile( $filename );
112 readfile( $filename );
113 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
117 * Save this cache object with the given text.
118 * Use this as an ob_start() handler.
119 * @param $text string
120 * @return bool Whether $wgUseFileCache is enabled
122 public function saveToFileCache( $text ) {
123 global $wgUseFileCache;
125 if ( !$wgUseFileCache ||
strlen( $text ) < 512 ) {
126 // Disabled or empty/broken output (OOM and PHP errors)
130 wfDebug( __METHOD__
. "()\n", false);
132 $now = wfTimestampNow();
133 if ( $this->useGzip() ) {
135 '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text );
138 '</html>', '<!-- Cached '.$now." -->\n</html>", $text );
141 // Store text to FS...
142 $compressed = $this->saveText( $text );
143 if ( $compressed === false ) {
144 return $text; // error
147 // gzip output to buffer as needed and set headers...
148 if ( $this->useGzip() ) {
149 // @TODO: ugly wfClientAcceptsGzip() function - use context!
150 if ( wfClientAcceptsGzip() ) {
151 header( 'Content-Encoding: gzip' );
162 * Clear the file caches for a page for all actions
163 * @param $title Title
164 * @return bool Whether $wgUseFileCache is enabled
166 public static function clearFileCache( Title
$title ) {
167 global $wgUseFileCache;
169 if ( !$wgUseFileCache ) {
173 foreach ( self
::cacheablePageActions() as $type ) {
174 $fc = self
::newFromTitle( $title, $type );