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.
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.
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
19 * @author Roan Kattouw
20 * @author Trevor Parscal
23 defined( 'MEDIAWIKI' ) ||
die( 1 );
26 * Dynamic JavaScript and CSS resource loading system.
28 * Most of the documention is on the MediaWiki documentation wiki starting at
29 * http://www.mediawiki.org/wiki/ResourceLoader
31 class ResourceLoader
{
33 /* Protected Static Members */
35 // @var array list of module name/ResourceLoaderModule object pairs
36 protected $modules = array();
38 /* Protected Methods */
41 * Loads information stored in the database about modules
43 * This is not inside the module code because it's so much more performant to request all of the information at once
44 * than it is to have each module requests it's own information.
46 * This method grab modules dependencies from the database and initialize modules object.
47 * A first pass compute dependencies, a second one the blob mtime.
49 * @param $modules Array List of module names to preload information for
50 * @param $context ResourceLoaderContext Context to load the information within
52 protected function preloadModuleInfo( array $modules, ResourceLoaderContext
$context ) {
53 if ( !count( $modules ) ) {
54 return; # or Database*::select() will explode
56 $dbr = wfGetDb( DB_SLAVE
);
57 $skin = $context->getSkin();
58 $lang = $context->getLanguage();
60 // Get file dependency information
61 $res = $dbr->select( 'module_deps', array( 'md_module', 'md_deps' ), array(
62 'md_module' => $modules,
63 'md_skin' => $context->getSkin()
67 // Set modules dependecies
68 $modulesWithDeps = array();
69 foreach ( $res as $row ) {
70 $this->modules
[$row->md_module
]->setFileDependencies( $skin,
71 FormatJson
::decode( $row->md_deps
, true )
73 $modulesWithDeps[] = $row->md_module
;
75 // Register the absence of a dependency row too
76 foreach ( array_diff( $modules, $modulesWithDeps ) as $name ) {
77 $this->modules
[$name]->setFileDependencies( $skin, array() );
80 // Get message blob mtimes. Only do this for modules with messages
81 $modulesWithMessages = array();
82 $modulesWithoutMessages = array();
83 foreach ( $modules as $name ) {
84 if ( count( $this->modules
[$name]->getMessages() ) ) {
85 $modulesWithMessages[] = $name;
87 $modulesWithoutMessages[] = $name;
90 if ( count( $modulesWithMessages ) ) {
91 $res = $dbr->select( 'msg_resource', array( 'mr_resource', 'mr_timestamp' ), array(
92 'mr_resource' => $modulesWithMessages,
96 foreach ( $res as $row ) {
97 $this->modules
[$row->mr_resource
]->setMsgBlobMtime( $lang, $row->mr_timestamp
);
100 foreach ( $modulesWithoutMessages as $name ) {
101 $this->modules
[$name]->setMsgBlobMtime( $lang, 0 );
106 * Runs text (js,CSS) through a filter, caching the filtered result for future calls.
108 * Availables filters are:
109 * - minify-js \see JSMin::minify
110 * - minify-css \see CSSMin::minify
111 * - flip-css \see CSSJanus::transform
112 * When the filter names does not exist, text is returned as is.
114 * @param $filter String: name of filter to run
115 * @param $data String: text to filter, such as JavaScript or CSS text
116 * @param $file String: path to file being filtered, (optional: only required for CSS to resolve paths)
117 * @return String: filtered data
119 protected function filter( $filter, $data ) {
121 wfProfileIn( __METHOD__
);
123 // For empty or whitespace-only things, don't do any processing
124 # FIXME: we should return the data unfiltered if $filter is not supported.
125 # that would save up a md5 computation and one memcached get.
126 if ( trim( $data ) === '' ) {
127 wfProfileOut( __METHOD__
);
132 $key = wfMemcKey( 'resourceloader', 'filter', $filter, md5( $data ) );
133 $cached = $wgMemc->get( $key );
135 if ( $cached !== false && $cached !== null ) {
136 wfProfileOut( __METHOD__
);
143 # note: if adding a new filter. Please update method documentation above.
145 $result = JSMin
::minify( $data );
148 $result = CSSMin
::minify( $data );
151 $result = CSSJanus
::transform( $data, true, false );
154 // Don't cache anything, just pass right through
155 wfProfileOut( __METHOD__
);
158 } catch ( Exception
$exception ) {
159 throw new MWException( 'Filter threw an exception: ' . $exception->getMessage() );
162 // Save filtered text to memcached
163 $wgMemc->set( $key, $result );
165 wfProfileOut( __METHOD__
);
172 * Registers core modules and runs registration hooks
174 public function __construct() {
177 wfProfileIn( __METHOD__
);
179 // Register core modules
180 $this->register( include( "$IP/resources/Resources.php" ) );
181 // Register extension modules
182 wfRunHooks( 'ResourceLoaderRegisterModules', array( &$this ) );
184 wfProfileOut( __METHOD__
);
188 * Registers a module with the ResourceLoader system.
190 * Note that registering the same object under multiple names is not supported
191 * and may silently fail in all kinds of interesting ways.
193 * @param $name Mixed: string of name of module or array of name/object pairs
194 * @param $object ResourceLoaderModule: module object (optional when using
195 * multiple-registration calling style)
196 * @return Boolean: false if there were any errors, in which case one or more
197 * modules were not registered
199 * @todo We need much more clever error reporting, not just in detailing what
200 * happened, but in bringing errors to the client in a way that they can
201 * easily see them if they want to, such as by using FireBug
203 public function register( $name, ResourceLoaderModule
$object = null ) {
204 wfProfileIn( __METHOD__
);
206 // Allow multiple modules to be registered in one call
207 if ( is_array( $name ) && !isset( $object ) ) {
208 foreach ( $name as $key => $value ) {
209 $this->register( $key, $value );
212 wfProfileOut( __METHOD__
);
216 // Disallow duplicate registrations
217 if ( isset( $this->modules
[$name] ) ) {
218 // A module has already been registered by this name
219 throw new MWException( 'Another module has already been registered as ' . $name );
222 // Validate the input (type hinting lets null through)
223 if ( !( $object instanceof ResourceLoaderModule
) ) {
224 throw new MWException( 'Invalid ResourceLoader module error. Instances of ResourceLoaderModule expected.' );
228 $this->modules
[$name] = $object;
229 $object->setName( $name );
231 wfProfileOut( __METHOD__
);
235 * Gets a map of all modules and their options
237 * @return Array: array( modulename => ResourceLoaderModule )
239 public function getModules() {
240 return $this->modules
;
244 * Get the ResourceLoaderModule object for a given module name
246 * @param $name String: module name
247 * @return mixed ResourceLoaderModule or null if not registered
249 public function getModule( $name ) {
250 return isset( $this->modules
[$name] ) ?
$this->modules
[$name] : null;
254 * Outputs a response to a resource load-request, including a content-type header
256 * @param $context ResourceLoaderContext object
258 public function respond( ResourceLoaderContext
$context ) {
259 global $wgResourceLoaderMaxage, $wgCacheEpoch;
261 wfProfileIn( __METHOD__
);
263 // Split requested modules into two groups, modules and missing
267 foreach ( $context->getModules() as $name ) {
268 if ( isset( $this->modules
[$name] ) ) {
269 $modules[$name] = $this->modules
[$name];
275 // If a version wasn't specified we need a shorter expiry time for updates to
276 // propagate to clients quickly
277 if ( is_null( $context->getVersion() ) ) {
278 $maxage = $wgResourceLoaderMaxage['unversioned']['client'];
279 $smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
281 // If a version was specified we can use a longer expiry time since changing
282 // version numbers causes cache misses
284 $maxage = $wgResourceLoaderMaxage['versioned']['client'];
285 $smaxage = $wgResourceLoaderMaxage['versioned']['server'];
288 // Preload information needed to the mtime calculation below
289 $this->preloadModuleInfo( array_keys( $modules ), $context );
291 // To send Last-Modified and support If-Modified-Since, we need to detect
292 // the last modified time
293 wfProfileIn( __METHOD__
.'-getModifiedTime' );
294 $mtime = wfTimestamp( TS_UNIX
, $wgCacheEpoch );
295 foreach ( $modules as $module ) {
296 // Bypass squid cache if the request includes any private modules
297 if ( $module->getGroup() === 'private' ) {
300 // Calculate maximum modified time
301 $mtime = max( $mtime, $module->getModifiedTime( $context ) );
303 wfProfileOut( __METHOD__
.'-getModifiedTime' );
305 header( 'Content-Type: ' . ( $context->getOnly() === 'styles' ?
'text/css' : 'text/javascript' ) );
306 header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822
, $mtime ) );
307 header( "Cache-Control: public, max-age=$maxage, s-maxage=$smaxage" );
308 header( 'Expires: ' . wfTimestamp( TS_RFC2822
, min( $maxage, $smaxage ) +
time() ) );
310 // If there's an If-Modified-Since header, respond with a 304 appropriately
311 $ims = $context->getRequest()->getHeader( 'If-Modified-Since' );
312 if ( $ims !== false && $mtime <= wfTimestamp( TS_UNIX
, $ims ) ) {
313 header( 'HTTP/1.0 304 Not Modified' );
314 header( 'Status: 304 Not Modified' );
315 wfProfileOut( __METHOD__
);
319 $response = $this->makeModuleResponse( $context, $modules, $missing );
320 if ( $context->getDebug() && strlen( $warnings = ob_get_contents() ) ) {
321 $response .= "/*\n$warnings\n*/";
323 // Clear any warnings from the buffer
327 wfProfileOut( __METHOD__
);
332 * @param $context ResourceLoaderContext
333 * @param $modules Array array( modulename => ResourceLoaderModule )
334 * @param $missing Unavailables modules (Default null)
336 public function makeModuleResponse( ResourceLoaderContext
$context, array $modules, $missing = null ) {
338 $blobs = $context->shouldIncludeMessages() ?
339 MessageBlobStore
::get( $this, $modules, $context->getLanguage() ) : array();
343 foreach ( $modules as $name => $module ) {
344 wfProfileIn( __METHOD__
. '-' . $name );
348 if ( $context->shouldIncludeScripts() ) {
349 $scripts .= $module->getScript( $context ) . "\n";
354 if ( $context->shouldIncludeStyles() && ( count( $styles = $module->getStyles( $context ) ) ) ) {
355 // Flip CSS on a per-module basis
356 if ( $this->modules
[$name]->getFlip( $context ) ) {
357 foreach ( $styles as $media => $style ) {
358 $styles[$media] = $this->filter( 'flip-css', $style );
364 $messages = isset( $blobs[$name] ) ?
$blobs[$name] : '{}';
367 switch ( $context->getOnly() ) {
372 $out .= self
::makeCombinedStyles( $styles );
375 $out .= self
::makeMessageSetScript( $messages );
378 // Minify CSS before embedding in mediaWiki.loader.implement call (unless in debug mode)
379 if ( !$context->getDebug() ) {
380 foreach ( $styles as $media => $style ) {
381 $styles[$media] = $this->filter( 'minify-css', $style );
384 $out .= self
::makeLoaderImplementScript( $name, $scripts, $styles, $messages );
388 wfProfileOut( __METHOD__
. '-' . $name );
391 // Update module states
392 if ( $context->shouldIncludeScripts() ) {
393 // Set the state of modules loaded as only scripts to ready
394 if ( count( $modules ) && $context->getOnly() === 'scripts' && !isset( $modules['startup'] ) ) {
395 $out .= self
::makeLoaderStateScript( array_fill_keys( array_keys( $modules ), 'ready' ) );
397 // Set the state of modules which were requested but unavailable as missing
398 if ( is_array( $missing ) && count( $missing ) ) {
399 $out .= self
::makeLoaderStateScript( array_fill_keys( $missing, 'missing' ) );
403 if ( $context->getDebug() ) {
406 if ( $context->getOnly() === 'styles' ) {
407 return $this->filter( 'minify-css', $out );
409 return $this->filter( 'minify-js', $out );
416 public static function makeLoaderImplementScript( $name, $scripts, $styles, $messages ) {
417 if ( is_array( $scripts ) ) {
418 $scripts = implode( $scripts, "\n" );
420 if ( is_array( $styles ) ) {
421 $styles = count( $styles ) ? FormatJson
::encode( $styles ) : 'null';
423 if ( is_array( $messages ) ) {
424 $messages = count( $messages ) ? FormatJson
::encode( $messages ) : 'null';
426 return "mediaWiki.loader.implement( '$name', function() {{$scripts}},\n$styles,\n$messages );\n";
429 public static function makeMessageSetScript( $messages ) {
430 if ( is_array( $messages ) ) {
431 $messages = count( $messages ) ? FormatJson
::encode( $messages ) : 'null';
433 return "mediaWiki.msg.set( $messages );\n";
436 public static function makeCombinedStyles( array $styles ) {
438 foreach ( $styles as $media => $style ) {
439 $out .= "@media $media {\n" . str_replace( "\n", "\n\t", "\t" . $style ) . "\n}\n";
444 public static function makeLoaderStateScript( $name, $state = null ) {
445 if ( is_array( $name ) ) {
446 $statuses = FormatJson
::encode( $name );
447 return "mediaWiki.loader.state( $statuses );\n";
449 $name = Xml
::escapeJsString( $name );
450 $state = Xml
::escapeJsString( $state );
451 return "mediaWiki.loader.state( '$name', '$state' );\n";
455 public static function makeCustomLoaderScript( $name, $version, $dependencies, $group, $script ) {
456 $name = Xml
::escapeJsString( $name );
457 $version = (int) $version > 1 ?
(int) $version : 1;
458 $dependencies = FormatJson
::encode( $dependencies );
459 $group = FormatJson
::encode( $group );
460 $script = str_replace( "\n", "\n\t", trim( $script ) );
461 return "( function( name, version, dependencies, group ) {\n\t$script\n} )" .
462 "( '$name', $version, $dependencies, $group );\n";
465 public static function makeLoaderRegisterScript( $name, $version = null, $dependencies = null, $group = null ) {
466 if ( is_array( $name ) ) {
467 $registrations = FormatJson
::encode( $name );
468 return "mediaWiki.loader.register( $registrations );\n";
470 $name = Xml
::escapeJsString( $name );
471 $version = (int) $version > 1 ?
(int) $version : 1;
472 $dependencies = FormatJson
::encode( $dependencies );
473 $group = FormatJson
::encode( $group );
474 return "mediaWiki.loader.register( '$name', $version, $dependencies, $group );\n";
478 public static function makeLoaderConditionalScript( $script ) {
479 $script = str_replace( "\n", "\n\t", trim( $script ) );
480 return "if ( window.mediaWiki ) {\n\t$script\n}\n";
483 public static function makeConfigSetScript( array $configuration ) {
484 $configuration = FormatJson
::encode( $configuration );
485 return "mediaWiki.config.set( $configuration );\n";