3 * Contain the HTMLFileCache class
7 class HTMLFileCache
extends FileCacheBase
{
9 public static function newFromTitle( Title
$title, $action ) {
12 $allowedTypes = self
::cacheablePageActions();
13 if ( !in_array( $action, $allowedTypes ) ) {
14 throw new MWException( "Invalid filecache type given." );
16 $cache->mKey
= $title->getPrefixedDBkey();
17 $cache->mType
= (string)$action;
18 $cache->mExt
= 'html';
27 protected static function cacheablePageActions() {
28 return array( 'view', 'history' );
32 * Get the base file cache directory
35 protected function cacheDirectory() {
36 global $wgCacheDirectory, $wgFileCacheDirectory, $wgFileCacheDepth;
37 if ( $wgFileCacheDirectory ) {
38 $dir = $wgFileCacheDirectory;
39 } elseif ( $wgCacheDirectory ) {
40 $dir = "$wgCacheDirectory/object";
42 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
48 * Check if pages can be cached for this request/user
49 * @param $context RequestContext
52 public static function useFileCache( RequestContext
$context ) {
53 global $wgUseFileCache, $wgShowIPinHeader, $wgContLang;
54 if ( !$wgUseFileCache ) {
57 // Get all query values
58 $queryVals = $context->getRequest()->getValues();
59 foreach ( $queryVals as $query => $val ) {
60 if ( $query == 'title' ||
$query == 'curid' ) {
61 continue; // note: curid sets title
62 // Normal page view in query form can have action=view.
63 // Raw hits for pages also stored, like .css pages for example.
64 } elseif ( $query == 'action' && in_array( $val, self
::cacheablePageActions() ) ) {
66 // Below are header setting params
67 } elseif ( $query == 'maxage' ||
$query == 'smaxage' ) {
73 $user = $context->getUser();
74 // Check for non-standard user language; this covers uselang,
75 // and extensions for auto-detecting user language.
76 $ulang = $context->getLang()->getCode();
77 $clang = $wgContLang->getCode();
78 // Check that there are no other sources of variation
79 return !$wgShowIPinHeader && !$user->getId() && !$user->getNewtalk() && $ulang == $clang;
83 * Read from cache to context output
84 * @param $context RequestContext
87 public function loadFromFileCache( RequestContext
$context ) {
88 global $wgMimeType, $wgLanguageCode;
90 wfDebug( __METHOD__
. "()\n");
91 $filename = $this->cachePath();
92 $context->getOutput()->sendCacheControl();
93 header( "Content-Type: $wgMimeType; charset=UTF-8" );
94 header( "Content-Language: $wgLanguageCode" );
95 if ( $this->useGzip() ) {
96 if ( wfClientAcceptsGzip() ) {
97 header( 'Content-Encoding: gzip' );
99 /* Send uncompressed */
100 readgzfile( $filename );
104 readfile( $filename );
105 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
109 * Save this cache object with the given text.
110 * Use this as an ob_start() handler.
111 * @param $text string
112 * @return bool Whether $wgUseFileCache is enabled
114 public function saveToFileCache( $text ) {
115 global $wgUseFileCache;
117 if ( !$wgUseFileCache ||
strlen( $text ) < 512 ) {
118 // Disabled or empty/broken output (OOM and PHP errors)
122 wfDebug( __METHOD__
. "()\n", false);
124 $now = wfTimestampNow();
125 if ( $this->useGzip() ) {
127 '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text );
130 '</html>', '<!-- Cached '.$now." -->\n</html>", $text );
133 // Store text to FS...
134 $compressed = $this->saveText( $text );
135 if ( $compressed === false ) {
136 return $text; // error
139 // gzip output to buffer as needed and set headers...
140 if ( $this->useGzip() ) {
141 // @TODO: ugly wfClientAcceptsGzip() function - use context!
142 if ( wfClientAcceptsGzip() ) {
143 header( 'Content-Encoding: gzip' );
154 * Clear the file caches for a page for all actions
155 * @param $title Title
156 * @return bool Whether $wgUseFileCache is enabled
158 public static function clearFileCache( Title
$title ) {
159 global $wgUseFileCache;
161 if ( !$wgUseFileCache ) {
165 foreach ( self
::cacheablePageActions() as $type ) {
166 $fc = self
::newFromTitle( $title, $type );