Create a user.groups module in ResourceLoader, which bundles a CSS and JS page for...
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.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 Roan Kattouw
20 * @author Trevor Parscal
21 */
22
23 /**
24 * Dynamic JavaScript and CSS resource loading system.
25 *
26 * Most of the documention is on the MediaWiki documentation wiki starting at:
27 * http://www.mediawiki.org/wiki/ResourceLoader
28 */
29 class ResourceLoader {
30
31 /* Protected Static Members */
32
33 /** Array: List of module name/ResourceLoaderModule object pairs */
34 protected $modules = array();
35 /** Associative array mapping module name to info associative array */
36 protected $moduleInfos = array();
37
38 /* Protected Methods */
39
40 /**
41 * Loads information stored in the database about modules.
42 *
43 * This method grabs modules dependencies from the database and updates modules
44 * objects.
45 *
46 * This is not inside the module code because it is much faster to
47 * request all of the information at once than it is to have each module
48 * requests its own information. This sacrifice of modularity yields a substantial
49 * performance improvement.
50 *
51 * @param $modules Array: List of module names to preload information for
52 * @param $context ResourceLoaderContext: Context to load the information within
53 */
54 public function preloadModuleInfo( array $modules, ResourceLoaderContext $context ) {
55 if ( !count( $modules ) ) {
56 return; // or else Database*::select() will explode, plus it's cheaper!
57 }
58 $dbr = wfGetDB( DB_SLAVE );
59 $skin = $context->getSkin();
60 $lang = $context->getLanguage();
61
62 // Get file dependency information
63 $res = $dbr->select( 'module_deps', array( 'md_module', 'md_deps' ), array(
64 'md_module' => $modules,
65 'md_skin' => $context->getSkin()
66 ), __METHOD__
67 );
68
69 // Set modules' dependencies
70 $modulesWithDeps = array();
71 foreach ( $res as $row ) {
72 $this->getModule( $row->md_module )->setFileDependencies( $skin,
73 FormatJson::decode( $row->md_deps, true )
74 );
75 $modulesWithDeps[] = $row->md_module;
76 }
77
78 // Register the absence of a dependency row too
79 foreach ( array_diff( $modules, $modulesWithDeps ) as $name ) {
80 $this->getModule( $name )->setFileDependencies( $skin, array() );
81 }
82
83 // Get message blob mtimes. Only do this for modules with messages
84 $modulesWithMessages = array();
85 foreach ( $modules as $name ) {
86 if ( count( $this->getModule( $name )->getMessages() ) ) {
87 $modulesWithMessages[] = $name;
88 }
89 }
90 $modulesWithoutMessages = array_flip( $modules ); // Will be trimmed down by the loop below
91 if ( count( $modulesWithMessages ) ) {
92 $res = $dbr->select( 'msg_resource', array( 'mr_resource', 'mr_timestamp' ), array(
93 'mr_resource' => $modulesWithMessages,
94 'mr_lang' => $lang
95 ), __METHOD__
96 );
97 foreach ( $res as $row ) {
98 $this->getModule( $row->mr_resource )->setMsgBlobMtime( $lang,
99 wfTimestamp( TS_UNIX, $row->mr_timestamp ) );
100 unset( $modulesWithoutMessages[$row->mr_resource] );
101 }
102 }
103 foreach ( array_keys( $modulesWithoutMessages ) as $name ) {
104 $this->getModule( $name )->setMsgBlobMtime( $lang, 0 );
105 }
106 }
107
108 /**
109 * Runs JavaScript or CSS data through a filter, caching the filtered result for future calls.
110 *
111 * Available filters are:
112 * - minify-js \see JavaScriptDistiller::stripWhiteSpace
113 * - minify-css \see CSSMin::minify
114 *
115 * If $data is empty, only contains whitespace or the filter was unknown,
116 * $data is returned unmodified.
117 *
118 * @param $filter String: Name of filter to run
119 * @param $data String: Text to filter, such as JavaScript or CSS text
120 * @return String: Filtered data, or a comment containing an error message
121 */
122 protected function filter( $filter, $data ) {
123 global $wgResourceLoaderMinifyJSVerticalSpace;
124
125 wfProfileIn( __METHOD__ );
126
127 // For empty/whitespace-only data or for unknown filters, don't perform
128 // any caching or processing
129 if ( trim( $data ) === ''
130 || !in_array( $filter, array( 'minify-js', 'minify-css' ) ) )
131 {
132 wfProfileOut( __METHOD__ );
133 return $data;
134 }
135
136 // Try for cache hit
137 // Use CACHE_ANYTHING since filtering is very slow compared to DB queries
138 $key = wfMemcKey( 'resourceloader', 'filter', $filter, md5( $data ) );
139 $cache = wfGetCache( CACHE_ANYTHING );
140 $cacheEntry = $cache->get( $key );
141 if ( is_string( $cacheEntry ) ) {
142 wfProfileOut( __METHOD__ );
143 return $cacheEntry;
144 }
145
146 $result = '';
147 // Run the filter - we've already verified one of these will work
148 try {
149 switch ( $filter ) {
150 case 'minify-js':
151 $result = JavaScriptDistiller::stripWhiteSpace(
152 $data, $wgResourceLoaderMinifyJSVerticalSpace
153 );
154 break;
155 case 'minify-css':
156 $result = CSSMin::minify( $data );
157 break;
158 }
159
160 // Save filtered text to Memcached
161 $cache->set( $key, $result );
162 } catch ( Exception $exception ) {
163 // Return exception as a comment
164 $result = "/*\n{$exception->__toString()}\n*/\n";
165 }
166
167 wfProfileOut( __METHOD__ );
168
169 return $result;
170 }
171
172 /* Methods */
173
174 /**
175 * Registers core modules and runs registration hooks.
176 */
177 public function __construct() {
178 global $IP, $wgResourceModules;
179
180 wfProfileIn( __METHOD__ );
181
182 // Register core modules
183 $this->register( include( "$IP/resources/Resources.php" ) );
184 // Register extension modules
185 wfRunHooks( 'ResourceLoaderRegisterModules', array( &$this ) );
186 $this->register( $wgResourceModules );
187
188 wfProfileOut( __METHOD__ );
189 }
190
191 /**
192 * Registers a module with the ResourceLoader system.
193 *
194 * @param $name Mixed: Name of module as a string or List of name/object pairs as an array
195 * @param $info Module info array. For backwards compatibility with 1.17alpha,
196 * this may also be a ResourceLoaderModule object. Optional when using
197 * multiple-registration calling style.
198 * @throws MWException: If a duplicate module registration is attempted
199 * @throws MWException: If something other than a ResourceLoaderModule is being registered
200 * @return Boolean: False if there were any errors, in which case one or more modules were not
201 * registered
202 */
203 public function register( $name, $info = null ) {
204 wfProfileIn( __METHOD__ );
205
206 // Allow multiple modules to be registered in one call
207 if ( is_array( $name ) ) {
208 foreach ( $name as $key => $value ) {
209 $this->register( $key, $value );
210 }
211 wfProfileOut( __METHOD__ );
212 return;
213 }
214
215 // Disallow duplicate registrations
216 if ( isset( $this->moduleInfos[$name] ) ) {
217 // A module has already been registered by this name
218 throw new MWException(
219 'ResourceLoader duplicate registration error. ' .
220 'Another module has already been registered as ' . $name
221 );
222 }
223
224 // Attach module
225 if ( is_object( $info ) ) {
226 // Old calling convention
227 // Validate the input
228 if ( !( $info instanceof ResourceLoaderModule ) ) {
229 throw new MWException( 'ResourceLoader invalid module error. ' .
230 'Instances of ResourceLoaderModule expected.' );
231 }
232
233 $this->moduleInfos[$name] = array( 'object' => $info );
234 $info->setName( $name );
235 $this->modules[$name] = $info;
236 } else {
237 // New calling convention
238 $this->moduleInfos[$name] = $info;
239 }
240
241 wfProfileOut( __METHOD__ );
242 }
243
244 /**
245 * Get a list of module names
246 *
247 * @return Array: List of module names
248 */
249 public function getModuleNames() {
250 return array_keys( $this->moduleInfos );
251 }
252
253 /**
254 * Get the ResourceLoaderModule object for a given module name.
255 *
256 * @param $name String: Module name
257 * @return ResourceLoaderModule if module has been registered, null otherwise
258 */
259 public function getModule( $name ) {
260 if ( !isset( $this->modules[$name] ) ) {
261 if ( !isset( $this->moduleInfos[$name] ) ) {
262 // No such module
263 return null;
264 }
265 // Construct the requested object
266 $info = $this->moduleInfos[$name];
267 if ( isset( $info['object'] ) ) {
268 // Object given in info array
269 $object = $info['object'];
270 } else {
271 if ( !isset( $info['class'] ) ) {
272 $class = 'ResourceLoaderFileModule';
273 } else {
274 $class = $info['class'];
275 }
276 $object = new $class( $info );
277 }
278 $object->setName( $name );
279 $this->modules[$name] = $object;
280 }
281
282 return $this->modules[$name];
283 }
284
285 /**
286 * Outputs a response to a resource load-request, including a content-type header.
287 *
288 * @param $context ResourceLoaderContext: Context in which a response should be formed
289 */
290 public function respond( ResourceLoaderContext $context ) {
291 global $wgResourceLoaderMaxage, $wgCacheEpoch;
292
293 // Buffer output to catch warnings. Normally we'd use ob_clean() on the
294 // top-level output buffer to clear warnings, but that breaks when ob_gzhandler
295 // is used: ob_clean() will clear the GZIP header in that case and it won't come
296 // back for subsequent output, resulting in invalid GZIP. So we have to wrap
297 // the whole thing in our own output buffer to be sure the active buffer
298 // doesn't use ob_gzhandler.
299 // See http://bugs.php.net/bug.php?id=36514
300 ob_start();
301
302 wfProfileIn( __METHOD__ );
303 $exceptions = '';
304
305 // Split requested modules into two groups, modules and missing
306 $modules = array();
307 $missing = array();
308 foreach ( $context->getModules() as $name ) {
309 if ( isset( $this->moduleInfos[$name] ) ) {
310 $modules[$name] = $this->getModule( $name );
311 } else {
312 $missing[] = $name;
313 }
314 }
315
316 // If a version wasn't specified we need a shorter expiry time for updates
317 // to propagate to clients quickly
318 if ( is_null( $context->getVersion() ) ) {
319 $maxage = $wgResourceLoaderMaxage['unversioned']['client'];
320 $smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
321 }
322 // If a version was specified we can use a longer expiry time since changing
323 // version numbers causes cache misses
324 else {
325 $maxage = $wgResourceLoaderMaxage['versioned']['client'];
326 $smaxage = $wgResourceLoaderMaxage['versioned']['server'];
327 }
328
329 // Preload information needed to the mtime calculation below
330 try {
331 $this->preloadModuleInfo( array_keys( $modules ), $context );
332 } catch( Exception $e ) {
333 // Add exception to the output as a comment
334 $exceptions .= "/*\n{$e->__toString()}\n*/\n";
335 }
336
337 wfProfileIn( __METHOD__.'-getModifiedTime' );
338
339 $private = false;
340 // To send Last-Modified and support If-Modified-Since, we need to detect
341 // the last modified time
342 $mtime = wfTimestamp( TS_UNIX, $wgCacheEpoch );
343 foreach ( $modules as $module ) {
344 try {
345 // Bypass Squid and other shared caches if the request includes any private modules
346 if ( $module->getGroup() === 'private' ) {
347 $private = true;
348 }
349 // Calculate maximum modified time
350 $mtime = max( $mtime, $module->getModifiedTime( $context ) );
351 } catch ( Exception $e ) {
352 // Add exception to the output as a comment
353 $exceptions .= "/*\n{$e->__toString()}\n*/\n";
354 }
355 }
356
357 wfProfileOut( __METHOD__.'-getModifiedTime' );
358
359 if ( $context->getOnly() === 'styles' ) {
360 header( 'Content-Type: text/css' );
361 } else {
362 header( 'Content-Type: text/javascript' );
363 }
364 header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $mtime ) );
365 if ( $context->getDebug() ) {
366 // Do not cache debug responses
367 header( 'Cache-Control: private, no-cache, must-revalidate' );
368 header( 'Pragma: no-cache' );
369 } else {
370 if ( $private ) {
371 header( "Cache-Control: private, max-age=$maxage" );
372 $exp = $maxage;
373 } else {
374 header( "Cache-Control: public, max-age=$maxage, s-maxage=$smaxage" );
375 $exp = min( $maxage, $smaxage );
376 }
377 header( 'Expires: ' . wfTimestamp( TS_RFC2822, $exp + time() ) );
378 }
379
380 // If there's an If-Modified-Since header, respond with a 304 appropriately
381 // Some clients send "timestamp;length=123". Strip the part after the first ';'
382 // so we get a valid timestamp.
383 $ims = $context->getRequest()->getHeader( 'If-Modified-Since' );
384 // Never send 304s in debug mode
385 if ( $ims !== false && !$context->getDebug() ) {
386 $imsTS = strtok( $ims, ';' );
387 if ( $mtime <= wfTimestamp( TS_UNIX, $imsTS ) ) {
388 // There's another bug in ob_gzhandler (see also the comment at
389 // the top of this function) that causes it to gzip even empty
390 // responses, meaning it's impossible to produce a truly empty
391 // response (because the gzip header is always there). This is
392 // a problem because 304 responses have to be completely empty
393 // per the HTTP spec, and Firefox behaves buggily when they're not.
394 // See also http://bugs.php.net/bug.php?id=51579
395 // To work around this, we tear down all output buffering before
396 // sending the 304.
397 // On some setups, ob_get_level() doesn't seem to go down to zero
398 // no matter how often we call ob_get_clean(), so instead of doing
399 // the more intuitive while ( ob_get_level() > 0 ) ob_get_clean();
400 // we have to be safe here and avoid an infinite loop.
401 for ( $i = 0; $i < ob_get_level(); $i++ ) {
402 ob_end_clean();
403 }
404
405 header( 'HTTP/1.0 304 Not Modified' );
406 header( 'Status: 304 Not Modified' );
407 wfProfileOut( __METHOD__ );
408 return;
409 }
410 }
411
412 // Generate a response
413 $response = $this->makeModuleResponse( $context, $modules, $missing );
414
415 // Prepend comments indicating exceptions
416 $response = $exceptions . $response;
417
418 // Capture any PHP warnings from the output buffer and append them to the
419 // response in a comment if we're in debug mode.
420 if ( $context->getDebug() && strlen( $warnings = ob_get_contents() ) ) {
421 $response = "/*\n$warnings\n*/\n" . $response;
422 }
423
424 // Remove the output buffer and output the response
425 ob_end_clean();
426 echo $response;
427
428 wfProfileOut( __METHOD__ );
429 }
430
431 /**
432 * Generates code for a response
433 *
434 * @param $context ResourceLoaderContext: Context in which to generate a response
435 * @param $modules Array: List of module objects keyed by module name
436 * @param $missing Array: List of unavailable modules (optional)
437 * @return String: Response data
438 */
439 public function makeModuleResponse( ResourceLoaderContext $context,
440 array $modules, $missing = array() )
441 {
442 $out = '';
443 $exceptions = '';
444 if ( $modules === array() && $missing === array() ) {
445 return '/* No modules requested. Max made me put this here */';
446 }
447
448 wfProfileIn( __METHOD__ );
449 // Pre-fetch blobs
450 if ( $context->shouldIncludeMessages() ) {
451 try {
452 $blobs = MessageBlobStore::get( $this, $modules, $context->getLanguage() );
453 } catch ( Exception $e ) {
454 // Add exception to the output as a comment
455 $exceptions .= "/*\n{$e->__toString()}\n*/\n";
456 }
457 } else {
458 $blobs = array();
459 }
460
461 // Generate output
462 foreach ( $modules as $name => $module ) {
463 wfProfileIn( __METHOD__ . '-' . $name );
464 try {
465 // Scripts
466 $scripts = '';
467 if ( $context->shouldIncludeScripts() ) {
468 $scripts .= $module->getScript( $context ) . "\n";
469 }
470
471 // Styles
472 $styles = array();
473 if ( $context->shouldIncludeStyles() ) {
474 $styles = $module->getStyles( $context );
475 }
476
477 // Messages
478 $messagesBlob = isset( $blobs[$name] ) ? $blobs[$name] : '{}';
479
480 // Append output
481 switch ( $context->getOnly() ) {
482 case 'scripts':
483 $out .= $scripts;
484 break;
485 case 'styles':
486 $out .= self::makeCombinedStyles( $styles );
487 break;
488 case 'messages':
489 $out .= self::makeMessageSetScript( new XmlJsCode( $messagesBlob ) );
490 break;
491 default:
492 // Minify CSS before embedding in mediaWiki.loader.implement call
493 // (unless in debug mode)
494 if ( !$context->getDebug() ) {
495 foreach ( $styles as $media => $style ) {
496 $styles[$media] = $this->filter( 'minify-css', $style );
497 }
498 }
499 $out .= self::makeLoaderImplementScript( $name, $scripts, $styles,
500 new XmlJsCode( $messagesBlob ) );
501 break;
502 }
503 } catch ( Exception $e ) {
504 // Add exception to the output as a comment
505 $exceptions .= "/*\n{$e->__toString()}\n*/\n";
506
507 // Register module as missing
508 $missing[] = $name;
509 unset( $modules[$name] );
510 }
511 wfProfileOut( __METHOD__ . '-' . $name );
512 }
513
514 // Update module states
515 if ( $context->shouldIncludeScripts() ) {
516 // Set the state of modules loaded as only scripts to ready
517 if ( count( $modules ) && $context->getOnly() === 'scripts'
518 && !isset( $modules['startup'] ) )
519 {
520 $out .= self::makeLoaderStateScript(
521 array_fill_keys( array_keys( $modules ), 'ready' ) );
522 }
523 // Set the state of modules which were requested but unavailable as missing
524 if ( is_array( $missing ) && count( $missing ) ) {
525 $out .= self::makeLoaderStateScript( array_fill_keys( $missing, 'missing' ) );
526 }
527 }
528
529 if ( !$context->getDebug() ) {
530 if ( $context->getOnly() === 'styles' ) {
531 $out = $this->filter( 'minify-css', $out );
532 } else {
533 $out = $this->filter( 'minify-js', $out );
534 }
535 }
536
537 wfProfileOut( __METHOD__ );
538 return $exceptions . $out;
539 }
540
541 /* Static Methods */
542
543 /**
544 * Returns JS code to call to mediaWiki.loader.implement for a module with
545 * given properties.
546 *
547 * @param $name Module name
548 * @param $scripts Array: List of JavaScript code snippets to be executed after the
549 * module is loaded
550 * @param $styles Array: List of CSS strings keyed by media type
551 * @param $messages Mixed: List of messages associated with this module. May either be an
552 * associative array mapping message key to value, or a JSON-encoded message blob containing
553 * the same data, wrapped in an XmlJsCode object.
554 */
555 public static function makeLoaderImplementScript( $name, $scripts, $styles, $messages ) {
556 if ( is_array( $scripts ) ) {
557 $scripts = implode( $scripts, "\n" );
558 }
559 return Xml::encodeJsCall(
560 'mediaWiki.loader.implement',
561 array(
562 $name,
563 new XmlJsCode( "function( $, mw ) {{$scripts}}" ),
564 (object)$styles,
565 (object)$messages
566 ) );
567 }
568
569 /**
570 * Returns JS code which, when called, will register a given list of messages.
571 *
572 * @param $messages Mixed: Either an associative array mapping message key to value, or a
573 * JSON-encoded message blob containing the same data, wrapped in an XmlJsCode object.
574 */
575 public static function makeMessageSetScript( $messages ) {
576 return Xml::encodeJsCall( 'mediaWiki.messages.set', array( (object)$messages ) );
577 }
578
579 /**
580 * Combines an associative array mapping media type to CSS into a
581 * single stylesheet with @media blocks.
582 *
583 * @param $styles Array: List of CSS strings keyed by media type
584 */
585 public static function makeCombinedStyles( array $styles ) {
586 $out = '';
587 foreach ( $styles as $media => $style ) {
588 // Transform the media type based on request params and config
589 // The way that this relies on $wgRequest to propagate request params is slightly evil
590 $media = OutputPage::transformCssMedia( $media );
591
592 if ( $media === null ) {
593 // Skip
594 } else if ( $media === '' || $media == 'all' ) {
595 // Don't output invalid or frivolous @media statements
596 $out .= "$style\n";
597 } else {
598 $out .= "@media $media {\n" . str_replace( "\n", "\n\t", "\t" . $style ) . "\n}\n";
599 }
600 }
601 return $out;
602 }
603
604 /**
605 * Returns a JS call to mediaWiki.loader.state, which sets the state of a
606 * module or modules to a given value. Has two calling conventions:
607 *
608 * - ResourceLoader::makeLoaderStateScript( $name, $state ):
609 * Set the state of a single module called $name to $state
610 *
611 * - ResourceLoader::makeLoaderStateScript( array( $name => $state, ... ) ):
612 * Set the state of modules with the given names to the given states
613 */
614 public static function makeLoaderStateScript( $name, $state = null ) {
615 if ( is_array( $name ) ) {
616 return Xml::encodeJsCall( 'mediaWiki.loader.state', array( $name ) );
617 } else {
618 return Xml::encodeJsCall( 'mediaWiki.loader.state', array( $name, $state ) );
619 }
620 }
621
622 /**
623 * Returns JS code which calls the script given by $script. The script will
624 * be called with local variables name, version, dependencies and group,
625 * which will have values corresponding to $name, $version, $dependencies
626 * and $group as supplied.
627 *
628 * @param $name String: Module name
629 * @param $version Integer: Module version number as a timestamp
630 * @param $dependencies Array: List of module names on which this module depends
631 * @param $group String: Group which the module is in.
632 * @param $script String: JavaScript code
633 */
634 public static function makeCustomLoaderScript( $name, $version, $dependencies, $group, $script ) {
635 $script = str_replace( "\n", "\n\t", trim( $script ) );
636 return Xml::encodeJsCall(
637 "( function( name, version, dependencies, group ) {\n\t$script\n} )",
638 array( $name, $version, $dependencies, $group ) );
639 }
640
641 /**
642 * Returns JS code which calls mediaWiki.loader.register with the given
643 * parameters. Has three calling conventions:
644 *
645 * - ResourceLoader::makeLoaderRegisterScript( $name, $version, $dependencies, $group ):
646 * Register a single module.
647 *
648 * - ResourceLoader::makeLoaderRegisterScript( array( $name1, $name2 ) ):
649 * Register modules with the given names.
650 *
651 * - ResourceLoader::makeLoaderRegisterScript( array(
652 * array( $name1, $version1, $dependencies1, $group1 ),
653 * array( $name2, $version2, $dependencies1, $group2 ),
654 * ...
655 * ) ):
656 * Registers modules with the given names and parameters.
657 *
658 * @param $name String: Module name
659 * @param $version Integer: Module version number as a timestamp
660 * @param $dependencies Array: List of module names on which this module depends
661 * @param $group String: group which the module is in.
662 */
663 public static function makeLoaderRegisterScript( $name, $version = null,
664 $dependencies = null, $group = null )
665 {
666 if ( is_array( $name ) ) {
667 return Xml::encodeJsCall( 'mediaWiki.loader.register', array( $name ) );
668 } else {
669 $version = (int) $version > 1 ? (int) $version : 1;
670 return Xml::encodeJsCall( 'mediaWiki.loader.register',
671 array( $name, $version, $dependencies, $group ) );
672 }
673 }
674
675 /**
676 * Returns JS code which runs given JS code if the client-side framework is
677 * present.
678 *
679 * @param $script String: JavaScript code
680 */
681 public static function makeLoaderConditionalScript( $script ) {
682 $script = str_replace( "\n", "\n\t", trim( $script ) );
683 return "if ( window.mediaWiki ) {\n\t$script\n}\n";
684 }
685
686 /**
687 * Returns JS code which will set the MediaWiki configuration array to
688 * the given value.
689 *
690 * @param $configuration Array: List of configuration values keyed by variable name
691 */
692 public static function makeConfigSetScript( array $configuration ) {
693 return Xml::encodeJsCall( 'mediaWiki.config.set', array( $configuration ) );
694 }
695
696 /**
697 * Determine whether debug mode was requested
698 * Order of priority is 1) request param, 2) cookie, 3) $wg setting
699 * @return bool
700 */
701 public static function inDebugMode() {
702 global $wgRequest, $wgResourceLoaderDebug;
703 static $retval = null;
704 if ( !is_null( $retval ) )
705 return $retval;
706 return $retval = $wgRequest->getFuzzyBool( 'debug',
707 $wgRequest->getCookie( 'resourceLoaderDebug', '', $wgResourceLoaderDebug ) );
708 }
709 }