From: Krinkle Date: Tue, 10 May 2011 23:17:13 +0000 (+0000) Subject: Creating a way to toggle mw.config LEGACY_GLOBALS from LocalSettings (bug 28916). X-Git-Tag: 1.31.0-rc.0~30287 X-Git-Url: http://git.cyclocoop.org/%24href?a=commitdiff_plain;h=49ce5de7d94bed8b023a73daab55d7eaa5c09ba1;p=lhc%2Fweb%2Fwiklou.git Creating a way to toggle mw.config LEGACY_GLOBALS from LocalSettings (bug 28916). * I thought a while for a way to somehow get that global variable from php to the start of the main mediaWiki object creation. Considered using a (temporary) global variable and deleting afterwards, but that looked like a hack and wasn't sure about the cross-browser functioning of it. Instead ended up by moving it to the startUp module where other global variables are accessed as well. This seems to work pretty good. * Can be toggled from LocalSettings by setting $wgLegacyJavaScriptGlobals. * Changed some usages of mediaWiki to use the global mw alias instead. --- diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index ec94af2105..c373be7d19 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -2529,6 +2529,19 @@ $wgResourceLoaderMinifierMaxLineLength = 1000; */ $wgIncludeLegacyJavaScript = true; +/** + * Whether or not to assing configuration variables to the global window object. + * If this is set to false, old code using deprecated variables like: + * " if ( window.wgRestrictionEdit ) ..." + * or: + * " if ( wgIsArticle ) ..." + * will no longer work and needs to use mw.config instead. For example: + * " if ( mw.config.exists('wgRestrictionEdit') )" + * or + * " if ( mw.config.get('wgIsArticle') )". + */ +$wgLegacyJavaScriptGlobals = true; + /** * If set to a positive number, ResourceLoader will not generate URLs whose * query string is more than this many characters long, and will instead use diff --git a/includes/resourceloader/ResourceLoader.php b/includes/resourceloader/ResourceLoader.php index 7e8d09942f..4da9e2c892 100644 --- a/includes/resourceloader/ResourceLoader.php +++ b/includes/resourceloader/ResourceLoader.php @@ -675,10 +675,10 @@ class ResourceLoader { $dependencies = null, $group = null ) { if ( is_array( $name ) ) { - return Xml::encodeJsCall( 'mediaWiki.loader.register', array( $name ) ); + return Xml::encodeJsCall( 'mw.loader.register', array( $name ) ); } else { $version = (int) $version > 1 ? (int) $version : 1; - return Xml::encodeJsCall( 'mediaWiki.loader.register', + return Xml::encodeJsCall( 'mw.loader.register', array( $name, $version, $dependencies, $group ) ); } } @@ -701,7 +701,7 @@ class ResourceLoader { * @param $configuration Array: List of configuration values keyed by variable name */ public static function makeConfigSetScript( array $configuration ) { - return Xml::encodeJsCall( 'mediaWiki.config.set', array( $configuration ) ); + return Xml::encodeJsCall( 'mw.config.set', array( $configuration ) ); } /** diff --git a/includes/resourceloader/ResourceLoaderStartUpModule.php b/includes/resourceloader/ResourceLoaderStartUpModule.php index 73961f4902..b63d4df1dd 100644 --- a/includes/resourceloader/ResourceLoaderStartUpModule.php +++ b/includes/resourceloader/ResourceLoaderStartUpModule.php @@ -33,12 +33,12 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule { * @return array */ protected function getConfig( $context ) { - global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension, - $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang, + global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension, + $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang, $wgVariantArticlePath, $wgActionPaths, $wgUseAjax, - $wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest, + $wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest, $wgSitename, $wgFileExtensions, $wgExtensionAssetsPath, $wgProto, - $wgCookiePrefix, $wgResourceLoaderMaxQueryLength; + $wgCookiePrefix, $wgResourceLoaderMaxQueryLength, $wgLegacyJavaScriptGlobals; // Pre-process information $separatorTransTable = $wgContLang->separatorTransformTable(); @@ -85,15 +85,16 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule { 'wgSiteName' => $wgSitename, 'wgFileExtensions' => array_values( $wgFileExtensions ), 'wgDBname' => $wgDBname, - // This sucks, it is only needed on Special:Upload, but I could + // This sucks, it is only needed on Special:Upload, but I could // not find a way to add vars only for a certain module 'wgFileCanRotate' => BitmapHandler::canRotate(), 'wgAvailableSkins' => Skin::getSkinNames(), 'wgExtensionAssetsPath' => $wgExtensionAssetsPath, 'wgProto' => $wgProto, - // mediawiki sets cookies to have this prefix by default + // MediaWiki sets cookies to have this prefix by default 'wgCookiePrefix' => $wgCookiePrefix, 'wgResourceLoaderMaxQueryLength' => $wgResourceLoaderMaxQueryLength, + 'wgLegacyJavaScriptGlobals' => $wgLegacyJavaScriptGlobals, ); if ( $wgUseAjax && $wgEnableMWSuggest ) { $vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate(); @@ -162,7 +163,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule { /* Methods */ public function getScript( ResourceLoaderContext $context ) { - global $IP, $wgLoadScript; + global $IP, $wgLoadScript, $wgLegacyJavaScriptGlobals; $out = file_get_contents( "$IP/resources/startup.js" ); if ( $context->getOnly() === 'scripts' ) { @@ -189,20 +190,21 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule { ); // Ensure uniform query order ksort( $query ); - + // Startup function $configuration = $this->getConfig( $context ); $registrations = self::getModuleRegistrations( $context ); - $out .= "var startUp = function() {\n" . - "\t$registrations\n" . - "\t" . Xml::encodeJsCall( 'mediaWiki.config.set', array( $configuration ) ) . + $out .= "var startUp = function() {\n" . + "\tmw.config = new " . Xml::encodeJsCall( 'mw.Map', array( $wgLegacyJavaScriptGlobals ) ) . "\n" . + "\t$registrations\n" . + "\t" . Xml::encodeJsCall( 'mw.config.set', array( $configuration ) ) . "};\n"; - + // Conditional script injection $scriptTag = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) ); - $out .= "if ( isCompatible() ) {\n" . - "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) . - "}\n" . + $out .= "if ( isCompatible() ) {\n" . + "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) . + "}\n" . "delete isCompatible;"; } diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index a870bf5a10..68d860ae9c 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -127,9 +127,6 @@ window.mediaWiki = new ( function( $ ) { /* Constants */ - // This will not change until we are 100% ready to turn off legacy globals - var LEGACY_GLOBALS = true; - /* Private Members */ // List of messages that have been requested to be loaded @@ -503,9 +500,11 @@ window.mediaWiki = new ( function( $ ) { /* * List of configuration values * - * In legacy mode the values this object wraps will be in the global space + * Dummy placeholder. Initiated in startUp module as a new instance of mw.Map(). + * If $wgLegacyJavaScriptGlobals is true, this Map will have its values + * in the global window object. */ - this.config = new this.Map( LEGACY_GLOBALS ); + this.config = null; /* * Information about the current user