MediaWiki master
UserOptionsModule.php
Go to the documentation of this file.
1<?php
2
4
7
39
40 protected $origin = self::ORIGIN_CORE_INDIVIDUAL;
41
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
81 public function supportsURLLoading() {
82 return false;
83 }
84
88 public function getGroup() {
89 return self::GROUP_PRIVATE;
90 }
91}
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:45
encodeJson( $data)
Wrapper around json_encode that avoids needless escapes, and pretty-prints in debug mode.
Definition Context.php:493
getUserObj()
Get the possibly-cached User object for the specified username.
Definition Context.php:300
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition Module.php:49
getHookRunner()
Get a HookRunner for running core hooks.
Definition Module.php:335
Module for per-user private data that is transmitted on all HTML web responses.
Provides access to user options.