Apply wfBCP47() to HTTP response header Content-Language
[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 /**
25 * Page view caching in the file system.
26 * The only cacheable actions are "view" and "history". Also special pages
27 * will not be cached.
28 *
29 * @ingroup Cache
30 */
31 class HTMLFileCache extends FileCacheBase {
32 const MODE_NORMAL = 0; // normal cache mode
33 const MODE_OUTAGE = 1; // fallback cache for DB outages
34
35 /**
36 * Construct an HTMLFileCache object from a Title and an action
37 *
38 * @deprecated since 1.24, instantiate this class directly
39 * @param Title|string $title Title object or prefixed DB key string
40 * @param string $action
41 * @throws MWException
42 * @return HTMLFileCache
43 */
44 public static function newFromTitle( $title, $action ) {
45 return new self( $title, $action );
46 }
47
48 /**
49 * @param Title|string $title Title object or prefixed DB key string
50 * @param string $action
51 * @throws MWException
52 */
53 public function __construct( $title, $action ) {
54 parent::__construct();
55 $allowedTypes = self::cacheablePageActions();
56 if ( !in_array( $action, $allowedTypes ) ) {
57 throw new MWException( 'Invalid file cache type given.' );
58 }
59 $this->mKey = ( $title instanceof Title )
60 ? $title->getPrefixedDBkey()
61 : (string)$title;
62 $this->mType = (string)$action;
63 $this->mExt = 'html';
64 }
65
66 /**
67 * Cacheable actions
68 * @return array
69 */
70 protected static function cacheablePageActions() {
71 return [ 'view', 'history' ];
72 }
73
74 /**
75 * Get the base file cache directory
76 * @return string
77 */
78 protected function cacheDirectory() {
79 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
80 }
81
82 /**
83 * Get the cache type subdirectory (with the trailing slash) or the empty string
84 * Alter the type -> directory mapping to put action=view cache at the root.
85 *
86 * @return string
87 */
88 protected function typeSubdirectory() {
89 if ( $this->mType === 'view' ) {
90 return ''; // b/c to not skip existing cache
91 } else {
92 return $this->mType . '/';
93 }
94 }
95
96 /**
97 * Check if pages can be cached for this request/user
98 * @param IContextSource $context
99 * @param integer $mode One of the HTMLFileCache::MODE_* constants
100 * @return bool
101 */
102 public static function useFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
103 global $wgUseFileCache, $wgDebugToolbar, $wgContLang;
104
105 if ( !$wgUseFileCache ) {
106 return false;
107 }
108 if ( $wgDebugToolbar ) {
109 wfDebug( "HTML file cache skipped. \$wgDebugToolbar on\n" );
110
111 return false;
112 }
113
114 // Get all query values
115 $queryVals = $context->getRequest()->getValues();
116 foreach ( $queryVals as $query => $val ) {
117 if ( $query === 'title' || $query === 'curid' ) {
118 continue; // note: curid sets title
119 // Normal page view in query form can have action=view.
120 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
121 continue;
122 // Below are header setting params
123 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
124 continue;
125 }
126
127 return false;
128 }
129
130 $user = $context->getUser();
131 // Check for non-standard user language; this covers uselang,
132 // and extensions for auto-detecting user language.
133 $ulang = $context->getLanguage();
134
135 // Check that there are no other sources of variation
136 if ( $user->getId() || !$ulang->equals( $wgContLang ) ) {
137 return false;
138 }
139
140 if ( $mode === self::MODE_NORMAL ) {
141 if ( $user->getNewtalk() ) {
142 return false;
143 }
144 }
145
146 // Allow extensions to disable caching
147 return Hooks::run( 'HTMLFileCache::useFileCache', [ $context ] );
148 }
149
150 /**
151 * Read from cache to context output
152 * @param IContextSource $context
153 * @param integer $mode One of the HTMLFileCache::MODE_* constants
154 * @return void
155 */
156 public function loadFromFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) {
157 global $wgMimeType, $wgContLang;
158
159 wfDebug( __METHOD__ . "()\n" );
160 $filename = $this->cachePath();
161
162 if ( $mode === self::MODE_OUTAGE ) {
163 // Avoid DB errors for queries in sendCacheControl()
164 $context->getTitle()->resetArticleID( 0 );
165 }
166
167 $context->getOutput()->sendCacheControl();
168 header( "Content-Type: $wgMimeType; charset=UTF-8" );
169 header( 'Content-Language: ' . $wgContLang->getHtmlCode() );
170 if ( $this->useGzip() ) {
171 if ( wfClientAcceptsGzip() ) {
172 header( 'Content-Encoding: gzip' );
173 readfile( $filename );
174 } else {
175 /* Send uncompressed */
176 wfDebug( __METHOD__ . " uncompressing cache file and sending it\n" );
177 readgzfile( $filename );
178 }
179 } else {
180 readfile( $filename );
181 }
182 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
183 }
184
185 /**
186 * Save this cache object with the given text.
187 * Use this as an ob_start() handler.
188 * @param string $text
189 * @return bool Whether $wgUseFileCache is enabled
190 */
191 public function saveToFileCache( $text ) {
192 global $wgUseFileCache;
193
194 if ( !$wgUseFileCache || strlen( $text ) < 512 ) {
195 // Disabled or empty/broken output (OOM and PHP errors)
196 return $text;
197 }
198
199 wfDebug( __METHOD__ . "()\n", 'private' );
200
201 $now = wfTimestampNow();
202 if ( $this->useGzip() ) {
203 $text = str_replace(
204 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
205 } else {
206 $text = str_replace(
207 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
208 }
209
210 // Store text to FS...
211 $compressed = $this->saveText( $text );
212 if ( $compressed === false ) {
213 return $text; // error
214 }
215
216 // gzip output to buffer as needed and set headers...
217 if ( $this->useGzip() ) {
218 // @todo Ugly wfClientAcceptsGzip() function - use context!
219 if ( wfClientAcceptsGzip() ) {
220 header( 'Content-Encoding: gzip' );
221
222 return $compressed;
223 } else {
224 return $text;
225 }
226 } else {
227 return $text;
228 }
229 }
230
231 /**
232 * Clear the file caches for a page for all actions
233 * @param Title $title
234 * @return bool Whether $wgUseFileCache is enabled
235 */
236 public static function clearFileCache( Title $title ) {
237 global $wgUseFileCache;
238
239 if ( !$wgUseFileCache ) {
240 return false;
241 }
242
243 foreach ( self::cacheablePageActions() as $type ) {
244 $fc = new self( $title, $type );
245 $fc->clearCache();
246 }
247
248 return true;
249 }
250 }