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