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 global $wgCacheDirectory, $wgFileCacheDirectory, $wgFileCacheDepth;
42 if ( $wgFileCacheDirectory ) {
43 $dir = $wgFileCacheDirectory;
44 } elseif ( $wgCacheDirectory ) {
45 $dir = "$wgCacheDirectory/html";
47 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
53 * Check if pages can be cached for this request/user
54 * @param $context IContextSource
57 public static function useFileCache( IContextSource
$context ) {
58 global $wgUseFileCache, $wgShowIPinHeader, $wgContLang;
59 if ( !$wgUseFileCache ) {
62 // Get all query values
63 $queryVals = $context->getRequest()->getValues();
64 foreach ( $queryVals as $query => $val ) {
65 if ( $query == 'title' ||
$query == 'curid' ) {
66 continue; // note: curid sets title
67 // Normal page view in query form can have action=view.
68 // Raw hits for pages also stored, like .css pages for example.
69 } elseif ( $query == 'action' && in_array( $val, self
::cacheablePageActions() ) ) {
71 // Below are header setting params
72 } elseif ( $query == 'maxage' ||
$query == 'smaxage' ) {
78 $user = $context->getUser();
79 // Check for non-standard user language; this covers uselang,
80 // and extensions for auto-detecting user language.
81 $ulang = $context->getLang()->getCode();
82 $clang = $wgContLang->getCode();
83 // Check that there are no other sources of variation
84 return !$wgShowIPinHeader && !$user->getId() && !$user->getNewtalk() && $ulang == $clang;
88 * Read from cache to context output
89 * @param $context IContextSource
92 public function loadFromFileCache( IContextSource
$context ) {
93 global $wgMimeType, $wgLanguageCode;
95 wfDebug( __METHOD__
. "()\n");
96 $filename = $this->cachePath();
97 $context->getOutput()->sendCacheControl();
98 header( "Content-Type: $wgMimeType; charset=UTF-8" );
99 header( "Content-Language: $wgLanguageCode" );
100 if ( $this->useGzip() ) {
101 if ( wfClientAcceptsGzip() ) {
102 header( 'Content-Encoding: gzip' );
104 /* Send uncompressed */
105 readgzfile( $filename );
109 readfile( $filename );
110 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
114 * Save this cache object with the given text.
115 * Use this as an ob_start() handler.
116 * @param $text string
117 * @return bool Whether $wgUseFileCache is enabled
119 public function saveToFileCache( $text ) {
120 global $wgUseFileCache;
122 if ( !$wgUseFileCache ||
strlen( $text ) < 512 ) {
123 // Disabled or empty/broken output (OOM and PHP errors)
127 wfDebug( __METHOD__
. "()\n", false);
129 $now = wfTimestampNow();
130 if ( $this->useGzip() ) {
132 '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text );
135 '</html>', '<!-- Cached '.$now." -->\n</html>", $text );
138 // Store text to FS...
139 $compressed = $this->saveText( $text );
140 if ( $compressed === false ) {
141 return $text; // error
144 // gzip output to buffer as needed and set headers...
145 if ( $this->useGzip() ) {
146 // @TODO: ugly wfClientAcceptsGzip() function - use context!
147 if ( wfClientAcceptsGzip() ) {
148 header( 'Content-Encoding: gzip' );
159 * Clear the file caches for a page for all actions
160 * @param $title Title
161 * @return bool Whether $wgUseFileCache is enabled
163 public static function clearFileCache( Title
$title ) {
164 global $wgUseFileCache;
166 if ( !$wgUseFileCache ) {
170 foreach ( self
::cacheablePageActions() as $type ) {
171 $fc = self
::newFromTitle( $title, $type );