MediaWiki master
UserOptionsModule.php
Go to the documentation of this file.
1<?php
2
4
7
39
41 protected $origin = self::ORIGIN_CORE_INDIVIDUAL;
42
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
82 public function supportsURLLoading() {
83 return false;
84 }
85
89 public function getGroup() {
90 return self::GROUP_PRIVATE;
91 }
92}
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Context object that contains information about the state of a specific ResourceLoader web request.
Definition Context.php:46
encodeJson( $data)
Wrapper around json_encode that avoids needless escapes, and pretty-prints in debug mode.
Definition Context.php:480
getUserObj()
Get the possibly-cached User object for the specified username.
Definition Context.php:301
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition Module.php:47
getHookRunner()
Get a HookRunner for running core hooks.
Definition Module.php:316
Module for per-user private data that is transmitted on all HTML web responses.
Provides access to user options.
getOptions(UserIdentity $user, int $flags=0, int $queryFlags=IDBAccessObject::READ_NORMAL)
Get all user's options.