Merge "Fix IE9's lack of support for console.warn.apply"
[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 /**
33 * Construct an ObjectFileCache from a Title and an action
34 * @param Title|string $title Title object or prefixed DB key string
35 * @param string $action
36 * @throws MWException
37 * @return HTMLFileCache
38 */
39 public static function newFromTitle( $title, $action ) {
40 $cache = new self();
41
42 $allowedTypes = self::cacheablePageActions();
43 if ( !in_array( $action, $allowedTypes ) ) {
44 throw new MWException( "Invalid filecache type given." );
45 }
46 $cache->mKey = ( $title instanceof Title )
47 ? $title->getPrefixedDBkey()
48 : (string)$title;
49 $cache->mType = (string)$action;
50 $cache->mExt = 'html';
51
52 return $cache;
53 }
54
55 /**
56 * Cacheable actions
57 * @return array
58 */
59 protected static function cacheablePageActions() {
60 return array( 'view', 'history' );
61 }
62
63 /**
64 * Get the base file cache directory
65 * @return string
66 */
67 protected function cacheDirectory() {
68 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
69 }
70
71 /**
72 * Get the cache type subdirectory (with the trailing slash) or the empty string
73 * Alter the type -> directory mapping to put action=view cache at the root.
74 *
75 * @return string
76 */
77 protected function typeSubdirectory() {
78 if ( $this->mType === 'view' ) {
79 return ''; // b/c to not skip existing cache
80 } else {
81 return $this->mType . '/';
82 }
83 }
84
85 /**
86 * Check if pages can be cached for this request/user
87 * @param IContextSource $context
88 * @return bool
89 */
90 public static function useFileCache( IContextSource $context ) {
91 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang;
92 if ( !$wgUseFileCache ) {
93 return false;
94 }
95 if ( $wgShowIPinHeader || $wgDebugToolbar ) {
96 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" );
97
98 return false;
99 }
100
101 // Get all query values
102 $queryVals = $context->getRequest()->getValues();
103 foreach ( $queryVals as $query => $val ) {
104 if ( $query === 'title' || $query === 'curid' ) {
105 continue; // note: curid sets title
106 // Normal page view in query form can have action=view.
107 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
108 continue;
109 // Below are header setting params
110 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
111 continue;
112 }
113
114 return false;
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()->getCode();
120 $clang = $wgContLang->getCode();
121
122 // Check that there are no other sources of variation
123 return !$user->getId() && !$user->getNewtalk() && $ulang == $clang;
124 }
125
126 /**
127 * Read from cache to context output
128 * @param IContextSource $context
129 * @return void
130 */
131 public function loadFromFileCache( IContextSource $context ) {
132 global $wgMimeType, $wgLanguageCode;
133
134 wfDebug( __METHOD__ . "()\n" );
135 $filename = $this->cachePath();
136
137 $context->getOutput()->sendCacheControl();
138 header( "Content-Type: $wgMimeType; charset=UTF-8" );
139 header( "Content-Language: $wgLanguageCode" );
140 if ( $this->useGzip() ) {
141 if ( wfClientAcceptsGzip() ) {
142 header( 'Content-Encoding: gzip' );
143 readfile( $filename );
144 } else {
145 /* Send uncompressed */
146 wfDebug( __METHOD__ . " uncompressing cache file and sending it\n" );
147 readgzfile( $filename );
148 }
149 } else {
150 readfile( $filename );
151 }
152 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
153 }
154
155 /**
156 * Save this cache object with the given text.
157 * Use this as an ob_start() handler.
158 * @param string $text
159 * @return bool Whether $wgUseFileCache is enabled
160 */
161 public function saveToFileCache( $text ) {
162 global $wgUseFileCache;
163
164 if ( !$wgUseFileCache || strlen( $text ) < 512 ) {
165 // Disabled or empty/broken output (OOM and PHP errors)
166 return $text;
167 }
168
169 wfDebug( __METHOD__ . "()\n", 'log' );
170
171 $now = wfTimestampNow();
172 if ( $this->useGzip() ) {
173 $text = str_replace(
174 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
175 } else {
176 $text = str_replace(
177 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
178 }
179
180 // Store text to FS...
181 $compressed = $this->saveText( $text );
182 if ( $compressed === false ) {
183 return $text; // error
184 }
185
186 // gzip output to buffer as needed and set headers...
187 if ( $this->useGzip() ) {
188 // @todo Ugly wfClientAcceptsGzip() function - use context!
189 if ( wfClientAcceptsGzip() ) {
190 header( 'Content-Encoding: gzip' );
191
192 return $compressed;
193 } else {
194 return $text;
195 }
196 } else {
197 return $text;
198 }
199 }
200
201 /**
202 * Clear the file caches for a page for all actions
203 * @param Title $title
204 * @return bool Whether $wgUseFileCache is enabled
205 */
206 public static function clearFileCache( Title $title ) {
207 global $wgUseFileCache;
208
209 if ( !$wgUseFileCache ) {
210 return false;
211 }
212
213 foreach ( self::cacheablePageActions() as $type ) {
214 $fc = self::newFromTitle( $title, $type );
215 $fc->clearCache();
216 }
217
218 return true;
219 }
220 }