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 | protected $origin = self::ORIGIN_CORE_INDIVIDUAL; |
41 | |
42 | /** |
43 | * @param Context $context |
44 | * @return string JavaScript code |
45 | */ |
46 | public function getScript( Context $context ) { |
47 | $user = $context->getUserObj(); |
48 | |
49 | $tokens = [ |
50 | // Replacement is tricky - T287542 |
51 | 'patrolToken' => $user->getEditToken( 'patrol' ), |
52 | 'watchToken' => $user->getEditToken( 'watch' ), |
53 | 'csrfToken' => $user->getEditToken(), |
54 | ]; |
55 | $script = 'mw.user.tokens.set(' . $context->encodeJson( $tokens ) . ');' . "\n"; |
56 | |
57 | $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup(); |
58 | |
59 | // Optimisation: Exclude the defaults, which we load separately and allow the browser |
60 | // to cache across page views. The defaults are loaded before this code executes, |
61 | // as part of the "mediawiki.base" module. |
62 | $options = $userOptionsLookup->getOptions( $user, UserOptionsLookup::EXCLUDE_DEFAULTS ); |
63 | |
64 | $keysToExclude = []; |
65 | $this->getHookRunner()->onResourceLoaderExcludeUserOptions( $keysToExclude, $context ); |
66 | foreach ( $keysToExclude as $excludedKey ) { |
67 | unset( $options[ $excludedKey ] ); |
68 | } |
69 | |
70 | // Optimisation: Only output this function call if the user has non-default settings. |
71 | if ( $options ) { |
72 | $script .= 'mw.user.options.set(' . $context->encodeJson( $options ) . ');' . "\n"; |
73 | } |
74 | |
75 | return $script; |
76 | } |
77 | |
78 | /** |
79 | * @return bool |
80 | */ |
81 | public function supportsURLLoading() { |
82 | return false; |
83 | } |
84 | |
85 | /** |
86 | * @return string |
87 | */ |
88 | public function getGroup() { |
89 | return self::GROUP_PRIVATE; |
90 | } |
91 | } |