3 * Contain the HTMLFileCache class
7 class HTMLFileCache
extends FileCacheBase
{
9 * Construct an ObjectFileCache from a Title and an action
10 * @param $title Title|string Title object or prefixed DB key string
11 * @param $action string
12 * @return HTMLFileCache
14 public static function newFromTitle( $title, $action ) {
17 $allowedTypes = self
::cacheablePageActions();
18 if ( !in_array( $action, $allowedTypes ) ) {
19 throw new MWException( "Invalid filecache type given." );
21 $cache->mKey
= ( $title instanceof Title
)
22 ?
$title->getPrefixedDBkey()
24 $cache->mType
= (string)$action;
25 $cache->mExt
= 'html';
34 protected static function cacheablePageActions() {
35 return array( 'view', 'history' );
39 * Get the base file cache directory
42 protected function cacheDirectory() {
43 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
47 * Get the cache type subdirectory (with the trailing slash) or the empty string
48 * Alter the type -> directory mapping to put action=view cache at the root.
52 protected function typeSubdirectory() {
53 if ( $this->mType
=== 'view' ) {
54 return ''; // b/c to not skip existing cache
56 return $this->mType
. '/';
61 * Check if pages can be cached for this request/user
62 * @param $context IContextSource
65 public static function useFileCache( IContextSource
$context ) {
66 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang;
67 if ( !$wgUseFileCache ) {
70 if ( $wgShowIPinHeader ||
$wgDebugToolbar ) {
71 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" );
75 // Get all query values
76 $queryVals = $context->getRequest()->getValues();
77 foreach ( $queryVals as $query => $val ) {
78 if ( $query === 'title' ||
$query === 'curid' ) {
79 continue; // note: curid sets title
80 // Normal page view in query form can have action=view.
81 } elseif ( $query === 'action' && in_array( $val, self
::cacheablePageActions() ) ) {
83 // Below are header setting params
84 } elseif ( $query === 'maxage' ||
$query === 'smaxage' ) {
89 $user = $context->getUser();
90 // Check for non-standard user language; this covers uselang,
91 // and extensions for auto-detecting user language.
92 $ulang = $context->getLanguage()->getCode();
93 $clang = $wgContLang->getCode();
94 // Check that there are no other sources of variation
95 return !$user->getId() && !$user->getNewtalk() && $ulang == $clang;
99 * Read from cache to context output
100 * @param $context IContextSource
103 public function loadFromFileCache( IContextSource
$context ) {
104 global $wgMimeType, $wgLanguageCode;
106 wfDebug( __METHOD__
. "()\n");
107 $filename = $this->cachePath();
109 $context->getOutput()->sendCacheControl();
110 header( "Content-Type: $wgMimeType; charset=UTF-8" );
111 header( "Content-Language: $wgLanguageCode" );
112 if ( $this->useGzip() ) {
113 if ( wfClientAcceptsGzip() ) {
114 header( 'Content-Encoding: gzip' );
115 readfile( $filename );
117 /* Send uncompressed */
118 wfDebug( __METHOD__
. " uncompressing cache file and sending it\n" );
119 readgzfile( $filename );
122 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
126 * Save this cache object with the given text.
127 * Use this as an ob_start() handler.
128 * @param $text string
129 * @return bool Whether $wgUseFileCache is enabled
131 public function saveToFileCache( $text ) {
132 global $wgUseFileCache;
134 if ( !$wgUseFileCache ||
strlen( $text ) < 512 ) {
135 // Disabled or empty/broken output (OOM and PHP errors)
139 wfDebug( __METHOD__
. "()\n", false);
141 $now = wfTimestampNow();
142 if ( $this->useGzip() ) {
144 '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text );
147 '</html>', '<!-- Cached '.$now." -->\n</html>", $text );
150 // Store text to FS...
151 $compressed = $this->saveText( $text );
152 if ( $compressed === false ) {
153 return $text; // error
156 // gzip output to buffer as needed and set headers...
157 if ( $this->useGzip() ) {
158 // @TODO: ugly wfClientAcceptsGzip() function - use context!
159 if ( wfClientAcceptsGzip() ) {
160 header( 'Content-Encoding: gzip' );
171 * Clear the file caches for a page for all actions
172 * @param $title Title
173 * @return bool Whether $wgUseFileCache is enabled
175 public static function clearFileCache( Title
$title ) {
176 global $wgUseFileCache;
178 if ( !$wgUseFileCache ) {
182 foreach ( self
::cacheablePageActions() as $type ) {
183 $fc = self
::newFromTitle( $title, $type );