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
26 * Object passed around to modules which contains information about the state
27 * of a specific loader request
29 class ResourceLoaderContext
{
30 /* Protected Members */
32 protected $resourceLoader;
53 * @param ResourceLoader $resourceLoader
54 * @param WebRequest $request
56 public function __construct( ResourceLoader
$resourceLoader, WebRequest
$request ) {
57 $this->resourceLoader
= $resourceLoader;
58 $this->request
= $request;
62 $modules = $request->getVal( 'modules' );
63 $this->modules
= $modules ? self
::expandModuleNames( $modules ) : array();
65 $this->skin
= $request->getVal( 'skin' );
66 $this->user
= $request->getVal( 'user' );
67 $this->debug
= $request->getFuzzyBool(
68 'debug', $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
70 $this->only
= $request->getVal( 'only' );
71 $this->version
= $request->getVal( 'version' );
72 $this->raw
= $request->getFuzzyBool( 'raw' );
74 $this->image
= $request->getVal( 'image' );
75 $this->variant
= $request->getVal( 'variant' );
76 $this->format
= $request->getVal( 'format' );
78 $skinnames = Skin
::getSkinNames();
79 // If no skin is specified, or we don't recognize the skin, use the default skin
80 if ( !$this->skin ||
!isset( $skinnames[$this->skin
] ) ) {
81 $this->skin
= $resourceLoader->getConfig()->get( 'DefaultSkin' );
86 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
87 * an array of module names like array( 'jquery.foo', 'jquery.bar',
88 * 'jquery.ui.baz', 'jquery.ui.quux' )
89 * @param string $modules Packed module name list
90 * @return array Array of module names
92 public static function expandModuleNames( $modules ) {
94 $exploded = explode( '|', $modules );
95 foreach ( $exploded as $group ) {
96 if ( strpos( $group, ',' ) === false ) {
97 // This is not a set of modules in foo.bar,baz notation
98 // but a single module
101 // This is a set of modules in foo.bar,baz notation
102 $pos = strrpos( $group, '.' );
103 if ( $pos === false ) {
104 // Prefixless modules, i.e. without dots
105 $retval = array_merge( $retval, explode( ',', $group ) );
107 // We have a prefix and a bunch of suffixes
108 $prefix = substr( $group, 0, $pos ); // 'foo'
109 $suffixes = explode( ',', substr( $group, $pos +
1 ) ); // array( 'bar', 'baz' )
110 foreach ( $suffixes as $suffix ) {
111 $retval[] = "$prefix.$suffix";
120 * Return a dummy ResourceLoaderContext object suitable for passing into
121 * things that don't "really" need a context.
122 * @return ResourceLoaderContext
124 public static function newDummyContext() {
125 return new self( new ResourceLoader(
126 ConfigFactory
::getDefaultInstance()->makeConfig( 'main' )
127 ), new FauxRequest( array() ) );
131 * @return ResourceLoader
133 public function getResourceLoader() {
134 return $this->resourceLoader
;
140 public function getRequest() {
141 return $this->request
;
147 public function getModules() {
148 return $this->modules
;
154 public function getLanguage() {
155 if ( $this->language
=== null ) {
156 // Must be a valid language code after this point (bug 62849)
157 $this->language
= RequestContext
::sanitizeLangCode( $this->request
->getVal( 'lang' ) );
159 return $this->language
;
165 public function getDirection() {
166 if ( $this->direction
=== null ) {
167 $this->direction
= $this->request
->getVal( 'dir' );
168 if ( !$this->direction
) {
169 // Determine directionality based on user language (bug 6100)
170 $this->direction
= Language
::factory( $this->getLanguage() )->getDir();
173 return $this->direction
;
177 * @return string|null
179 public function getSkin() {
184 * @return string|null
186 public function getUser() {
191 * Get the possibly-cached User object for the specified username
194 * @return User|bool false if a valid object cannot be created
196 public function getUserObj() {
197 if ( $this->userObj
=== null ) {
198 $username = $this->getUser();
200 // Optimize: Avoid loading a new User object if possible
202 if ( is_object( $wgUser ) && $wgUser->getName() === $username ) {
203 $this->userObj
= $wgUser;
205 $this->userObj
= User
::newFromName( $username );
208 $this->userObj
= new User
; // Anonymous user
212 return $this->userObj
;
218 public function getDebug() {
223 * @return string|null
225 public function getOnly() {
230 * @return string|null
232 public function getVersion() {
233 return $this->version
;
239 public function getRaw() {
244 * @return string|null
246 public function getImage() {
251 * @return string|null
253 public function getVariant() {
254 return $this->variant
;
258 * @return string|null
260 public function getFormat() {
261 return $this->format
;
265 * If this is a request for an image, get the ResourceLoaderImage object.
268 * @return ResourceLoaderImage|bool false if a valid object cannot be created
270 public function getImageObj() {
271 if ( $this->imageObj
=== null ) {
272 $this->imageObj
= false;
274 if ( !$this->image
) {
275 return $this->imageObj
;
278 $modules = $this->getModules();
279 if ( count( $modules ) !== 1 ) {
280 return $this->imageObj
;
283 $module = $this->getResourceLoader()->getModule( $modules[0] );
284 if ( !$module ||
!$module instanceof ResourceLoaderImageModule
) {
285 return $this->imageObj
;
288 $image = $module->getImage( $this->image
);
290 return $this->imageObj
;
293 $this->imageObj
= $image;
296 return $this->imageObj
;
302 public function shouldIncludeScripts() {
303 return is_null( $this->getOnly() ) ||
$this->getOnly() === 'scripts';
309 public function shouldIncludeStyles() {
310 return is_null( $this->getOnly() ) ||
$this->getOnly() === 'styles';
316 public function shouldIncludeMessages() {
317 return is_null( $this->getOnly() ) ||
$this->getOnly() === 'messages';
323 public function getHash() {
324 if ( !isset( $this->hash
) ) {
325 $this->hash
= implode( '|', array(
326 $this->getLanguage(), $this->getDirection(), $this->getSkin(), $this->getUser(),
327 $this->getImage(), $this->getVariant(), $this->getFormat(),
328 $this->getDebug(), $this->getOnly(), $this->getVersion()