MediaWiki  master
ResourceLoaderContext.php
Go to the documentation of this file.
1 <?php
26 
32  protected $resourceLoader;
33  protected $request;
34  protected $logger;
35 
36  // Module content vary
37  protected $skin;
38  protected $language;
39  protected $debug;
40  protected $user;
41 
42  // Request vary (in addition to cache vary)
43  protected $modules;
44  protected $only;
45  protected $version;
46  protected $raw;
47  protected $image;
48  protected $variant;
49  protected $format;
50 
51  protected $direction;
52  protected $hash;
53  protected $userObj;
54  protected $imageObj;
55 
61  $this->resourceLoader = $resourceLoader;
62  $this->request = $request;
63  $this->logger = $resourceLoader->getLogger();
64 
65  // List of modules
66  $modules = $request->getVal( 'modules' );
67  $this->modules = $modules ? self::expandModuleNames( $modules ) : [];
68 
69  // Various parameters
70  $this->user = $request->getVal( 'user' );
71  $this->debug = $request->getFuzzyBool(
72  'debug',
73  $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
74  );
75  $this->only = $request->getVal( 'only', null );
76  $this->version = $request->getVal( 'version', null );
77  $this->raw = $request->getFuzzyBool( 'raw' );
78 
79  // Image requests
80  $this->image = $request->getVal( 'image' );
81  $this->variant = $request->getVal( 'variant' );
82  $this->format = $request->getVal( 'format' );
83 
84  $this->skin = $request->getVal( 'skin' );
85  $skinnames = Skin::getSkinNames();
86  // If no skin is specified, or we don't recognize the skin, use the default skin
87  if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
88  $this->skin = $resourceLoader->getConfig()->get( 'DefaultSkin' );
89  }
90  }
91 
99  public static function expandModuleNames( $modules ) {
100  $retval = [];
101  $exploded = explode( '|', $modules );
102  foreach ( $exploded as $group ) {
103  if ( strpos( $group, ',' ) === false ) {
104  // This is not a set of modules in foo.bar,baz notation
105  // but a single module
106  $retval[] = $group;
107  } else {
108  // This is a set of modules in foo.bar,baz notation
109  $pos = strrpos( $group, '.' );
110  if ( $pos === false ) {
111  // Prefixless modules, i.e. without dots
112  $retval = array_merge( $retval, explode( ',', $group ) );
113  } else {
114  // We have a prefix and a bunch of suffixes
115  $prefix = substr( $group, 0, $pos ); // 'foo'
116  $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
117  foreach ( $suffixes as $suffix ) {
118  $retval[] = "$prefix.$suffix";
119  }
120  }
121  }
122  }
123  return $retval;
124  }
125 
131  public static function newDummyContext() {
132  return new self( new ResourceLoader(
133  ConfigFactory::getDefaultInstance()->makeConfig( 'main' ),
134  LoggerFactory::getInstance( 'resourceloader' )
135  ), new FauxRequest( [] ) );
136  }
137 
141  public function getResourceLoader() {
142  return $this->resourceLoader;
143  }
144 
148  public function getRequest() {
149  return $this->request;
150  }
151 
156  public function getLogger() {
157  return $this->logger;
158  }
159 
163  public function getModules() {
164  return $this->modules;
165  }
166 
170  public function getLanguage() {
171  if ( $this->language === null ) {
172  // Must be a valid language code after this point (T64849)
173  // Only support uselang values that follow built-in conventions (T102058)
174  $lang = $this->getRequest()->getVal( 'lang', '' );
175  // Stricter version of RequestContext::sanitizeLangCode()
177  wfDebug( "Invalid user language code\n" );
180  }
181  $this->language = $lang;
182  }
183  return $this->language;
184  }
185 
189  public function getDirection() {
190  if ( $this->direction === null ) {
191  $this->direction = $this->getRequest()->getVal( 'dir' );
192  if ( !$this->direction ) {
193  // Determine directionality based on user language (bug 6100)
194  $this->direction = Language::factory( $this->getLanguage() )->getDir();
195  }
196  }
197  return $this->direction;
198  }
199 
203  public function getSkin() {
204  return $this->skin;
205  }
206 
210  public function getUser() {
211  return $this->user;
212  }
213 
221  public function msg() {
222  return call_user_func_array( 'wfMessage', func_get_args() )
223  ->inLanguage( $this->getLanguage() );
224  }
225 
232  public function getUserObj() {
233  if ( $this->userObj === null ) {
234  $username = $this->getUser();
235  if ( $username ) {
236  // Use provided username if valid, fallback to anonymous user
237  $this->userObj = User::newFromName( $username ) ?: new User;
238  } else {
239  // Anonymous user
240  $this->userObj = new User;
241  }
242  }
243 
244  return $this->userObj;
245  }
246 
250  public function getDebug() {
251  return $this->debug;
252  }
253 
257  public function getOnly() {
258  return $this->only;
259  }
260 
266  public function getVersion() {
267  return $this->version;
268  }
269 
273  public function getRaw() {
274  return $this->raw;
275  }
276 
280  public function getImage() {
281  return $this->image;
282  }
283 
287  public function getVariant() {
288  return $this->variant;
289  }
290 
294  public function getFormat() {
295  return $this->format;
296  }
297 
304  public function getImageObj() {
305  if ( $this->imageObj === null ) {
306  $this->imageObj = false;
307 
308  if ( !$this->image ) {
309  return $this->imageObj;
310  }
311 
312  $modules = $this->getModules();
313  if ( count( $modules ) !== 1 ) {
314  return $this->imageObj;
315  }
316 
317  $module = $this->getResourceLoader()->getModule( $modules[0] );
318  if ( !$module || !$module instanceof ResourceLoaderImageModule ) {
319  return $this->imageObj;
320  }
321 
322  $image = $module->getImage( $this->image, $this );
323  if ( !$image ) {
324  return $this->imageObj;
325  }
326 
327  $this->imageObj = $image;
328  }
329 
330  return $this->imageObj;
331  }
332 
336  public function shouldIncludeScripts() {
337  return $this->getOnly() === null || $this->getOnly() === 'scripts';
338  }
339 
343  public function shouldIncludeStyles() {
344  return $this->getOnly() === null || $this->getOnly() === 'styles';
345  }
346 
350  public function shouldIncludeMessages() {
351  return $this->getOnly() === null;
352  }
353 
365  public function getHash() {
366  if ( !isset( $this->hash ) ) {
367  $this->hash = implode( '|', [
368  // Module content vary
369  $this->getLanguage(),
370  $this->getSkin(),
371  $this->getDebug(),
372  $this->getUser(),
373  // Request vary
374  $this->getOnly(),
375  $this->getVersion(),
376  $this->getRaw(),
377  $this->getImage(),
378  $this->getVariant(),
379  $this->getFormat(),
380  ] );
381  }
382  return $this->hash;
383  }
384 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:522
ResourceLoader module for generated and embedded images.
static newDummyContext()
Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need...
static expandModuleNames($modules)
Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to an array of module names like array(...
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
msg()
Get a Message object with context set.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:37
if(!isset($args[0])) $lang
getFuzzyBool($name, $default=false)
Fetch a boolean value from the input or return $default if not set.
Definition: WebRequest.php:548
static getSkinNames()
Fetch the set of available skins.
Definition: Skin.php:49
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Prior to version
Definition: maintenance.txt:1
$wgLanguageCode
Site language code.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Wikitext formatted, in the key only.
Definition: distributors.txt:9
static isValidBuiltInCode($code)
Returns true if a language code is of a valid form for the purposes of internal customisation of Medi...
Definition: Language.php:358
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
getVal($name, $default=null)
Fetch a scalar from the input or return $default if it's not set.
Definition: WebRequest.php:407
static getDefaultInstance()
getUserObj()
Get the possibly-cached User object for the specified username.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:776
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method.MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances.The"Spi"in MediaWiki\Logger\Spi stands for"service provider interface".An SPI is an API intended to be implemented or extended by a third party.This software design pattern is intended to enable framework extension and replaceable components.It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki.The service provider interface allows the backend logging library to be implemented in multiple ways.The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime.This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance.Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
getHash()
All factors that uniquely identify this request, except 'modules'.
__construct(ResourceLoader $resourceLoader, WebRequest $request)
getImageObj()
If this is a request for an image, get the ResourceLoaderImage object.
this hook is for auditing only etc instead of letting the login form give the generic error message that the account does not exist For when the account has been renamed or deleted or an array to pass a message key and parameters create2 Corresponds to logging log_action database field and which is displayed in the UI similar to $comment this hook should only be used to add variables that depend on the current page request
Definition: hooks.txt:1994
static factory($code)
Get a cached or new language object for a given language code.
Definition: Language.php:179
Dynamic JavaScript and CSS resource loading system.
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account incomplete not yet checked for validity & $retval
Definition: hooks.txt:242
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a skin(according to that user's preference)
if the prop value should be in the metadata multi language array format
Definition: hooks.txt:1490
Object passed around to modules which contains information about the state of a specific loader reque...