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