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