clean up Html::inlineScript usage
authorKrinkle <krinkle@users.mediawiki.org>
Wed, 28 Sep 2011 22:47:05 +0000 (22:47 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Wed, 28 Sep 2011 22:47:05 +0000 (22:47 +0000)
* ResourceLoader::makeLoaderConditionalScript:
-- window.mediaWiki -> window.mw; Not just because it's shorter but because that's the variable that is actually being used inside the script.
* ProtectionForm::buildCleanupScript
-- Use a single quote string and no new line
-- Use ResourceLoader::makeLoaderConditionalScript instead of duplicating this logic everywhere
* ProtectionForm::buildCleanupScript:
-- Use Xml::encodeJsCall to render javascript code
-- Use ResourceLoader::makeLoaderConditionalScript
-- Fix bug 31230 by using the new OutputPage->addJsConfigVars( r98374 )
(had to use $wgOut for now since there wasn't an obvious route to a context that I could find here (Article has context, but ProtectionForm::__construct takes any WikiPage, not just Article)
* EditPage::getEditToolbar:
-- Use Xml::encodeJsCall
-- Use ResourceLoader::makeLoaderConditionalScript

Fixes:
* (bug 31230) ProtectionForm should set wgCascadeableLevels in mw.confg instead of globally

includes/EditPage.php
includes/ProtectionForm.php
includes/resourceloader/ResourceLoader.php

index 98039bc..d8f913f 100644 (file)
@@ -2539,7 +2539,6 @@ HTML
                                'key'    => 'R'
                        )
                );
-               $toolbar = "<div id='toolbar'>\n";
 
                $script = '';
                foreach ( $toolarray as $tool ) {
@@ -2560,15 +2559,11 @@ HTML
                                $cssId = $tool['id'],
                        );
 
-                       $paramList = implode( ',',
-                               array_map( array( 'Xml', 'encodeJsVar' ), $params ) );
-                       $script .= "mw.toolbar.addButton($paramList);\n";
+                       $script .= Xml::encodeJsCall( 'mw.toolbar.addButton', $params );
                }
-               $wgOut->addScript( Html::inlineScript(
-                       "if ( window.mediaWiki ) {{$script}}"
-               ) );
+               $wgOut->addScript( Html::inlineScript( ResourceLoader::makeLoaderConditionalScript( $script ) ) );
 
-               $toolbar .= "\n</div>";
+               $toolbar = '<div id="toolbar"></div>';
 
                wfRunHooks( 'EditPageBeforeEditToolbar', array( &$toolbar ) );
 
index b73558b..9771493 100644 (file)
@@ -576,25 +576,26 @@ class ProtectionForm {
        }
        
        function buildCleanupScript() {
-               global $wgRestrictionLevels, $wgGroupPermissions;
-               $script = 'var wgCascadeableLevels=';
-               $CascadeableLevels = array();
+               global $wgRestrictionLevels, $wgGroupPermissions, $wgOut;
+
+               $cascadeableLevels = array();
                foreach( $wgRestrictionLevels as $key ) {
-                       if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) || $key == 'protect' ) {
-                               $CascadeableLevels[] = "'" . Xml::escapeJsString( $key ) . "'";
+                       if ( ( isset( $wgGroupPermissions[$key]['protect'] ) && $wgGroupPermissions[$key]['protect'] )
+                               || $key == 'protect' 
+                       ) {
+                               $cascadeableLevels[] = $key;
                        }
                }
-               $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
-               $options = (object)array(
+               $options = array(
                        'tableId' => 'mwProtectSet',
-                       'labelText' => wfMsg( 'protect-unchain-permissions' ),
-                       'numTypes' => count($this->mApplicableTypes),
-                       'existingMatch' => 1 == count( array_unique( $this->mExistingExpiry ) ),
+                       'labelText' => wfMessage( 'protect-unchain-permissions' )->plain(),
+                       'numTypes' => count( $this->mApplicableTypes ),
+                       'existingMatch' => count( array_unique( $this->mExistingExpiry ) ) === 1,
                );
-               $encOptions = Xml::encodeJsVar( $options );
 
-               $script .= "ProtectionForm.init($encOptions)";
-               return Html::inlineScript( "if ( window.mediaWiki ) { $script }" );
+               $wgOut->addJsConfigVars( 'wgCascadeableLevels', $cascadeableLevels );
+               $script = Xml::encodeJsCall( 'ProtectionForm.init', array( $options ) );
+               return Html::inlineScript( ResourceLoader::makeLoaderConditionalScript( $script ) );
        }
 
        /**
index 70f1d4f..d703264 100644 (file)
@@ -812,7 +812,7 @@ class ResourceLoader {
         */
        public static function makeLoaderConditionalScript( $script ) {
                $script = str_replace( "\n", "\n\t", trim( $script ) );
-               return "if ( window.mediaWiki ) {\n\t$script\n}\n";
+               return "if(window.mw){\n\t$script\n}\n";
        }
 
        /**