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