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 Members */
34 protected $resourceLoader;
37 // Module content vary
43 // Request vary (in addition to cache vary)
60 * @param ResourceLoader $resourceLoader
61 * @param WebRequest $request
63 public function __construct( ResourceLoader
$resourceLoader, WebRequest
$request ) {
64 $this->resourceLoader
= $resourceLoader;
65 $this->request
= $request;
68 $modules = $request->getVal( 'modules' );
69 $this->modules
= $modules ? self
::expandModuleNames( $modules ) : array();
72 $this->user
= $request->getVal( 'user' );
73 $this->debug
= $request->getFuzzyBool(
75 $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
77 $this->only
= $request->getVal( 'only', null );
78 $this->version
= $request->getVal( 'version', null );
79 $this->raw
= $request->getFuzzyBool( 'raw' );
82 $this->image
= $request->getVal( 'image' );
83 $this->variant
= $request->getVal( 'variant' );
84 $this->format
= $request->getVal( 'format' );
86 $this->skin
= $request->getVal( 'skin' );
87 $skinnames = Skin
::getSkinNames();
88 // If no skin is specified, or we don't recognize the skin, use the default skin
89 if ( !$this->skin ||
!isset( $skinnames[$this->skin
] ) ) {
90 $this->skin
= $resourceLoader->getConfig()->get( 'DefaultSkin' );
95 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
96 * an array of module names like array( 'jquery.foo', 'jquery.bar',
97 * 'jquery.ui.baz', 'jquery.ui.quux' )
98 * @param string $modules Packed module name list
99 * @return array Array of module names
101 public static function expandModuleNames( $modules ) {
103 $exploded = explode( '|', $modules );
104 foreach ( $exploded as $group ) {
105 if ( strpos( $group, ',' ) === false ) {
106 // This is not a set of modules in foo.bar,baz notation
107 // but a single module
110 // This is a set of modules in foo.bar,baz notation
111 $pos = strrpos( $group, '.' );
112 if ( $pos === false ) {
113 // Prefixless modules, i.e. without dots
114 $retval = array_merge( $retval, explode( ',', $group ) );
116 // We have a prefix and a bunch of suffixes
117 $prefix = substr( $group, 0, $pos ); // 'foo'
118 $suffixes = explode( ',', substr( $group, $pos +
1 ) ); // array( 'bar', 'baz' )
119 foreach ( $suffixes as $suffix ) {
120 $retval[] = "$prefix.$suffix";
129 * Return a dummy ResourceLoaderContext object suitable for passing into
130 * things that don't "really" need a context.
131 * @return ResourceLoaderContext
133 public static function newDummyContext() {
134 return new self( new ResourceLoader(
135 ConfigFactory
::getDefaultInstance()->makeConfig( 'main' ),
136 LoggerFactory
::getInstance( 'resourceloader' )
137 ), new FauxRequest( array() ) );
141 * @return ResourceLoader
143 public function getResourceLoader() {
144 return $this->resourceLoader
;
150 public function getRequest() {
151 return $this->request
;
157 public function getModules() {
158 return $this->modules
;
164 public function getLanguage() {
165 if ( $this->language
=== null ) {
166 // Must be a valid language code after this point (T64849)
167 // Only support uselang values that follow built-in conventions (T102058)
168 $lang = $this->getRequest()->getVal( 'lang', '' );
169 // Stricter version of RequestContext::sanitizeLangCode()
170 if ( !Language
::isValidBuiltInCode( $lang ) ) {
171 wfDebug( "Invalid user language code\n" );
172 global $wgLanguageCode;
173 $lang = $wgLanguageCode;
175 $this->language
= $lang;
177 return $this->language
;
183 public function getDirection() {
184 if ( $this->direction
=== null ) {
185 $this->direction
= $this->getRequest()->getVal( 'dir' );
186 if ( !$this->direction
) {
187 // Determine directionality based on user language (bug 6100)
188 $this->direction
= Language
::factory( $this->getLanguage() )->getDir();
191 return $this->direction
;
197 public function getSkin() {
202 * @return string|null
204 public function getUser() {
209 * Get the possibly-cached User object for the specified username
212 * @return User|bool false if a valid object cannot be created
214 public function getUserObj() {
215 if ( $this->userObj
=== null ) {
216 $username = $this->getUser();
218 // Optimize: Avoid loading a new User object if possible
220 if ( is_object( $wgUser ) && $wgUser->getName() === $username ) {
221 $this->userObj
= $wgUser;
223 $this->userObj
= User
::newFromName( $username );
226 $this->userObj
= new User
; // Anonymous user
230 return $this->userObj
;
236 public function getDebug() {
241 * @return string|null
243 public function getOnly() {
248 * @see ResourceLoaderModule::getVersionHash
249 * @see OutputPage::makeResourceLoaderLink
250 * @return string|null
252 public function getVersion() {
253 return $this->version
;
259 public function getRaw() {
264 * @return string|null
266 public function getImage() {
271 * @return string|null
273 public function getVariant() {
274 return $this->variant
;
278 * @return string|null
280 public function getFormat() {
281 return $this->format
;
285 * If this is a request for an image, get the ResourceLoaderImage object.
288 * @return ResourceLoaderImage|bool false if a valid object cannot be created
290 public function getImageObj() {
291 if ( $this->imageObj
=== null ) {
292 $this->imageObj
= false;
294 if ( !$this->image
) {
295 return $this->imageObj
;
298 $modules = $this->getModules();
299 if ( count( $modules ) !== 1 ) {
300 return $this->imageObj
;
303 $module = $this->getResourceLoader()->getModule( $modules[0] );
304 if ( !$module ||
!$module instanceof ResourceLoaderImageModule
) {
305 return $this->imageObj
;
308 $image = $module->getImage( $this->image
, $this );
310 return $this->imageObj
;
313 $this->imageObj
= $image;
316 return $this->imageObj
;
322 public function shouldIncludeScripts() {
323 return $this->getOnly() === null ||
$this->getOnly() === 'scripts';
329 public function shouldIncludeStyles() {
330 return $this->getOnly() === null ||
$this->getOnly() === 'styles';
336 public function shouldIncludeMessages() {
337 return $this->getOnly() === null;
341 * All factors that uniquely identify this request, except 'modules'.
343 * The list of modules is excluded here for legacy reasons as most callers already
344 * split up handling of individual modules. Including it here would massively fragment
345 * the cache and decrease its usefulness.
347 * E.g. Used by RequestFileCache to form a cache key for storing the reponse output.
351 public function getHash() {
352 if ( !isset( $this->hash
) ) {
353 $this->hash
= implode( '|', array(
354 // Module content vary
355 $this->getLanguage(),