MediaWiki  1.28.0
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  // Future developers: Avoid use of getVal() in this class, which performs
66  // expensive UTF normalisation by default. Use getRawVal() instead.
67  // Values here are either one of a finite number of internal IDs,
68  // or previously-stored user input (e.g. titles, user names) that were passed
69  // to this endpoint by ResourceLoader itself from the canonical value.
70  // Values do not come directly from user input and need not match.
71 
72  // List of modules
73  $modules = $request->getRawVal( 'modules' );
74  $this->modules = $modules ? self::expandModuleNames( $modules ) : [];
75 
76  // Various parameters
77  $this->user = $request->getRawVal( 'user' );
78  $this->debug = $request->getFuzzyBool(
79  'debug',
80  $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
81  );
82  $this->only = $request->getRawVal( 'only', null );
83  $this->version = $request->getRawVal( 'version', null );
84  $this->raw = $request->getFuzzyBool( 'raw' );
85 
86  // Image requests
87  $this->image = $request->getRawVal( 'image' );
88  $this->variant = $request->getRawVal( 'variant' );
89  $this->format = $request->getRawVal( 'format' );
90 
91  $this->skin = $request->getRawVal( 'skin' );
92  $skinnames = Skin::getSkinNames();
93  // If no skin is specified, or we don't recognize the skin, use the default skin
94  if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
95  $this->skin = $resourceLoader->getConfig()->get( 'DefaultSkin' );
96  }
97  }
98 
106  public static function expandModuleNames( $modules ) {
107  $retval = [];
108  $exploded = explode( '|', $modules );
109  foreach ( $exploded as $group ) {
110  if ( strpos( $group, ',' ) === false ) {
111  // This is not a set of modules in foo.bar,baz notation
112  // but a single module
113  $retval[] = $group;
114  } else {
115  // This is a set of modules in foo.bar,baz notation
116  $pos = strrpos( $group, '.' );
117  if ( $pos === false ) {
118  // Prefixless modules, i.e. without dots
119  $retval = array_merge( $retval, explode( ',', $group ) );
120  } else {
121  // We have a prefix and a bunch of suffixes
122  $prefix = substr( $group, 0, $pos ); // 'foo'
123  $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // [ 'bar', 'baz' ]
124  foreach ( $suffixes as $suffix ) {
125  $retval[] = "$prefix.$suffix";
126  }
127  }
128  }
129  }
130  return $retval;
131  }
132 
138  public static function newDummyContext() {
139  return new self( new ResourceLoader(
140  ConfigFactory::getDefaultInstance()->makeConfig( 'main' ),
141  LoggerFactory::getInstance( 'resourceloader' )
142  ), new FauxRequest( [] ) );
143  }
144 
148  public function getResourceLoader() {
149  return $this->resourceLoader;
150  }
151 
155  public function getRequest() {
156  return $this->request;
157  }
158 
163  public function getLogger() {
164  return $this->logger;
165  }
166 
170  public function getModules() {
171  return $this->modules;
172  }
173 
177  public function getLanguage() {
178  if ( $this->language === null ) {
179  // Must be a valid language code after this point (T64849)
180  // Only support uselang values that follow built-in conventions (T102058)
181  $lang = $this->getRequest()->getRawVal( 'lang', '' );
182  // Stricter version of RequestContext::sanitizeLangCode()
184  wfDebug( "Invalid user language code\n" );
185  $lang = $this->getResourceLoader()->getConfig()->get( 'LanguageCode' );
186  }
187  $this->language = $lang;
188  }
189  return $this->language;
190  }
191 
195  public function getDirection() {
196  if ( $this->direction === null ) {
197  $this->direction = $this->getRequest()->getRawVal( 'dir' );
198  if ( !$this->direction ) {
199  // Determine directionality based on user language (bug 6100)
200  $this->direction = Language::factory( $this->getLanguage() )->getDir();
201  }
202  }
203  return $this->direction;
204  }
205 
209  public function getSkin() {
210  return $this->skin;
211  }
212 
216  public function getUser() {
217  return $this->user;
218  }
219 
227  public function msg() {
228  return call_user_func_array( 'wfMessage', func_get_args() )
229  ->inLanguage( $this->getLanguage() )
230  // Use a dummy title because there is no real title
231  // for this endpoint, and the cache won't vary on it
232  // anyways.
233  ->title( Title::newFromText( 'Dwimmerlaik' ) );
234  }
235 
242  public function getUserObj() {
243  if ( $this->userObj === null ) {
244  $username = $this->getUser();
245  if ( $username ) {
246  // Use provided username if valid, fallback to anonymous user
247  $this->userObj = User::newFromName( $username ) ?: new User;
248  } else {
249  // Anonymous user
250  $this->userObj = new User;
251  }
252  }
253 
254  return $this->userObj;
255  }
256 
260  public function getDebug() {
261  return $this->debug;
262  }
263 
267  public function getOnly() {
268  return $this->only;
269  }
270 
276  public function getVersion() {
277  return $this->version;
278  }
279 
283  public function getRaw() {
284  return $this->raw;
285  }
286 
290  public function getImage() {
291  return $this->image;
292  }
293 
297  public function getVariant() {
298  return $this->variant;
299  }
300 
304  public function getFormat() {
305  return $this->format;
306  }
307 
314  public function getImageObj() {
315  if ( $this->imageObj === null ) {
316  $this->imageObj = false;
317 
318  if ( !$this->image ) {
319  return $this->imageObj;
320  }
321 
322  $modules = $this->getModules();
323  if ( count( $modules ) !== 1 ) {
324  return $this->imageObj;
325  }
326 
327  $module = $this->getResourceLoader()->getModule( $modules[0] );
328  if ( !$module || !$module instanceof ResourceLoaderImageModule ) {
329  return $this->imageObj;
330  }
331 
332  $image = $module->getImage( $this->image, $this );
333  if ( !$image ) {
334  return $this->imageObj;
335  }
336 
337  $this->imageObj = $image;
338  }
339 
340  return $this->imageObj;
341  }
342 
346  public function shouldIncludeScripts() {
347  return $this->getOnly() === null || $this->getOnly() === 'scripts';
348  }
349 
353  public function shouldIncludeStyles() {
354  return $this->getOnly() === null || $this->getOnly() === 'styles';
355  }
356 
360  public function shouldIncludeMessages() {
361  return $this->getOnly() === null;
362  }
363 
375  public function getHash() {
376  if ( !isset( $this->hash ) ) {
377  $this->hash = implode( '|', [
378  // Module content vary
379  $this->getLanguage(),
380  $this->getSkin(),
381  $this->getDebug(),
382  $this->getUser(),
383  // Request vary
384  $this->getOnly(),
385  $this->getVersion(),
386  $this->getRaw(),
387  $this->getImage(),
388  $this->getVariant(),
389  $this->getFormat(),
390  ] );
391  }
392  return $this->hash;
393  }
394 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:525
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 [ 'jqu...
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:578
getRawVal($name, $default=null)
Fetch a scalar from the input without normalization, or return $default if it's not set...
Definition: WebRequest.php:413
static getSkinNames()
Fetch the set of available skins.
Definition: Skin.php:49
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
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
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging a wrapping ErrorException 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:2102
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:360
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
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:802
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.
static factory($code)
Get a cached or new language object for a given language code.
Definition: Language.php:181
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:1610
Object passed around to modules which contains information about the state of a specific loader reque...