Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.89% |
16 / 18 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
UserOptionsModule | |
88.89% |
16 / 18 |
|
33.33% |
1 / 3 |
5.03 | |
0.00% |
0 / 1 |
getScript | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
3 | |||
supportsURLLoading | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getGroup | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\ResourceLoader; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | use MediaWiki\User\Options\UserOptionsLookup; |
7 | |
8 | /** |
9 | * This program is free software; you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation; either version 2 of the License, or |
12 | * (at your option) any later version. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write to the Free Software Foundation, Inc., |
21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
22 | * http://www.gnu.org/copyleft/gpl.html |
23 | * |
24 | * @file |
25 | * @author Trevor Parscal |
26 | * @author Roan Kattouw |
27 | */ |
28 | |
29 | /** |
30 | * Module for per-user private data that is transmitted on all HTML web responses. |
31 | * |
32 | * This module is embedded by ClientHtml and sent to the browser |
33 | * by OutputPage as part of the HTML `<head>`. |
34 | * |
35 | * @ingroup ResourceLoader |
36 | * @internal |
37 | */ |
38 | class UserOptionsModule extends Module { |
39 | |
40 | /** @inheritDoc */ |
41 | protected $origin = self::ORIGIN_CORE_INDIVIDUAL; |
42 | |
43 | /** |
44 | * @param Context $context |
45 | * @return string JavaScript code |
46 | */ |
47 | public function getScript( Context $context ) { |
48 | $user = $context->getUserObj(); |
49 | |
50 | $tokens = [ |
51 | // Replacement is tricky - T287542 |
52 | 'patrolToken' => $user->getEditToken( 'patrol' ), |
53 | 'watchToken' => $user->getEditToken( 'watch' ), |
54 | 'csrfToken' => $user->getEditToken(), |
55 | ]; |
56 | $script = 'mw.user.tokens.set(' . $context->encodeJson( $tokens ) . ');' . "\n"; |
57 | |
58 | $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup(); |
59 | |
60 | // Optimisation: Exclude the defaults, which we load separately and allow the browser |
61 | // to cache across page views. The defaults are loaded before this code executes, |
62 | // as part of the "mediawiki.base" module. |
63 | $options = $userOptionsLookup->getOptions( $user, UserOptionsLookup::EXCLUDE_DEFAULTS ); |
64 | |
65 | $keysToExclude = []; |
66 | $this->getHookRunner()->onResourceLoaderExcludeUserOptions( $keysToExclude, $context ); |
67 | foreach ( $keysToExclude as $excludedKey ) { |
68 | unset( $options[ $excludedKey ] ); |
69 | } |
70 | |
71 | // Optimisation: Only output this function call if the user has non-default settings. |
72 | if ( $options ) { |
73 | $script .= 'mw.user.options.set(' . $context->encodeJson( $options ) . ');' . "\n"; |
74 | } |
75 | |
76 | return $script; |
77 | } |
78 | |
79 | /** |
80 | * @return bool |
81 | */ |
82 | public function supportsURLLoading() { |
83 | return false; |
84 | } |
85 | |
86 | /** |
87 | * @return string |
88 | */ |
89 | public function getGroup() { |
90 | return self::GROUP_PRIVATE; |
91 | } |
92 | } |