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;
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 (bug 62849)
162 $this->language
= RequestContext
::sanitizeLangCode( $this->getRequest()->getVal( 'lang' ) );
164 return $this->language
;
170 public function getDirection() {
171 if ( $this->direction
=== null ) {
172 $this->direction
= $this->getRequest()->getVal( 'dir' );
173 if ( !$this->direction
) {
174 // Determine directionality based on user language (bug 6100)
175 $this->direction
= Language
::factory( $this->getLanguage() )->getDir();
178 return $this->direction
;
184 public function getSkin() {
189 * @return string|null
191 public function getUser() {
196 * Get the possibly-cached User object for the specified username
199 * @return User|bool false if a valid object cannot be created
201 public function getUserObj() {
202 if ( $this->userObj
=== null ) {
203 $username = $this->getUser();
205 // Optimize: Avoid loading a new User object if possible
207 if ( is_object( $wgUser ) && $wgUser->getName() === $username ) {
208 $this->userObj
= $wgUser;
210 $this->userObj
= User
::newFromName( $username );
213 $this->userObj
= new User
; // Anonymous user
217 return $this->userObj
;
223 public function getDebug() {
228 * @return string|null
230 public function getOnly() {
235 * @see ResourceLoaderModule::getVersionHash
236 * @see OutputPage::makeResourceLoaderLink
237 * @return string|null
239 public function getVersion() {
240 return $this->version
;
246 public function getRaw() {
251 * @return string|null
253 public function getImage() {
258 * @return string|null
260 public function getVariant() {
261 return $this->variant
;
265 * @return string|null
267 public function getFormat() {
268 return $this->format
;
272 * If this is a request for an image, get the ResourceLoaderImage object.
275 * @return ResourceLoaderImage|bool false if a valid object cannot be created
277 public function getImageObj() {
278 if ( $this->imageObj
=== null ) {
279 $this->imageObj
= false;
281 if ( !$this->image
) {
282 return $this->imageObj
;
285 $modules = $this->getModules();
286 if ( count( $modules ) !== 1 ) {
287 return $this->imageObj
;
290 $module = $this->getResourceLoader()->getModule( $modules[0] );
291 if ( !$module ||
!$module instanceof ResourceLoaderImageModule
) {
292 return $this->imageObj
;
295 $image = $module->getImage( $this->image
, $this );
297 return $this->imageObj
;
300 $this->imageObj
= $image;
303 return $this->imageObj
;
309 public function shouldIncludeScripts() {
310 return $this->getOnly() === null ||
$this->getOnly() === 'scripts';
316 public function shouldIncludeStyles() {
317 return $this->getOnly() === null ||
$this->getOnly() === 'styles';
323 public function shouldIncludeMessages() {
324 return $this->getOnly() === null;
330 public function getHash() {
331 if ( !isset( $this->hash
) ) {
332 $this->hash
= implode( '|', array(
333 $this->getLanguage(), $this->getDirection(), $this->getSkin(), $this->getUser(),
334 $this->getImage(), $this->getVariant(), $this->getFormat(),
335 $this->getDebug(), $this->getOnly(), $this->getVersion()