MediaWiki  master
ResourceLoaderUserOptionsModule.php
Go to the documentation of this file.
1 <?php
32  protected $origin = self::ORIGIN_CORE_INDIVIDUAL;
33 
34  protected $targets = [ 'desktop', 'mobile' ];
35 
40  public function getDependencies( ResourceLoaderContext $context = null ) {
41  return [ 'user.defaults' ];
42  }
43 
48  public function getScript( ResourceLoaderContext $context ) {
49  $user = $context->getUserObj();
50 
51  $tokens = [
52  'patrolToken' => $user->getEditToken( 'patrol' ),
53  'watchToken' => $user->getEditToken( 'watch' ),
54  'csrfToken' => $user->getEditToken(),
55  ];
56  $script = 'mw.user.tokens.set(' . $context->encodeJson( $tokens ) . ');';
57 
58  $options = $user->getOptions( User::GETOPTIONS_EXCLUDE_DEFAULTS );
59  // Optimisation: Only output this function call if the user has non-default settings.
60  if ( $options ) {
61  $script .= 'mw.user.options.set(' . $context->encodeJson( $options ) . ');';
62  }
63 
64  // Use FILTER_NOMIN annotation to prevent needless minification and caching (T84960).
65  return ResourceLoader::FILTER_NOMIN . $script;
66  }
67 
71  public function supportsURLLoading() {
72  return false;
73  }
74 
78  public function getGroup() {
79  return 'private';
80  }
81 }
ResourceLoaderContext
Context object that contains information about the state of a specific ResourceLoader web request.
Definition: ResourceLoaderContext.php:33
ResourceLoaderUserOptionsModule\$origin
$origin
Definition: ResourceLoaderUserOptionsModule.php:32
ResourceLoaderUserOptionsModule
Module for per-user private data that is transmitted on all HTML web responses.
Definition: ResourceLoaderUserOptionsModule.php:31
ResourceLoaderUserOptionsModule\getScript
getScript(ResourceLoaderContext $context)
Definition: ResourceLoaderUserOptionsModule.php:48
User\GETOPTIONS_EXCLUDE_DEFAULTS
const GETOPTIONS_EXCLUDE_DEFAULTS
Exclude user options that are set to their default value.
Definition: User.php:80
ResourceLoaderUserOptionsModule\$targets
$targets
Definition: ResourceLoaderUserOptionsModule.php:34
ResourceLoaderUserOptionsModule\supportsURLLoading
supportsURLLoading()
Definition: ResourceLoaderUserOptionsModule.php:71
ResourceLoaderModule
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition: ResourceLoaderModule.php:39
ResourceLoaderUserOptionsModule\getGroup
getGroup()
Definition: ResourceLoaderUserOptionsModule.php:78
ResourceLoaderUserOptionsModule\getDependencies
getDependencies(ResourceLoaderContext $context=null)
Definition: ResourceLoaderUserOptionsModule.php:40
ResourceLoaderContext\getUserObj
getUserObj()
Get the possibly-cached User object for the specified username.
Definition: ResourceLoaderContext.php:223
ResourceLoaderContext\encodeJson
encodeJson( $data)
Wrapper around json_encode that avoids needless escapes, and pretty-prints in debug mode.
Definition: ResourceLoaderContext.php:405