Remove unused 'XMPGetInfo' and 'XMPGetResults' hooks
[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 /* Protected Members */
31
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;
49
50 /* Methods */
51
52 /**
53 * @param ResourceLoader $resourceLoader
54 * @param WebRequest $request
55 */
56 public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) {
57 $this->resourceLoader = $resourceLoader;
58 $this->request = $request;
59
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' )
69 );
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' );
77
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' );
82 }
83 }
84
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
91 */
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";
112 }
113 }
114 }
115 }
116 return $retval;
117 }
118
119 /**
120 * Return a dummy ResourceLoaderContext object suitable for passing into
121 * things that don't "really" need a context.
122 * @return ResourceLoaderContext
123 */
124 public static function newDummyContext() {
125 return new self( new ResourceLoader(
126 ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
127 ), new FauxRequest( array() ) );
128 }
129
130 /**
131 * @return ResourceLoader
132 */
133 public function getResourceLoader() {
134 return $this->resourceLoader;
135 }
136
137 /**
138 * @return WebRequest
139 */
140 public function getRequest() {
141 return $this->request;
142 }
143
144 /**
145 * @return array
146 */
147 public function getModules() {
148 return $this->modules;
149 }
150
151 /**
152 * @return string
153 */
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' ) );
158 }
159 return $this->language;
160 }
161
162 /**
163 * @return string
164 */
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();
171 }
172 }
173 return $this->direction;
174 }
175
176 /**
177 * @return string|null
178 */
179 public function getSkin() {
180 return $this->skin;
181 }
182
183 /**
184 * @return string|null
185 */
186 public function getUser() {
187 return $this->user;
188 }
189
190 /**
191 * Get the possibly-cached User object for the specified username
192 *
193 * @since 1.25
194 * @return User|bool false if a valid object cannot be created
195 */
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 );
206 }
207 } else {
208 $this->userObj = new User; // Anonymous user
209 }
210 }
211
212 return $this->userObj;
213 }
214
215 /**
216 * @return bool
217 */
218 public function getDebug() {
219 return $this->debug;
220 }
221
222 /**
223 * @return string|null
224 */
225 public function getOnly() {
226 return $this->only;
227 }
228
229 /**
230 * @return string|null
231 */
232 public function getVersion() {
233 return $this->version;
234 }
235
236 /**
237 * @return bool
238 */
239 public function getRaw() {
240 return $this->raw;
241 }
242
243 /**
244 * @return string|null
245 */
246 public function getImage() {
247 return $this->image;
248 }
249
250 /**
251 * @return string|null
252 */
253 public function getVariant() {
254 return $this->variant;
255 }
256
257 /**
258 * @return string|null
259 */
260 public function getFormat() {
261 return $this->format;
262 }
263
264 /**
265 * If this is a request for an image, get the ResourceLoaderImage object.
266 *
267 * @since 1.25
268 * @return ResourceLoaderImage|bool false if a valid object cannot be created
269 */
270 public function getImageObj() {
271 if ( $this->imageObj === null ) {
272 $this->imageObj = false;
273
274 if ( !$this->image ) {
275 return $this->imageObj;
276 }
277
278 $modules = $this->getModules();
279 if ( count( $modules ) !== 1 ) {
280 return $this->imageObj;
281 }
282
283 $module = $this->getResourceLoader()->getModule( $modules[0] );
284 if ( !$module || !$module instanceof ResourceLoaderImageModule ) {
285 return $this->imageObj;
286 }
287
288 $image = $module->getImage( $this->image );
289 if ( !$image ) {
290 return $this->imageObj;
291 }
292
293 $this->imageObj = $image;
294 }
295
296 return $this->imageObj;
297 }
298
299 /**
300 * @return bool
301 */
302 public function shouldIncludeScripts() {
303 return is_null( $this->getOnly() ) || $this->getOnly() === 'scripts';
304 }
305
306 /**
307 * @return bool
308 */
309 public function shouldIncludeStyles() {
310 return is_null( $this->getOnly() ) || $this->getOnly() === 'styles';
311 }
312
313 /**
314 * @return bool
315 */
316 public function shouldIncludeMessages() {
317 return is_null( $this->getOnly() ) || $this->getOnly() === 'messages';
318 }
319
320 /**
321 * @return string
322 */
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 ) );
330 }
331 return $this->hash;
332 }
333 }