From 32327650c5e7e73e392ae3b3a97d97650d4fb0af Mon Sep 17 00:00:00 2001 From: Krinkle Date: Wed, 28 Sep 2011 22:47:05 +0000 Subject: [PATCH] clean up Html::inlineScript usage * 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 | 11 +++------ includes/ProtectionForm.php | 27 +++++++++++----------- includes/resourceloader/ResourceLoader.php | 2 +- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/includes/EditPage.php b/includes/EditPage.php index 98039bc0bb..d8f913ffd6 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -2539,7 +2539,6 @@ HTML 'key' => 'R' ) ); - $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
"; + $toolbar = '
'; wfRunHooks( 'EditPageBeforeEditToolbar', array( &$toolbar ) ); diff --git a/includes/ProtectionForm.php b/includes/ProtectionForm.php index b73558bee2..97714935ce 100644 --- a/includes/ProtectionForm.php +++ b/includes/ProtectionForm.php @@ -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 ) ); } /** diff --git a/includes/resourceloader/ResourceLoader.php b/includes/resourceloader/ResourceLoader.php index 70f1d4fed0..d703264336 100644 --- a/includes/resourceloader/ResourceLoader.php +++ b/includes/resourceloader/ResourceLoader.php @@ -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"; } /** -- 2.20.1