Moved Skin::makeGlobalVariablesScript() to OutputPage::getJSVars()
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Wed, 16 Feb 2011 18:25:44 +0000 (18:25 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Wed, 16 Feb 2011 18:25:44 +0000 (18:25 +0000)
* merged <script> and if ( window.mediaWiki ) block with the one of mediaWiki.loader as stated in the doc
* removed dependency of $wgTitle
* the two only calls to this functions are in SemanticForms, but will not affect current version of MediaWiki:
** specials/SF_UploadWindow.php: this file is only used before 1.16
** specials/SF_UploadWindow2.php: the call is part of the else branch of a "method_exists( $wgOut, 'addModules' )" check, which means it's not called since 1.17

includes/OutputPage.php
includes/Skin.php

index 9877b51..ca2acbf 100644 (file)
@@ -2535,23 +2535,22 @@ class OutputPage {
                // Startup - this will immediately load jquery and mediawiki modules
                $scripts = $this->makeResourceLoaderLink( $sk, 'startup', ResourceLoaderModule::TYPE_SCRIPTS, true );
 
-               // Configuration -- This could be merged together with the load and go, but
-               // makeGlobalVariablesScript returns a whole script tag -- grumble grumble...
-               $scripts .= Skin::makeGlobalVariablesScript( $sk->getSkinName() ) . "\n";
-
                // Script and Messages "only" requests
                $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleScripts( true ), ResourceLoaderModule::TYPE_SCRIPTS );
                $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleMessages( true ), ResourceLoaderModule::TYPE_MESSAGES );
 
                // Modules requests - let the client calculate dependencies and batch requests as it likes
+               $loader = '';
                if ( $this->getModules( true ) ) {
-                       $scripts .= Html::inlineScript(
-                               ResourceLoader::makeLoaderConditionalScript(
-                                       Xml::encodeJsCall( 'mediaWiki.loader.load', array( $this->getModules( true ) ) ) .
-                                       Xml::encodeJsCall( 'mediaWiki.loader.go', array() )
-                               )
-                       ) . "\n";
+                       $loader = Xml::encodeJsCall( 'mediaWiki.loader.load', array( $this->getModules( true ) ) ) .
+                               Xml::encodeJsCall( 'mediaWiki.loader.go', array() );
                }
+               
+               $scripts .= Html::inlineScript(
+                       ResourceLoader::makeLoaderConditionalScript(
+                               ResourceLoader::makeConfigSetScript( $this->getJSVars() ) . $loader
+                       )
+               );
 
                // Legacy Scripts
                $scripts .= "\n" . $this->mScripts;
@@ -2582,6 +2581,53 @@ class OutputPage {
                return $scripts;
        }
 
+       /**
+        * Get an array containing global JS variables
+        * 
+        * Do not add things here which can be evaluated in
+        * ResourceLoaderStartupScript - in other words, without state.
+        * You will only be adding bloat to the page and causing page caches to
+        * have to be purged on configuration changes.
+        */
+       protected function getJSVars() {
+               global $wgUser, $wgRequest, $wgUseAjax, $wgEnableMWSuggest, $wgContLang;
+
+               $title = $this->getTitle();
+               $ns = $title->getNamespace();
+               $nsname = MWNamespace::exists( $ns ) ? MWNamespace::getCanonicalName( $ns ) : $title->getNsText();
+
+               $vars = array(
+                       'wgCanonicalNamespace' => $nsname,
+                       'wgCanonicalSpecialPageName' => $ns == NS_SPECIAL ?
+                               SpecialPage::resolveAlias( $title->getDBkey() ) : false, # bug 21115
+                       'wgNamespaceNumber' => $title->getNamespace(),
+                       'wgPageName' => $title->getPrefixedDBKey(),
+                       'wgTitle' => $title->getText(),
+                       'wgCurRevisionId' => $title->getLatestRevID(),
+                       'wgArticleId' => $title->getArticleId(),
+                       'wgIsArticle' => $this->isArticle(),
+                       'wgAction' => $wgRequest->getText( 'action', 'view' ),
+                       'wgUserName' => $wgUser->isAnon() ? null : $wgUser->getName(),
+                       'wgUserGroups' => $wgUser->getEffectiveGroups(),
+                       'wgCategories' => $this->getCategories(),
+                       'wgBreakFrames' => $this->getFrameOptions() == 'DENY',
+               );
+               if ( $wgContLang->hasVariants() ) {
+                       $vars['wgUserVariant'] = $wgContLang->getPreferredVariant();
+               }
+               foreach ( $title->getRestrictionTypes() as $type ) {
+                       $vars['wgRestriction' . ucfirst( $type )] = $title->getRestrictions( $type );
+               }
+               if ( $wgUseAjax && $wgEnableMWSuggest && !$wgUser->getOption( 'disablesuggest', false ) ) {
+                       $vars['wgSearchNamespaces'] = SearchEngine::userNamespaces( $wgUser );
+               }
+               
+               // Allow extensions to add their custom variables to the global JS variables
+               wfRunHooks( 'MakeGlobalVariablesScript', array( &$vars ) );
+               
+               return $vars;
+       }
+
        /**
         * Add default \<meta\> tags
         */
index 70eef48..489bda8 100644 (file)
@@ -470,52 +470,6 @@ abstract class Skin extends Linker {
                } 
        }
 
-       /**
-        * Make a <script> tag containing global variables
-        * @param $skinName string Name of the skin
-        * The odd calling convention is for backwards compatibility
-        * @todo FIXME: Make this not depend on $wgTitle!
-        * 
-        * Do not add things here which can be evaluated in ResourceLoaderStartupScript - in other words, without state.
-        * You will only be adding bloat to the page and causing page caches to have to be purged on configuration changes.
-        */
-       static function makeGlobalVariablesScript( $skinName ) {
-               global $wgTitle, $wgUser, $wgRequest, $wgOut, $wgUseAjax, $wgEnableMWSuggest, $wgContLang;
-               
-               $ns = $wgTitle->getNamespace();
-               $nsname = MWNamespace::exists( $ns ) ? MWNamespace::getCanonicalName( $ns ) : $wgTitle->getNsText();
-               $vars = array(
-                       'wgCanonicalNamespace' => $nsname,
-                       'wgCanonicalSpecialPageName' => $ns == NS_SPECIAL ?
-                               SpecialPage::resolveAlias( $wgTitle->getDBkey() ) : false, # bug 21115
-                       'wgNamespaceNumber' => $wgTitle->getNamespace(),
-                       'wgPageName' => $wgTitle->getPrefixedDBKey(),
-                       'wgTitle' => $wgTitle->getText(),
-                       'wgAction' => $wgRequest->getText( 'action', 'view' ),
-                       'wgArticleId' => $wgTitle->getArticleId(),
-                       'wgIsArticle' => $wgOut->isArticle(),
-                       'wgUserName' => $wgUser->isAnon() ? null : $wgUser->getName(),
-                       'wgUserGroups' => $wgUser->getEffectiveGroups(),
-                       'wgCurRevisionId' => $wgTitle->getLatestRevID(),
-                       'wgCategories' => $wgOut->getCategories(),
-                       'wgBreakFrames' => $wgOut->getFrameOptions() == 'DENY',
-               );
-               if ( $wgContLang->hasVariants() ) {
-                       $vars['wgUserVariant'] = $wgContLang->getPreferredVariant();
-               }
-               foreach ( $wgTitle->getRestrictionTypes() as $type ) {
-                       $vars['wgRestriction' . ucfirst( $type )] = $wgTitle->getRestrictions( $type );
-               }
-               if ( $wgUseAjax && $wgEnableMWSuggest && !$wgUser->getOption( 'disablesuggest', false ) ) {
-                       $vars['wgSearchNamespaces'] = SearchEngine::userNamespaces( $wgUser );
-               }
-               
-               // Allow extensions to add their custom variables to the global JS variables
-               wfRunHooks( 'MakeGlobalVariablesScript', array( &$vars ) );
-               
-               return self::makeVariablesScript( $vars );
-       }
-
        /**
         * To make it harder for someone to slip a user a fake
         * user-JavaScript or user-CSS preview, a random token