MediaWiki  1.27.1
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  $this->userObj = User::newFromName( $username );
237  } else {
238  $this->userObj = new User; // Anonymous user
239  }
240  }
241 
242  return $this->userObj;
243  }
244 
248  public function getDebug() {
249  return $this->debug;
250  }
251 
255  public function getOnly() {
256  return $this->only;
257  }
258 
264  public function getVersion() {
265  return $this->version;
266  }
267 
271  public function getRaw() {
272  return $this->raw;
273  }
274 
278  public function getImage() {
279  return $this->image;
280  }
281 
285  public function getVariant() {
286  return $this->variant;
287  }
288 
292  public function getFormat() {
293  return $this->format;
294  }
295 
302  public function getImageObj() {
303  if ( $this->imageObj === null ) {
304  $this->imageObj = false;
305 
306  if ( !$this->image ) {
307  return $this->imageObj;
308  }
309 
310  $modules = $this->getModules();
311  if ( count( $modules ) !== 1 ) {
312  return $this->imageObj;
313  }
314 
315  $module = $this->getResourceLoader()->getModule( $modules[0] );
316  if ( !$module || !$module instanceof ResourceLoaderImageModule ) {
317  return $this->imageObj;
318  }
319 
320  $image = $module->getImage( $this->image, $this );
321  if ( !$image ) {
322  return $this->imageObj;
323  }
324 
325  $this->imageObj = $image;
326  }
327 
328  return $this->imageObj;
329  }
330 
334  public function shouldIncludeScripts() {
335  return $this->getOnly() === null || $this->getOnly() === 'scripts';
336  }
337 
341  public function shouldIncludeStyles() {
342  return $this->getOnly() === null || $this->getOnly() === 'styles';
343  }
344 
348  public function shouldIncludeMessages() {
349  return $this->getOnly() === null;
350  }
351 
363  public function getHash() {
364  if ( !isset( $this->hash ) ) {
365  $this->hash = implode( '|', [
366  // Module content vary
367  $this->getLanguage(),
368  $this->getSkin(),
369  $this->getDebug(),
370  $this->getUser(),
371  // Request vary
372  $this->getOnly(),
373  $this->getVersion(),
374  $this->getRaw(),
375  $this->getImage(),
376  $this->getVariant(),
377  $this->getFormat(),
378  ] );
379  }
380  return $this->hash;
381  }
382 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:568
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.
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:545
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:404
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:762
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:1946
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:1473
Object passed around to modules which contains information about the state of a specific loader reque...