MediaWiki  master
ResourceLoaderUserOptionsModule.php
Go to the documentation of this file.
1 <?php
2 
5 
37 
38  protected $origin = self::ORIGIN_CORE_INDIVIDUAL;
39 
40  protected $targets = [ 'desktop', 'mobile' ];
41 
46  public function getScript( ResourceLoaderContext $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 }
UserOptionsLookup $userOptionsLookup
MediaWikiServices is the service locator for the application scope of MediaWiki.
Provides access to user options.
getOptions(UserIdentity $user, int $flags=0, int $queryFlags=self::READ_NORMAL)
Get all user's options.
Context object that contains information about the state of a specific ResourceLoader web request.
getUserObj()
Get the possibly-cached User object for the specified username.
encodeJson( $data)
Wrapper around json_encode that avoids needless escapes, and pretty-prints in debug mode.
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
getHookRunner()
Get a HookRunner for running core hooks.
Module for per-user private data that is transmitted on all HTML web responses.
getScript(ResourceLoaderContext $context)