MediaWiki  master
UserOptionsModule.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
38 class UserOptionsModule extends Module {
39 
40  protected $origin = self::ORIGIN_CORE_INDIVIDUAL;
41 
42  protected $targets = [ 'desktop', 'mobile' ];
43 
48  public function getScript( Context $context ) {
49  $user = $context->getUserObj();
50 
51  $tokens = [
52  // Replacement is tricky - T287542
53  'patrolToken' => $user->getEditToken( 'patrol' ),
54  'watchToken' => $user->getEditToken( 'watch' ),
55  'csrfToken' => $user->getEditToken(),
56  ];
57  $script = 'mw.user.tokens.set(' . $context->encodeJson( $tokens ) . ');' . "\n";
58 
59  $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
60 
61  // Optimisation: Exclude the defaults, which we load separately and allow the browser
62  // to cache across page views. The defaults are loaded before this code executes,
63  // as part of the "mediawiki.base" module.
64  $options = $userOptionsLookup->getOptions( $user, UserOptionsLookup::EXCLUDE_DEFAULTS );
65 
66  $keysToExclude = [];
67  $this->getHookRunner()->onResourceLoaderExcludeUserOptions( $keysToExclude, $context );
68  foreach ( $keysToExclude as $excludedKey ) {
69  unset( $options[ $excludedKey ] );
70  }
71 
72  // Optimisation: Only output this function call if the user has non-default settings.
73  if ( $options ) {
74  $script .= 'mw.user.options.set(' . $context->encodeJson( $options ) . ');' . "\n";
75  }
76 
77  return $script;
78  }
79 
83  public function supportsURLLoading() {
84  return false;
85  }
86 
90  public function getGroup() {
91  return self::GROUP_PRIVATE;
92  }
93 }
94 
96 class_alias( UserOptionsModule::class, 'ResourceLoaderUserOptionsModule' );
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:493
getUserObj()
Get the possibly-cached User object for the specified username.
Definition: Context.php:308
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition: Module.php:48
getHookRunner()
Get a HookRunner for running core hooks.
Definition: Module.php:304
Module for per-user private data that is transmitted on all HTML web responses.
Provides access to user options.
const EXCLUDE_DEFAULTS
Exclude user options that are set to their default value.