From 8c7095be85156fb97fbbcda914d190ea067a3396 Mon Sep 17 00:00:00 2001 From: rlot Date: Wed, 11 Jan 2017 17:47:26 +0100 Subject: [PATCH] Improved parsing in reason suggests Bug: T155086 Change-Id: I3a3167b7bfd9b5921df1cf3e4a3cf3e1da4ca001 --- includes/ProtectionForm.php | 5 +++- includes/Xml.php | 30 +++++++++++++++++++ includes/page/Article.php | 7 +++-- includes/specials/SpecialBlock.php | 5 +++- .../src/mediawiki/mediawiki.reasonSuggest.js | 9 +----- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/includes/ProtectionForm.php b/includes/ProtectionForm.php index 58a04a1dcb..bcf4dda98a 100644 --- a/includes/ProtectionForm.php +++ b/includes/ProtectionForm.php @@ -184,9 +184,12 @@ class ProtectionForm { $out = $this->mContext->getOutput(); if ( !wfMessage( 'protect-dropdown' )->inContentLanguage()->isDisabled() ) { + $reasonsList = Xml::getArrayFromWikiTextList( + wfMessage( 'protect-dropdown' )->inContentLanguage()->text() + ); $out->addModules( 'mediawiki.reasonSuggest' ); $out->addJsConfigVars( [ - 'reasons' => 'protect-dropdown' + 'reasons' => $reasonsList ] ); } diff --git a/includes/Xml.php b/includes/Xml.php index e124c38b75..8f18046f5f 100644 --- a/includes/Xml.php +++ b/includes/Xml.php @@ -563,6 +563,36 @@ class Xml { . Xml::closeElement( 'select' ); } + /** + * Converts textual drop-down list to array + * + * @param string $list Correctly formatted text (newline delimited) to be + * used to generate the options. + * @return array + */ + public static function getArrayFromWikiTextList( $list = '' ) { + $options = []; + + foreach ( explode( "\n", $list ) as $option ) { + $value = trim( $option ); + if ( $value == '' ) { + continue; + } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) { + // A new group is starting ... + $value = trim( substr( $value, 1 ) ); + $options[] = $value; + } elseif ( substr( $value, 0, 2 ) == '**' ) { + // groupmember + $value = trim( substr( $value, 2 ) ); + $options[] = $value; + } else { + // groupless reason list + $options[] = $value; + } + } + return $options; + } + /** * Shortcut for creating fieldsets. * diff --git a/includes/page/Article.php b/includes/page/Article.php index d268e61de1..a4938547f3 100644 --- a/includes/page/Article.php +++ b/includes/page/Article.php @@ -1674,9 +1674,12 @@ class Article implements Page { $ctx = $this->getContext(); $outputPage = $ctx->getOutput(); if ( !wfMessage( 'deletereason-dropdown' )->inContentLanguage()->isDisabled() ) { + $reasonsList = Xml::getArrayFromWikiTextList( + wfMessage( 'deletereason-dropdown' )->inContentLanguage()->text() + ); $outputPage->addModules( 'mediawiki.reasonSuggest' ); $outputPage->addJsConfigVars( [ - 'reasons' => 'deletereason-dropdown' + 'reasons' => $reasonsList ] ); } $useMediaWikiUIEverywhere = $ctx->getConfig()->get( 'UseMediaWikiUIEverywhere' ); @@ -1693,7 +1696,6 @@ class Article implements Page { Hooks::run( 'ArticleConfirmDelete', [ $this, $outputPage, &$reason ] ); $user = $this->getContext()->getUser(); - if ( $user->isAllowed( 'suppressrevision' ) ) { $suppress = Html::openElement( 'div', [ 'id' => 'wpDeleteSuppressRow' ] ) . Xml::checkLabel( wfMessage( 'revdelete-suppress' )->text(), @@ -1703,7 +1705,6 @@ class Article implements Page { $suppress = ''; } $checkWatch = $user->getBoolOption( 'watchdeletion' ) || $user->isWatched( $title ); - $form = Html::openElement( 'form', [ 'method' => 'post', 'action' => $title->getLocalURL( 'action=delete' ), 'id' => 'deleteconfirm' ] ) . Html::openElement( 'fieldset', [ 'id' => 'mw-delete-table' ] ) . diff --git a/includes/specials/SpecialBlock.php b/includes/specials/SpecialBlock.php index 730d94103d..7e02974003 100644 --- a/includes/specials/SpecialBlock.php +++ b/includes/specials/SpecialBlock.php @@ -128,9 +128,12 @@ class SpecialBlock extends FormSpecialPage { protected function getFormFields() { global $wgBlockAllowsUTEdit; if ( !wfMessage( 'ipbreason-dropdown' )->inContentLanguage()->isDisabled() ) { + $reasonsList = Xml::getArrayFromWikiTextList( + wfMessage( 'ipbreason-dropdown' )->inContentLanguage()->text() + ); $this->getOutput()->addModules( 'mediawiki.reasonSuggest' ); $this->getOutput()->addJsConfigVars( [ - 'reasons' => 'ipbreason-dropdown' + 'reasons' => $reasonsList ] ); } $user = $this->getUser(); diff --git a/resources/src/mediawiki/mediawiki.reasonSuggest.js b/resources/src/mediawiki/mediawiki.reasonSuggest.js index 9042278d07..71efdb07f7 100644 --- a/resources/src/mediawiki/mediawiki.reasonSuggest.js +++ b/resources/src/mediawiki/mediawiki.reasonSuggest.js @@ -3,14 +3,7 @@ */ ( function ( mw, $ ) { $( function () { - var api = new mw.Api(), reasons = []; - // These messages can be really big, so its loaded on-the-go - api.loadMessagesIfMissing( [ mw.config.get( 'reasons' ) ] ) - .done( function () { - // Convert from string to array, first index is unneeded - reasons = mw.msg( mw.config.get( 'reasons' ) ).split( '\n** ' ); - reasons.splice( 0, 1 ); - } ); + var reasons = mw.config.get( 'reasons' ); // Add relevant suggestion $( '#mwProtect-reason, #wpReason, #mw-input-wpReason-other' ).suggestions( { -- 2.20.1