2010-09-04 04:00:09 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-09-05 13:31:34 +00:00
|
|
|
* @file
|
2010-09-04 04:00:09 +00:00
|
|
|
* @author Roan Kattouw
|
|
|
|
|
* @author Trevor Parscal
|
|
|
|
|
*/
|
|
|
|
|
|
2010-09-24 17:31:40 +00:00
|
|
|
defined( 'MEDIAWIKI' ) || die( 1 );
|
|
|
|
|
|
2010-09-05 13:31:34 +00:00
|
|
|
/**
|
2010-09-04 04:00:09 +00:00
|
|
|
* Dynamic JavaScript and CSS resource loading system
|
|
|
|
|
*/
|
|
|
|
|
class ResourceLoader {
|
2010-09-13 23:19:05 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/* Protected Static Members */
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// @var array list of module name/ResourceLoaderModule object pairs
|
2010-09-29 19:04:04 +00:00
|
|
|
protected $modules = array();
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-29 19:04:04 +00:00
|
|
|
/* Protected Methods */
|
2010-09-23 21:23:51 +00:00
|
|
|
|
2010-09-29 19:04:04 +00:00
|
|
|
/**
|
2010-09-24 18:49:19 +00:00
|
|
|
* Loads information stored in the database about modules
|
|
|
|
|
*
|
|
|
|
|
* This is not inside the module code because it's so much more performant to request all of the information at once
|
|
|
|
|
* than it is to have each module requests it's own information.
|
|
|
|
|
*
|
2010-09-24 22:10:25 +00:00
|
|
|
* @param $modules array list of module names to preload information for
|
2010-09-24 18:49:19 +00:00
|
|
|
* @param $context ResourceLoaderContext context to load the information within
|
|
|
|
|
*/
|
2010-09-29 19:04:04 +00:00
|
|
|
protected function preloadModuleInfo( array $modules, ResourceLoaderContext $context ) {
|
2010-10-02 10:18:48 +00:00
|
|
|
if ( !count( $modules ) ) {
|
|
|
|
|
return; # or Database*::select() will explode
|
|
|
|
|
}
|
2010-09-23 21:23:51 +00:00
|
|
|
$dbr = wfGetDb( DB_SLAVE );
|
|
|
|
|
$skin = $context->getSkin();
|
|
|
|
|
$lang = $context->getLanguage();
|
|
|
|
|
|
|
|
|
|
// Get file dependency information
|
|
|
|
|
$res = $dbr->select( 'module_deps', array( 'md_module', 'md_deps' ), array(
|
|
|
|
|
'md_module' => $modules,
|
|
|
|
|
'md_skin' => $context->getSkin()
|
|
|
|
|
), __METHOD__
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$modulesWithDeps = array();
|
|
|
|
|
foreach ( $res as $row ) {
|
2010-09-29 19:04:04 +00:00
|
|
|
$this->modules[$row->md_module]->setFileDependencies( $skin,
|
2010-09-23 21:23:51 +00:00
|
|
|
FormatJson::decode( $row->md_deps, true )
|
|
|
|
|
);
|
|
|
|
|
$modulesWithDeps[] = $row->md_module;
|
|
|
|
|
}
|
|
|
|
|
// Register the absence of a dependencies row too
|
|
|
|
|
foreach ( array_diff( $modules, $modulesWithDeps ) as $name ) {
|
2010-09-29 19:04:04 +00:00
|
|
|
$this->modules[$name]->setFileDependencies( $skin, array() );
|
2010-09-23 21:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get message blob mtimes. Only do this for modules with messages
|
|
|
|
|
$modulesWithMessages = array();
|
|
|
|
|
$modulesWithoutMessages = array();
|
|
|
|
|
foreach ( $modules as $name ) {
|
2010-09-29 19:04:04 +00:00
|
|
|
if ( count( $this->modules[$name]->getMessages() ) ) {
|
2010-09-23 21:23:51 +00:00
|
|
|
$modulesWithMessages[] = $name;
|
|
|
|
|
} else {
|
|
|
|
|
$modulesWithoutMessages[] = $name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( count( $modulesWithMessages ) ) {
|
|
|
|
|
$res = $dbr->select( 'msg_resource', array( 'mr_resource', 'mr_timestamp' ), array(
|
|
|
|
|
'mr_resource' => $modulesWithMessages,
|
|
|
|
|
'mr_lang' => $lang
|
|
|
|
|
), __METHOD__
|
|
|
|
|
);
|
|
|
|
|
foreach ( $res as $row ) {
|
2010-09-29 19:04:04 +00:00
|
|
|
$this->modules[$row->mr_resource]->setMsgBlobMtime( $lang, $row->mr_timestamp );
|
2010-09-23 21:23:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach ( $modulesWithoutMessages as $name ) {
|
2010-09-29 19:04:04 +00:00
|
|
|
$this->modules[$name]->setMsgBlobMtime( $lang, 0 );
|
2010-09-23 21:23:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-15 18:27:47 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Runs text through a filter, caching the filtered result for future calls
|
|
|
|
|
*
|
2010-09-05 13:31:34 +00:00
|
|
|
* @param $filter String: name of filter to run
|
|
|
|
|
* @param $data String: text to filter, such as JavaScript or CSS text
|
2010-09-24 18:49:19 +00:00
|
|
|
* @param $file String: path to file being filtered, (optional: only required for CSS to resolve paths)
|
2010-09-05 13:31:34 +00:00
|
|
|
* @return String: filtered data
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
2010-09-29 19:04:04 +00:00
|
|
|
protected function filter( $filter, $data ) {
|
2010-09-04 04:00:09 +00:00
|
|
|
global $wgMemc;
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// For empty or whitespace-only things, don't do any processing
|
|
|
|
|
if ( trim( $data ) === '' ) {
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2010-09-04 04:00:09 +00:00
|
|
|
return $data;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Try memcached
|
|
|
|
|
$key = wfMemcKey( 'resourceloader', 'filter', $filter, md5( $data ) );
|
|
|
|
|
$cached = $wgMemc->get( $key );
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
if ( $cached !== false && $cached !== null ) {
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2010-09-04 04:00:09 +00:00
|
|
|
return $cached;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Run the filter
|
|
|
|
|
try {
|
|
|
|
|
switch ( $filter ) {
|
|
|
|
|
case 'minify-js':
|
|
|
|
|
$result = JSMin::minify( $data );
|
|
|
|
|
break;
|
|
|
|
|
case 'minify-css':
|
|
|
|
|
$result = CSSMin::minify( $data );
|
|
|
|
|
break;
|
|
|
|
|
case 'flip-css':
|
|
|
|
|
$result = CSSJanus::transform( $data, true, false );
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// Don't cache anything, just pass right through
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2010-09-04 04:00:09 +00:00
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
} catch ( Exception $exception ) {
|
|
|
|
|
throw new MWException( 'Filter threw an exception: ' . $exception->getMessage() );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Save to memcached
|
|
|
|
|
$wgMemc->set( $key, $result );
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2010-09-04 04:00:09 +00:00
|
|
|
return $result;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-29 19:04:04 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Registers core modules and runs registration hooks
|
|
|
|
|
*/
|
|
|
|
|
public function __construct() {
|
|
|
|
|
global $IP;
|
|
|
|
|
|
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
|
|
|
|
|
|
// Register core modules
|
|
|
|
|
$this->register( include( "$IP/resources/Resources.php" ) );
|
|
|
|
|
// Register extension modules
|
|
|
|
|
wfRunHooks( 'ResourceLoaderRegisterModules', array( &$this ) );
|
|
|
|
|
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Registers a module with the ResourceLoader system.
|
|
|
|
|
*
|
2010-09-17 11:45:49 +00:00
|
|
|
* Note that registering the same object under multiple names is not supported
|
|
|
|
|
* and may silently fail in all kinds of interesting ways.
|
2010-09-04 12:53:01 +00:00
|
|
|
*
|
2010-09-05 13:31:34 +00:00
|
|
|
* @param $name Mixed: string of name of module or array of name/object pairs
|
2010-09-17 11:45:49 +00:00
|
|
|
* @param $object ResourceLoaderModule: module object (optional when using
|
|
|
|
|
* multiple-registration calling style)
|
|
|
|
|
* @return Boolean: false if there were any errors, in which case one or more
|
|
|
|
|
* modules were not registered
|
2010-09-04 12:53:01 +00:00
|
|
|
*
|
2010-09-17 11:45:49 +00:00
|
|
|
* @todo We need much more clever error reporting, not just in detailing what
|
|
|
|
|
* happened, but in bringing errors to the client in a way that they can
|
|
|
|
|
* easily see them if they want to, such as by using FireBug
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
2010-09-29 19:04:04 +00:00
|
|
|
public function register( $name, ResourceLoaderModule $object = null ) {
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2010-09-15 18:27:47 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Allow multiple modules to be registered in one call
|
|
|
|
|
if ( is_array( $name ) && !isset( $object ) ) {
|
|
|
|
|
foreach ( $name as $key => $value ) {
|
2010-09-29 19:04:04 +00:00
|
|
|
$this->register( $key, $value );
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2010-09-04 04:00:09 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Disallow duplicate registrations
|
2010-09-29 19:04:04 +00:00
|
|
|
if ( isset( $this->modules[$name] ) ) {
|
2010-09-04 04:00:09 +00:00
|
|
|
// A module has already been registered by this name
|
|
|
|
|
throw new MWException( 'Another module has already been registered as ' . $name );
|
|
|
|
|
}
|
2010-09-29 23:57:53 +00:00
|
|
|
|
|
|
|
|
// Validate the input (type hinting lets null through)
|
|
|
|
|
if ( !( $object instanceof ResourceLoaderModule ) ) {
|
|
|
|
|
throw new MWException( 'Invalid ResourceLoader module error. Instances of ResourceLoaderModule expected.' );
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Attach module
|
2010-09-29 19:04:04 +00:00
|
|
|
$this->modules[$name] = $object;
|
2010-09-04 04:00:09 +00:00
|
|
|
$object->setName( $name );
|
2010-09-29 19:04:04 +00:00
|
|
|
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Gets a map of all modules and their options
|
|
|
|
|
*
|
2010-09-05 13:31:34 +00:00
|
|
|
* @return Array: array( modulename => ResourceLoaderModule )
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
2010-09-29 19:04:04 +00:00
|
|
|
public function getModules() {
|
|
|
|
|
return $this->modules;
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the ResourceLoaderModule object for a given module name
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $name String: module name
|
2010-09-04 04:00:09 +00:00
|
|
|
* @return mixed ResourceLoaderModule or null if not registered
|
|
|
|
|
*/
|
2010-09-29 19:04:04 +00:00
|
|
|
public function getModule( $name ) {
|
|
|
|
|
return isset( $this->modules[$name] ) ? $this->modules[$name] : null;
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-05 13:31:34 +00:00
|
|
|
/**
|
2010-09-04 04:00:09 +00:00
|
|
|
* Outputs a response to a resource load-request, including a content-type header
|
|
|
|
|
*
|
2010-09-05 13:31:34 +00:00
|
|
|
* @param $context ResourceLoaderContext object
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
2010-09-29 19:04:04 +00:00
|
|
|
public function respond( ResourceLoaderContext $context ) {
|
2010-10-07 19:35:05 +00:00
|
|
|
global $wgResourceLoaderMaxage, $wgCacheEpoch;
|
2010-09-13 23:19:05 +00:00
|
|
|
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2010-09-15 18:15:36 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Split requested modules into two groups, modules and missing
|
|
|
|
|
$modules = array();
|
|
|
|
|
$missing = array();
|
2010-09-24 22:10:25 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
foreach ( $context->getModules() as $name ) {
|
2010-09-29 19:04:04 +00:00
|
|
|
if ( isset( $this->modules[$name] ) ) {
|
|
|
|
|
$modules[$name] = $this->modules[$name];
|
2010-09-04 04:00:09 +00:00
|
|
|
} else {
|
|
|
|
|
$missing[] = $name;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-17 11:45:49 +00:00
|
|
|
// If a version wasn't specified we need a shorter expiry time for updates to
|
|
|
|
|
// propagate to clients quickly
|
2010-09-13 23:19:05 +00:00
|
|
|
if ( is_null( $context->getVersion() ) ) {
|
2010-09-22 20:44:12 +00:00
|
|
|
$maxage = $wgResourceLoaderMaxage['unversioned']['client'];
|
|
|
|
|
$smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
|
2010-09-13 23:19:05 +00:00
|
|
|
}
|
2010-09-17 11:45:49 +00:00
|
|
|
// If a version was specified we can use a longer expiry time since changing
|
|
|
|
|
// version numbers causes cache misses
|
2010-09-13 23:19:05 +00:00
|
|
|
else {
|
2010-09-22 20:44:12 +00:00
|
|
|
$maxage = $wgResourceLoaderMaxage['versioned']['client'];
|
|
|
|
|
$smaxage = $wgResourceLoaderMaxage['versioned']['server'];
|
2010-09-13 23:19:05 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-23 21:23:51 +00:00
|
|
|
// Preload information needed to the mtime calculation below
|
2010-09-29 19:04:04 +00:00
|
|
|
$this->preloadModuleInfo( array_keys( $modules ), $context );
|
2010-09-23 21:23:51 +00:00
|
|
|
|
2010-09-17 11:45:49 +00:00
|
|
|
// To send Last-Modified and support If-Modified-Since, we need to detect
|
|
|
|
|
// the last modified time
|
|
|
|
|
wfProfileIn( __METHOD__.'-getModifiedTime' );
|
2010-10-08 10:25:23 +00:00
|
|
|
$mtime = wfTimestamp( TS_UNIX, $wgCacheEpoch );
|
2010-09-24 22:10:25 +00:00
|
|
|
foreach ( $modules as $module ) {
|
|
|
|
|
// Bypass squid cache if the request includes any private modules
|
|
|
|
|
if ( $module->getGroup() === 'private' ) {
|
|
|
|
|
$smaxage = 0;
|
|
|
|
|
}
|
|
|
|
|
// Calculate maximum modified time
|
|
|
|
|
$mtime = max( $mtime, $module->getModifiedTime( $context ) );
|
2010-09-04 08:38:45 +00:00
|
|
|
}
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__.'-getModifiedTime' );
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-13 23:19:05 +00:00
|
|
|
header( 'Content-Type: ' . ( $context->getOnly() === 'styles' ? 'text/css' : 'text/javascript' ) );
|
2010-09-04 04:00:09 +00:00
|
|
|
header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $mtime ) );
|
2010-09-08 16:35:20 +00:00
|
|
|
header( "Cache-Control: public, max-age=$maxage, s-maxage=$smaxage" );
|
2010-09-13 23:19:05 +00:00
|
|
|
header( 'Expires: ' . wfTimestamp( TS_RFC2822, min( $maxage, $smaxage ) + time() ) );
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-13 23:19:05 +00:00
|
|
|
// If there's an If-Modified-Since header, respond with a 304 appropriately
|
2010-09-04 04:00:09 +00:00
|
|
|
$ims = $context->getRequest()->getHeader( 'If-Modified-Since' );
|
2010-10-02 16:54:53 +00:00
|
|
|
if ( $ims !== false && $mtime <= wfTimestamp( TS_UNIX, $ims ) ) {
|
2010-09-04 04:00:09 +00:00
|
|
|
header( 'HTTP/1.0 304 Not Modified' );
|
|
|
|
|
header( 'Status: 304 Not Modified' );
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2010-09-04 04:00:09 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-10-18 20:16:19 +00:00
|
|
|
$response = $this->makeModuleResponse( $context, $modules, $missing );
|
2010-10-19 17:52:01 +00:00
|
|
|
if ( $context->getDebug() && strlen( $warnings = ob_get_contents() ) ) {
|
|
|
|
|
$response .= "/*\n$warnings\n*/";
|
|
|
|
|
}
|
2010-10-18 20:16:19 +00:00
|
|
|
// Clear any warnings from the buffer
|
|
|
|
|
ob_clean();
|
|
|
|
|
echo $response;
|
2010-09-24 22:10:25 +00:00
|
|
|
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-29 19:04:04 +00:00
|
|
|
public function makeModuleResponse( ResourceLoaderContext $context, array $modules, $missing = null ) {
|
2010-09-04 04:00:09 +00:00
|
|
|
// Pre-fetch blobs
|
|
|
|
|
$blobs = $context->shouldIncludeMessages() ?
|
2010-09-29 21:13:56 +00:00
|
|
|
MessageBlobStore::get( $this, $modules, $context->getLanguage() ) : array();
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Generate output
|
2010-09-24 18:49:19 +00:00
|
|
|
$out = '';
|
2010-09-24 22:10:25 +00:00
|
|
|
foreach ( $modules as $name => $module ) {
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileIn( __METHOD__ . '-' . $name );
|
2010-09-24 18:49:19 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Scripts
|
|
|
|
|
$scripts = '';
|
|
|
|
|
if ( $context->shouldIncludeScripts() ) {
|
2010-09-24 22:10:25 +00:00
|
|
|
$scripts .= $module->getScript( $context ) . "\n";
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Styles
|
2010-09-10 00:21:36 +00:00
|
|
|
$styles = array();
|
2010-10-13 23:52:37 +00:00
|
|
|
if ( $context->shouldIncludeStyles() && ( count( $styles = $module->getStyles( $context ) ) ) ) {
|
2010-09-24 18:49:19 +00:00
|
|
|
// Flip CSS on a per-module basis
|
2010-09-29 19:04:04 +00:00
|
|
|
if ( $this->modules[$name]->getFlip( $context ) ) {
|
2010-09-24 18:49:19 +00:00
|
|
|
foreach ( $styles as $media => $style ) {
|
2010-09-29 19:04:04 +00:00
|
|
|
$styles[$media] = $this->filter( 'flip-css', $style );
|
2010-09-10 00:21:36 +00:00
|
|
|
}
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Messages
|
|
|
|
|
$messages = isset( $blobs[$name] ) ? $blobs[$name] : '{}';
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-24 18:49:19 +00:00
|
|
|
// Append output
|
|
|
|
|
switch ( $context->getOnly() ) {
|
|
|
|
|
case 'scripts':
|
|
|
|
|
$out .= $scripts;
|
|
|
|
|
break;
|
|
|
|
|
case 'styles':
|
|
|
|
|
$out .= self::makeCombinedStyles( $styles );
|
|
|
|
|
break;
|
|
|
|
|
case 'messages':
|
|
|
|
|
$out .= self::makeMessageSetScript( $messages );
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2010-09-24 22:10:25 +00:00
|
|
|
// Minify CSS before embedding in mediaWiki.loader.implement call (unless in debug mode)
|
2010-09-24 18:49:19 +00:00
|
|
|
if ( !$context->getDebug() ) {
|
|
|
|
|
foreach ( $styles as $media => $style ) {
|
2010-09-29 19:04:04 +00:00
|
|
|
$styles[$media] = $this->filter( 'minify-css', $style );
|
2010-09-10 20:18:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-24 18:49:19 +00:00
|
|
|
$out .= self::makeLoaderImplementScript( $name, $scripts, $styles, $messages );
|
|
|
|
|
break;
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-24 18:49:19 +00:00
|
|
|
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__ . '-' . $name );
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-24 18:49:19 +00:00
|
|
|
// Update module states
|
2010-09-04 04:00:09 +00:00
|
|
|
if ( $context->shouldIncludeScripts() ) {
|
2010-09-24 18:49:19 +00:00
|
|
|
// Set the state of modules loaded as only scripts to ready
|
2010-09-24 22:10:25 +00:00
|
|
|
if ( count( $modules ) && $context->getOnly() === 'scripts' && !isset( $modules['startup'] ) ) {
|
|
|
|
|
$out .= self::makeLoaderStateScript( array_fill_keys( array_keys( $modules ), 'ready' ) );
|
2010-09-24 18:49:19 +00:00
|
|
|
}
|
|
|
|
|
// Set the state of modules which were requested but unavailable as missing
|
2010-09-24 22:10:25 +00:00
|
|
|
if ( is_array( $missing ) && count( $missing ) ) {
|
2010-09-24 18:49:19 +00:00
|
|
|
$out .= self::makeLoaderStateScript( array_fill_keys( $missing, 'missing' ) );
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-24 18:49:19 +00:00
|
|
|
if ( $context->getDebug() ) {
|
2010-09-24 22:10:25 +00:00
|
|
|
return $out;
|
2010-09-24 18:49:19 +00:00
|
|
|
} else {
|
|
|
|
|
if ( $context->getOnly() === 'styles' ) {
|
2010-09-29 19:04:04 +00:00
|
|
|
return $this->filter( 'minify-css', $out );
|
2010-09-04 04:00:09 +00:00
|
|
|
} else {
|
2010-09-29 19:04:04 +00:00
|
|
|
return $this->filter( 'minify-js', $out );
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-24 22:10:25 +00:00
|
|
|
|
2010-09-29 19:04:04 +00:00
|
|
|
/* Static Methods */
|
2010-09-24 22:10:25 +00:00
|
|
|
|
2010-09-24 18:49:19 +00:00
|
|
|
public static function makeLoaderImplementScript( $name, $scripts, $styles, $messages ) {
|
|
|
|
|
if ( is_array( $scripts ) ) {
|
|
|
|
|
$scripts = implode( $scripts, "\n" );
|
|
|
|
|
}
|
|
|
|
|
if ( is_array( $styles ) ) {
|
|
|
|
|
$styles = count( $styles ) ? FormatJson::encode( $styles ) : 'null';
|
|
|
|
|
}
|
|
|
|
|
if ( is_array( $messages ) ) {
|
|
|
|
|
$messages = count( $messages ) ? FormatJson::encode( $messages ) : 'null';
|
|
|
|
|
}
|
|
|
|
|
return "mediaWiki.loader.implement( '$name', function() {{$scripts}},\n$styles,\n$messages );\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function makeMessageSetScript( $messages ) {
|
|
|
|
|
if ( is_array( $messages ) ) {
|
|
|
|
|
$messages = count( $messages ) ? FormatJson::encode( $messages ) : 'null';
|
|
|
|
|
}
|
|
|
|
|
return "mediaWiki.msg.set( $messages );\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function makeCombinedStyles( array $styles ) {
|
|
|
|
|
$out = '';
|
|
|
|
|
foreach ( $styles as $media => $style ) {
|
|
|
|
|
$out .= "@media $media {\n" . str_replace( "\n", "\n\t", "\t" . $style ) . "\n}\n";
|
|
|
|
|
}
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function makeLoaderStateScript( $name, $state = null ) {
|
|
|
|
|
if ( is_array( $name ) ) {
|
|
|
|
|
$statuses = FormatJson::encode( $name );
|
|
|
|
|
return "mediaWiki.loader.state( $statuses );\n";
|
|
|
|
|
} else {
|
|
|
|
|
$name = Xml::escapeJsString( $name );
|
2010-10-01 22:06:03 +00:00
|
|
|
$state = Xml::escapeJsString( $state );
|
2010-09-24 18:49:19 +00:00
|
|
|
return "mediaWiki.loader.state( '$name', '$state' );\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-24 22:10:25 +00:00
|
|
|
|
2010-09-24 21:03:29 +00:00
|
|
|
public static function makeCustomLoaderScript( $name, $version, $dependencies, $group, $script ) {
|
|
|
|
|
$name = Xml::escapeJsString( $name );
|
|
|
|
|
$version = (int) $version > 1 ? (int) $version : 1;
|
2010-10-12 17:52:32 +00:00
|
|
|
$dependencies = FormatJson::encode( $dependencies );
|
|
|
|
|
$group = FormatJson::encode( $group );
|
2010-09-24 21:03:29 +00:00
|
|
|
$script = str_replace( "\n", "\n\t", trim( $script ) );
|
2010-10-01 20:26:18 +00:00
|
|
|
return "( function( name, version, dependencies, group ) {\n\t$script\n} )" .
|
2010-09-24 21:03:29 +00:00
|
|
|
"( '$name', $version, $dependencies, $group );\n";
|
|
|
|
|
}
|
2010-09-24 22:10:25 +00:00
|
|
|
|
2010-09-24 21:03:29 +00:00
|
|
|
public static function makeLoaderRegisterScript( $name, $version = null, $dependencies = null, $group = null ) {
|
|
|
|
|
if ( is_array( $name ) ) {
|
|
|
|
|
$registrations = FormatJson::encode( $name );
|
|
|
|
|
return "mediaWiki.loader.register( $registrations );\n";
|
|
|
|
|
} else {
|
|
|
|
|
$name = Xml::escapeJsString( $name );
|
|
|
|
|
$version = (int) $version > 1 ? (int) $version : 1;
|
2010-10-12 17:52:32 +00:00
|
|
|
$dependencies = FormatJson::encode( $dependencies );
|
|
|
|
|
$group = FormatJson::encode( $group );
|
2010-09-24 21:03:29 +00:00
|
|
|
return "mediaWiki.loader.register( '$name', $version, $dependencies, $group );\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-24 22:10:25 +00:00
|
|
|
|
|
|
|
|
public static function makeLoaderConditionalScript( $script ) {
|
|
|
|
|
$script = str_replace( "\n", "\n\t", trim( $script ) );
|
|
|
|
|
return "if ( window.mediaWiki ) {\n\t$script\n}\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function makeConfigSetScript( array $configuration ) {
|
|
|
|
|
$configuration = FormatJson::encode( $configuration );
|
|
|
|
|
return "mediaWiki.config.set( $configuration );\n";
|
|
|
|
|
}
|
2010-09-17 11:45:49 +00:00
|
|
|
}
|