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-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
|
|
|
|
|
protected static $modules = array();
|
2010-09-15 18:15:36 +00:00
|
|
|
protected static $initialized = false;
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/* Protected Static Methods */
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-23 21:23:51 +00:00
|
|
|
/**
|
2010-09-15 18:27:47 +00:00
|
|
|
* Registers core modules and runs registration hooks
|
|
|
|
|
*/
|
|
|
|
|
protected static function initialize() {
|
|
|
|
|
global $IP;
|
|
|
|
|
|
2010-09-15 18:33:51 +00:00
|
|
|
// Safety check - this should never be called more than once
|
|
|
|
|
if ( !self::$initialized ) {
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
|
// This needs to be first, because hooks might call ResourceLoader
|
|
|
|
|
// public interfaces which will call this
|
2010-09-15 18:33:51 +00:00
|
|
|
self::$initialized = true;
|
|
|
|
|
self::register( include( "$IP/resources/Resources.php" ) );
|
|
|
|
|
wfRunHooks( 'ResourceLoaderRegisterModules' );
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2010-09-15 18:27:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-23 21:23:51 +00:00
|
|
|
|
|
|
|
|
protected static function preloadModuleInfo( $modules, ResourceLoaderContext $context ) {
|
|
|
|
|
$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 ) {
|
|
|
|
|
self::$modules[$row->md_module]->setFileDependencies( $skin,
|
|
|
|
|
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 ) {
|
|
|
|
|
self::$modules[$name]->setFileDependencies( $skin, array() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get message blob mtimes. Only do this for modules with messages
|
|
|
|
|
$modulesWithMessages = array();
|
|
|
|
|
$modulesWithoutMessages = array();
|
|
|
|
|
foreach ( $modules as $name ) {
|
|
|
|
|
if ( count( self::$modules[$name]->getMessages() ) ) {
|
|
|
|
|
$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 ) {
|
|
|
|
|
self::$modules[$row->mr_resource]->setMsgBlobMtime( $lang, $row->mr_timestamp );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach ( $modulesWithoutMessages as $name ) {
|
|
|
|
|
self::$modules[$name]->setMsgBlobMtime( $lang, 0 );
|
|
|
|
|
}
|
|
|
|
|
}
|
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-17 11:45:49 +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
|
|
|
*/
|
|
|
|
|
protected static function filter( $filter, $data ) {
|
|
|
|
|
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-04 04:00:09 +00:00
|
|
|
/* Static Methods */
|
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
|
|
|
*/
|
|
|
|
|
public static function register( $name, ResourceLoaderModule $object = null ) {
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2010-09-15 18:33:51 +00:00
|
|
|
self::initialize();
|
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 ) {
|
|
|
|
|
self::register( $key, $value );
|
|
|
|
|
}
|
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
|
|
|
|
|
if ( isset( self::$modules[$name] ) ) {
|
|
|
|
|
// A module has already been registered by this name
|
|
|
|
|
throw new MWException( 'Another module has already been registered as ' . $name );
|
|
|
|
|
}
|
|
|
|
|
// Attach module
|
|
|
|
|
self::$modules[$name] = $object;
|
|
|
|
|
$object->setName( $name );
|
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
|
|
|
*/
|
|
|
|
|
public static function getModules() {
|
2010-09-15 18:27:47 +00:00
|
|
|
|
2010-09-15 18:33:51 +00:00
|
|
|
self::initialize();
|
2010-09-15 18:27:47 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
return self::$modules;
|
|
|
|
|
}
|
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
|
|
|
|
|
*/
|
|
|
|
|
public static function getModule( $name ) {
|
2010-09-15 18:27:47 +00:00
|
|
|
|
2010-09-15 18:33:51 +00:00
|
|
|
self::initialize();
|
2010-09-15 18:27:47 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
return isset( self::$modules[$name] ) ? self::$modules[$name] : null;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
2010-09-20 21:23:29 +00:00
|
|
|
* Gets registration code for all modules
|
2010-09-04 04:00:09 +00:00
|
|
|
*
|
2010-09-05 13:31:34 +00:00
|
|
|
* @param $context ResourceLoaderContext object
|
|
|
|
|
* @return String: JavaScript code for registering all modules with the client loader
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public static function getModuleRegistrations( ResourceLoaderContext $context ) {
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2010-09-15 18:33:51 +00:00
|
|
|
self::initialize();
|
2010-09-15 18:27:47 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
$scripts = '';
|
|
|
|
|
$registrations = array();
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
foreach ( self::$modules as $name => $module ) {
|
|
|
|
|
// Support module loader scripts
|
|
|
|
|
if ( ( $loader = $module->getLoaderScript() ) !== false ) {
|
2010-09-14 11:58:09 +00:00
|
|
|
$deps = FormatJson::encode( $module->getDependencies() );
|
2010-09-20 21:23:29 +00:00
|
|
|
$group = FormatJson::encode( $module->getGroup() );
|
|
|
|
|
$version = wfTimestamp( TS_ISO_8601, round( $module->getModifiedTime( $context ), -2 ) );
|
2010-09-17 11:45:49 +00:00
|
|
|
$scripts .= "( function( name, version, dependencies ) { $loader } )\n" .
|
2010-09-20 21:23:29 +00:00
|
|
|
"( '$name', '$version', $deps, $group );\n";
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
|
|
|
|
// Automatically register module
|
|
|
|
|
else {
|
2010-09-20 21:23:29 +00:00
|
|
|
// Modules without dependencies or a group pass two arguments (name, timestamp) to
|
2010-09-17 11:45:49 +00:00
|
|
|
// mediaWiki.loader.register()
|
2010-09-20 21:23:29 +00:00
|
|
|
if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
|
2010-09-04 04:00:09 +00:00
|
|
|
$registrations[] = array( $name, $module->getModifiedTime( $context ) );
|
|
|
|
|
}
|
2010-09-20 21:23:29 +00:00
|
|
|
// Modules with dependencies but no group pass three arguments (name, timestamp, dependencies)
|
|
|
|
|
// to mediaWiki.loader.register()
|
|
|
|
|
else if ( $module->getGroup() === null ) {
|
|
|
|
|
$registrations[] = array(
|
|
|
|
|
$name, $module->getModifiedTime( $context ), $module->getDependencies() );
|
|
|
|
|
}
|
|
|
|
|
// Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
|
2010-09-17 11:45:49 +00:00
|
|
|
// to mediaWiki.loader.register()
|
2010-09-04 04:00:09 +00:00
|
|
|
else {
|
2010-09-20 21:23:29 +00:00
|
|
|
$registrations[] = array(
|
|
|
|
|
$name, $module->getModifiedTime( $context ), $module->getDependencies(), $module->getGroup() );
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-17 11:45:49 +00:00
|
|
|
$out = $scripts . "mediaWiki.loader.register( " . FormatJson::encode( $registrations ) . " );\n";
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return $out;
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
2010-09-17 11:45:49 +00:00
|
|
|
* Get the highest modification time of all modules, based on a given
|
|
|
|
|
* combination of language code, skin name and debug mode flag.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $context ResourceLoaderContext object
|
|
|
|
|
* @return Integer: UNIX timestamp
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public static function getHighestModifiedTime( ResourceLoaderContext $context ) {
|
2010-09-15 18:27:47 +00:00
|
|
|
|
2010-09-15 18:33:51 +00:00
|
|
|
self::initialize();
|
2010-09-15 18:27:47 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
$time = 1; // wfTimestamp() treats 0 as 'now', so that's not a suitable choice
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
foreach ( self::$modules as $module ) {
|
|
|
|
|
$time = max( $time, $module->getModifiedTime( $context ) );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
return $time;
|
|
|
|
|
}
|
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
|
|
|
*/
|
|
|
|
|
public static function respond( ResourceLoaderContext $context ) {
|
2010-09-22 20:44:12 +00:00
|
|
|
global $wgResourceLoaderMaxage;
|
2010-09-13 23:19:05 +00:00
|
|
|
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2010-09-15 18:33:51 +00:00
|
|
|
self::initialize();
|
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-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
foreach ( $context->getModules() as $name ) {
|
|
|
|
|
if ( isset( self::$modules[$name] ) ) {
|
|
|
|
|
$modules[] = $name;
|
|
|
|
|
} 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
|
|
|
|
|
self::preloadModuleInfo( $modules, $context );
|
|
|
|
|
|
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-09-13 23:19:05 +00:00
|
|
|
$mtime = 1;
|
2010-09-04 04:00:09 +00:00
|
|
|
foreach ( $modules as $name ) {
|
|
|
|
|
$mtime = max( $mtime, self::$modules[$name]->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-09-13 23:19:05 +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-09-04 04:00:09 +00:00
|
|
|
// Use output buffering
|
|
|
|
|
ob_start();
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Pre-fetch blobs
|
|
|
|
|
$blobs = $context->shouldIncludeMessages() ?
|
2010-09-23 21:23:51 +00:00
|
|
|
MessageBlobStore::get( $modules, $context->getLanguage() ) : array();
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Generate output
|
|
|
|
|
foreach ( $modules as $name ) {
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileIn( __METHOD__ . '-' . $name );
|
2010-09-04 04:00:09 +00:00
|
|
|
// Scripts
|
|
|
|
|
$scripts = '';
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
if ( $context->shouldIncludeScripts() ) {
|
2010-09-17 11:45:49 +00:00
|
|
|
$scripts .= self::$modules[$name]->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-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
if (
|
2010-09-17 11:45:49 +00:00
|
|
|
$context->shouldIncludeStyles()
|
|
|
|
|
&& ( count( $styles = self::$modules[$name]->getStyles( $context ) ) )
|
2010-09-04 04:00:09 +00:00
|
|
|
) {
|
2010-09-10 00:21:36 +00:00
|
|
|
foreach ( $styles as $media => $style ) {
|
|
|
|
|
if ( self::$modules[$name]->getFlip( $context ) ) {
|
|
|
|
|
$styles[$media] = self::filter( 'flip-css', $style );
|
|
|
|
|
}
|
|
|
|
|
if ( !$context->getDebug() ) {
|
|
|
|
|
$styles[$media] = self::filter( 'minify-css', $style );
|
|
|
|
|
}
|
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-04 04:00:09 +00:00
|
|
|
// Output
|
|
|
|
|
if ( $context->getOnly() === 'styles' ) {
|
2010-09-10 20:18:24 +00:00
|
|
|
if ( $context->getDebug() ) {
|
|
|
|
|
echo "/* $name */\n";
|
|
|
|
|
foreach ( $styles as $media => $style ) {
|
|
|
|
|
echo "@media $media {\n" . str_replace( "\n", "\n\t", "\t" . $style ) . "\n}\n";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
foreach ( $styles as $media => $style ) {
|
|
|
|
|
if ( strlen( $style ) ) {
|
|
|
|
|
echo "@media $media{" . $style . "}";
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-10 00:21:36 +00:00
|
|
|
}
|
2010-09-04 04:00:09 +00:00
|
|
|
} else if ( $context->getOnly() === 'scripts' ) {
|
|
|
|
|
echo $scripts;
|
|
|
|
|
} else if ( $context->getOnly() === 'messages' ) {
|
|
|
|
|
echo "mediaWiki.msg.set( $messages );\n";
|
|
|
|
|
} else {
|
2010-09-16 20:53:06 +00:00
|
|
|
if ( count( $styles ) ) {
|
|
|
|
|
$styles = FormatJson::encode( $styles );
|
|
|
|
|
} else {
|
|
|
|
|
$styles = 'null';
|
|
|
|
|
}
|
2010-09-10 00:21:36 +00:00
|
|
|
echo "mediaWiki.loader.implement( '$name', function() {{$scripts}},\n$styles,\n$messages );\n";
|
2010-09-04 04:00:09 +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-04 04:00:09 +00:00
|
|
|
// Update the status of script-only modules
|
|
|
|
|
if ( $context->getOnly() === 'scripts' && !in_array( 'startup', $modules ) ) {
|
|
|
|
|
$statuses = array();
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
foreach ( $modules as $name ) {
|
|
|
|
|
$statuses[$name] = 'ready';
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
$statuses = FormatJson::encode( $statuses );
|
2010-09-11 08:03:11 +00:00
|
|
|
echo "mediaWiki.loader.state( $statuses );\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
|
|
|
// Register missing modules
|
|
|
|
|
if ( $context->shouldIncludeScripts() ) {
|
|
|
|
|
foreach ( $missing as $name ) {
|
|
|
|
|
echo "mediaWiki.loader.register( '$name', null, 'missing' );\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Output the appropriate header
|
2010-09-04 08:38:45 +00:00
|
|
|
if ( $context->getOnly() !== 'styles' ) {
|
2010-09-04 04:00:09 +00:00
|
|
|
if ( $context->getDebug() ) {
|
|
|
|
|
ob_end_flush();
|
|
|
|
|
} else {
|
|
|
|
|
echo self::filter( 'minify-js', ob_get_clean() );
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-17 11:45:49 +00:00
|
|
|
}
|