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