Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.00% covered (warning)
68.00%
17 / 25
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserOptionsModule
68.00% covered (warning)
68.00%
17 / 25
0.00% covered (danger)
0.00%
0 / 3
7.18
0.00% covered (danger)
0.00%
0 / 1
 getScript
73.91% covered (warning)
73.91%
17 / 23
0.00% covered (danger)
0.00%
0 / 1
4.28
 supportsURLLoading
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\ResourceLoader;
4
5use MediaWiki\MainConfigNames;
6use MediaWiki\MediaWikiServices;
7use MediaWiki\User\Options\UserOptionsLookup;
8use MediaWiki\User\UserTimeCorrection;
9
10/**
11 * @license GPL-2.0-or-later
12 * @file
13 * @author Trevor Parscal
14 * @author Roan Kattouw
15 */
16
17/**
18 * Module for per-user private data that is transmitted on all HTML web responses.
19 *
20 * This module is embedded by ClientHtml and sent to the browser
21 * by OutputPage as part of the HTML `<head>`.
22 *
23 * @ingroup ResourceLoader
24 * @internal
25 */
26class UserOptionsModule extends Module {
27
28    /** @inheritDoc */
29    protected $origin = self::ORIGIN_CORE_INDIVIDUAL;
30
31    /**
32     * @param Context $context
33     * @return string JavaScript code
34     */
35    public function getScript( Context $context ) {
36        $user = $context->getUserObj();
37
38        $tokens = [
39            // Replacement is tricky - T287542
40            'patrolToken' => $user->getEditToken( 'patrol' ),
41            'watchToken' => $user->getEditToken( 'watch' ),
42            'csrfToken' => $user->getEditToken(),
43        ];
44        $script = 'mw.user.tokens.set(' . $context->encodeJson( $tokens ) . ');' . "\n";
45
46        $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
47
48        // Optimisation: Exclude the defaults, which we load separately and allow the browser
49        // to cache across page views. The defaults are loaded before this code executes,
50        // as part of the "mediawiki.base" module.
51        $options = $userOptionsLookup->getOptions( $user, UserOptionsLookup::EXCLUDE_DEFAULTS );
52
53        $keysToExclude = [];
54        $this->getHookRunner()->onResourceLoaderExcludeUserOptions( $keysToExclude, $context );
55        foreach ( $keysToExclude as $excludedKey ) {
56            unset( $options[ $excludedKey ] );
57        }
58
59        // Update timezone offset (T323193)
60        if ( isset( $options['timecorrection'] ) ) {
61            $corr = new UserTimeCorrection(
62                $options['timecorrection'],
63                null,
64                $this->getConfig()->get( MainConfigNames::LocalTZoffset )
65            );
66            $options['timecorrection'] = $corr->toString();
67        }
68
69        // Optimisation: Only output this function call if the user has non-default settings.
70        if ( $options ) {
71            $script .= 'mw.user.options.set(' . $context->encodeJson( $options ) . ');' . "\n";
72        }
73
74        return $script;
75    }
76
77    /**
78     * @return bool
79     */
80    public function supportsURLLoading() {
81        return false;
82    }
83
84    /**
85     * @return string
86     */
87    public function getGroup() {
88        return self::GROUP_PRIVATE;
89    }
90}