Implement extension registration from an extension.json file
[mediawiki.git] / includes / resourceloader / ResourceLoaderContext.php
bloba6a7d34719e10eb943c3813ade79fca80f5d0dfc
1 <?php
2 /**
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
20 * @file
21 * @author Trevor Parscal
22 * @author Roan Kattouw
25 /**
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;
33 protected $request;
34 protected $modules;
35 protected $language;
36 protected $direction;
37 protected $skin;
38 protected $user;
39 protected $debug;
40 protected $only;
41 protected $version;
42 protected $hash;
43 protected $raw;
44 protected $image;
45 protected $variant;
46 protected $format;
47 protected $userObj;
48 protected $imageObj;
50 /* Methods */
52 /**
53 * @param ResourceLoader $resourceLoader
54 * @param WebRequest $request
56 public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) {
57 $this->resourceLoader = $resourceLoader;
58 $this->request = $request;
60 // Interpret request
61 // List of modules
62 $modules = $request->getVal( 'modules' );
63 $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
64 // Various parameters
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' );
73 // Image requests
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' );
85 /**
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 ) {
93 $retval = array();
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
99 $retval[] = $group;
100 } else {
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 ) );
106 } else {
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";
116 return $retval;
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;
138 * @return WebRequest
140 public function getRequest() {
141 return $this->request;
145 * @return array
147 public function getModules() {
148 return $this->modules;
152 * @return string
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;
163 * @return string
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() {
180 return $this->skin;
184 * @return string|null
186 public function getUser() {
187 return $this->user;
191 * Get the possibly-cached User object for the specified username
193 * @since 1.25
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();
199 if ( $username ) {
200 // Optimize: Avoid loading a new User object if possible
201 global $wgUser;
202 if ( is_object( $wgUser ) && $wgUser->getName() === $username ) {
203 $this->userObj = $wgUser;
204 } else {
205 $this->userObj = User::newFromName( $username );
207 } else {
208 $this->userObj = new User; // Anonymous user
212 return $this->userObj;
216 * @return bool
218 public function getDebug() {
219 return $this->debug;
223 * @return string|null
225 public function getOnly() {
226 return $this->only;
230 * @return string|null
232 public function getVersion() {
233 return $this->version;
237 * @return bool
239 public function getRaw() {
240 return $this->raw;
244 * @return string|null
246 public function getImage() {
247 return $this->image;
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.
267 * @since 1.25
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 );
289 if ( !$image ) {
290 return $this->imageObj;
293 $this->imageObj = $image;
296 return $this->imageObj;
300 * @return bool
302 public function shouldIncludeScripts() {
303 return is_null( $this->getOnly() ) || $this->getOnly() === 'scripts';
307 * @return bool
309 public function shouldIncludeStyles() {
310 return is_null( $this->getOnly() ) || $this->getOnly() === 'styles';
314 * @return bool
316 public function shouldIncludeMessages() {
317 return is_null( $this->getOnly() ) || $this->getOnly() === 'messages';
321 * @return string
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()
329 ) );
331 return $this->hash;