Merge "Add .pipeline/ with dev image variant"
[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 * @param Title|string $title Title object or prefixed DB key string
40 * @param string $action
41 * @throws MWException
42 */
43 public function __construct( $title, $action ) {
44 parent::__construct();
45
46 $allowedTypes = self::cacheablePageActions();
47 if ( !in_array( $action, $allowedTypes ) ) {
48 throw new MWException( 'Invalid file cache type given.' );
49 }
50 $this->mKey = ( $title instanceof Title )
51 ? $title->getPrefixedDBkey()
52 : (string)$title;
53 $this->mType = (string)$action;
54 $this->mExt = 'html';
55 }
56
57 /**
58 * Cacheable actions
59 * @return array
60 */
61 protected static function cacheablePageActions() {
62 return [ 'view', 'history' ];
63 }
64
65 /**
66 * Get the base file cache directory
67 * @return string
68 */
69 protected function cacheDirectory() {
70 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
71 }
72
73 /**
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.
76 *
77 * @return string
78 */
79 protected function typeSubdirectory() {
80 if ( $this->mType === 'view' ) {
81 return ''; // b/c to not skip existing cache
82 } else {
83 return $this->mType . '/';
84 }
85 }
86
87 /**
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)
91 * @return bool
92 */
93 public static function useFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
94 $config = MediaWikiServices::getInstance()->getMainConfig();
95
96 if ( !$config->get( 'UseFileCache' ) && $mode !== self::MODE_REBUILD ) {
97 return false;
98 }
99
100 // Get all query values
101 $queryVals = $context->getRequest()->getValues();
102 foreach ( $queryVals as $query => $val ) {
103 if ( $query === 'title' || $query === 'curid' ) {
104 continue; // note: curid sets title
105 // Normal page view in query form can have action=view.
106 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
107 continue;
108 // Below are header setting params
109 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
110 continue;
111 }
112
113 return false;
114 }
115
116 $user = $context->getUser();
117 // Check for non-standard user language; this covers uselang,
118 // and extensions for auto-detecting user language.
119 $ulang = $context->getLanguage();
120
121 // Check that there are no other sources of variation
122 if ( $user->getId() ||
123 !$ulang->equals( MediaWikiServices::getInstance()->getContentLanguage() ) ) {
124 return false;
125 }
126
127 if ( ( $mode === self::MODE_NORMAL ) && $user->getNewtalk() ) {
128 return false;
129 }
130
131 // Allow extensions to disable caching
132 return Hooks::run( 'HTMLFileCache::useFileCache', [ $context ] );
133 }
134
135 /**
136 * Read from cache to context output
137 * @param IContextSource $context
138 * @param int $mode One of the HTMLFileCache::MODE_* constants
139 * @return void
140 */
141 public function loadFromFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
142 $config = MediaWikiServices::getInstance()->getMainConfig();
143
144 wfDebug( __METHOD__ . "()\n" );
145 $filename = $this->cachePath();
146
147 if ( $mode === self::MODE_OUTAGE ) {
148 // Avoid DB errors for queries in sendCacheControl()
149 $context->getTitle()->resetArticleID( 0 );
150 }
151
152 $context->getOutput()->sendCacheControl();
153 header( "Content-Type: {$config->get( 'MimeType' )}; charset=UTF-8" );
154 header( 'Content-Language: ' .
155 MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() );
156 if ( $this->useGzip() ) {
157 if ( wfClientAcceptsGzip() ) {
158 header( 'Content-Encoding: gzip' );
159 readfile( $filename );
160 } else {
161 /* Send uncompressed */
162 wfDebug( __METHOD__ . " uncompressing cache file and sending it\n" );
163 readgzfile( $filename );
164 }
165 } else {
166 readfile( $filename );
167 }
168
169 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
170 }
171
172 /**
173 * Save this cache object with the given text.
174 * Use this as an ob_start() handler.
175 *
176 * Normally this is only registed as a handler if $wgUseFileCache is on.
177 * If can be explicitly called by rebuildFileCache.php when it takes over
178 * handling file caching itself, disabling any automatic handling the the
179 * process.
180 *
181 * @param string $text
182 * @return string|bool The annotated $text or false on error
183 */
184 public function saveToFileCache( $text ) {
185 if ( strlen( $text ) < 512 ) {
186 // Disabled or empty/broken output (OOM and PHP errors)
187 return $text;
188 }
189
190 wfDebug( __METHOD__ . "()\n", 'private' );
191
192 $now = wfTimestampNow();
193 if ( $this->useGzip() ) {
194 $text = str_replace(
195 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
196 } else {
197 $text = str_replace(
198 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
199 }
200
201 // Store text to FS...
202 $compressed = $this->saveText( $text );
203 if ( $compressed === false ) {
204 return $text; // error
205 }
206
207 // gzip output to buffer as needed and set headers...
208 // @todo Ugly wfClientAcceptsGzip() function - use context!
209 if ( $this->useGzip() && wfClientAcceptsGzip() ) {
210 header( 'Content-Encoding: gzip' );
211
212 return $compressed;
213 }
214
215 return $text;
216 }
217
218 /**
219 * Clear the file caches for a page for all actions
220 * @param Title $title
221 * @return bool Whether $wgUseFileCache is enabled
222 */
223 public static function clearFileCache( Title $title ) {
224 $config = MediaWikiServices::getInstance()->getMainConfig();
225
226 if ( !$config->get( 'UseFileCache' ) ) {
227 return false;
228 }
229
230 foreach ( self::cacheablePageActions() as $type ) {
231 $fc = new self( $title, $type );
232 $fc->clearCache();
233 }
234
235 return true;
236 }
237 }