Add a hook to allow extensions to prevent HTML file caching
[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 if ( $user->getId() || $user->getNewtalk() || $ulang != $clang ) {
124 return false;
125 }
126 // Allow extensions to disable caching
127 return wfRunHooks( 'HTMLFileCache::useFileCache', array( $context ) );
128 }
129
130 /**
131 * Read from cache to context output
132 * @param IContextSource $context
133 * @return void
134 */
135 public function loadFromFileCache( IContextSource $context ) {
136 global $wgMimeType, $wgLanguageCode;
137
138 wfDebug( __METHOD__ . "()\n" );
139 $filename = $this->cachePath();
140
141 $context->getOutput()->sendCacheControl();
142 header( "Content-Type: $wgMimeType; charset=UTF-8" );
143 header( "Content-Language: $wgLanguageCode" );
144 if ( $this->useGzip() ) {
145 if ( wfClientAcceptsGzip() ) {
146 header( 'Content-Encoding: gzip' );
147 readfile( $filename );
148 } else {
149 /* Send uncompressed */
150 wfDebug( __METHOD__ . " uncompressing cache file and sending it\n" );
151 readgzfile( $filename );
152 }
153 } else {
154 readfile( $filename );
155 }
156 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
157 }
158
159 /**
160 * Save this cache object with the given text.
161 * Use this as an ob_start() handler.
162 * @param string $text
163 * @return bool Whether $wgUseFileCache is enabled
164 */
165 public function saveToFileCache( $text ) {
166 global $wgUseFileCache;
167
168 if ( !$wgUseFileCache || strlen( $text ) < 512 ) {
169 // Disabled or empty/broken output (OOM and PHP errors)
170 return $text;
171 }
172
173 wfDebug( __METHOD__ . "()\n", 'log' );
174
175 $now = wfTimestampNow();
176 if ( $this->useGzip() ) {
177 $text = str_replace(
178 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
179 } else {
180 $text = str_replace(
181 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
182 }
183
184 // Store text to FS...
185 $compressed = $this->saveText( $text );
186 if ( $compressed === false ) {
187 return $text; // error
188 }
189
190 // gzip output to buffer as needed and set headers...
191 if ( $this->useGzip() ) {
192 // @todo Ugly wfClientAcceptsGzip() function - use context!
193 if ( wfClientAcceptsGzip() ) {
194 header( 'Content-Encoding: gzip' );
195
196 return $compressed;
197 } else {
198 return $text;
199 }
200 } else {
201 return $text;
202 }
203 }
204
205 /**
206 * Clear the file caches for a page for all actions
207 * @param Title $title
208 * @return bool Whether $wgUseFileCache is enabled
209 */
210 public static function clearFileCache( Title $title ) {
211 global $wgUseFileCache;
212
213 if ( !$wgUseFileCache ) {
214 return false;
215 }
216
217 foreach ( self::cacheablePageActions() as $type ) {
218 $fc = self::newFromTitle( $title, $type );
219 $fc->clearCache();
220 }
221
222 return true;
223 }
224 }