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();
68 $this->user
= $request->getVal( 'user' );
69 $this->debug
= $request->getFuzzyBool(
71 $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
73 $this->only
= $request->getVal( 'only', null );
74 $this->version
= $request->getVal( 'version', null );
75 $this->raw
= $request->getFuzzyBool( 'raw' );
78 $this->image
= $request->getVal( 'image' );
79 $this->variant
= $request->getVal( 'variant' );
80 $this->format
= $request->getVal( 'format' );
82 $this->skin
= $request->getVal( 'skin' );
83 $skinnames = Skin
::getSkinNames();
84 // If no skin is specified, or we don't recognize the skin, use the default skin
85 if ( !$this->skin ||
!isset( $skinnames[$this->skin
] ) ) {
86 $this->skin
= $resourceLoader->getConfig()->get( 'DefaultSkin' );
91 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
92 * an array of module names like array( 'jquery.foo', 'jquery.bar',
93 * 'jquery.ui.baz', 'jquery.ui.quux' )
94 * @param string $modules Packed module name list
95 * @return array Array of module names
97 public static function expandModuleNames( $modules ) {
99 $exploded = explode( '|', $modules );
100 foreach ( $exploded as $group ) {
101 if ( strpos( $group, ',' ) === false ) {
102 // This is not a set of modules in foo.bar,baz notation
103 // but a single module
106 // This is a set of modules in foo.bar,baz notation
107 $pos = strrpos( $group, '.' );
108 if ( $pos === false ) {
109 // Prefixless modules, i.e. without dots
110 $retval = array_merge( $retval, explode( ',', $group ) );
112 // We have a prefix and a bunch of suffixes
113 $prefix = substr( $group, 0, $pos ); // 'foo'
114 $suffixes = explode( ',', substr( $group, $pos +
1 ) ); // array( 'bar', 'baz' )
115 foreach ( $suffixes as $suffix ) {
116 $retval[] = "$prefix.$suffix";
125 * Return a dummy ResourceLoaderContext object suitable for passing into
126 * things that don't "really" need a context.
127 * @return ResourceLoaderContext
129 public static function newDummyContext() {
130 return new self( new ResourceLoader(
131 ConfigFactory
::getDefaultInstance()->makeConfig( 'main' ),
132 LoggerFactory
::getInstance( 'resourceloader' )
133 ), new FauxRequest( array() ) );
137 * @return ResourceLoader
139 public function getResourceLoader() {
140 return $this->resourceLoader
;
146 public function getRequest() {
147 return $this->request
;
153 public function getModules() {
154 return $this->modules
;
160 public function getLanguage() {
161 if ( $this->language
=== null ) {
162 // Must be a valid language code after this point (bug 62849)
163 $this->language
= RequestContext
::sanitizeLangCode( $this->request
->getVal( 'lang' ) );
165 return $this->language
;
171 public function getDirection() {
172 if ( $this->direction
=== null ) {
173 $this->direction
= $this->request
->getVal( 'dir' );
174 if ( !$this->direction
) {
175 // Determine directionality based on user language (bug 6100)
176 $this->direction
= Language
::factory( $this->getLanguage() )->getDir();
179 return $this->direction
;
185 public function getSkin() {
190 * @return string|null
192 public function getUser() {
197 * Get the possibly-cached User object for the specified username
200 * @return User|bool false if a valid object cannot be created
202 public function getUserObj() {
203 if ( $this->userObj
=== null ) {
204 $username = $this->getUser();
206 // Optimize: Avoid loading a new User object if possible
208 if ( is_object( $wgUser ) && $wgUser->getName() === $username ) {
209 $this->userObj
= $wgUser;
211 $this->userObj
= User
::newFromName( $username );
214 $this->userObj
= new User
; // Anonymous user
218 return $this->userObj
;
224 public function getDebug() {
229 * @return string|null
231 public function getOnly() {
236 * @see ResourceLoaderModule::getVersionHash
237 * @see OutputPage::makeResourceLoaderLink
238 * @return string|null
240 public function getVersion() {
241 return $this->version
;
247 public function getRaw() {
252 * @return string|null
254 public function getImage() {
259 * @return string|null
261 public function getVariant() {
262 return $this->variant
;
266 * @return string|null
268 public function getFormat() {
269 return $this->format
;
273 * If this is a request for an image, get the ResourceLoaderImage object.
276 * @return ResourceLoaderImage|bool false if a valid object cannot be created
278 public function getImageObj() {
279 if ( $this->imageObj
=== null ) {
280 $this->imageObj
= false;
282 if ( !$this->image
) {
283 return $this->imageObj
;
286 $modules = $this->getModules();
287 if ( count( $modules ) !== 1 ) {
288 return $this->imageObj
;
291 $module = $this->getResourceLoader()->getModule( $modules[0] );
292 if ( !$module ||
!$module instanceof ResourceLoaderImageModule
) {
293 return $this->imageObj
;
296 $image = $module->getImage( $this->image
, $this );
298 return $this->imageObj
;
301 $this->imageObj
= $image;
304 return $this->imageObj
;
310 public function shouldIncludeScripts() {
311 return $this->getOnly() === null ||
$this->getOnly() === 'scripts';
317 public function shouldIncludeStyles() {
318 return $this->getOnly() === null ||
$this->getOnly() === 'styles';
324 public function shouldIncludeMessages() {
325 return $this->getOnly() === null;
331 public function getHash() {
332 if ( !isset( $this->hash
) ) {
333 $this->hash
= implode( '|', array(
334 $this->getLanguage(), $this->getDirection(), $this->getSkin(), $this->getUser(),
335 $this->getImage(), $this->getVariant(), $this->getFormat(),
336 $this->getDebug(), $this->getOnly(), $this->getVersion()