* @author Roan Kattouw
*/
+defined( 'MEDIAWIKI' ) || die( 1 );
+
/**
* Abstraction for resource loader modules, with name registration and maxage functionality.
*/
abstract class ResourceLoaderModule {
+
/* Protected Members */
protected $name = null;
+
+ // In-object cache for file dependencies
+ protected $fileDeps = array();
+ // In-object cache for message blob mtime
+ protected $msgBlobMtime = array();
/* Methods */
$this->name = $name;
}
- /**
- * The maximum number of seconds to cache this module for in the
- * client-side (browser) cache. Override this only if you have a good
- * reason not to use $wgResourceLoaderClientMaxage.
- *
- * @return Integer: cache maxage in seconds
- */
- public function getClientMaxage() {
- global $wgResourceLoaderClientMaxage;
- return $wgResourceLoaderClientMaxage;
- }
-
- /**
- * The maximum number of seconds to cache this module for in the
- * server-side (Squid / proxy) cache. Override this only if you have a
- * good reason not to use $wgResourceLoaderServerMaxage.
- *
- * @return Integer: cache maxage in seconds
- */
- public function getServerMaxage() {
- global $wgResourceLoaderServerMaxage;
- return $wgResourceLoaderServerMaxage;
- }
-
/**
* Get whether CSS for this module should be flipped
*/
// Stub, override expected
return array();
}
+
+ /**
+ * Get the group this module is in.
+ *
+ * @return string of group name
+ */
+ public function getGroup() {
+ // Stub, override expected
+ return null;
+ }
/**
* Get the loader JS for this module, if set.
*/
public function getLoaderScript() {
// Stub, override expected
- return '';
+ return false;
}
/**
// Stub, override expected
return array();
}
+
+ /**
+ * Get the files this module depends on indirectly for a given skin.
+ * Currently these are only image files referenced by the module's CSS.
+ *
+ * @param $skin String: skin name
+ * @return array of files
+ */
+ public function getFileDependencies( $skin ) {
+ // Try in-object cache first
+ if ( isset( $this->fileDeps[$skin] ) ) {
+ return $this->fileDeps[$skin];
+ }
+ $dbr = wfGetDB( DB_SLAVE );
+ $deps = $dbr->selectField( 'module_deps', 'md_deps', array(
+ 'md_module' => $this->getName(),
+ 'md_skin' => $skin,
+ ), __METHOD__
+ );
+ if ( !is_null( $deps ) ) {
+ return $this->fileDeps[$skin] = (array) FormatJson::decode( $deps, true );
+ }
+ return $this->fileDeps[$skin] = array();
+ }
+
+ /**
+ * Set preloaded file dependency information. Used so we can load this
+ * information for all modules at once.
+ * @param $skin string Skin name
+ * @param $deps array Array of file names
+ */
+ public function setFileDependencies( $skin, $deps ) {
+ $this->fileDeps[$skin] = $deps;
+ }
+
+ /**
+ * Get the last modification timestamp of the message blob for this
+ * module in a given language.
+ * @param $lang string Language code
+ * @return int UNIX timestamp, or 0 if no blob found
+ */
+ public function getMsgBlobMtime( $lang ) {
+ if ( !count( $this->getMessages() ) )
+ return 0;
+
+ $dbr = wfGetDB( DB_SLAVE );
+ $msgBlobMtime = $dbr->selectField( 'msg_resource', 'mr_timestamp', array(
+ 'mr_resource' => $this->getName(),
+ 'mr_lang' => $lang
+ ), __METHOD__
+ );
+ $this->msgBlobMtime[$lang] = $msgBlobMtime ? wfTimestamp( TS_UNIX, $msgBlobMtime ) : 0;
+ return $this->msgBlobMtime[$lang];
+ }
+
+ /**
+ * Set a preloaded message blob last modification timestamp. Used so we
+ * can load this information for all modules at once.
+ * @param $lang string Language code
+ * @param $mtime int UNIX timestamp or 0 if there is no such blob
+ */
+ public function setMsgBlobMtime( $lang, $mtime ) {
+ $this->msgBlobMtime[$lang] = $mtime;
+ }
+
/* Abstract Methods */
/**
* @param $context ResourceLoaderContext object
* @return int UNIX timestamp
*/
- public abstract function getModifiedTime( ResourceLoaderContext $context );
+ public function getModifiedTime( ResourceLoaderContext $context ) {
+ // 0 would mean now
+ return 1;
+ }
}
/**
protected $scripts = array();
protected $styles = array();
protected $messages = array();
+ protected $group;
protected $dependencies = array();
protected $debugScripts = array();
protected $languageScripts = array();
* array(
* // Required module options (mutually exclusive)
* 'scripts' => 'dir/script.js' | array( 'dir/script1.js', 'dir/script2.js' ... ),
- *
+ *
* // Optional module options
* 'languageScripts' => array(
* '[lang name]' => 'dir/lang.js' | '[lang name]' => array( 'dir/lang1.js', 'dir/lang2.js' ... )
* ...
* ),
* 'messages' => array( 'message1', 'message2' ... ),
+ * 'group' => 'stuff',
* )
+ *
+ * @param $basePath String: base path to prepend to all paths in $options
*/
- public function __construct( $options = array() ) {
+ public function __construct( $options = array(), $basePath = null ) {
foreach ( $options as $option => $value ) {
switch ( $option ) {
case 'scripts':
- $this->scripts = (array)$value;
- break;
- case 'styles':
- $this->styles = (array)$value;
- break;
- case 'messages':
- $this->messages = (array)$value;
- break;
- case 'dependencies':
- $this->dependencies = (array)$value;
- break;
case 'debugScripts':
- $this->debugScripts = (array)$value;
- break;
case 'languageScripts':
- $this->languageScripts = (array)$value;
- break;
case 'skinScripts':
- $this->skinScripts = (array)$value;
+ case 'loaders':
+ $this->{$option} = (array)$value;
+ // Automatically prefix script paths
+ if ( is_string( $basePath ) ) {
+ foreach ( $this->{$option} as $key => $value ) {
+ $this->{$option}[$key] = $basePath . $value;
+ }
+ }
break;
+ case 'styles':
case 'skinStyles':
- $this->skinStyles = (array)$value;
+ $this->{$option} = (array)$value;
+ // Automatically prefix style paths
+ if ( is_string( $basePath ) ) {
+ foreach ( $this->{$option} as $key => $value ) {
+ if ( is_array( $value ) ) {
+ $this->{$option}[$basePath . $key] = $value;
+ unset( $this->{$option}[$key] );
+ } else {
+ $this->{$option}[$key] = $basePath . $value;
+ }
+ }
+ }
break;
- case 'loaders':
- $this->loaders = (array)$value;
+ case 'dependencies':
+ case 'messages':
+ $this->{$option} = (array)$value;
+ break;
+ case 'group':
+ $this->group = (string)$value;
break;
}
}
}
-
+
/**
* Add script files to this module. In order to be valid, a module
* must contain at least one script file.
public function addMessages( $messages ) {
$this->messages = array_merge( $this->messages, (array)$messages );
}
-
+
+ /**
+ * Sets the group of this module.
+ *
+ * @param $group string group name
+ */
+ public function setGroup( $group ) {
+ $this->group = $group;
+ }
+
/**
* Add dependencies. Dependency information is taken into account when
* loading a module on the client side. When adding a module on the
// Collect referenced files
$files = array();
- foreach ( $styles as $media => $style ) {
+ foreach ( $styles as $style ) {
// Extract and store the list of referenced files
$files = array_merge( $files, CSSMin::getLocalFileReferences( $style ) );
}
// Only store if modified
if ( $files !== $this->getFileDependencies( $context->getSkin() ) ) {
$encFiles = FormatJson::encode( $files );
- $dbw = wfGetDb( DB_MASTER );
+ $dbw = wfGetDB( DB_MASTER );
$dbw->replace( 'module_deps',
array( array( 'md_module', 'md_skin' ) ), array(
'md_module' => $this->getName(),
'md_deps' => $encFiles,
)
);
-
- // Save into memcached
- global $wgMemc;
-
- $key = wfMemcKey( 'resourceloader', 'module_deps', $this->getName(), $context->getSkin() );
- $wgMemc->set( $key, $encFiles );
}
return $styles;
return $this->messages;
}
+ public function getGroup() {
+ return $this->group;
+ }
+
public function getDependencies() {
return $this->dependencies;
}
if ( isset( $this->modifiedTime[$context->getHash()] ) ) {
return $this->modifiedTime[$context->getHash()];
}
+ wfProfileIn( __METHOD__ );
// Sort of nasty way we can get a flat list of files depended on by all styles
$styles = array();
- foreach ( self::organizeFilesByOption( $this->styles, 'media', 'all' ) as $media => $styleFiles ) {
+ foreach ( self::organizeFilesByOption( $this->styles, 'media', 'all' ) as $styleFiles ) {
$styles = array_merge( $styles, $styleFiles );
}
$skinFiles = (array) self::getSkinFiles(
$context->getSkin(), self::organizeFilesByOption( $this->skinStyles, 'media', 'all' )
);
- foreach ( $skinFiles as $media => $styleFiles ) {
+ foreach ( $skinFiles as $styleFiles ) {
$styles = array_merge( $styles, $styleFiles );
}
$this->getFileDependencies( $context->getSkin() )
);
+ wfProfileIn( __METHOD__.'-filemtime' );
$filesMtime = max( array_map( 'filemtime', array_map( array( __CLASS__, 'remapFilename' ), $files ) ) );
-
- // Get the mtime of the message blob
- // TODO: This timestamp is queried a lot and queried separately for each module. Maybe it should be put in memcached?
- $dbr = wfGetDb( DB_SLAVE );
- $msgBlobMtime = $dbr->selectField( 'msg_resource', 'mr_timestamp', array(
- 'mr_resource' => $this->getName(),
- 'mr_lang' => $context->getLanguage()
- ), __METHOD__
- );
- $msgBlobMtime = $msgBlobMtime ? wfTimestamp( TS_UNIX, $msgBlobMtime ) : 0;
-
- $this->modifiedTime[$context->getHash()] = max( $filesMtime, $msgBlobMtime );
+ wfProfileOut( __METHOD__.'-filemtime' );
+ $this->modifiedTime[$context->getHash()] = max( $filesMtime, $this->getMsgBlobMtime( $context->getLanguage() ) );
+ wfProfileOut( __METHOD__ );
return $this->modifiedTime[$context->getHash()];
}
* Get the primary CSS for this module. This is pulled from the CSS
* files added through addStyles()
*
- * @return String: JS
+ * @return Array
*/
protected function getPrimaryStyles() {
return self::concatStyles( $this->styles );
return $retval;
}
- /**
- * Get the files this module depends on indirectly for a given skin.
- * Currently these are only image files referenced by the module's CSS.
- *
- * @param $skin String: skin name
- * @return array of files
- */
- protected function getFileDependencies( $skin ) {
- // Try in-object cache first
- if ( isset( $this->fileDeps[$skin] ) ) {
- return $this->fileDeps[$skin];
- }
-
- // Now try memcached
- global $wgMemc;
-
- $key = wfMemcKey( 'resourceloader', 'module_deps', $this->getName(), $skin );
- $deps = $wgMemc->get( $key );
-
- if ( !$deps ) {
- $dbr = wfGetDb( DB_SLAVE );
- $deps = $dbr->selectField( 'module_deps', 'md_deps', array(
- 'md_module' => $this->getName(),
- 'md_skin' => $skin,
- ), __METHOD__
- );
- if ( !$deps ) {
- $deps = '[]'; // Empty array so we can do negative caching
- }
- $wgMemc->set( $key, $deps );
- }
-
- $this->fileDeps = FormatJson::decode( $deps, true );
-
- return $this->fileDeps;
- }
-
/**
* Get the contents of a set of files and concatenate them, with
* newlines in between. Each file is used only once.
* @return String: concatenated contents of $files
*/
protected static function concatScripts( $files ) {
- return implode( "\n", array_map( 'file_get_contents', array_map( array( __CLASS__, 'remapFilename' ), array_unique( (array) $files ) ) ) );
+ return implode( "\n",
+ array_map(
+ 'file_get_contents',
+ array_map(
+ array( __CLASS__, 'remapFilename' ),
+ array_unique( (array) $files ) ) ) );
}
protected static function organizeFilesByOption( $files, $option, $default ) {
* Get the contents of a set of CSS files, remap then and concatenate
* them, with newlines in between. Each file is used only once.
*
- * @param $files Array of file names
+ * @param $styles Array of file names
* @return Array: list of concatenated and remapped contents of $files keyed by media type
*/
protected static function concatStyles( $styles ) {
$styles = self::organizeFilesByOption( $styles, 'media', 'all' );
foreach ( $styles as $media => $files ) {
$styles[$media] =
- implode( "\n", array_map( array( __CLASS__, 'remapStyle' ), array_unique( (array) $files ) ) );
+ implode( "\n",
+ array_map(
+ array( __CLASS__, 'remapStyle' ),
+ array_unique( (array) $files ) ) );
}
return $styles;
}
* @return string Remapped CSS
*/
protected static function remapStyle( $file ) {
- global $wgUseDataURLs, $wgScriptPath;
+ global $wgScriptPath;
return CSSMin::remap(
file_get_contents( self::remapFilename( $file ) ),
dirname( $file ),
$wgScriptPath . '/' . dirname( $file ),
- $wgUseDataURLs
+ true
);
}
}
/**
* Abstraction for resource loader modules which pull from wiki pages
+ *
+ * This can only be used for wiki pages in the MediaWiki and User namespaces, because of it's dependence on the
+ * functionality of Title::isValidCssJsSubpage.
*/
abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
/* Methods */
public function getScript( ResourceLoaderContext $context ) {
- global $wgCanonicalNamespaceNames;
-
$scripts = '';
foreach ( $this->getPages( $context ) as $page => $options ) {
if ( $options['type'] === 'script' ) {
if ( $script = $this->getContent( $page, $options['ns'] ) ) {
- $ns = $wgCanonicalNamespaceNames[$options['ns']];
+ $ns = MWNamespace::getCanonicalName( $options['ns'] );
$scripts .= "/*$ns:$page */\n$script\n";
}
}
}
public function getStyles( ResourceLoaderContext $context ) {
- global $wgCanonicalNamespaceNames;
$styles = array();
foreach ( $this->getPages( $context ) as $page => $options ) {
if ( !isset( $styles[$media] ) ) {
$styles[$media] = '';
}
- $ns = $wgCanonicalNamespaceNames[$options['ns']];
+ $ns = MWNamespace::getCanonicalName( $options['ns'] );
$styles[$media] .= "/* $ns:$page */\n$style\n";
}
}
if ( isset( $this->modifiedTime[$hash] ) ) {
return $this->modifiedTime[$hash];
}
+
$titles = array();
foreach ( $this->getPages( $context ) as $page => $options ) {
- $titles[] = Title::makeTitle( $options['ns'], $page );
+ $titles[$options['ns']][$page] = true;
}
- // Do batch existence check
- // TODO: This would work better if page_touched were loaded by this as well
- $lb = new LinkBatch( $titles );
- $lb->execute();
+
$modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
- foreach ( $titles as $title ) {
- if ( $title->exists() ) {
- $modifiedTime = max( $modifiedTime, wfTimestamp( TS_UNIX, $title->getTouched() ) );
+
+ if ( $titles ) {
+ $dbr = wfGetDB( DB_SLAVE );
+ $latest = $dbr->selectField( 'page', 'MAX(page_touched)',
+ $dbr->makeWhereFrom2d( $titles, 'page_namespace', 'page_title' ),
+ __METHOD__ );
+
+ if ( $latest ) {
+ $modifiedTime = wfTimestamp( TS_UNIX, $latest );
}
}
+
return $this->modifiedTime[$hash] = $modifiedTime;
}
}
}
return $pages;
}
+
+ /* Methods */
+
+ public function getGroup() {
+ return 'site';
+ }
}
/**
}
return array();
}
+
+ /* Methods */
+
+ public function getGroup() {
+ return 'user';
+ }
}
/**
* Module for user preference customizations
*/
-class ResourceLoaderUserPreferencesModule extends ResourceLoaderModule {
+class ResourceLoaderUserOptionsModule extends ResourceLoaderModule {
/* Protected Members */
if ( isset( $this->modifiedTime[$hash] ) ) {
return $this->modifiedTime[$hash];
}
- if ( $context->getUser() && $user = User::newFromName( $context->getUser() ) ) {
- return $this->modifiedTime[$hash] = $user->getTouched();
+
+ global $wgUser;
+
+ if ( $context->getUser() === $wgUser->getName() ) {
+ return $this->modifiedTime[$hash] = $wgUser->getTouched();
+ } else {
+ return 1;
+ }
+ }
+
+ /**
+ * Fetch the context's user options, or if it doesn't match current user,
+ * the default options.
+ *
+ * @param $context ResourceLoaderContext
+ * @return array
+ */
+ protected function contextUserOptions( ResourceLoaderContext $context ) {
+ global $wgUser;
+
+ // Verify identity -- this is a private module
+ if ( $context->getUser() === $wgUser->getName() ) {
+ return $wgUser->getOptions();
} else {
- return 0;
+ return User::getDefaultOptions();
}
}
+ public function getScript( ResourceLoaderContext $context ) {
+ $encOptions = FormatJson::encode( $this->contextUserOptions( $context ) );
+ return "mediaWiki.user.options.set( $encOptions );";
+ }
+
public function getStyles( ResourceLoaderContext $context ) {
global $wgAllowUserCssPrefs;
+
if ( $wgAllowUserCssPrefs ) {
- $user = User::newFromName( $context->getUser() );
+ $options = $this->contextUserOptions( $context );
+
+ // Build CSS rules
$rules = array();
- if ( ( $underline = $user->getOption( 'underline' ) ) < 2 ) {
- $rules[] = "a { text-decoration: " . ( $underline ? 'underline' : 'none' ) . "; }";
+ if ( $options['underline'] < 2 ) {
+ $rules[] = "a { text-decoration: " . ( $options['underline'] ? 'underline' : 'none' ) . "; }";
}
- if ( $user->getOption( 'highlightbroken' ) ) {
- $rules[] = "a.new, #quickbar a.new { color: #CC2200; }\n";
+ if ( $options['highlightbroken'] ) {
+ $rules[] = "a.new, #quickbar a.new { color: #ba0000; }\n";
} else {
$rules[] = "a.new, #quickbar a.new, a.stub, #quickbar a.stub { color: inherit; }";
- $rules[] = "a.new:after, #quickbar a.new:after { content: '?'; color: #CC2200; }";
+ $rules[] = "a.new:after, #quickbar a.new:after { content: '?'; color: #ba0000; }";
$rules[] = "a.stub:after, #quickbar a.stub:after { content: '!'; color: #772233; }";
}
- if ( $user->getOption( 'justify' ) ) {
+ if ( $options['justify'] ) {
$rules[] = "#article, #bodyContent, #mw_content { text-align: justify; }\n";
}
- if ( !$user->getOption( 'showtoc' ) ) {
+ if ( !$options['showtoc'] ) {
$rules[] = "#toc { display: none; }\n";
}
- if ( !$user->getOption( 'editsection' ) ) {
+ if ( !$options['editsection'] ) {
$rules[] = ".editsection { display: none; }\n";
}
- if ( ( $fontstyle = $user->getOption( 'editfont' ) ) !== 'default' ) {
- $rules[] = "textarea { font-family: $fontstyle; }\n";
+ if ( $options['editfont'] !== 'default' ) {
+ $rules[] = "textarea { font-family: {$options['editfont']}; }\n";
}
return array( 'all' => implode( "\n", $rules ) );
}
return $wgContLang->getDir() !== $context->getDirection();
}
+
+ public function getGroup() {
+ return 'private';
+ }
}
class ResourceLoaderStartUpModule extends ResourceLoaderModule {
/* Protected Methods */
protected function getConfig( $context ) {
- global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension, $wgArticlePath, $wgScriptPath, $wgServer,
- $wgContLang, $wgBreakFrames, $wgVariantArticlePath, $wgActionPaths, $wgUseAjax, $wgAjaxWatch, $wgVersion,
- $wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest, $wgSitename, $wgFileExtensions;
+ global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension,
+ $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang, $wgBreakFrames,
+ $wgVariantArticlePath, $wgActionPaths, $wgUseAjax, $wgVersion,
+ $wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest,
+ $wgSitename, $wgFileExtensions;
// Pre-process information
$separatorTransTable = $wgContLang->separatorTransformTable();
'wgNamespaceIds' => $wgContLang->getNamespaceIds(),
'wgSiteName' => $wgSitename,
'wgFileExtensions' => $wgFileExtensions,
+ 'wgDBname' => $wgDBname,
);
if ( $wgContLang->hasVariants() ) {
$vars['wgUserVariant'] = $wgContLang->getPreferredVariant();
}
if ( $wgUseAjax && $wgEnableMWSuggest ) {
$vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate();
- $vars['wgDBname'] = $wgDBname;
}
return $vars;
}
+ /**
+ * Gets registration code for all modules
+ *
+ * @param $context ResourceLoaderContext object
+ * @return String: JavaScript code for registering all modules with the client loader
+ */
+ public static function getModuleRegistrations( ResourceLoaderContext $context ) {
+ global $wgCacheEpoch;
+ wfProfileIn( __METHOD__ );
+
+ $out = '';
+ $registrations = array();
+ foreach ( $context->getResourceLoader()->getModules() as $name => $module ) {
+ // Support module loader scripts
+ if ( ( $loader = $module->getLoaderScript() ) !== false ) {
+ $deps = $module->getDependencies();
+ $group = $module->getGroup();
+ $version = wfTimestamp( TS_ISO_8601_BASIC, round( $module->getModifiedTime( $context ), -2 ) );
+ $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
+ }
+ // Automatically register module
+ else {
+ $mtime = max( $module->getModifiedTime( $context ), wfTimestamp( TS_UNIX, $wgCacheEpoch ) );
+ // Modules without dependencies or a group pass two arguments (name, timestamp) to
+ // mediaWiki.loader.register()
+ 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()
+ else if ( $module->getGroup() === null ) {
+ $registrations[] = array(
+ $name, $mtime, $module->getDependencies() );
+ }
+ // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
+ // to mediaWiki.loader.register()
+ else {
+ $registrations[] = array(
+ $name, $mtime, $module->getDependencies(), $module->getGroup() );
+ }
+ }
+ }
+ $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
+
+ wfProfileOut( __METHOD__ );
+ return $out;
+ }
+
/* Methods */
public function getScript( ResourceLoaderContext $context ) {
- global $IP, $wgStylePath, $wgLoadScript;
-
- $scripts = file_get_contents( "$IP/resources/startup.js" );
+ global $IP, $wgLoadScript;
+ $out = file_get_contents( "$IP/resources/startup.js" );
if ( $context->getOnly() === 'scripts' ) {
- // Get all module registrations
- $registration = ResourceLoader::getModuleRegistrations( $context );
- // Build configuration
- $config = FormatJson::encode( $this->getConfig( $context ) );
- // Add a well-known start-up function
- $scripts .= "window.startUp = function() { $registration mediaWiki.config.set( $config ); };";
// Build load query for jquery and mediawiki modules
$query = array(
'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
'lang' => $context->getLanguage(),
'skin' => $context->getSkin(),
'debug' => $context->getDebug() ? 'true' : 'false',
- 'version' => wfTimestamp( TS_ISO_8601, round( max(
- ResourceLoader::getModule( 'jquery' )->getModifiedTime( $context ),
- ResourceLoader::getModule( 'mediawiki' )->getModifiedTime( $context )
+ 'version' => wfTimestamp( TS_ISO_8601_BASIC, round( max(
+ $context->getResourceLoader()->getModule( 'jquery' )->getModifiedTime( $context ),
+ $context->getResourceLoader()->getModule( 'mediawiki' )->getModifiedTime( $context )
), -2 ) )
);
- // Uniform query order
+ // Ensure uniform query order
ksort( $query );
- // Build HTML code for loading jquery and mediawiki modules
- $loadScript = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
- // Add code to add jquery and mediawiki loading code; only if the current client is compatible
- $scripts .= "if ( isCompatible() ) { document.write( '$loadScript' ); }";
- // Delete the compatible function - it's not needed anymore
- $scripts .= "delete window['isCompatible'];";
+
+ // 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};";
+
+ // Conditional script injection
+ $scriptTag = Xml::escapeJsString( Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) ) );
+ $out .= "if ( isCompatible() ) {\n\tdocument.write( '$scriptTag' );\n}\ndelete isCompatible;";
}
- return $scripts;
+ return $out;
}
public function getModifiedTime( ResourceLoaderContext $context ) {
- global $IP;
+ global $IP, $wgCacheEpoch;
$hash = $context->getHash();
if ( isset( $this->modifiedTime[$hash] ) ) {
return $this->modifiedTime[$hash];
}
$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!
- $this->modifiedTime[$hash] = ResourceLoader::getHighestModifiedTime( $context );
- return $this->modifiedTime[$hash];
- }
-
- public function getClientMaxage() {
- return 300; // 5 minutes
- }
-
- public function getServerMaxage() {
- return 300; // 5 minutes
+ $time = wfTimestamp( TS_UNIX, $wgCacheEpoch );
+ foreach ( $context->getResourceLoader()->getModules() as $module ) {
+ $time = max( $time, $module->getModifiedTime( $context ) );
+ }
+ return $this->modifiedTime[$hash] = $time;
}
public function getFlip( $context ) {
return $wgContLang->getDir() !== $context->getDirection();
}
+
+ /* Methods */
+
+ public function getGroup() {
+ return 'startup';
+ }
}