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;
55 * @param ResourceLoader $resourceLoader
56 * @param WebRequest $request
58 public function __construct( ResourceLoader
$resourceLoader, WebRequest
$request ) {
59 $this->resourceLoader
= $resourceLoader;
60 $this->request
= $request;
63 $modules = $request->getVal( 'modules' );
64 $this->modules
= $modules ? self
::expandModuleNames( $modules ) : array();
67 $this->user
= $request->getVal( 'user' );
68 $this->debug
= $request->getFuzzyBool(
70 $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
72 $this->only
= $request->getVal( 'only', null );
73 $this->version
= $request->getVal( 'version', null );
74 $this->raw
= $request->getFuzzyBool( 'raw' );
77 $this->image
= $request->getVal( 'image' );
78 $this->variant
= $request->getVal( 'variant' );
79 $this->format
= $request->getVal( 'format' );
81 $this->skin
= $request->getVal( 'skin' );
82 $skinnames = Skin
::getSkinNames();
83 // If no skin is specified, or we don't recognize the skin, use the default skin
84 if ( !$this->skin ||
!isset( $skinnames[$this->skin
] ) ) {
85 $this->skin
= $resourceLoader->getConfig()->get( 'DefaultSkin' );
90 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
91 * an array of module names like array( 'jquery.foo', 'jquery.bar',
92 * 'jquery.ui.baz', 'jquery.ui.quux' )
93 * @param string $modules Packed module name list
94 * @return array Array of module names
96 public static function expandModuleNames( $modules ) {
98 $exploded = explode( '|', $modules );
99 foreach ( $exploded as $group ) {
100 if ( strpos( $group, ',' ) === false ) {
101 // This is not a set of modules in foo.bar,baz notation
102 // but a single module
105 // This is a set of modules in foo.bar,baz notation
106 $pos = strrpos( $group, '.' );
107 if ( $pos === false ) {
108 // Prefixless modules, i.e. without dots
109 $retval = array_merge( $retval, explode( ',', $group ) );
111 // We have a prefix and a bunch of suffixes
112 $prefix = substr( $group, 0, $pos ); // 'foo'
113 $suffixes = explode( ',', substr( $group, $pos +
1 ) ); // array( 'bar', 'baz' )
114 foreach ( $suffixes as $suffix ) {
115 $retval[] = "$prefix.$suffix";
124 * Return a dummy ResourceLoaderContext object suitable for passing into
125 * things that don't "really" need a context.
126 * @return ResourceLoaderContext
128 public static function newDummyContext() {
129 return new self( new ResourceLoader(
130 ConfigFactory
::getDefaultInstance()->makeConfig( 'main' ),
131 LoggerFactory
::getInstance( 'resourceloader' )
132 ), new FauxRequest( array() ) );
136 * @return ResourceLoader
138 public function getResourceLoader() {
139 return $this->resourceLoader
;
145 public function getRequest() {
146 return $this->request
;
152 public function getModules() {
153 return $this->modules
;
159 public function getLanguage() {
160 if ( $this->language
=== null ) {
161 // Must be a valid language code after this point (T64849)
162 // Only support uselang values that follow built-in conventions (T102058)
163 $lang = $this->getRequest()->getVal( 'lang', '' );
164 // Stricter version of RequestContext::sanitizeLangCode()
165 if ( !Language
::isValidBuiltInCode( $lang ) ) {
166 wfDebug( "Invalid user language code\n" );
167 global $wgLanguageCode;
168 $lang = $wgLanguageCode;
170 $this->language
= $lang;
172 return $this->language
;
178 public function getDirection() {
179 if ( $this->direction
=== null ) {
180 $this->direction
= $this->getRequest()->getVal( 'dir' );
181 if ( !$this->direction
) {
182 // Determine directionality based on user language (bug 6100)
183 $this->direction
= Language
::factory( $this->getLanguage() )->getDir();
186 return $this->direction
;
192 public function getSkin() {
197 * @return string|null
199 public function getUser() {
204 * Get the possibly-cached User object for the specified username
207 * @return User|bool false if a valid object cannot be created
209 public function getUserObj() {
210 if ( $this->userObj
=== null ) {
211 $username = $this->getUser();
213 // Optimize: Avoid loading a new User object if possible
215 if ( is_object( $wgUser ) && $wgUser->getName() === $username ) {
216 $this->userObj
= $wgUser;
218 $this->userObj
= User
::newFromName( $username );
221 $this->userObj
= new User
; // Anonymous user
225 return $this->userObj
;
231 public function getDebug() {
236 * @return string|null
238 public function getOnly() {
243 * @see ResourceLoaderModule::getVersionHash
244 * @see OutputPage::makeResourceLoaderLink
245 * @return string|null
247 public function getVersion() {
248 return $this->version
;
254 public function getRaw() {
259 * @return string|null
261 public function getImage() {
266 * @return string|null
268 public function getVariant() {
269 return $this->variant
;
273 * @return string|null
275 public function getFormat() {
276 return $this->format
;
280 * If this is a request for an image, get the ResourceLoaderImage object.
283 * @return ResourceLoaderImage|bool false if a valid object cannot be created
285 public function getImageObj() {
286 if ( $this->imageObj
=== null ) {
287 $this->imageObj
= false;
289 if ( !$this->image
) {
290 return $this->imageObj
;
293 $modules = $this->getModules();
294 if ( count( $modules ) !== 1 ) {
295 return $this->imageObj
;
298 $module = $this->getResourceLoader()->getModule( $modules[0] );
299 if ( !$module ||
!$module instanceof ResourceLoaderImageModule
) {
300 return $this->imageObj
;
303 $image = $module->getImage( $this->image
, $this );
305 return $this->imageObj
;
308 $this->imageObj
= $image;
311 return $this->imageObj
;
317 public function shouldIncludeScripts() {
318 return $this->getOnly() === null ||
$this->getOnly() === 'scripts';
324 public function shouldIncludeStyles() {
325 return $this->getOnly() === null ||
$this->getOnly() === 'styles';
331 public function shouldIncludeMessages() {
332 return $this->getOnly() === null;
338 public function getHash() {
339 if ( !isset( $this->hash
) ) {
340 $this->hash
= implode( '|', array(
341 $this->getLanguage(), $this->getDirection(), $this->getSkin(), $this->getUser(),
342 $this->getImage(), $this->getVariant(), $this->getFormat(),
343 $this->getDebug(), $this->getOnly(), $this->getVersion()