resourceloader: Omit getDirection() ResourceLoaderContext hash
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderContext.php
1 <?php
2 /**
3 * Context for ResourceLoader 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 use MediaWiki\Logger\LoggerFactory;
26
27 /**
28 * Object passed around to modules which contains information about the state
29 * of a specific loader request
30 */
31 class ResourceLoaderContext {
32 /* Protected Members */
33
34 protected $resourceLoader;
35 protected $request;
36 protected $modules;
37 protected $language;
38 protected $direction;
39 protected $skin;
40 protected $user;
41 protected $debug;
42 protected $only;
43 protected $version;
44 protected $hash;
45 protected $raw;
46 protected $image;
47 protected $variant;
48 protected $format;
49 protected $userObj;
50 protected $imageObj;
51
52 /* Methods */
53
54 /**
55 * @param ResourceLoader $resourceLoader
56 * @param WebRequest $request
57 */
58 public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) {
59 $this->resourceLoader = $resourceLoader;
60 $this->request = $request;
61
62 // List of modules
63 $modules = $request->getVal( 'modules' );
64 $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
65
66 // Various parameters
67 $this->user = $request->getVal( 'user' );
68 $this->debug = $request->getFuzzyBool(
69 'debug',
70 $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
71 );
72 $this->only = $request->getVal( 'only', null );
73 $this->version = $request->getVal( 'version', null );
74 $this->raw = $request->getFuzzyBool( 'raw' );
75
76 // Image requests
77 $this->image = $request->getVal( 'image' );
78 $this->variant = $request->getVal( 'variant' );
79 $this->format = $request->getVal( 'format' );
80
81 $this->skin = $request->getVal( 'skin' );
82 $skinnames = Skin::getSkinNames();
83 // If no skin is specified, or we don't recognize the skin, use the default skin
84 if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
85 $this->skin = $resourceLoader->getConfig()->get( 'DefaultSkin' );
86 }
87 }
88
89 /**
90 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
91 * an array of module names like array( 'jquery.foo', 'jquery.bar',
92 * 'jquery.ui.baz', 'jquery.ui.quux' )
93 * @param string $modules Packed module name list
94 * @return array Array of module names
95 */
96 public static function expandModuleNames( $modules ) {
97 $retval = array();
98 $exploded = explode( '|', $modules );
99 foreach ( $exploded as $group ) {
100 if ( strpos( $group, ',' ) === false ) {
101 // This is not a set of modules in foo.bar,baz notation
102 // but a single module
103 $retval[] = $group;
104 } else {
105 // This is a set of modules in foo.bar,baz notation
106 $pos = strrpos( $group, '.' );
107 if ( $pos === false ) {
108 // Prefixless modules, i.e. without dots
109 $retval = array_merge( $retval, explode( ',', $group ) );
110 } else {
111 // We have a prefix and a bunch of suffixes
112 $prefix = substr( $group, 0, $pos ); // 'foo'
113 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
114 foreach ( $suffixes as $suffix ) {
115 $retval[] = "$prefix.$suffix";
116 }
117 }
118 }
119 }
120 return $retval;
121 }
122
123 /**
124 * Return a dummy ResourceLoaderContext object suitable for passing into
125 * things that don't "really" need a context.
126 * @return ResourceLoaderContext
127 */
128 public static function newDummyContext() {
129 return new self( new ResourceLoader(
130 ConfigFactory::getDefaultInstance()->makeConfig( 'main' ),
131 LoggerFactory::getInstance( 'resourceloader' )
132 ), new FauxRequest( array() ) );
133 }
134
135 /**
136 * @return ResourceLoader
137 */
138 public function getResourceLoader() {
139 return $this->resourceLoader;
140 }
141
142 /**
143 * @return WebRequest
144 */
145 public function getRequest() {
146 return $this->request;
147 }
148
149 /**
150 * @return array
151 */
152 public function getModules() {
153 return $this->modules;
154 }
155
156 /**
157 * @return string
158 */
159 public function getLanguage() {
160 if ( $this->language === null ) {
161 // Must be a valid language code after this point (T64849)
162 // Only support uselang values that follow built-in conventions (T102058)
163 $lang = $this->getRequest()->getVal( 'lang', '' );
164 // Stricter version of RequestContext::sanitizeLangCode()
165 if ( !Language::isValidBuiltInCode( $lang ) ) {
166 wfDebug( "Invalid user language code\n" );
167 global $wgLanguageCode;
168 $lang = $wgLanguageCode;
169 }
170 $this->language = $lang;
171 }
172 return $this->language;
173 }
174
175 /**
176 * @return string
177 */
178 public function getDirection() {
179 if ( $this->direction === null ) {
180 $this->direction = $this->getRequest()->getVal( 'dir' );
181 if ( !$this->direction ) {
182 // Determine directionality based on user language (bug 6100)
183 $this->direction = Language::factory( $this->getLanguage() )->getDir();
184 }
185 }
186 return $this->direction;
187 }
188
189 /**
190 * @return string
191 */
192 public function getSkin() {
193 return $this->skin;
194 }
195
196 /**
197 * @return string|null
198 */
199 public function getUser() {
200 return $this->user;
201 }
202
203 /**
204 * Get the possibly-cached User object for the specified username
205 *
206 * @since 1.25
207 * @return User|bool false if a valid object cannot be created
208 */
209 public function getUserObj() {
210 if ( $this->userObj === null ) {
211 $username = $this->getUser();
212 if ( $username ) {
213 // Optimize: Avoid loading a new User object if possible
214 global $wgUser;
215 if ( is_object( $wgUser ) && $wgUser->getName() === $username ) {
216 $this->userObj = $wgUser;
217 } else {
218 $this->userObj = User::newFromName( $username );
219 }
220 } else {
221 $this->userObj = new User; // Anonymous user
222 }
223 }
224
225 return $this->userObj;
226 }
227
228 /**
229 * @return bool
230 */
231 public function getDebug() {
232 return $this->debug;
233 }
234
235 /**
236 * @return string|null
237 */
238 public function getOnly() {
239 return $this->only;
240 }
241
242 /**
243 * @see ResourceLoaderModule::getVersionHash
244 * @see OutputPage::makeResourceLoaderLink
245 * @return string|null
246 */
247 public function getVersion() {
248 return $this->version;
249 }
250
251 /**
252 * @return bool
253 */
254 public function getRaw() {
255 return $this->raw;
256 }
257
258 /**
259 * @return string|null
260 */
261 public function getImage() {
262 return $this->image;
263 }
264
265 /**
266 * @return string|null
267 */
268 public function getVariant() {
269 return $this->variant;
270 }
271
272 /**
273 * @return string|null
274 */
275 public function getFormat() {
276 return $this->format;
277 }
278
279 /**
280 * If this is a request for an image, get the ResourceLoaderImage object.
281 *
282 * @since 1.25
283 * @return ResourceLoaderImage|bool false if a valid object cannot be created
284 */
285 public function getImageObj() {
286 if ( $this->imageObj === null ) {
287 $this->imageObj = false;
288
289 if ( !$this->image ) {
290 return $this->imageObj;
291 }
292
293 $modules = $this->getModules();
294 if ( count( $modules ) !== 1 ) {
295 return $this->imageObj;
296 }
297
298 $module = $this->getResourceLoader()->getModule( $modules[0] );
299 if ( !$module || !$module instanceof ResourceLoaderImageModule ) {
300 return $this->imageObj;
301 }
302
303 $image = $module->getImage( $this->image, $this );
304 if ( !$image ) {
305 return $this->imageObj;
306 }
307
308 $this->imageObj = $image;
309 }
310
311 return $this->imageObj;
312 }
313
314 /**
315 * @return bool
316 */
317 public function shouldIncludeScripts() {
318 return $this->getOnly() === null || $this->getOnly() === 'scripts';
319 }
320
321 /**
322 * @return bool
323 */
324 public function shouldIncludeStyles() {
325 return $this->getOnly() === null || $this->getOnly() === 'styles';
326 }
327
328 /**
329 * @return bool
330 */
331 public function shouldIncludeMessages() {
332 return $this->getOnly() === null;
333 }
334
335 /**
336 * All factors that uniquely identify this request, except 'modules'.
337 *
338 * The list of modules is excluded here for legacy reasons as most callers already
339 * split up handling of individual modules. Including it here would massively fragment
340 * the cache and decrease its usefulness.
341 *
342 * E.g. Used by RequestFileCache to form a cache key for storing the reponse output.
343 *
344 * @return string
345 */
346 public function getHash() {
347 if ( !isset( $this->hash ) ) {
348 $this->hash = implode( '|', array(
349 $this->getLanguage(), $this->getSkin(), $this->getUser(),
350 $this->getImage(), $this->getVariant(), $this->getFormat(),
351 $this->getDebug(), $this->getOnly(), $this->getVersion()
352 ) );
353 }
354 return $this->hash;
355 }
356 }