Debian Codegraph @

includes/ResourceLoader/UserOptionsModule.php

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