3 * Page view caching in the file system.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\MediaWikiServices
;
27 * Page view caching in the file system.
28 * The only cacheable actions are "view" and "history". Also special pages
33 class HTMLFileCache
extends FileCacheBase
{
34 const MODE_NORMAL
= 0; // normal cache mode
35 const MODE_OUTAGE
= 1; // fallback cache for DB outages
36 const MODE_REBUILD
= 2; // background cache rebuild mode
39 * @param Title|string $title Title object or prefixed DB key string
40 * @param string $action
43 public function __construct( $title, $action ) {
44 parent
::__construct();
46 $allowedTypes = self
::cacheablePageActions();
47 if ( !in_array( $action, $allowedTypes ) ) {
48 throw new MWException( 'Invalid file cache type given.' );
50 $this->mKey
= ( $title instanceof Title
)
51 ?
$title->getPrefixedDBkey()
53 $this->mType
= (string)$action;
61 protected static function cacheablePageActions() {
62 return [ 'view', 'history' ];
66 * Get the base file cache directory
69 protected function cacheDirectory() {
70 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
74 * Get the cache type subdirectory (with the trailing slash) or the empty string
75 * Alter the type -> directory mapping to put action=view cache at the root.
79 protected function typeSubdirectory() {
80 if ( $this->mType
=== 'view' ) {
81 return ''; // b/c to not skip existing cache
83 return $this->mType
. '/';
88 * Check if pages can be cached for this request/user
89 * @param IContextSource $context
90 * @param int $mode One of the HTMLFileCache::MODE_* constants (since 1.28)
93 public static function useFileCache( IContextSource
$context, $mode = self
::MODE_NORMAL
) {
95 $config = MediaWikiServices
::getInstance()->getMainConfig();
97 if ( !$config->get( 'UseFileCache' ) && $mode !== self
::MODE_REBUILD
) {
99 } elseif ( $config->get( 'DebugToolbar' ) ) {
100 wfDebug( "HTML file cache skipped. \$wgDebugToolbar on\n" );
105 // Get all query values
106 $queryVals = $context->getRequest()->getValues();
107 foreach ( $queryVals as $query => $val ) {
108 if ( $query === 'title' ||
$query === 'curid' ) {
109 continue; // note: curid sets title
110 // Normal page view in query form can have action=view.
111 } elseif ( $query === 'action' && in_array( $val, self
::cacheablePageActions() ) ) {
113 // Below are header setting params
114 } elseif ( $query === 'maxage' ||
$query === 'smaxage' ) {
121 $user = $context->getUser();
122 // Check for non-standard user language; this covers uselang,
123 // and extensions for auto-detecting user language.
124 $ulang = $context->getLanguage();
126 // Check that there are no other sources of variation
127 if ( $user->getId() ||
!$ulang->equals( $wgContLang ) ) {
131 if ( $mode === self
::MODE_NORMAL
) {
132 if ( $user->getNewtalk() ) {
137 // Allow extensions to disable caching
138 return Hooks
::run( 'HTMLFileCache::useFileCache', [ $context ] );
142 * Read from cache to context output
143 * @param IContextSource $context
144 * @param int $mode One of the HTMLFileCache::MODE_* constants
147 public function loadFromFileCache( IContextSource
$context, $mode = self
::MODE_NORMAL
) {
149 $config = MediaWikiServices
::getInstance()->getMainConfig();
151 wfDebug( __METHOD__
. "()\n" );
152 $filename = $this->cachePath();
154 if ( $mode === self
::MODE_OUTAGE
) {
155 // Avoid DB errors for queries in sendCacheControl()
156 $context->getTitle()->resetArticleID( 0 );
159 $context->getOutput()->sendCacheControl();
160 header( "Content-Type: {$config->get( 'MimeType' )}; charset=UTF-8" );
161 header( "Content-Language: {$wgContLang->getHtmlCode()}" );
162 if ( $this->useGzip() ) {
163 if ( wfClientAcceptsGzip() ) {
164 header( 'Content-Encoding: gzip' );
165 readfile( $filename );
167 /* Send uncompressed */
168 wfDebug( __METHOD__
. " uncompressing cache file and sending it\n" );
169 readgzfile( $filename );
172 readfile( $filename );
175 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
179 * Save this cache object with the given text.
180 * Use this as an ob_start() handler.
182 * Normally this is only registed as a handler if $wgUseFileCache is on.
183 * If can be explicitly called by rebuildFileCache.php when it takes over
184 * handling file caching itself, disabling any automatic handling the the
187 * @param string $text
188 * @return string|bool The annotated $text or false on error
190 public function saveToFileCache( $text ) {
191 if ( strlen( $text ) < 512 ) {
192 // Disabled or empty/broken output (OOM and PHP errors)
196 wfDebug( __METHOD__
. "()\n", 'private' );
198 $now = wfTimestampNow();
199 if ( $this->useGzip() ) {
201 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
204 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
207 // Store text to FS...
208 $compressed = $this->saveText( $text );
209 if ( $compressed === false ) {
210 return $text; // error
213 // gzip output to buffer as needed and set headers...
214 if ( $this->useGzip() ) {
215 // @todo Ugly wfClientAcceptsGzip() function - use context!
216 if ( wfClientAcceptsGzip() ) {
217 header( 'Content-Encoding: gzip' );
229 * Clear the file caches for a page for all actions
230 * @param Title $title
231 * @return bool Whether $wgUseFileCache is enabled
233 public static function clearFileCache( Title
$title ) {
234 $config = MediaWikiServices
::getInstance()->getMainConfig();
236 if ( !$config->get( 'UseFileCache' ) ) {
240 foreach ( self
::cacheablePageActions() as $type ) {
241 $fc = new self( $title, $type );