3 * Context for ResourceLoader modules.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Trevor Parscal
22 * @author Roan Kattouw
25 use MediaWiki\Logger\LoggerFactory
;
28 * Object passed around to modules which contains information about the state
29 * of a specific loader request
31 class ResourceLoaderContext
{
32 protected $resourceLoader;
36 // Module content vary
42 // Request vary (in addition to cache vary)
57 * @param ResourceLoader $resourceLoader
58 * @param WebRequest $request
60 public function __construct( ResourceLoader
$resourceLoader, WebRequest
$request ) {
61 $this->resourceLoader
= $resourceLoader;
62 $this->request
= $request;
63 $this->logger
= $resourceLoader->getLogger();
66 $modules = $request->getVal( 'modules' );
67 $this->modules
= $modules ? self
::expandModuleNames( $modules ) : [];
70 $this->user
= $request->getVal( 'user' );
71 $this->debug
= $request->getFuzzyBool(
73 $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
75 $this->only
= $request->getVal( 'only', null );
76 $this->version
= $request->getVal( 'version', null );
77 $this->raw
= $request->getFuzzyBool( 'raw' );
80 $this->image
= $request->getVal( 'image' );
81 $this->variant
= $request->getVal( 'variant' );
82 $this->format
= $request->getVal( 'format' );
84 $this->skin
= $request->getVal( 'skin' );
85 $skinnames = Skin
::getSkinNames();
86 // If no skin is specified, or we don't recognize the skin, use the default skin
87 if ( !$this->skin ||
!isset( $skinnames[$this->skin
] ) ) {
88 $this->skin
= $resourceLoader->getConfig()->get( 'DefaultSkin' );
93 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
94 * an array of module names like array( 'jquery.foo', 'jquery.bar',
95 * 'jquery.ui.baz', 'jquery.ui.quux' )
96 * @param string $modules Packed module name list
97 * @return array Array of module names
99 public static function expandModuleNames( $modules ) {
101 $exploded = explode( '|', $modules );
102 foreach ( $exploded as $group ) {
103 if ( strpos( $group, ',' ) === false ) {
104 // This is not a set of modules in foo.bar,baz notation
105 // but a single module
108 // This is a set of modules in foo.bar,baz notation
109 $pos = strrpos( $group, '.' );
110 if ( $pos === false ) {
111 // Prefixless modules, i.e. without dots
112 $retval = array_merge( $retval, explode( ',', $group ) );
114 // We have a prefix and a bunch of suffixes
115 $prefix = substr( $group, 0, $pos ); // 'foo'
116 $suffixes = explode( ',', substr( $group, $pos +
1 ) ); // array( 'bar', 'baz' )
117 foreach ( $suffixes as $suffix ) {
118 $retval[] = "$prefix.$suffix";
127 * Return a dummy ResourceLoaderContext object suitable for passing into
128 * things that don't "really" need a context.
129 * @return ResourceLoaderContext
131 public static function newDummyContext() {
132 return new self( new ResourceLoader(
133 ConfigFactory
::getDefaultInstance()->makeConfig( 'main' ),
134 LoggerFactory
::getInstance( 'resourceloader' )
135 ), new FauxRequest( [] ) );
139 * @return ResourceLoader
141 public function getResourceLoader() {
142 return $this->resourceLoader
;
148 public function getRequest() {
149 return $this->request
;
154 * @return \Psr\Log\LoggerInterface
156 public function getLogger() {
157 return $this->logger
;
163 public function getModules() {
164 return $this->modules
;
170 public function getLanguage() {
171 if ( $this->language
=== null ) {
172 // Must be a valid language code after this point (T64849)
173 // Only support uselang values that follow built-in conventions (T102058)
174 $lang = $this->getRequest()->getVal( 'lang', '' );
175 // Stricter version of RequestContext::sanitizeLangCode()
176 if ( !Language
::isValidBuiltInCode( $lang ) ) {
177 wfDebug( "Invalid user language code\n" );
178 global $wgLanguageCode;
179 $lang = $wgLanguageCode;
181 $this->language
= $lang;
183 return $this->language
;
189 public function getDirection() {
190 if ( $this->direction
=== null ) {
191 $this->direction
= $this->getRequest()->getVal( 'dir' );
192 if ( !$this->direction
) {
193 // Determine directionality based on user language (bug 6100)
194 $this->direction
= Language
::factory( $this->getLanguage() )->getDir();
197 return $this->direction
;
203 public function getSkin() {
208 * @return string|null
210 public function getUser() {
215 * Get a Message object with context set. See wfMessage for parameters.
221 public function msg() {
222 return call_user_func_array( 'wfMessage', func_get_args() )
223 ->inLanguage( $this->getLanguage() );
227 * Get the possibly-cached User object for the specified username
232 public function getUserObj() {
233 if ( $this->userObj
=== null ) {
234 $username = $this->getUser();
236 // Use provided username if valid, fallback to anonymous user
237 $this->userObj
= User
::newFromName( $username ) ?
: new User
;
240 $this->userObj
= new User
;
244 return $this->userObj
;
250 public function getDebug() {
255 * @return string|null
257 public function getOnly() {
262 * @see ResourceLoaderModule::getVersionHash
263 * @see OutputPage::makeResourceLoaderLink
264 * @return string|null
266 public function getVersion() {
267 return $this->version
;
273 public function getRaw() {
278 * @return string|null
280 public function getImage() {
285 * @return string|null
287 public function getVariant() {
288 return $this->variant
;
292 * @return string|null
294 public function getFormat() {
295 return $this->format
;
299 * If this is a request for an image, get the ResourceLoaderImage object.
302 * @return ResourceLoaderImage|bool false if a valid object cannot be created
304 public function getImageObj() {
305 if ( $this->imageObj
=== null ) {
306 $this->imageObj
= false;
308 if ( !$this->image
) {
309 return $this->imageObj
;
312 $modules = $this->getModules();
313 if ( count( $modules ) !== 1 ) {
314 return $this->imageObj
;
317 $module = $this->getResourceLoader()->getModule( $modules[0] );
318 if ( !$module ||
!$module instanceof ResourceLoaderImageModule
) {
319 return $this->imageObj
;
322 $image = $module->getImage( $this->image
, $this );
324 return $this->imageObj
;
327 $this->imageObj
= $image;
330 return $this->imageObj
;
336 public function shouldIncludeScripts() {
337 return $this->getOnly() === null ||
$this->getOnly() === 'scripts';
343 public function shouldIncludeStyles() {
344 return $this->getOnly() === null ||
$this->getOnly() === 'styles';
350 public function shouldIncludeMessages() {
351 return $this->getOnly() === null;
355 * All factors that uniquely identify this request, except 'modules'.
357 * The list of modules is excluded here for legacy reasons as most callers already
358 * split up handling of individual modules. Including it here would massively fragment
359 * the cache and decrease its usefulness.
361 * E.g. Used by RequestFileCache to form a cache key for storing the reponse output.
365 public function getHash() {
366 if ( !isset( $this->hash
) ) {
367 $this->hash
= implode( '|', [
368 // Module content vary
369 $this->getLanguage(),