No more undefined usage of rev{Start,End}Id warnings
[mediawiki.git] / includes / resourceloader / ResourceLoaderContext.php
blobdd69bb01d54aff6b76f7425bd1127cf74c87a26b
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @author Trevor Parscal
20 * @author Roan Kattouw
23 /**
24 * Object passed around to modules which contains information about the state
25 * of a specific loader request
27 class ResourceLoaderContext {
29 /* Protected Members */
31 protected $resourceLoader;
32 protected $request;
33 protected $modules;
34 protected $language;
35 protected $direction;
36 protected $skin;
37 protected $user;
38 protected $debug;
39 protected $only;
40 protected $version;
41 protected $hash;
43 /* Methods */
45 /**
46 * @param $resourceLoader ResourceLoader
47 * @param $request WebRequest
49 public function __construct( $resourceLoader, WebRequest $request ) {
50 global $wgDefaultSkin, $wgResourceLoaderDebug;
52 $this->resourceLoader = $resourceLoader;
53 $this->request = $request;
55 // Interpret request
56 // List of modules
57 $modules = $request->getVal( 'modules' );
58 $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
59 // Various parameters
60 $this->skin = $request->getVal( 'skin' );
61 $this->user = $request->getVal( 'user' );
62 $this->debug = $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug );
63 $this->only = $request->getVal( 'only' );
64 $this->version = $request->getVal( 'version' );
66 $skinnames = Skin::getSkinNames();
67 // If no skin is specified, or we don't recognize the skin, use the default skin
68 if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
69 $this->skin = $wgDefaultSkin;
73 /**
74 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
75 * an array of module names like array( 'jquery.foo', 'jquery.bar',
76 * 'jquery.ui.baz', 'jquery.ui.quux' )
77 * @param $modules String Packed module name list
78 * @return array of module names
80 public static function expandModuleNames( $modules ) {
81 $retval = array();
82 // For backwards compatibility with an earlier hack, replace ! with .
83 $modules = str_replace( '!', '.', $modules );
84 $exploded = explode( '|', $modules );
85 foreach ( $exploded as $group ) {
86 if ( strpos( $group, ',' ) === false ) {
87 // This is not a set of modules in foo.bar,baz notation
88 // but a single module
89 $retval[] = $group;
90 } else {
91 // This is a set of modules in foo.bar,baz notation
92 $pos = strrpos( $group, '.' );
93 if ( $pos === false ) {
94 // Prefixless modules, i.e. without dots
95 $retval = explode( ',', $group );
96 } else {
97 // We have a prefix and a bunch of suffixes
98 $prefix = substr( $group, 0, $pos ); // 'foo'
99 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
100 foreach ( $suffixes as $suffix ) {
101 $retval[] = "$prefix.$suffix";
106 return $retval;
110 * Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need a context
111 * @return ResourceLoaderContext
113 public static function newDummyContext() {
114 return new self( null, new FauxRequest( array() ) );
118 * @return ResourceLoader
120 public function getResourceLoader() {
121 return $this->resourceLoader;
125 * @return WebRequest
127 public function getRequest() {
128 return $this->request;
132 * @return array
134 public function getModules() {
135 return $this->modules;
139 * @return string
141 public function getLanguage() {
142 if ( $this->language === null ) {
143 global $wgLang;
144 $this->language = $this->request->getVal( 'lang' );
145 if ( !$this->language ) {
146 $this->language = $wgLang->getCode();
149 return $this->language;
153 * @return string
155 public function getDirection() {
156 if ( $this->direction === null ) {
157 $this->direction = $this->request->getVal( 'dir' );
158 if ( !$this->direction ) {
159 # directionality based on user language (see bug 6100)
160 $this->direction = Language::factory( $this->language )->getDir();
163 return $this->direction;
167 * @return string|null
169 public function getSkin() {
170 return $this->skin;
174 * @return string|null
176 public function getUser() {
177 return $this->user;
181 * @return bool
183 public function getDebug() {
184 return $this->debug;
188 * @return String|null
190 public function getOnly() {
191 return $this->only;
195 * @return String|null
197 public function getVersion() {
198 return $this->version;
202 * @return bool
204 public function shouldIncludeScripts() {
205 return is_null( $this->only ) || $this->only === 'scripts';
209 * @return bool
211 public function shouldIncludeStyles() {
212 return is_null( $this->only ) || $this->only === 'styles';
216 * @return bool
218 public function shouldIncludeMessages() {
219 return is_null( $this->only ) || $this->only === 'messages';
223 * @return string
225 public function getHash() {
226 if ( !isset( $this->hash ) ) {
227 $this->hash = implode( '|', array(
228 $this->getLanguage(), $this->getDirection(), $this->skin, $this->user,
229 $this->debug, $this->only, $this->version
230 ) );
232 return $this->hash;