MediaWiki REL1_33
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 no skin is specified, or we don't recognize the skin, use the default skin
88 if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
89 $this->skin = $this->getConfig()->get( 'DefaultSkin' );
90 }
91 }
92
101 public static function expandModuleNames( $modules ) {
102 wfDeprecated( __METHOD__, '1.33' );
103 return ResourceLoader::expandModuleNames( $modules );
104 }
105
115 public static function newDummyContext() {
116 // This currently creates a non-empty instance of ResourceLoader (all modules registered),
117 // but that's probably not needed. So once that moves into ServiceWiring, this'll
118 // become more like the EmptyResourceLoader class we have in PHPUnit tests, which
119 // is what this should've had originally. If this turns out to be untrue, change to:
120 // `MediaWikiServices::getInstance()->getResourceLoader()` instead.
121 return new self( new ResourceLoader(
122 MediaWikiServices::getInstance()->getMainConfig(),
123 LoggerFactory::getInstance( 'resourceloader' )
124 ), new FauxRequest( [] ) );
125 }
126
130 public function getResourceLoader() {
132 }
133
137 public function getConfig() {
138 return $this->getResourceLoader()->getConfig();
139 }
140
144 public function getRequest() {
145 return $this->request;
146 }
147
152 public function getLogger() {
153 return $this->logger;
154 }
155
159 public function getModules() {
160 return $this->modules;
161 }
162
166 public function getLanguage() {
167 if ( $this->language === null ) {
168 // Must be a valid language code after this point (T64849)
169 // Only support uselang values that follow built-in conventions (T102058)
170 $lang = $this->getRequest()->getRawVal( 'lang', '' );
171 // Stricter version of RequestContext::sanitizeLangCode()
172 if ( !Language::isValidBuiltInCode( $lang ) ) {
173 $lang = $this->getConfig()->get( 'LanguageCode' );
174 }
175 $this->language = $lang;
176 }
177 return $this->language;
178 }
179
183 public function getDirection() {
184 if ( $this->direction === null ) {
185 $this->direction = $this->getRequest()->getRawVal( 'dir' );
186 if ( !$this->direction ) {
187 // Determine directionality based on user language (T8100)
188 $this->direction = Language::factory( $this->getLanguage() )->getDir();
189 }
190 }
191 return $this->direction;
192 }
193
197 public function getSkin() {
198 return $this->skin;
199 }
200
204 public function getUser() {
205 return $this->user;
206 }
207
217 public function msg( $key ) {
218 return wfMessage( ...func_get_args() )
219 ->inLanguage( $this->getLanguage() )
220 // Use a dummy title because there is no real title
221 // for this endpoint, and the cache won't vary on it
222 // anyways.
223 ->title( Title::newFromText( 'Dwimmerlaik' ) );
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
345 public function getContentOverrideCallback() {
346 return null;
347 }
348
352 public function shouldIncludeScripts() {
353 return $this->getOnly() === null || $this->getOnly() === 'scripts';
354 }
355
359 public function shouldIncludeStyles() {
360 return $this->getOnly() === null || $this->getOnly() === 'styles';
361 }
362
366 public function shouldIncludeMessages() {
367 return $this->getOnly() === null;
368 }
369
381 public function getHash() {
382 if ( !isset( $this->hash ) ) {
383 $this->hash = implode( '|', [
384 // Module content vary
385 $this->getLanguage(),
386 $this->getSkin(),
387 $this->getDebug(),
388 $this->getUser(),
389 // Request vary
390 $this->getOnly(),
391 $this->getVersion(),
392 $this->getRaw(),
393 $this->getImage(),
394 $this->getVariant(),
395 $this->getFormat(),
396 ] );
397 }
398 return $this->hash;
399 }
400}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
WebRequest clone which takes values from a provided array.
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Object passed around to modules which contains information about the state of a specific loader reque...
__construct(ResourceLoader $resourceLoader, WebRequest $request)
getImageObj()
If this is a request for an image, get the ResourceLoaderImage object.
msg( $key)
Get a Message object with context set.
getUserObj()
Get the possibly-cached User object for the specified username.
getHash()
All factors that uniquely identify this request, except 'modules'.
static newDummyContext()
Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need...
static expandModuleNames( $modules)
Reverse the process done by ResourceLoader::makePackedModulesString().
getContentOverrideCallback()
Return the replaced-content mapping callback.
ResourceLoader module for generated and embedded images.
Dynamic JavaScript and CSS resource loading system.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:585
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
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.
if the prop value should be in the metadata multi language array format
Definition hooks.txt:1651
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;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
this hook is for auditing only or null if authentication failed before getting that far $username
Definition hooks.txt:782
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:2224
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:37
Interface for localizing messages in MediaWiki.
Prior to version
if(!isset( $args[0])) $lang