Added reason suggestion in block/delete/protect forms
authorrlot <rj23@protonmail.com>
Wed, 4 Jan 2017 17:52:17 +0000 (18:52 +0100)
committerFlorianschmidtwelzow <florian.schmidt.stargatewissen@gmail.com>
Tue, 10 Jan 2017 21:18:51 +0000 (21:18 +0000)
Bug: T34950
Change-Id: I9778c4992b127c36355949665b4fdf7482e7e0e7

RELEASE-NOTES-1.29
includes/ProtectionForm.php
includes/page/Article.php
includes/specials/SpecialBlock.php
resources/Resources.php
resources/src/mediawiki/mediawiki.reasonSuggest.js [new file with mode: 0644]

index 3630abc..4cdb341 100644 (file)
@@ -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 ===
 
index 454ffcc..58a04a1 100644 (file)
@@ -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();
index 4bcb655..d268e61 100644 (file)
@@ -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 );
index 585f70b..730d941 100644 (file)
@@ -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();
index 1c16cc3..86673ee 100644 (file)
@@ -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 (file)
index 0000000..9042278
--- /dev/null
@@ -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 ) );