MediaWiki  master
ResourceLoaderContext.php
Go to the documentation of this file.
1 <?php
27 
33  protected $resourceLoader;
34  protected $request;
35  protected $logger;
36 
37  // Module content vary
38  protected $skin;
39  protected $language;
40  protected $debug;
41  protected $user;
42 
43  // Request vary (in addition to cache vary)
44  protected $modules;
45  protected $only;
46  protected $version;
47  protected $raw;
48  protected $image;
49  protected $variant;
50  protected $format;
51 
52  protected $direction;
53  protected $hash;
54  protected $userObj;
55  protected $imageObj;
56 
62  $this->resourceLoader = $resourceLoader;
63  $this->request = $request;
64  $this->logger = $resourceLoader->getLogger();
65 
66  // Future developers: Use WebRequest::getRawVal() instead of getVal().
67  // The getVal() method performs slow Language+UTF logic. (f303bb9360)
68 
69  // List of modules
70  $modules = $request->getRawVal( 'modules' );
71  $this->modules = $modules ? ResourceLoader::expandModuleNames( $modules ) : [];
72 
73  // Various parameters
74  $this->user = $request->getRawVal( 'user' );
75  $this->debug = $request->getRawVal( 'debug' ) === 'true';
76  $this->only = $request->getRawVal( 'only', null );
77  $this->version = $request->getRawVal( 'version', null );
78  $this->raw = $request->getFuzzyBool( 'raw' );
79 
80  // Image requests
81  $this->image = $request->getRawVal( 'image' );
82  $this->variant = $request->getRawVal( 'variant' );
83  $this->format = $request->getRawVal( 'format' );
84 
85  $this->skin = $request->getRawVal( 'skin' );
86  $skinnames = Skin::getSkinNames();
87  if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
88  // The 'skin' parameter is required. (Not yet enforced.)
89  // For requests without a known skin specified,
90  // use MediaWiki's 'fallback' skin for skin-specific decisions.
91  $this->skin = 'fallback';
92  }
93  }
94 
103  public static function expandModuleNames( $modules ) {
104  wfDeprecated( __METHOD__, '1.33' );
106  }
107 
117  public static function newDummyContext() {
118  // This currently creates a non-empty instance of ResourceLoader (all modules registered),
119  // but that's probably not needed. So once that moves into ServiceWiring, this'll
120  // become more like the EmptyResourceLoader class we have in PHPUnit tests, which
121  // is what this should've had originally. If this turns out to be untrue, change to:
122  // `MediaWikiServices::getInstance()->getResourceLoader()` instead.
123  return new self( new ResourceLoader(
124  MediaWikiServices::getInstance()->getMainConfig(),
125  LoggerFactory::getInstance( 'resourceloader' )
126  ), new FauxRequest( [] ) );
127  }
128 
132  public function getResourceLoader() {
133  return $this->resourceLoader;
134  }
135 
139  public function getConfig() {
140  return $this->getResourceLoader()->getConfig();
141  }
142 
146  public function getRequest() {
147  return $this->request;
148  }
149 
154  public function getLogger() {
155  return $this->logger;
156  }
157 
161  public function getModules() {
162  return $this->modules;
163  }
164 
168  public function getLanguage() {
169  if ( $this->language === null ) {
170  // Must be a valid language code after this point (T64849)
171  // Only support uselang values that follow built-in conventions (T102058)
172  $lang = $this->getRequest()->getRawVal( 'lang', '' );
173  // Stricter version of RequestContext::sanitizeLangCode()
175  // The 'lang' parameter is required. (Not yet enforced.)
176  // If omitted, localise with the dummy language code.
177  $lang = 'qqx';
178  }
179  $this->language = $lang;
180  }
181  return $this->language;
182  }
183 
187  public function getDirection() {
188  if ( $this->direction === null ) {
189  $this->direction = $this->getRequest()->getRawVal( 'dir' );
190  if ( !$this->direction ) {
191  // Determine directionality based on user language (T8100)
192  $this->direction = Language::factory( $this->getLanguage() )->getDir();
193  }
194  }
195  return $this->direction;
196  }
197 
201  public function getSkin() {
202  return $this->skin;
203  }
204 
208  public function getUser() {
209  return $this->user;
210  }
211 
221  public function msg( $key ) {
222  return wfMessage( ...func_get_args() )
223  ->inLanguage( $this->getLanguage() )
224  // Use a dummy title because there is no real title
225  // for this endpoint, and the cache won't vary on it
226  // anyways.
227  ->title( Title::newFromText( 'Dwimmerlaik' ) );
228  }
229 
236  public function getUserObj() {
237  if ( $this->userObj === null ) {
238  $username = $this->getUser();
239  if ( $username ) {
240  // Use provided username if valid, fallback to anonymous user
241  $this->userObj = User::newFromName( $username ) ?: new User;
242  } else {
243  // Anonymous user
244  $this->userObj = new User;
245  }
246  }
247 
248  return $this->userObj;
249  }
250 
254  public function getDebug() {
255  return $this->debug;
256  }
257 
261  public function getOnly() {
262  return $this->only;
263  }
264 
270  public function getVersion() {
271  return $this->version;
272  }
273 
277  public function getRaw() {
278  return $this->raw;
279  }
280 
284  public function getImage() {
285  return $this->image;
286  }
287 
291  public function getVariant() {
292  return $this->variant;
293  }
294 
298  public function getFormat() {
299  return $this->format;
300  }
301 
308  public function getImageObj() {
309  if ( $this->imageObj === null ) {
310  $this->imageObj = false;
311 
312  if ( !$this->image ) {
313  return $this->imageObj;
314  }
315 
316  $modules = $this->getModules();
317  if ( count( $modules ) !== 1 ) {
318  return $this->imageObj;
319  }
320 
321  $module = $this->getResourceLoader()->getModule( $modules[0] );
322  if ( !$module || !$module instanceof ResourceLoaderImageModule ) {
323  return $this->imageObj;
324  }
325 
326  $image = $module->getImage( $this->image, $this );
327  if ( !$image ) {
328  return $this->imageObj;
329  }
330 
331  $this->imageObj = $image;
332  }
333 
334  return $this->imageObj;
335  }
336 
349  public function getContentOverrideCallback() {
350  return null;
351  }
352 
356  public function shouldIncludeScripts() {
357  return $this->getOnly() === null || $this->getOnly() === 'scripts';
358  }
359 
363  public function shouldIncludeStyles() {
364  return $this->getOnly() === null || $this->getOnly() === 'styles';
365  }
366 
370  public function shouldIncludeMessages() {
371  return $this->getOnly() === null;
372  }
373 
385  public function getHash() {
386  if ( !isset( $this->hash ) ) {
387  $this->hash = implode( '|', [
388  // Module content vary
389  $this->getLanguage(),
390  $this->getSkin(),
391  $this->getDebug(),
392  $this->getUser(),
393  // Request vary
394  $this->getOnly(),
395  $this->getVersion(),
396  $this->getRaw(),
397  $this->getImage(),
398  $this->getVariant(),
399  $this->getFormat(),
400  ] );
401  }
402  return $this->hash;
403  }
404 }
getRawVal( $name, $default=null)
Fetch a scalar from the input without normalization, or return $default if it&#39;s not set...
Definition: WebRequest.php:414
ResourceLoader module for generated and embedded images.
static newDummyContext()
Return a dummy ResourceLoaderContext object suitable for passing into things that don&#39;t "really" need...
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
if(!isset( $args[0])) $lang
static getSkinNames()
Fetch the set of available skins.
Definition: Skin.php:57
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 MediaWikiServices
Definition: injection.txt:23
static expandModuleNames( $modules)
Reverse the process done by ResourceLoader::makePackedModulesString().
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:412
Prior to version
Definition: maintenance.txt:1
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
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
getContentOverrideCallback()
Return the replaced-content mapping callback.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
getFuzzyBool( $name, $default=false)
Fetch a boolean value from the input or return $default if not set.
Definition: WebRequest.php:579
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:215
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging a wrapping ErrorException create2 Corresponds to logging log_action database field and which is displayed in the UI similar to $comment or false if none Defaults to false if not set multiOccurrence Can this option be passed multiple times Defaults to false if not set this hook should only be used to add variables that depend on the current page request
Definition: hooks.txt:2159
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:780
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
static expandModuleNames( $modules)
Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to an array of module names like `[ &#39;jq...
msg( $key)
Get a Message object with context set.
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 &#39;modules&#39;.
__construct(ResourceLoader $resourceLoader, WebRequest $request)
getImageObj()
If this is a request for an image, get the ResourceLoaderImage object.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:587
if the prop value should be in the metadata multi language array format
Definition: hooks.txt:1641
Object passed around to modules which contains information about the state of a specific loader reque...
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319