Improved parsing in reason suggests
authorrlot <rj23@protonmail.com>
Wed, 11 Jan 2017 16:47:26 +0000 (17:47 +0100)
committerrlot <rj23@protonmail.com>
Wed, 11 Jan 2017 18:00:00 +0000 (19:00 +0100)
Bug: T155086
Change-Id: I3a3167b7bfd9b5921df1cf3e4a3cf3e1da4ca001

includes/ProtectionForm.php
includes/Xml.php
includes/page/Article.php
includes/specials/SpecialBlock.php
resources/src/mediawiki/mediawiki.reasonSuggest.js

index 58a04a1..bcf4dda 100644 (file)
@@ -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
                        ] );
                }
 
index e124c38..8f18046 100644 (file)
@@ -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.
         *
index d268e61..a493854 100644 (file)
@@ -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' ] ) .
index 730d941..7e02974 100644 (file)
@@ -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();
index 9042278..71efdb0 100644 (file)
@@ -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( {