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