Florianschmidtwelzow has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/296938

Change subject: [WIP] Show warnings in HTMLForm
......................................................................

[WIP] Show warnings in HTMLForm

This commit changes the way how HTMLForm handles a Status object
when executed from a request. It now handles, beside the errors,
also the warnings of a Status object and prints them out wrapped
in a warning box.

This is a WIP change, so with it, any error of LoginSignupSpecialPage
will be outputted as a warning, instead of an error. This needs to
be changed once there's a conclusion about my thoughts about the
topic (see the linked task).

ToDo:
 * Finish work in HTMLForm (comments, handle multiple warnings, ...)
 * Let the AuthenticationResponse decide, if the message should be
   an error or a warning

Bug: T139179
Change-Id: I9a27911613e62b5c4cb86bea40696cb37c4f49c2
---
M includes/DefaultSettings.php
M includes/htmlform/HTMLForm.php
M includes/specialpage/LoginSignupSpecialPage.php
3 files changed, 55 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/38/296938/1

diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php
index 6d08eec..e286be6 100644
--- a/includes/DefaultSettings.php
+++ b/includes/DefaultSettings.php
@@ -5612,11 +5612,11 @@
  */
 $wgPasswordAttemptThrottle = [
        // Short term limit
-       [ 'count' => 5, 'seconds' => 300 ],
+       #[ 'count' => 5, 'seconds' => 300 ],
        // Long term limit. We need to balance the risk
        // of somebody using this as a DoS attack to lock someone
        // out of their account, and someone doing a brute force attack.
-       [ 'count' => 150, 'seconds' => 60*60*48 ],
+       #[ 'count' => 150, 'seconds' => 60*60*48 ],
 ];
 
 /**
diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php
index 8ac4cf2..0b6f4b2 100644
--- a/includes/htmlform/HTMLForm.php
+++ b/includes/htmlform/HTMLForm.php
@@ -977,6 +977,7 @@
 
                $html = ''
                        . $this->getErrors( $submitResult )
+                       . $this->getWarnings( $submitResult )
                        . $this->getHeaderText()
                        . $this->getBody()
                        . $this->getHiddenFields()
@@ -1189,23 +1190,60 @@
         * @return string
         */
        public function getErrors( $errors ) {
-               if ( $errors instanceof Status ) {
-                       if ( $errors->isOK() ) {
-                               $errorstr = '';
+               return $this->getErrorsOrWarnings( $errors, 'error' );
+       }
+
+       /**
+        * Format and display an error message stack.
+        *
+        * @param string|array|Status $errors
+        *
+        * @return string
+        */
+       public function getWarnings( $warnings ) {
+               return $this->getErrorsOrWarnings( $warnings, 'warning' );
+       }
+
+       public function getErrorsOrWarnings( $elements, $elementsType ) {
+               if ( !in_array( $elementsType, [ 'error', 'warning' ] ) ) {
+                       throw new DomainException( $elementsType . ' is not a 
valid error or warnings type.' );
+               }
+               if ( $elements instanceof Status ) {
+                       if ( $elementsType === 'error' ? $elements->isOK() : 
$elements->isGood() ) {
+                               $elementstr = '';
                        } else {
-                               $errorstr = $this->getOutput()->parse( 
$errors->getWikiText() );
+                               if ( $elementsType === 'warning' ) {
+                                       $elementstr = $this->getOutput()->parse(
+                                               
$this->getWikitextFromStatusWarnings(
+                                                       $elements
+                                               )
+                                       );
+                               } else {
+                                       $elementstr = $elements->getWikiText();
+                               }
                        }
-               } elseif ( is_array( $errors ) ) {
-                       $errorstr = $this->formatErrors( $errors );
+               } elseif ( is_array( $elements ) ) {
+                       $elementstr = $this->formatErrors( $elements );
                } else {
-                       $errorstr = $errors;
+                       $elementstr = $elements;
                }
 
-               return $errorstr
-                       ? Html::rawElement( 'div', [ 'class' => 'error' ], 
$errorstr )
+               return $elementstr
+                       ? Html::rawElement( 'div', [ 'class' => $elementsType 
], $elementstr )
                        : '';
        }
 
+       private function getWikitextFromStatusWarnings( Status $status) {
+               if ( $status->isGood() ) {
+                       return '';
+               }
+               $warnings = '';
+               foreach ( $status->getErrorsByType( 'warning' ) as $warning ) {
+                       $warnings .= $warning['message'];
+               }
+               return $warnings;
+       }
+
        /**
         * Format a stack of error messages into a single HTML string
         *
diff --git a/includes/specialpage/LoginSignupSpecialPage.php 
b/includes/specialpage/LoginSignupSpecialPage.php
index 27b4f32..fe2756b 100644
--- a/includes/specialpage/LoginSignupSpecialPage.php
+++ b/includes/specialpage/LoginSignupSpecialPage.php
@@ -495,7 +495,12 @@
 
                $form = $this->getAuthForm( $requests, $this->authAction, $msg, 
$msgtype );
                $form->prepareForm();
-               $formHtml = $form->getHTML( $msg ? Status::newFatal( $msg ) : 
false );
+               $submitStatus = false;
+               if ( $msg ) {
+                       $submitStatus = Status::newGood();
+                       $submitStatus->warning( $msg );
+               }
+               $formHtml = $form->getHTML( $submitStatus );
 
                $out->addHTML( $this->getPageHtml( $formHtml ) );
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/296938
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9a27911613e62b5c4cb86bea40696cb37c4f49c2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Florianschmidtwelzow <florian.schmidt.stargatewis...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to