* @author Trevor Parscal
*/
-defined( 'MEDIAWIKI' ) || die( 1 );
-
/**
* Dynamic JavaScript and CSS resource loading system.
*
/**
* Loads information stored in the database about modules.
*
- * This method grabs modules dependencies from the database and updates modules objects.
+ * This method grabs modules dependencies from the database and updates modules
+ * objects.
*
- * 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 its own information. This sacrifice of modularity yields a profound
+ * 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 its own information. This sacrifice of modularity yields a profound
* performance improvement.
*
* @param $modules Array: list of module names to preload information for
* - minify-css \see CSSMin::minify
* - flip-css \see CSSJanus::transform
*
- * If $data is empty, only contains whitespace or the filter was unknown, $data is returned unmodified.
+ * If $data is empty, only contains whitespace or the filter was unknown,
+ * $data is returned unmodified.
*
* @param $filter String: name of filter to run
* @param $data String: text to filter, such as JavaScript or CSS text
wfProfileIn( __METHOD__ );
- // For empty/whitespace-only data or for unknown filters, don't perform any caching or processing
- if ( trim( $data ) === '' || !in_array( $filter, array( 'minify-js', 'minify-css', 'flip-css' ) ) ) {
+ // For empty/whitespace-only data or for unknown filters, don't perform
+ // any caching or processing
+ if ( trim( $data ) === ''
+ || !in_array( $filter, array( 'minify-js', 'minify-css', 'flip-css' ) ) )
+ {
wfProfileOut( __METHOD__ );
return $data;
}
// Try for Memcached hit
$key = wfMemcKey( 'resourceloader', 'filter', $filter, md5( $data ) );
- if ( is_string( $cache = $wgMemc->get( $key ) ) ) {
+ $cacheEntry = $wgMemc->get( $key );
+ if ( is_string( $cacheEntry ) ) {
wfProfileOut( __METHOD__ );
- return $cache;
+ return $cacheEntry;
}
// Run the filter - we've already verified one of these will work
break;
}
} catch ( Exception $exception ) {
- throw new MWException( 'ResourceLoader filter error. Exception was thrown: ' . $exception->getMessage() );
+ throw new MWException( 'ResourceLoader filter error. ' .
+ 'Exception was thrown: ' . $exception->getMessage() );
}
// Save filtered text to Memcached
* Registers a module with the ResourceLoader system.
*
* @param $name Mixed: string of name of module or array of name/object pairs
- * @param $object ResourceLoaderModule: module object (optional when using multiple-registration calling style)
+ * @param $object ResourceLoaderModule: module object (optional when using
+ * multiple-registration calling style)
* @throws MWException If a duplicate module registration is attempted
- * @throws MWException If something other than a ResourceLoaderModule is being registered
- * @return Boolean: false if there were any errors, in which case one or more modules were not registered
+ * @throws MWException If something other than a ResourceLoaderModule is being
+ * registered
+ * @return Boolean: false if there were any errors, in which case one or more
+ * modules were not registered
*/
public function register( $name, ResourceLoaderModule $object = null ) {
if ( isset( $this->modules[$name] ) ) {
// A module has already been registered by this name
throw new MWException(
- 'ResourceLoader duplicate registration error. Another module has already been registered as ' . $name
+ 'ResourceLoader duplicate registration error. ' .
+ 'Another module has already been registered as ' . $name
);
}
// Validate the input (type hinting lets null through)
if ( !( $object instanceof ResourceLoaderModule ) ) {
- throw new MWException( 'ResourceLoader invalid module error. Instances of ResourceLoaderModule expected.' );
+ throw new MWException( 'ResourceLoader invalid module error. ' .
+ 'Instances of ResourceLoaderModule expected.' );
}
// Attach module
}
}
- // If a version wasn't specified we need a shorter expiry time for updates to propagate to clients quickly
+ // If a version wasn't specified we need a shorter expiry time for updates
+ // to propagate to clients quickly
if ( is_null( $context->getVersion() ) ) {
$maxage = $wgResourceLoaderMaxage['unversioned']['client'];
$smaxage = $wgResourceLoaderMaxage['unversioned']['server'];
}
- // If a version was specified we can use a longer expiry time since changing version numbers causes cache misses
+ // If a version was specified we can use a longer expiry time since changing
+ // version numbers causes cache misses
else {
$maxage = $wgResourceLoaderMaxage['versioned']['client'];
$smaxage = $wgResourceLoaderMaxage['versioned']['server'];
wfProfileIn( __METHOD__.'-getModifiedTime' );
- // To send Last-Modified and support If-Modified-Since, we need to detect the last modified time
+ // To send Last-Modified and support If-Modified-Since, we need to detect
+ // the last modified time
$mtime = wfTimestamp( TS_UNIX, $wgCacheEpoch );
foreach ( $modules as $module ) {
// Bypass squid cache if the request includes any private modules
wfProfileOut( __METHOD__.'-getModifiedTime' );
- header( 'Content-Type: ' . ( $context->getOnly() === 'styles' ? 'text/css' : 'text/javascript' ) );
+ if ( $context->getOnly() === 'styles' ) {
+ header( 'Content-Type: text/css' );
+ } else {
+ header( 'Content-Type: text/javascript' );
+ }
header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $mtime ) );
if ( $context->getDebug() ) {
header( 'Cache-Control: must-revalidate' );
* @param $missing Array: list of unavailable modules (optional)
* @return String: response data
*/
- public function makeModuleResponse( ResourceLoaderContext $context, array $modules, $missing = array() ) {
+ public function makeModuleResponse( ResourceLoaderContext $context,
+ array $modules, $missing = array() )
+ {
// Pre-fetch blobs
- $blobs = $context->shouldIncludeMessages() ?
- MessageBlobStore::get( $this, $modules, $context->getLanguage() ) : array();
+ if ( $context->shouldIncludeMessages() ) {
+ $blobs = MessageBlobStore::get( $this, $modules, $context->getLanguage() );
+ } else {
+ $blobs = array();
+ }
// Generate output
$out = '';
// Styles
$styles = array();
- if ( $context->shouldIncludeStyles() && ( count( $styles = $module->getStyles( $context ) ) ) ) {
+ if ( $context->shouldIncludeStyles() ) {
+ $styles = $module->getStyles( $context );
// Flip CSS on a per-module basis
- if ( $this->modules[$name]->getFlip( $context ) ) {
+ if ( $styles && $this->modules[$name]->getFlip( $context ) ) {
foreach ( $styles as $media => $style ) {
$styles[$media] = $this->filter( 'flip-css', $style );
}
$out .= self::makeMessageSetScript( $messages );
break;
default:
- // Minify CSS before embedding in mediaWiki.loader.implement call (unless in debug mode)
+ // Minify CSS before embedding in mediaWiki.loader.implement call
+ // (unless in debug mode)
if ( !$context->getDebug() ) {
foreach ( $styles as $media => $style ) {
$styles[$media] = $this->filter( 'minify-css', $style );
// Update module states
if ( $context->shouldIncludeScripts() ) {
// Set the state of modules loaded as only scripts to ready
- if ( count( $modules ) && $context->getOnly() === 'scripts' && !isset( $modules['startup'] ) ) {
- $out .= self::makeLoaderStateScript( array_fill_keys( array_keys( $modules ), 'ready' ) );
+ if ( count( $modules ) && $context->getOnly() === 'scripts'
+ && !isset( $modules['startup'] ) )
+ {
+ $out .= self::makeLoaderStateScript(
+ array_fill_keys( array_keys( $modules ), 'ready' ) );
}
// Set the state of modules which were requested but unavailable as missing
if ( is_array( $missing ) && count( $missing ) ) {
"( '$name', $version, $dependencies, $group );\n";
}
- public static function makeLoaderRegisterScript( $name, $version = null, $dependencies = null, $group = null ) {
+ 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";
* @author Roan Kattouw
*/
-defined( 'MEDIAWIKI' ) || die( 1 );
-
class ResourceLoaderStartUpModule extends ResourceLoaderModule {
/* Protected Members */
if ( ( $loader = $module->getLoaderScript() ) !== false ) {
$deps = $module->getDependencies();
$group = $module->getGroup();
- $version = wfTimestamp( TS_ISO_8601_BASIC, round( $module->getModifiedTime( $context ), -2 ) );
+ $version = wfTimestamp( TS_ISO_8601_BASIC,
+ round( $module->getModifiedTime( $context ), -2 ) );
$out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
}
// Automatically register module
if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
$registrations[] = array( $name, $mtime );
}
- // Modules with dependencies but no group pass three arguments (name, timestamp, dependencies)
- // to mediaWiki.loader.register()
+ // Modules with dependencies but no group pass three arguments
+ // (name, timestamp, dependencies) to mediaWiki.loader.register()
else if ( $module->getGroup() === null ) {
$registrations[] = array(
$name, $mtime, $module->getDependencies() );
// Startup function
$configuration = FormatJson::encode( $this->getConfig( $context ) );
$registrations = self::getModuleRegistrations( $context );
- $out .= "var startUp = function() {\n\t$registrations\n\tmediaWiki.config.set( $configuration );\n};";
+ $out .= "var startUp = function() {\n" .
+ "\t$registrations\n" .
+ "\tmediaWiki.config.set( $configuration );" .
+ "\n};";
// Conditional script injection
- $scriptTag = Xml::escapeJsString( Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) ) );
- $out .= "if ( isCompatible() ) {\n\tdocument.write( '$scriptTag' );\n}\ndelete isCompatible;";
+ $scriptTag = Xml::escapeJsString(
+ Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) ) );
+ $out .= "if ( isCompatible() ) {\n" .
+ "\tdocument.write( '$scriptTag' );\n" .
+ "}\n" .
+ "delete isCompatible;";
}
return $out;
}
$this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
- // ATTENTION!: Because of the line above, this is not going to cause infinite recursion - think carefully
- // before making changes to this code!
+ // ATTENTION!: Because of the line above, this is not going to cause
+ // infinite recursion - think carefully before making changes to this
+ // code!
$time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
foreach ( $context->getResourceLoader()->getModules() as $module ) {
$time = max( $time, $module->getModifiedTime( $context ) );