Creating a way to toggle mw.config LEGACY_GLOBALS from LocalSettings (bug 28916).
authorKrinkle <krinkle@users.mediawiki.org>
Tue, 10 May 2011 23:17:13 +0000 (23:17 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Tue, 10 May 2011 23:17:13 +0000 (23:17 +0000)
* 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.

includes/DefaultSettings.php
includes/resourceloader/ResourceLoader.php
includes/resourceloader/ResourceLoaderStartUpModule.php
resources/mediawiki/mediawiki.js

index ec94af2..c373be7 100644 (file)
@@ -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
index 7e8d099..4da9e2c 100644 (file)
@@ -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 ) );
        }
        
        /**
index 73961f4..b63d4df 100644 (file)
@@ -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;";
                }
 
index a870bf5..68d860a 100644 (file)
@@ -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