MediaWiki REL1_31
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 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 ? self::expandModuleNames( $modules ) : [];
72
73 // Various parameters
74 $this->user = $request->getRawVal( 'user' );
75 $this->debug = $request->getFuzzyBool(
76 'debug',
77 $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
78 );
79 $this->only = $request->getRawVal( 'only', null );
80 $this->version = $request->getRawVal( 'version', null );
81 $this->raw = $request->getFuzzyBool( 'raw' );
82
83 // Image requests
84 $this->image = $request->getRawVal( 'image' );
85 $this->variant = $request->getRawVal( 'variant' );
86 $this->format = $request->getRawVal( 'format' );
87
88 $this->skin = $request->getRawVal( 'skin' );
89 $skinnames = Skin::getSkinNames();
90 // If no skin is specified, or we don't recognize the skin, use the default skin
91 if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
92 $this->skin = $resourceLoader->getConfig()->get( 'DefaultSkin' );
93 }
94 }
95
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 MediaWikiServices::getInstance()->getMainConfig(),
141 LoggerFactory::getInstance( 'resourceloader' )
142 ), new FauxRequest( [] ) );
143 }
144
148 public function getResourceLoader() {
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()
183 if ( !Language::isValidBuiltInCode( $lang ) ) {
184 $lang = $this->getResourceLoader()->getConfig()->get( 'LanguageCode' );
185 }
186 $this->language = $lang;
187 }
188 return $this->language;
189 }
190
194 public function getDirection() {
195 if ( $this->direction === null ) {
196 $this->direction = $this->getRequest()->getRawVal( 'dir' );
197 if ( !$this->direction ) {
198 // Determine directionality based on user language (T8100)
199 $this->direction = Language::factory( $this->getLanguage() )->getDir();
200 }
201 }
202 return $this->direction;
203 }
204
208 public function getSkin() {
209 return $this->skin;
210 }
211
215 public function getUser() {
216 return $this->user;
217 }
218
228 public function msg( $key ) {
229 return call_user_func_array( 'wfMessage', func_get_args() )
230 ->inLanguage( $this->getLanguage() )
231 // Use a dummy title because there is no real title
232 // for this endpoint, and the cache won't vary on it
233 // anyways.
234 ->title( Title::newFromText( 'Dwimmerlaik' ) );
235 }
236
243 public function getUserObj() {
244 if ( $this->userObj === null ) {
245 $username = $this->getUser();
246 if ( $username ) {
247 // Use provided username if valid, fallback to anonymous user
248 $this->userObj = User::newFromName( $username ) ?: new User;
249 } else {
250 // Anonymous user
251 $this->userObj = new User;
252 }
253 }
254
255 return $this->userObj;
256 }
257
261 public function getDebug() {
262 return $this->debug;
263 }
264
268 public function getOnly() {
269 return $this->only;
270 }
271
277 public function getVersion() {
278 return $this->version;
279 }
280
284 public function getRaw() {
285 return $this->raw;
286 }
287
291 public function getImage() {
292 return $this->image;
293 }
294
298 public function getVariant() {
299 return $this->variant;
300 }
301
305 public function getFormat() {
306 return $this->format;
307 }
308
315 public function getImageObj() {
316 if ( $this->imageObj === null ) {
317 $this->imageObj = false;
318
319 if ( !$this->image ) {
320 return $this->imageObj;
321 }
322
323 $modules = $this->getModules();
324 if ( count( $modules ) !== 1 ) {
325 return $this->imageObj;
326 }
327
328 $module = $this->getResourceLoader()->getModule( $modules[0] );
329 if ( !$module || !$module instanceof ResourceLoaderImageModule ) {
330 return $this->imageObj;
331 }
332
333 $image = $module->getImage( $this->image, $this );
334 if ( !$image ) {
335 return $this->imageObj;
336 }
337
338 $this->imageObj = $image;
339 }
340
341 return $this->imageObj;
342 }
343
347 public function shouldIncludeScripts() {
348 return $this->getOnly() === null || $this->getOnly() === 'scripts';
349 }
350
354 public function shouldIncludeStyles() {
355 return $this->getOnly() === null || $this->getOnly() === 'styles';
356 }
357
361 public function shouldIncludeMessages() {
362 return $this->getOnly() === null;
363 }
364
376 public function getHash() {
377 if ( !isset( $this->hash ) ) {
378 $this->hash = implode( '|', [
379 // Module content vary
380 $this->getLanguage(),
381 $this->getSkin(),
382 $this->getDebug(),
383 $this->getUser(),
384 // Request vary
385 $this->getOnly(),
386 $this->getVersion(),
387 $this->getRaw(),
388 $this->getImage(),
389 $this->getVariant(),
390 $this->getFormat(),
391 ] );
392 }
393 return $this->hash;
394 }
395}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of either verbatim or with modifications and or translated into another language(Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying
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 ‘[ 'jq...
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:53
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:591
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 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 and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser getSkin(). See also skin.txt. Language Represents the language used for incidental text
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2806
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
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition hooks.txt:895
this hook is for auditing only or null if authentication failed before getting that far $username
Definition hooks.txt:785
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned $skin
Definition hooks.txt:2011
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext such as when responding to a resource loader request or generating HTML output & $resourceLoader
Definition hooks.txt:2818
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 $user
Definition hooks.txt:247
Interface for localizing messages in MediaWiki.
Prior to version
$debug
Definition mcc.php:31
if(!isset( $args[0])) $lang