3 * Context for resource loader 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;
64 $modules = $request->getVal( 'modules' );
65 $this->modules
= $modules ? self
::expandModuleNames( $modules ) : array();
67 $this->skin
= $request->getVal( 'skin' );
68 $this->user
= $request->getVal( 'user' );
69 $this->debug
= $request->getFuzzyBool(
70 'debug', $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
72 $this->only
= $request->getVal( 'only' );
73 $this->version
= $request->getVal( 'version' );
74 $this->raw
= $request->getFuzzyBool( 'raw' );
76 $this->image
= $request->getVal( 'image' );
77 $this->variant
= $request->getVal( 'variant' );
78 $this->format
= $request->getVal( 'format' );
80 $skinnames = Skin
::getSkinNames();
81 // If no skin is specified, or we don't recognize the skin, use the default skin
82 if ( !$this->skin ||
!isset( $skinnames[$this->skin
] ) ) {
83 $this->skin
= $resourceLoader->getConfig()->get( 'DefaultSkin' );
88 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
89 * an array of module names like array( 'jquery.foo', 'jquery.bar',
90 * 'jquery.ui.baz', 'jquery.ui.quux' )
91 * @param string $modules Packed module name list
92 * @return array Array of module names
94 public static function expandModuleNames( $modules ) {
96 $exploded = explode( '|', $modules );
97 foreach ( $exploded as $group ) {
98 if ( strpos( $group, ',' ) === false ) {
99 // This is not a set of modules in foo.bar,baz notation
100 // but a single module
103 // This is a set of modules in foo.bar,baz notation
104 $pos = strrpos( $group, '.' );
105 if ( $pos === false ) {
106 // Prefixless modules, i.e. without dots
107 $retval = array_merge( $retval, explode( ',', $group ) );
109 // We have a prefix and a bunch of suffixes
110 $prefix = substr( $group, 0, $pos ); // 'foo'
111 $suffixes = explode( ',', substr( $group, $pos +
1 ) ); // array( 'bar', 'baz' )
112 foreach ( $suffixes as $suffix ) {
113 $retval[] = "$prefix.$suffix";
122 * Return a dummy ResourceLoaderContext object suitable for passing into
123 * things that don't "really" need a context.
124 * @return ResourceLoaderContext
126 public static function newDummyContext() {
127 return new self( new ResourceLoader(
128 ConfigFactory
::getDefaultInstance()->makeConfig( 'main' ),
129 LoggerFactory
::getInstance( 'resourceloader' )
130 ), new FauxRequest( array() ) );
134 * @return ResourceLoader
136 public function getResourceLoader() {
137 return $this->resourceLoader
;
143 public function getRequest() {
144 return $this->request
;
150 public function getModules() {
151 return $this->modules
;
157 public function getLanguage() {
158 if ( $this->language
=== null ) {
159 // Must be a valid language code after this point (bug 62849)
160 $this->language
= RequestContext
::sanitizeLangCode( $this->request
->getVal( 'lang' ) );
162 return $this->language
;
168 public function getDirection() {
169 if ( $this->direction
=== null ) {
170 $this->direction
= $this->request
->getVal( 'dir' );
171 if ( !$this->direction
) {
172 // Determine directionality based on user language (bug 6100)
173 $this->direction
= Language
::factory( $this->getLanguage() )->getDir();
176 return $this->direction
;
180 * @return string|null
182 public function getSkin() {
187 * @return string|null
189 public function getUser() {
194 * Get the possibly-cached User object for the specified username
197 * @return User|bool false if a valid object cannot be created
199 public function getUserObj() {
200 if ( $this->userObj
=== null ) {
201 $username = $this->getUser();
203 // Optimize: Avoid loading a new User object if possible
205 if ( is_object( $wgUser ) && $wgUser->getName() === $username ) {
206 $this->userObj
= $wgUser;
208 $this->userObj
= User
::newFromName( $username );
211 $this->userObj
= new User
; // Anonymous user
215 return $this->userObj
;
221 public function getDebug() {
226 * @return string|null
228 public function getOnly() {
233 * @see ResourceLoaderModule::getVersionHash
234 * @see OutputPage::makeResourceLoaderLink
235 * @return string|null
237 public function getVersion() {
238 return $this->version
;
244 public function getRaw() {
249 * @return string|null
251 public function getImage() {
256 * @return string|null
258 public function getVariant() {
259 return $this->variant
;
263 * @return string|null
265 public function getFormat() {
266 return $this->format
;
270 * If this is a request for an image, get the ResourceLoaderImage object.
273 * @return ResourceLoaderImage|bool false if a valid object cannot be created
275 public function getImageObj() {
276 if ( $this->imageObj
=== null ) {
277 $this->imageObj
= false;
279 if ( !$this->image
) {
280 return $this->imageObj
;
283 $modules = $this->getModules();
284 if ( count( $modules ) !== 1 ) {
285 return $this->imageObj
;
288 $module = $this->getResourceLoader()->getModule( $modules[0] );
289 if ( !$module ||
!$module instanceof ResourceLoaderImageModule
) {
290 return $this->imageObj
;
293 $image = $module->getImage( $this->image
, $this );
295 return $this->imageObj
;
298 $this->imageObj
= $image;
301 return $this->imageObj
;
307 public function shouldIncludeScripts() {
308 return is_null( $this->getOnly() ) ||
$this->getOnly() === 'scripts';
314 public function shouldIncludeStyles() {
315 return is_null( $this->getOnly() ) ||
$this->getOnly() === 'styles';
321 public function shouldIncludeMessages() {
322 return is_null( $this->getOnly() );
328 public function getHash() {
329 if ( !isset( $this->hash
) ) {
330 $this->hash
= implode( '|', array(
331 $this->getLanguage(), $this->getDirection(), $this->getSkin(), $this->getUser(),
332 $this->getImage(), $this->getVariant(), $this->getFormat(),
333 $this->getDebug(), $this->getOnly(), $this->getVersion()