From faab2411c29a2ad0851c596f1cadfec1d7e06892 Mon Sep 17 00:00:00 2001 From: rlot Date: Wed, 4 Jan 2017 18:52:17 +0100 Subject: [PATCH] Added reason suggestion in block/delete/protect forms Bug: T34950 Change-Id: I9778c4992b127c36355949665b4fdf7482e7e0e7 --- RELEASE-NOTES-1.29 | 2 ++ includes/ProtectionForm.php | 10 ++++++- includes/page/Article.php | 6 +++++ includes/specials/SpecialBlock.php | 7 ++++- resources/Resources.php | 7 +++++ .../src/mediawiki/mediawiki.reasonSuggest.js | 27 +++++++++++++++++++ 6 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 resources/src/mediawiki/mediawiki.reasonSuggest.js diff --git a/RELEASE-NOTES-1.29 b/RELEASE-NOTES-1.29 index 3630abc47a..4cdb341863 100644 --- a/RELEASE-NOTES-1.29 +++ b/RELEASE-NOTES-1.29 @@ -43,6 +43,8 @@ production. from certain IP ranges (e.g. private IPs). * (T59603) Added new magic word {{PAGELANGUAGE}} which returns the language code of the page being parsed. +* Added JavaScript that provides as-you-type suggestions for reason + on the block, delete and protect forms. === External library changes in 1.29 === diff --git a/includes/ProtectionForm.php b/includes/ProtectionForm.php index 454ffcc978..58a04a1dcb 100644 --- a/includes/ProtectionForm.php +++ b/includes/ProtectionForm.php @@ -182,10 +182,18 @@ class ProtectionForm { throw new ErrorPageError( 'protect-badnamespace-title', 'protect-badnamespace-text' ); } + $out = $this->mContext->getOutput(); + if ( !wfMessage( 'protect-dropdown' )->inContentLanguage()->isDisabled() ) { + $out->addModules( 'mediawiki.reasonSuggest' ); + $out->addJsConfigVars( [ + 'reasons' => 'protect-dropdown' + ] ); + } + if ( $this->mContext->getRequest()->wasPosted() ) { if ( $this->save() ) { $q = $this->mArticle->isRedirect() ? 'redirect=no' : ''; - $this->mContext->getOutput()->redirect( $this->mTitle->getFullURL( $q ) ); + $out->redirect( $this->mTitle->getFullURL( $q ) ); } } else { $this->show(); diff --git a/includes/page/Article.php b/includes/page/Article.php index 4bcb655e97..d268e61de1 100644 --- a/includes/page/Article.php +++ b/includes/page/Article.php @@ -1673,6 +1673,12 @@ class Article implements Page { $title = $this->getTitle(); $ctx = $this->getContext(); $outputPage = $ctx->getOutput(); + if ( !wfMessage( 'deletereason-dropdown' )->inContentLanguage()->isDisabled() ) { + $outputPage->addModules( 'mediawiki.reasonSuggest' ); + $outputPage->addJsConfigVars( [ + 'reasons' => 'deletereason-dropdown' + ] ); + } $useMediaWikiUIEverywhere = $ctx->getConfig()->get( 'UseMediaWikiUIEverywhere' ); $outputPage->setPageTitle( wfMessage( 'delete-confirm', $title->getPrefixedText() ) ); $outputPage->addBacklinkSubtitle( $title ); diff --git a/includes/specials/SpecialBlock.php b/includes/specials/SpecialBlock.php index 585f70b86b..730d94103d 100644 --- a/includes/specials/SpecialBlock.php +++ b/includes/specials/SpecialBlock.php @@ -127,7 +127,12 @@ class SpecialBlock extends FormSpecialPage { */ protected function getFormFields() { global $wgBlockAllowsUTEdit; - + if ( !wfMessage( 'ipbreason-dropdown' )->inContentLanguage()->isDisabled() ) { + $this->getOutput()->addModules( 'mediawiki.reasonSuggest' ); + $this->getOutput()->addJsConfigVars( [ + 'reasons' => 'ipbreason-dropdown' + ] ); + } $user = $this->getUser(); $suggestedDurations = self::getSuggestedDurations(); diff --git a/resources/Resources.php b/resources/Resources.php index 1c16cc3b19..86673ee5a1 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -1396,6 +1396,13 @@ return [ ], 'targets' => [ 'desktop', 'mobile' ], ], + 'mediawiki.reasonSuggest' => [ + 'scripts' => 'resources/src/mediawiki/mediawiki.reasonSuggest.js', + 'dependencies' => [ + 'jquery.suggestions', + 'mediawiki.api.messages' + ] + ], 'mediawiki.userSuggest' => [ 'scripts' => 'resources/src/mediawiki/mediawiki.userSuggest.js', 'dependencies' => [ diff --git a/resources/src/mediawiki/mediawiki.reasonSuggest.js b/resources/src/mediawiki/mediawiki.reasonSuggest.js new file mode 100644 index 0000000000..9042278d07 --- /dev/null +++ b/resources/src/mediawiki/mediawiki.reasonSuggest.js @@ -0,0 +1,27 @@ +/*! +* Add autocomplete suggestions for action forms reasons. +*/ +( 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 ); + } ); + + // Add relevant suggestion + $( '#mwProtect-reason, #wpReason, #mw-input-wpReason-other' ).suggestions( { + fetch: function () { + var $this = $( this ), relevantSuggestions; + relevantSuggestions = $.grep( reasons, function ( reason ) { + return ( reason.toLowerCase().indexOf( $this.val().toLowerCase() ) > -1 ); + } ); + $this.suggestions( 'suggestions', relevantSuggestions ); + }, + highlightInput: true + } ); + } ); +}( mediaWiki, jQuery ) ); -- 2.20.1