MediaWiki  1.29.2
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 
61  public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) {
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() {
150  return $this->resourceLoader;
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()
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 
228  public function msg() {
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 }
ResourceLoaderContext\$hash
$hash
Definition: ResourceLoaderContext.php:53
ResourceLoaderContext
Object passed around to modules which contains information about the state of a specific loader reque...
Definition: ResourceLoaderContext.php:32
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:265
ResourceLoaderContext\$logger
$logger
Definition: ResourceLoaderContext.php:35
ResourceLoaderContext\getDirection
getDirection()
Definition: ResourceLoaderContext.php:196
ResourceLoaderContext\newDummyContext
static newDummyContext()
Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need...
Definition: ResourceLoaderContext.php:139
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
ResourceLoaderContext\getResourceLoader
getResourceLoader()
Definition: ResourceLoaderContext.php:149
ResourceLoaderContext\$version
$version
Definition: ResourceLoaderContext.php:46
ResourceLoaderContext\$skin
$skin
Definition: ResourceLoaderContext.php:38
captcha-old.count
count
Definition: captcha-old.py:225
version
Prior to version
Definition: maintenance.txt:1
ResourceLoaderContext\$direction
$direction
Definition: ResourceLoaderContext.php:52
ResourceLoaderContext\getImage
getImage()
Definition: ResourceLoaderContext.php:291
ResourceLoaderContext\getModules
getModules()
Definition: ResourceLoaderContext.php:171
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ResourceLoaderContext\$user
$user
Definition: ResourceLoaderContext.php:41
ResourceLoaderContext\$image
$image
Definition: ResourceLoaderContext.php:48
ResourceLoaderContext\expandModuleNames
static expandModuleNames( $modules)
Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to an array of module names like [ 'jqu...
Definition: ResourceLoaderContext.php:107
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:556
User
User
Definition: All_system_messages.txt:425
ResourceLoaderContext\getOnly
getOnly()
Definition: ResourceLoaderContext.php:268
ResourceLoaderContext\getVariant
getVariant()
Definition: ResourceLoaderContext.php:298
ResourceLoaderContext\getFormat
getFormat()
Definition: ResourceLoaderContext.php:305
php
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
Skin\getSkinNames
static getSkinNames()
Fetch the set of available skins.
Definition: Skin.php:49
ResourceLoaderContext\$modules
$modules
Definition: ResourceLoaderContext.php:44
user
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
Definition: distributors.txt:9
ResourceLoaderContext\getRequest
getRequest()
Definition: ResourceLoaderContext.php:156
ResourceLoaderContext\$imageObj
$imageObj
Definition: ResourceLoaderContext.php:55
ResourceLoaderContext\$language
$language
Definition: ResourceLoaderContext.php:39
ResourceLoaderContext\getDebug
getDebug()
Definition: ResourceLoaderContext.php:261
ResourceLoaderContext\$raw
$raw
Definition: ResourceLoaderContext.php:47
request
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:2122
ResourceLoaderContext\getLanguage
getLanguage()
Definition: ResourceLoaderContext.php:178
ResourceLoaderContext\msg
msg()
Get a Message object with context set.
Definition: ResourceLoaderContext.php:228
ResourceLoaderContext\getVersion
getVersion()
Definition: ResourceLoaderContext.php:277
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:999
ResourceLoaderContext\shouldIncludeStyles
shouldIncludeStyles()
Definition: ResourceLoaderContext.php:354
WebRequest\getRawVal
getRawVal( $name, $default=null)
Fetch a scalar from the input without normalization, or return $default if it's not set.
Definition: WebRequest.php:413
ResourceLoaderContext\$userObj
$userObj
Definition: ResourceLoaderContext.php:54
ResourceLoaderContext\$resourceLoader
$resourceLoader
Definition: ResourceLoaderContext.php:33
Language\isValidBuiltInCode
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:362
skin
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)
ResourceLoaderContext\__construct
__construct(ResourceLoader $resourceLoader, WebRequest $request)
Definition: ResourceLoaderContext.php:61
ResourceLoaderContext\$debug
$debug
Definition: ResourceLoaderContext.php:40
$retval
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 account incomplete not yet checked for validity & $retval
Definition: hooks.txt:246
language
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
ResourceLoaderContext\shouldIncludeMessages
shouldIncludeMessages()
Definition: ResourceLoaderContext.php:361
ResourceLoaderContext\getSkin
getSkin()
Definition: ResourceLoaderContext.php:210
ResourceLoaderContext\shouldIncludeScripts
shouldIncludeScripts()
Definition: ResourceLoaderContext.php:347
format
if the prop value should be in the metadata multi language array format
Definition: hooks.txt:1630
ResourceLoaderContext\getUser
getUser()
Definition: ResourceLoaderContext.php:217
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:38
ResourceLoaderContext\$request
$request
Definition: ResourceLoaderContext.php:34
ResourceLoaderContext\getLogger
getLogger()
Definition: ResourceLoaderContext.php:164
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 as
Definition: distributors.txt:9
ResourceLoaderContext\$only
$only
Definition: ResourceLoaderContext.php:45
LoggerFactory
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
ResourceLoaderContext\$format
$format
Definition: ResourceLoaderContext.php:50
ResourceLoaderContext\$variant
$variant
Definition: ResourceLoaderContext.php:49
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:183
ResourceLoaderContext\getHash
getHash()
All factors that uniquely identify this request, except 'modules'.
Definition: ResourceLoaderContext.php:376
ResourceLoaderContext\getImageObj
getImageObj()
If this is a request for an image, get the ResourceLoaderImage object.
Definition: ResourceLoaderContext.php:315
MediaWikiServices
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
ResourceLoaderContext\getUserObj
getUserObj()
Get the possibly-cached User object for the specified username.
Definition: ResourceLoaderContext.php:243
ResourceLoaderImageModule
ResourceLoader module for generated and embedded images.
Definition: ResourceLoaderImageModule.php:29
$username
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:783
ResourceLoaderContext\getRaw
getRaw()
Definition: ResourceLoaderContext.php:284