MediaWiki  master
ResourceLoaderUserOptionsModule.php
Go to the documentation of this file.
1 <?php
2 
5 
36  protected $origin = self::ORIGIN_CORE_INDIVIDUAL;
37 
38  protected $targets = [ 'desktop', 'mobile' ];
39 
44  public function getDependencies( ResourceLoaderContext $context = null ) {
45  return [ 'user.defaults' ];
46  }
47 
52  public function getScript( ResourceLoaderContext $context ) {
53  $user = $context->getUserObj();
54 
55  $tokens = [
56  'patrolToken' => $user->getEditToken( 'patrol' ),
57  'watchToken' => $user->getEditToken( 'watch' ),
58  'csrfToken' => $user->getEditToken(),
59  ];
60  $script = 'mw.user.tokens.set(' . $context->encodeJson( $tokens ) . ');';
61 
62  $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
63  $options = $userOptionsLookup->getOptions( $user, UserOptionsLookup::EXCLUDE_DEFAULTS );
64  // Optimisation: Only output this function call if the user has non-default settings.
65  if ( $options ) {
66  $script .= 'mw.user.options.set(' . $context->encodeJson( $options ) . ');';
67  }
68 
69  // Use FILTER_NOMIN annotation to prevent needless minification and caching (T84960).
70  return ResourceLoader::FILTER_NOMIN . $script;
71  }
72 
76  public function supportsURLLoading() {
77  return false;
78  }
79 
83  public function getGroup() {
84  return 'private';
85  }
86 }
ResourceLoaderContext
Context object that contains information about the state of a specific ResourceLoader web request.
Definition: ResourceLoaderContext.php:34
MediaWiki\User\UserOptionsLookup\getOptions
getOptions(UserIdentity $user, int $flags=0, int $queryFlags=self::READ_NORMAL)
Get all user's options.
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
ResourceLoaderUserOptionsModule\$origin
$origin
Definition: ResourceLoaderUserOptionsModule.php:36
ResourceLoaderUserOptionsModule
Module for per-user private data that is transmitted on all HTML web responses.
Definition: ResourceLoaderUserOptionsModule.php:35
ResourceLoaderUserOptionsModule\getScript
getScript(ResourceLoaderContext $context)
Definition: ResourceLoaderUserOptionsModule.php:52
ResourceLoaderUserOptionsModule\$targets
$targets
Definition: ResourceLoaderUserOptionsModule.php:38
ResourceLoaderUserOptionsModule\supportsURLLoading
supportsURLLoading()
Definition: ResourceLoaderUserOptionsModule.php:76
MediaWiki\User\UserOptionsLookup
Provides access to user options.
Definition: UserOptionsLookup.php:29
$userOptionsLookup
UserOptionsLookup $userOptionsLookup
Definition: ApiWatchlistTrait.php:33
ResourceLoaderModule
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition: ResourceLoaderModule.php:39
ResourceLoaderUserOptionsModule\getGroup
getGroup()
Definition: ResourceLoaderUserOptionsModule.php:83
ResourceLoaderUserOptionsModule\getDependencies
getDependencies(ResourceLoaderContext $context=null)
Definition: ResourceLoaderUserOptionsModule.php:44
ResourceLoaderContext\getUserObj
getUserObj()
Get the possibly-cached User object for the specified username.
Definition: ResourceLoaderContext.php:250
ResourceLoaderContext\encodeJson
encodeJson( $data)
Wrapper around json_encode that avoids needless escapes, and pretty-prints in debug mode.
Definition: ResourceLoaderContext.php:432