Remove extra unneeded whitespace
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderContext.php
1 <?php
2 /**
3 * Context for resource loader modules.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Trevor Parscal
22 * @author Roan Kattouw
23 */
24
25 /**
26 * Object passed around to modules which contains information about the state
27 * of a specific loader request
28 */
29 class ResourceLoaderContext {
30
31 /* Protected Members */
32
33 protected $resourceLoader;
34 protected $request;
35 protected $modules;
36 protected $language;
37 protected $direction;
38 protected $skin;
39 protected $user;
40 protected $debug;
41 protected $only;
42 protected $version;
43 protected $hash;
44
45 /* Methods */
46
47 /**
48 * @param $resourceLoader ResourceLoader
49 * @param $request WebRequest
50 */
51 public function __construct( $resourceLoader, WebRequest $request ) {
52 global $wgDefaultSkin, $wgResourceLoaderDebug;
53
54 $this->resourceLoader = $resourceLoader;
55 $this->request = $request;
56
57 // Interpret request
58 // List of modules
59 $modules = $request->getVal( 'modules' );
60 $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
61 // Various parameters
62 $this->skin = $request->getVal( 'skin' );
63 $this->user = $request->getVal( 'user' );
64 $this->debug = $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug );
65 $this->only = $request->getVal( 'only' );
66 $this->version = $request->getVal( 'version' );
67
68 $skinnames = Skin::getSkinNames();
69 // If no skin is specified, or we don't recognize the skin, use the default skin
70 if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
71 $this->skin = $wgDefaultSkin;
72 }
73 }
74
75 /**
76 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
77 * an array of module names like array( 'jquery.foo', 'jquery.bar',
78 * 'jquery.ui.baz', 'jquery.ui.quux' )
79 * @param $modules String Packed module name list
80 * @return array of module names
81 */
82 public static function expandModuleNames( $modules ) {
83 $retval = array();
84 // For backwards compatibility with an earlier hack, replace ! with .
85 $modules = str_replace( '!', '.', $modules );
86 $exploded = explode( '|', $modules );
87 foreach ( $exploded as $group ) {
88 if ( strpos( $group, ',' ) === false ) {
89 // This is not a set of modules in foo.bar,baz notation
90 // but a single module
91 $retval[] = $group;
92 } else {
93 // This is a set of modules in foo.bar,baz notation
94 $pos = strrpos( $group, '.' );
95 if ( $pos === false ) {
96 // Prefixless modules, i.e. without dots
97 $retval = explode( ',', $group );
98 } else {
99 // We have a prefix and a bunch of suffixes
100 $prefix = substr( $group, 0, $pos ); // 'foo'
101 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
102 foreach ( $suffixes as $suffix ) {
103 $retval[] = "$prefix.$suffix";
104 }
105 }
106 }
107 }
108 return $retval;
109 }
110
111 /**
112 * Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need a context
113 * @return ResourceLoaderContext
114 */
115 public static function newDummyContext() {
116 return new self( null, new FauxRequest( array() ) );
117 }
118
119 /**
120 * @return ResourceLoader
121 */
122 public function getResourceLoader() {
123 return $this->resourceLoader;
124 }
125
126 /**
127 * @return WebRequest
128 */
129 public function getRequest() {
130 return $this->request;
131 }
132
133 /**
134 * @return array
135 */
136 public function getModules() {
137 return $this->modules;
138 }
139
140 /**
141 * @return string
142 */
143 public function getLanguage() {
144 if ( $this->language === null ) {
145 global $wgLang;
146 $this->language = $this->request->getVal( 'lang' );
147 if ( !$this->language ) {
148 $this->language = $wgLang->getCode();
149 }
150 }
151 return $this->language;
152 }
153
154 /**
155 * @return string
156 */
157 public function getDirection() {
158 if ( $this->direction === null ) {
159 $this->direction = $this->request->getVal( 'dir' );
160 if ( !$this->direction ) {
161 # directionality based on user language (see bug 6100)
162 $this->direction = Language::factory( $this->language )->getDir();
163 }
164 }
165 return $this->direction;
166 }
167
168 /**
169 * @return string|null
170 */
171 public function getSkin() {
172 return $this->skin;
173 }
174
175 /**
176 * @return string|null
177 */
178 public function getUser() {
179 return $this->user;
180 }
181
182 /**
183 * @return bool
184 */
185 public function getDebug() {
186 return $this->debug;
187 }
188
189 /**
190 * @return String|null
191 */
192 public function getOnly() {
193 return $this->only;
194 }
195
196 /**
197 * @return String|null
198 */
199 public function getVersion() {
200 return $this->version;
201 }
202
203 /**
204 * @return bool
205 */
206 public function shouldIncludeScripts() {
207 return is_null( $this->only ) || $this->only === 'scripts';
208 }
209
210 /**
211 * @return bool
212 */
213 public function shouldIncludeStyles() {
214 return is_null( $this->only ) || $this->only === 'styles';
215 }
216
217 /**
218 * @return bool
219 */
220 public function shouldIncludeMessages() {
221 return is_null( $this->only ) || $this->only === 'messages';
222 }
223
224 /**
225 * @return string
226 */
227 public function getHash() {
228 if ( !isset( $this->hash ) ) {
229 $this->hash = implode( '|', array(
230 $this->getLanguage(), $this->getDirection(), $this->skin, $this->user,
231 $this->debug, $this->only, $this->version
232 ) );
233 }
234 return $this->hash;
235 }
236 }