MediaWiki REL1_30
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: Avoid use of getVal() in this class, which performs
67 // expensive UTF normalisation by default. Use getRawVal() instead.
68 // Values here are either one of a finite number of internal IDs,
69 // or previously-stored user input (e.g. titles, user names) that were passed
70 // to this endpoint by ResourceLoader itself from the canonical value.
71 // Values do not come directly from user input and need not match.
72
73 // List of modules
74 $modules = $request->getRawVal( 'modules' );
75 $this->modules = $modules ? self::expandModuleNames( $modules ) : [];
76
77 // Various parameters
78 $this->user = $request->getRawVal( 'user' );
79 $this->debug = $request->getFuzzyBool(
80 'debug',
81 $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
82 );
83 $this->only = $request->getRawVal( 'only', null );
84 $this->version = $request->getRawVal( 'version', null );
85 $this->raw = $request->getFuzzyBool( 'raw' );
86
87 // Image requests
88 $this->image = $request->getRawVal( 'image' );
89 $this->variant = $request->getRawVal( 'variant' );
90 $this->format = $request->getRawVal( 'format' );
91
92 $this->skin = $request->getRawVal( 'skin' );
93 $skinnames = Skin::getSkinNames();
94 // If no skin is specified, or we don't recognize the skin, use the default skin
95 if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
96 $this->skin = $resourceLoader->getConfig()->get( 'DefaultSkin' );
97 }
98 }
99
107 public static function expandModuleNames( $modules ) {
108 $retval = [];
109 $exploded = explode( '|', $modules );
110 foreach ( $exploded as $group ) {
111 if ( strpos( $group, ',' ) === false ) {
112 // This is not a set of modules in foo.bar,baz notation
113 // but a single module
114 $retval[] = $group;
115 } else {
116 // This is a set of modules in foo.bar,baz notation
117 $pos = strrpos( $group, '.' );
118 if ( $pos === false ) {
119 // Prefixless modules, i.e. without dots
120 $retval = array_merge( $retval, explode( ',', $group ) );
121 } else {
122 // We have a prefix and a bunch of suffixes
123 $prefix = substr( $group, 0, $pos ); // 'foo'
124 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // [ 'bar', 'baz' ]
125 foreach ( $suffixes as $suffix ) {
126 $retval[] = "$prefix.$suffix";
127 }
128 }
129 }
130 }
131 return $retval;
132 }
133
139 public static function newDummyContext() {
140 return new self( new ResourceLoader(
141 MediaWikiServices::getInstance()->getMainConfig(),
142 LoggerFactory::getInstance( 'resourceloader' )
143 ), new FauxRequest( [] ) );
144 }
145
149 public function getResourceLoader() {
151 }
152
156 public function getRequest() {
157 return $this->request;
158 }
159
164 public function getLogger() {
165 return $this->logger;
166 }
167
171 public function getModules() {
172 return $this->modules;
173 }
174
178 public function getLanguage() {
179 if ( $this->language === null ) {
180 // Must be a valid language code after this point (T64849)
181 // Only support uselang values that follow built-in conventions (T102058)
182 $lang = $this->getRequest()->getRawVal( 'lang', '' );
183 // Stricter version of RequestContext::sanitizeLangCode()
184 if ( !Language::isValidBuiltInCode( $lang ) ) {
185 wfDebug( "Invalid user language code\n" );
186 $lang = $this->getResourceLoader()->getConfig()->get( 'LanguageCode' );
187 }
188 $this->language = $lang;
189 }
190 return $this->language;
191 }
192
196 public function getDirection() {
197 if ( $this->direction === null ) {
198 $this->direction = $this->getRequest()->getRawVal( 'dir' );
199 if ( !$this->direction ) {
200 // Determine directionality based on user language (T8100)
201 $this->direction = Language::factory( $this->getLanguage() )->getDir();
202 }
203 }
204 return $this->direction;
205 }
206
210 public function getSkin() {
211 return $this->skin;
212 }
213
217 public function getUser() {
218 return $this->user;
219 }
220
230 public function msg( $key ) {
231 return call_user_func_array( 'wfMessage', func_get_args() )
232 ->inLanguage( $this->getLanguage() )
233 // Use a dummy title because there is no real title
234 // for this endpoint, and the cache won't vary on it
235 // anyways.
236 ->title( Title::newFromText( 'Dwimmerlaik' ) );
237 }
238
245 public function getUserObj() {
246 if ( $this->userObj === null ) {
247 $username = $this->getUser();
248 if ( $username ) {
249 // Use provided username if valid, fallback to anonymous user
250 $this->userObj = User::newFromName( $username ) ?: new User;
251 } else {
252 // Anonymous user
253 $this->userObj = new User;
254 }
255 }
256
257 return $this->userObj;
258 }
259
263 public function getDebug() {
264 return $this->debug;
265 }
266
270 public function getOnly() {
271 return $this->only;
272 }
273
279 public function getVersion() {
280 return $this->version;
281 }
282
286 public function getRaw() {
287 return $this->raw;
288 }
289
293 public function getImage() {
294 return $this->image;
295 }
296
300 public function getVariant() {
301 return $this->variant;
302 }
303
307 public function getFormat() {
308 return $this->format;
309 }
310
317 public function getImageObj() {
318 if ( $this->imageObj === null ) {
319 $this->imageObj = false;
320
321 if ( !$this->image ) {
322 return $this->imageObj;
323 }
324
325 $modules = $this->getModules();
326 if ( count( $modules ) !== 1 ) {
327 return $this->imageObj;
328 }
329
330 $module = $this->getResourceLoader()->getModule( $modules[0] );
331 if ( !$module || !$module instanceof ResourceLoaderImageModule ) {
332 return $this->imageObj;
333 }
334
335 $image = $module->getImage( $this->image, $this );
336 if ( !$image ) {
337 return $this->imageObj;
338 }
339
340 $this->imageObj = $image;
341 }
342
343 return $this->imageObj;
344 }
345
349 public function shouldIncludeScripts() {
350 return $this->getOnly() === null || $this->getOnly() === 'scripts';
351 }
352
356 public function shouldIncludeStyles() {
357 return $this->getOnly() === null || $this->getOnly() === 'styles';
358 }
359
363 public function shouldIncludeMessages() {
364 return $this->getOnly() === null;
365 }
366
378 public function getHash() {
379 if ( !isset( $this->hash ) ) {
380 $this->hash = implode( '|', [
381 // Module content vary
382 $this->getLanguage(),
383 $this->getSkin(),
384 $this->getDebug(),
385 $this->getUser(),
386 // Request vary
387 $this->getOnly(),
388 $this->getVersion(),
389 $this->getRaw(),
390 $this->getImage(),
391 $this->getVariant(),
392 $this->getFormat(),
393 ] );
394 }
395 return $this->hash;
396 }
397}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
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)
Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to an array of module names like [ 'jqu...
ResourceLoader module for generated and embedded images.
Dynamic JavaScript and CSS resource loading system.
static getSkinNames()
Fetch the set of available skins.
Definition Skin.php:51
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
getRawVal( $name, $default=null)
Fetch a scalar from the input without normalization, or return $default if it's not set.
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)
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
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.
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:266
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:2194
if the prop value should be in the metadata multi language array format
Definition hooks.txt:1646
this hook is for auditing only or null if authentication failed before getting that far $username
Definition hooks.txt:783
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