Merge "Add CollationFa"
[lhc/web/wiklou.git] / includes / cache / HTMLFileCache.php
1 <?php
2 /**
3 * Page view caching in the file system.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 use MediaWiki\MediaWikiServices;
25
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.
30 *
31 * @ingroup Cache
32 */
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
37
38 /**
39 * Construct an HTMLFileCache object from a Title and an action
40 *
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
46 */
47 public static function newFromTitle( $title, $action ) {
48 return new self( $title, $action );
49 }
50
51 /**
52 * @param Title|string $title Title object or prefixed DB key string
53 * @param string $action
54 * @throws MWException
55 */
56 public function __construct( $title, $action ) {
57 parent::__construct();
58
59 $allowedTypes = self::cacheablePageActions();
60 if ( !in_array( $action, $allowedTypes ) ) {
61 throw new MWException( 'Invalid file cache type given.' );
62 }
63 $this->mKey = ( $title instanceof Title )
64 ? $title->getPrefixedDBkey()
65 : (string)$title;
66 $this->mType = (string)$action;
67 $this->mExt = 'html';
68 }
69
70 /**
71 * Cacheable actions
72 * @return array
73 */
74 protected static function cacheablePageActions() {
75 return [ 'view', 'history' ];
76 }
77
78 /**
79 * Get the base file cache directory
80 * @return string
81 */
82 protected function cacheDirectory() {
83 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
84 }
85
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.
89 *
90 * @return string
91 */
92 protected function typeSubdirectory() {
93 if ( $this->mType === 'view' ) {
94 return ''; // b/c to not skip existing cache
95 } else {
96 return $this->mType . '/';
97 }
98 }
99
100 /**
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
105 */
106 public static function useFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
107 $config = MediaWikiServices::getInstance()->getMainConfig();
108
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" );
113
114 return false;
115 }
116
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;
128 }
129
130 return false;
131 }
132
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();
137
138 // Check that there are no other sources of variation
139 if ( $user->getId() || $ulang->getCode() !== $config->get( 'LanguageCode' ) ) {
140 return false;
141 }
142
143 if ( $mode === self::MODE_NORMAL ) {
144 if ( $user->getNewtalk() ) {
145 return false;
146 }
147 }
148
149 // Allow extensions to disable caching
150 return Hooks::run( 'HTMLFileCache::useFileCache', [ $context ] );
151 }
152
153 /**
154 * Read from cache to context output
155 * @param IContextSource $context
156 * @param integer $mode One of the HTMLFileCache::MODE_* constants
157 * @return void
158 */
159 public function loadFromFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
160 global $wgContLang;
161 $config = MediaWikiServices::getInstance()->getMainConfig();
162
163 wfDebug( __METHOD__ . "()\n" );
164 $filename = $this->cachePath();
165
166 if ( $mode === self::MODE_OUTAGE ) {
167 // Avoid DB errors for queries in sendCacheControl()
168 $context->getTitle()->resetArticleID( 0 );
169 }
170
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 );
182 }
183 } else {
184 readfile( $filename );
185 }
186
187 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
188 }
189
190 /**
191 * Save this cache object with the given text.
192 * Use this as an ob_start() handler.
193 *
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.
198 *
199 * @param string $text
200 * @return string|bool The annotated $text or false on error
201 */
202 public function saveToFileCache( $text ) {
203 if ( strlen( $text ) < 512 ) {
204 // Disabled or empty/broken output (OOM and PHP errors)
205 return $text;
206 }
207
208 wfDebug( __METHOD__ . "()\n", 'private' );
209
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 );
217 }
218
219 // Store text to FS...
220 $compressed = $this->saveText( $text );
221 if ( $compressed === false ) {
222 return $text; // error
223 }
224
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' );
230
231 return $compressed;
232 } else {
233 return $text;
234 }
235 } else {
236 return $text;
237 }
238 }
239
240 /**
241 * Clear the file caches for a page for all actions
242 * @param Title $title
243 * @return bool Whether $wgUseFileCache is enabled
244 */
245 public static function clearFileCache( Title $title ) {
246 $config = MediaWikiServices::getInstance()->getMainConfig();
247
248 if ( !$config->get( 'UseFileCache' ) ) {
249 return false;
250 }
251
252 foreach ( self::cacheablePageActions() as $type ) {
253 $fc = new self( $title, $type );
254 $fc->clearCache();
255 }
256
257 return true;
258 }
259 }