*/
/**
- * Interface for resource loader modules, with name registration and maxage functionality.
+ * Abstraction for resource loader modules, with name registration and maxage functionality.
*/
abstract class ResourceLoaderModule {
/* Protected Members */
$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
*/
return $context->getDirection() === 'rtl';
}
- /* Abstract Methods */
-
/**
* Get all JS for this module for a given language and skin.
* Includes all relevant JS except loader scripts.
* @param $context ResourceLoaderContext object
* @return String: JS
*/
- public abstract function getScript( ResourceLoaderContext $context );
+ public function getScript( ResourceLoaderContext $context ) {
+ // Stub, override expected
+ return '';
+ }
/**
* Get all CSS for this module for a given skin.
* @param $context ResourceLoaderContext object
* @return array: strings of CSS keyed by media type
*/
- public abstract function getStyles( ResourceLoaderContext $context );
+ public function getStyles( ResourceLoaderContext $context ) {
+ // Stub, override expected
+ return '';
+ }
/**
* Get the messages needed for this module.
*
* @return array of message keys. Keys may occur more than once
*/
- public abstract function getMessages();
+ public function getMessages() {
+ // Stub, override expected
+ return array();
+ }
/**
* Get the loader JS for this module, if set.
*
* @return Mixed: loader JS (string) or false if no custom loader set
*/
- public abstract function getLoaderScript();
+ public function getLoaderScript() {
+ // Stub, override expected
+ return '';
+ }
/**
* Get a list of modules this module depends on.
* loader script, see getLoaderScript()
* @return Array of module names (strings)
*/
- public abstract function getDependencies();
+ public function getDependencies() {
+ // Stub, override expected
+ return array();
+ }
+ /* Abstract Methods */
+
/**
* Get this module's last modification timestamp for a given
* combination of language, skin and debug mode flag. This is typically
// 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(),
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();
$this->loaders,
$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;
-
+ wfProfileOut( __METHOD__.'-filemtime' );
+ // Only get the message timestamp if there are messages in the module
+ $msgBlobMtime = 0;
+ if ( count( $this->messages ) ) {
+ // 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__ );
return $this->modifiedTime[$context->getHash()];
}
$deps = $wgMemc->get( $key );
if ( !$deps ) {
- $dbr = wfGetDb( DB_SLAVE );
+ $dbr = wfGetDB( DB_SLAVE );
$deps = $dbr->selectField( 'module_deps', 'md_deps', array(
'md_module' => $this->getName(),
'md_skin' => $skin,
* @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 ) {
$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;
- return CSSMin::remap( file_get_contents( self::remapFilename( $file ) ), dirname( $file ), $wgUseDataURLs );
+ global $wgUseDataURLs, $wgScriptPath;
+ return CSSMin::remap(
+ file_get_contents( self::remapFilename( $file ) ),
+ dirname( $file ),
+ $wgScriptPath . '/' . dirname( $file ),
+ $wgUseDataURLs
+ );
}
}
/**
- * Custom module for MediaWiki:Common.js and MediaWiki:Skinname.js
- * TODO: Add Site CSS functionality too
+ * Abstraction for resource loader modules which pull from wiki pages
*/
-class ResourceLoaderSiteModule extends ResourceLoaderModule {
+abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
+
/* Protected Members */
-
+
// In-object cache for modified time
- protected $modifiedTime = null;
-
+ protected $modifiedTime = array();
+
+ /* Abstract Protected Methods */
+
+ abstract protected function getPages( ResourceLoaderContext $context );
+
+ /* Protected Methods */
+
+ protected function getContent( $page, $ns ) {
+ if ( $ns === NS_MEDIAWIKI ) {
+ return wfMsgExt( $page, 'content' );
+ }
+ if ( $title = Title::newFromText( $page, $ns ) ) {
+ if ( $title->isValidCssJsSubpage() && $revision = Revision::newFromTitle( $title ) ) {
+ return $revision->getRawText();
+ }
+ }
+ return null;
+ }
+
/* Methods */
public function getScript( ResourceLoaderContext $context ) {
- return Skin::newFromKey( $context->getSkin() )->generateUserJs();
+ 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']];
+ $scripts .= "/*$ns:$page */\n$script\n";
+ }
+ }
+ }
+ return $scripts;
}
- public function getModifiedTime( ResourceLoaderContext $context ) {
- global $wgHandheldStyle;
+ public function getStyles( ResourceLoaderContext $context ) {
+ global $wgCanonicalNamespaceNames;
- if ( isset( $this->modifiedTime[$context->getHash()] ) ) {
- return $this->modifiedTime[$context->getHash()];
+ $styles = array();
+ foreach ( $this->getPages( $context ) as $page => $options ) {
+ if ( $options['type'] === 'style' ) {
+ $media = isset( $options['media'] ) ? $options['media'] : 'all';
+ if ( $style = $this->getContent( $page, $options['ns'] ) ) {
+ if ( !isset( $styles[$media] ) ) {
+ $styles[$media] = '';
+ }
+ $ns = $wgCanonicalNamespaceNames[$options['ns']];
+ $styles[$media] .= "/* $ns:$page */\n$style\n";
+ }
+ }
}
+ return $styles;
+ }
- // HACK: We duplicate the message names from generateUserJs()
- // here and weird things (i.e. mtime moving backwards) can happen
- // when a MediaWiki:Something.js page is deleted
- $pages = array(
- Title::makeTitle( NS_MEDIAWIKI, 'Common.js' ),
- Title::makeTitle( NS_MEDIAWIKI, 'Common.css' ),
- Title::makeTitle( NS_MEDIAWIKI, ucfirst( $context->getSkin() ) . '.js' ),
- Title::makeTitle( NS_MEDIAWIKI, ucfirst( $context->getSkin() ) . '.css' ),
- Title::makeTitle( NS_MEDIAWIKI, 'Print.css' ),
- );
- if ( $wgHandheldStyle ) {
- $pages[] = Title::makeTitle( NS_MEDIAWIKI, 'Handheld.css' );
+ public function getModifiedTime( ResourceLoaderContext $context ) {
+ $hash = $context->getHash();
+ if ( isset( $this->modifiedTime[$hash] ) ) {
+ return $this->modifiedTime[$hash];
+ }
+
+ $titles = array();
+ foreach ( $this->getPages( $context ) as $page => $options ) {
+ $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( $pages );
- $lb->execute();
+ $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
- $this->modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
+ if ( $titles ) {
+ $dbr = wfGetDB( DB_SLAVE );
+ $latest = $dbr->selectField( 'page', 'MAX(page_touched)',
+ $dbr->makeWhereFrom2d( $titles, 'page_namespace', 'page_title' ),
+ __METHOD__ );
- foreach ( $pages as $page ) {
- if ( $page->exists() ) {
- $this->modifiedTime = max( $this->modifiedTime, wfTimestamp( TS_UNIX, $page->getTouched() ) );
+ if ( $latest ) {
+ $modifiedTime = wfTimestamp( TS_UNIX, $modifiedTime );
}
}
- return $this->modifiedTime;
+ return $this->modifiedTime[$hash] = $modifiedTime;
}
+}
- public function getStyles( ResourceLoaderContext $context ) {
+/**
+ * Module for site customizations
+ */
+class ResourceLoaderSiteModule extends ResourceLoaderWikiModule {
+
+ /* Protected Methods */
+
+ protected function getPages( ResourceLoaderContext $context ) {
global $wgHandheldStyle;
- $styles = array(
- 'all' => array( 'Common.css', $context->getSkin() . '.css' ),
- 'print' => array( 'Print.css' ),
+
+ $pages = array(
+ 'Common.js' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'script' ),
+ 'Common.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style' ),
+ ucfirst( $context->getSkin() ) . '.js' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'script' ),
+ ucfirst( $context->getSkin() ) . '.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style' ),
+ 'Print.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style', 'media' => 'print' ),
);
if ( $wgHandheldStyle ) {
- $sources['handheld'] = array( 'Handheld.css' );
- }
- foreach ( $styles as $media => $messages ) {
- foreach ( $messages as $i => $message ) {
- $style = wfMsgExt( $message, 'content' );
- if ( !wfEmptyMsg( $message, $style ) ) {
- $styles[$media][$i] = $style;
- }
- }
+ $pages['Handheld.css'] = array( 'ns' => NS_MEDIAWIKI, 'type' => 'style', 'media' => 'handheld' );
}
- foreach ( $styles as $media => $messages ) {
- $styles[$media] = implode( "\n", $messages );
+ return $pages;
+ }
+}
+
+/**
+ * Module for user customizations
+ */
+class ResourceLoaderUserModule extends ResourceLoaderWikiModule {
+
+ /* Protected Methods */
+
+ protected function getPages( ResourceLoaderContext $context ) {
+ global $wgAllowUserCss;
+
+ if ( $context->getUser() && $wgAllowUserCss ) {
+ $username = $context->getUser();
+ return array(
+ "$username/common.js" => array( 'ns' => NS_USER, 'type' => 'script' ),
+ "$username/" . $context->getSkin() . '.js' => array( 'ns' => NS_USER, 'type' => 'script' ),
+ "$username/common.css" => array( 'ns' => NS_USER, 'type' => 'style' ),
+ "$username/" . $context->getSkin() . '.css' => array( 'ns' => NS_USER, 'type' => 'style' ),
+ );
}
- return $styles;
+ return array();
}
- public function getMessages() { return array(); }
- public function getLoaderScript() { return ''; }
- public function getDependencies() { return array(); }
}
+/**
+ * Module for user preference customizations
+ */
+class ResourceLoaderUserOptionsModule extends ResourceLoaderModule {
+
+ /* Protected Members */
+
+ protected $modifiedTime = array();
+
+ /* Methods */
+
+ public function getModifiedTime( ResourceLoaderContext $context ) {
+ $hash = $context->getHash();
+ if ( isset( $this->modifiedTime[$hash] ) ) {
+ return $this->modifiedTime[$hash];
+ }
+
+ global $wgUser;
+ $username = $context->getUser();
+ // Avoid extra db query by using $wgUser if possible
+ $user = $wgUser->getName() === $username ? $wgUser : User::newFromName( $username );
+
+ if ( $user ) {
+ return $this->modifiedTime[$hash] = $user->getTouched();
+ } else {
+ return 0;
+ }
+ }
+
+ public function getScript( ResourceLoaderContext $context ) {
+ $user = User::newFromName( $context->getUser() );
+ if ( $user instanceof User ) {
+ $options = FormatJson::encode( $user->getOptions() );
+ } else {
+ $options = FormatJson::encode( User::getDefaultOptions() );
+ }
+ return "mediaWiki.user.options.set( $options );";
+ }
+
+ public function getStyles( ResourceLoaderContext $context ) {
+ global $wgAllowUserCssPrefs;
+ if ( $wgAllowUserCssPrefs ) {
+ $user = User::newFromName( $context->getUser() );
+ $options = $user instanceof User ? $user->getOptions() : User::getDefaultOptions();
+
+ $rules = array();
+ if ( $options['underline'] < 2 ) {
+ $rules[] = "a { text-decoration: " . ( $options['underline'] ? 'underline' : 'none' ) . "; }";
+ }
+ if ( $options['highlightbroken'] ) {
+ $rules[] = "a.new, #quickbar a.new { color: #CC2200; }\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.stub:after, #quickbar a.stub:after { content: '!'; color: #772233; }";
+ }
+ if ( $options['justify'] ) {
+ $rules[] = "#article, #bodyContent, #mw_content { text-align: justify; }\n";
+ }
+ if ( !$options['showtoc'] ) {
+ $rules[] = "#toc { display: none; }\n";
+ }
+ if ( !$options['editsection'] ) {
+ $rules[] = ".editsection { display: none; }\n";
+ }
+ if ( $options['editfont'] !== 'default' ) {
+ $rules[] = "textarea { font-family: {$options['editfont']}; }\n";
+ }
+ return array( 'all' => implode( "\n", $rules ) );
+ }
+ return array();
+ }
+
+ public function getFlip( $context ) {
+ global $wgContLang;
+
+ return $wgContLang->getDir() !== $context->getDirection();
+ }
+}
class ResourceLoaderStartUpModule extends ResourceLoaderModule {
/* Protected Members */
- protected $modifiedTime = null;
+ protected $modifiedTime = array();
+ /* Protected Methods */
+
+ protected function getConfig( $context ) {
+ 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();
+ $separatorTransTable = $separatorTransTable ? $separatorTransTable : array();
+ $compactSeparatorTransTable = array(
+ implode( "\t", array_keys( $separatorTransTable ) ),
+ implode( "\t", $separatorTransTable ),
+ );
+ $digitTransTable = $wgContLang->digitTransformTable();
+ $digitTransTable = $digitTransTable ? $digitTransTable : array();
+ $compactDigitTransTable = array(
+ implode( "\t", array_keys( $digitTransTable ) ),
+ implode( "\t", $digitTransTable ),
+ );
+ $mainPage = Title::newMainPage();
+
+ // Build list of variables
+ $vars = array(
+ 'wgLoadScript' => $wgLoadScript,
+ 'debug' => $context->getDebug(),
+ 'skin' => $context->getSkin(),
+ 'stylepath' => $wgStylePath,
+ 'wgUrlProtocols' => wfUrlProtocols(),
+ 'wgArticlePath' => $wgArticlePath,
+ 'wgScriptPath' => $wgScriptPath,
+ 'wgScriptExtension' => $wgScriptExtension,
+ 'wgScript' => $wgScript,
+ 'wgVariantArticlePath' => $wgVariantArticlePath,
+ 'wgActionPaths' => $wgActionPaths,
+ 'wgServer' => $wgServer,
+ 'wgUserLanguage' => $context->getLanguage(),
+ 'wgContentLanguage' => $wgContLang->getCode(),
+ 'wgBreakFrames' => $wgBreakFrames,
+ 'wgVersion' => $wgVersion,
+ 'wgEnableAPI' => $wgEnableAPI,
+ 'wgEnableWriteAPI' => $wgEnableWriteAPI,
+ 'wgSeparatorTransformTable' => $compactSeparatorTransTable,
+ 'wgDigitTransformTable' => $compactDigitTransTable,
+ 'wgMainPageTitle' => $mainPage ? $mainPage->getPrefixedText() : null,
+ 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
+ 'wgNamespaceIds' => $wgContLang->getNamespaceIds(),
+ 'wgSiteName' => $wgSitename,
+ 'wgFileExtensions' => $wgFileExtensions,
+ );
+ if ( $wgContLang->hasVariants() ) {
+ $vars['wgUserVariant'] = $wgContLang->getPreferredVariant();
+ }
+ if ( $wgUseAjax && $wgEnableMWSuggest ) {
+ $vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate();
+ $vars['wgDBname'] = $wgDBname;
+ }
+
+ return $vars;
+ }
+
/* Methods */
public function getScript( ResourceLoaderContext $context ) {
- global $IP;
+ global $IP, $wgLoadScript;
$scripts = file_get_contents( "$IP/resources/startup.js" );
// Get all module registrations
$registration = ResourceLoader::getModuleRegistrations( $context );
// Build configuration
- $config = FormatJson::encode(
- array( 'server' => $context->getServer(), 'debug' => $context->getDebug() )
- );
+ $config = FormatJson::encode( $this->getConfig( $context ) );
// Add a well-known start-up function
- $scripts .= "window.startUp = function() { $registration mediaWiki.config.set( $config ); };";
+ $scripts .= <<<JAVASCRIPT
+window.startUp = function() {
+ $registration
+ mediaWiki.config.set( $config );
+};
+JAVASCRIPT;
// Build load query for jquery and mediawiki modules
- $query = wfArrayToCGI(
- array(
- 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
- 'only' => 'scripts',
- 'lang' => $context->getLanguage(),
- 'dir' => $context->getDirection(),
- 'skin' => $context->getSkin(),
- 'debug' => $context->getDebug(),
- 'version' => wfTimestamp( TS_ISO_8601, round( max(
- ResourceLoader::getModule( 'jquery' )->getModifiedTime( $context ),
- ResourceLoader::getModule( 'mediawiki' )->getModifiedTime( $context )
- ), -2 ) )
- )
+ $query = array(
+ 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
+ 'only' => 'scripts',
+ '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 )
+ ), -2 ) )
);
-
+ // Uniform query order
+ ksort( $query );
// Build HTML code for loading jquery and mediawiki modules
- $loadScript = Html::linkedScript( $context->getServer() . "?$query" );
+ $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' ); }";
+ $scripts .= "if ( isCompatible() ) { document.write( " . FormatJson::encode( $loadScript ) . "); }\n";
// Delete the compatible function - it's not needed anymore
- $scripts .= "delete window['isCompatible'];";
+ $scripts .= "delete window['isCompatible'];\n";
}
return $scripts;
public function getModifiedTime( ResourceLoaderContext $context ) {
global $IP;
- if ( !is_null( $this->modifiedTime ) ) {
- return $this->modifiedTime;
+ $hash = $context->getHash();
+ if ( isset( $this->modifiedTime[$hash] ) ) {
+ return $this->modifiedTime[$hash];
}
-
- // HACK getHighestModifiedTime() calls this function, so protect against infinite recursion
- $this->modifiedTime = filemtime( "$IP/resources/startup.js" );
- $this->modifiedTime = ResourceLoader::getHighestModifiedTime( $context );
- return $this->modifiedTime;
+ $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
- }
-
- public function getStyles( ResourceLoaderContext $context ) { return array(); }
-
public function getFlip( $context ) {
global $wgContLang;
return $wgContLang->getDir() !== $context->getDirection();
}
- public function getMessages() { return array(); }
- public function getLoaderScript() { return ''; }
- public function getDependencies() { return array(); }
}