Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / cache / HTMLFileCache.php
blobae8efa9c82baa8b72ad4e7fb53a84e71b313f277
1 <?php
2 /**
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
20 * @file
21 * @ingroup Cache
24 use MediaWiki\MediaWikiServices;
26 /**
27 * Page view caching in the file system.
28 * The only cacheable actions are "view" and "history". Also special pages
29 * will not be cached.
31 * @ingroup Cache
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
38 /**
39 * Construct an HTMLFileCache object from a Title and an action
41 * @deprecated since 1.24, instantiate this class directly
42 * @param Title|string $title Title object or prefixed DB key string
43 * @param string $action
44 * @throws MWException
45 * @return HTMLFileCache
47 public static function newFromTitle( $title, $action ) {
48 return new self( $title, $action );
51 /**
52 * @param Title|string $title Title object or prefixed DB key string
53 * @param string $action
54 * @throws MWException
56 public function __construct( $title, $action ) {
57 parent::__construct();
59 $allowedTypes = self::cacheablePageActions();
60 if ( !in_array( $action, $allowedTypes ) ) {
61 throw new MWException( 'Invalid file cache type given.' );
63 $this->mKey = ( $title instanceof Title )
64 ? $title->getPrefixedDBkey()
65 : (string)$title;
66 $this->mType = (string)$action;
67 $this->mExt = 'html';
70 /**
71 * Cacheable actions
72 * @return array
74 protected static function cacheablePageActions() {
75 return [ 'view', 'history' ];
78 /**
79 * Get the base file cache directory
80 * @return string
82 protected function cacheDirectory() {
83 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
86 /**
87 * Get the cache type subdirectory (with the trailing slash) or the empty string
88 * Alter the type -> directory mapping to put action=view cache at the root.
90 * @return string
92 protected function typeSubdirectory() {
93 if ( $this->mType === 'view' ) {
94 return ''; // b/c to not skip existing cache
95 } else {
96 return $this->mType . '/';
101 * Check if pages can be cached for this request/user
102 * @param IContextSource $context
103 * @param integer $mode One of the HTMLFileCache::MODE_* constants (since 1.28)
104 * @return bool
106 public static function useFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
107 $config = MediaWikiServices::getInstance()->getMainConfig();
109 if ( !$config->get( 'UseFileCache' ) && $mode !== self::MODE_REBUILD ) {
110 return false;
111 } elseif ( $config->get( 'DebugToolbar' ) ) {
112 wfDebug( "HTML file cache skipped. \$wgDebugToolbar on\n" );
114 return false;
117 // Get all query values
118 $queryVals = $context->getRequest()->getValues();
119 foreach ( $queryVals as $query => $val ) {
120 if ( $query === 'title' || $query === 'curid' ) {
121 continue; // note: curid sets title
122 // Normal page view in query form can have action=view.
123 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
124 continue;
125 // Below are header setting params
126 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
127 continue;
130 return false;
133 $user = $context->getUser();
134 // Check for non-standard user language; this covers uselang,
135 // and extensions for auto-detecting user language.
136 $ulang = $context->getLanguage();
138 // Check that there are no other sources of variation
139 if ( $user->getId() || $ulang->getCode() !== $config->get( 'LanguageCode' ) ) {
140 return false;
143 if ( $mode === self::MODE_NORMAL ) {
144 if ( $user->getNewtalk() ) {
145 return false;
149 // Allow extensions to disable caching
150 return Hooks::run( 'HTMLFileCache::useFileCache', [ $context ] );
154 * Read from cache to context output
155 * @param IContextSource $context
156 * @param integer $mode One of the HTMLFileCache::MODE_* constants
157 * @return void
159 public function loadFromFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
160 global $wgContLang;
161 $config = MediaWikiServices::getInstance()->getMainConfig();
163 wfDebug( __METHOD__ . "()\n" );
164 $filename = $this->cachePath();
166 if ( $mode === self::MODE_OUTAGE ) {
167 // Avoid DB errors for queries in sendCacheControl()
168 $context->getTitle()->resetArticleID( 0 );
171 $context->getOutput()->sendCacheControl();
172 header( "Content-Type: {$config->get( 'MimeType' )}; charset=UTF-8" );
173 header( "Content-Language: {$wgContLang->getHtmlCode()}" );
174 if ( $this->useGzip() ) {
175 if ( wfClientAcceptsGzip() ) {
176 header( 'Content-Encoding: gzip' );
177 readfile( $filename );
178 } else {
179 /* Send uncompressed */
180 wfDebug( __METHOD__ . " uncompressing cache file and sending it\n" );
181 readgzfile( $filename );
183 } else {
184 readfile( $filename );
187 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
191 * Save this cache object with the given text.
192 * Use this as an ob_start() handler.
194 * Normally this is only registed as a handler if $wgUseFileCache is on.
195 * If can be explicitly called by rebuildFileCache.php when it takes over
196 * handling file caching itself, disabling any automatic handling the the
197 * process.
199 * @param string $text
200 * @return string|bool The annotated $text or false on error
202 public function saveToFileCache( $text ) {
203 if ( strlen( $text ) < 512 ) {
204 // Disabled or empty/broken output (OOM and PHP errors)
205 return $text;
208 wfDebug( __METHOD__ . "()\n", 'private' );
210 $now = wfTimestampNow();
211 if ( $this->useGzip() ) {
212 $text = str_replace(
213 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
214 } else {
215 $text = str_replace(
216 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
219 // Store text to FS...
220 $compressed = $this->saveText( $text );
221 if ( $compressed === false ) {
222 return $text; // error
225 // gzip output to buffer as needed and set headers...
226 if ( $this->useGzip() ) {
227 // @todo Ugly wfClientAcceptsGzip() function - use context!
228 if ( wfClientAcceptsGzip() ) {
229 header( 'Content-Encoding: gzip' );
231 return $compressed;
232 } else {
233 return $text;
235 } else {
236 return $text;
241 * Clear the file caches for a page for all actions
242 * @param Title $title
243 * @return bool Whether $wgUseFileCache is enabled
245 public static function clearFileCache( Title $title ) {
246 $config = MediaWikiServices::getInstance()->getMainConfig();
248 if ( !$config->get( 'UseFileCache' ) ) {
249 return false;
252 foreach ( self::cacheablePageActions() as $type ) {
253 $fc = new self( $title, $type );
254 $fc->clearCache();
257 return true;