Provide message/warning/error box abstraction
authorjdlrobson <jdlrobson@gmail.com>
Thu, 28 Sep 2017 18:42:32 +0000 (13:42 -0500)
committerJdlrobson <jrobson@wikimedia.org>
Mon, 13 Nov 2017 23:19:45 +0000 (23:19 +0000)
This will help us consolidate the various uses into one single
method which will help us drive standardisation of these defacto
widgets.

Hopefully, by being a method of the Html class, which has a very
low barrier for use will drive down the inconsistent display of
warning/error boxes across MediaWiki's products

Various usages of warningbox and errorbox have been ported over.
I've retained some more complicated usages which make use of the
parser (wrapWikiMsg) and any where id and class are medled with
- we'll probably want to consider whether we want to encourage
those going forward as they encourage adjusting the styling.

Bug: T166915
Change-Id: I2757e1f4ff2599e93a7257fc644cab69063896d2

RELEASE-NOTES-1.31
includes/Html.php
includes/exception/MWException.php
includes/page/Article.php
includes/skins/SkinFallbackTemplate.php
includes/specials/SpecialEditTags.php
includes/specials/SpecialMovepage.php
includes/specials/SpecialRecentchangeslinked.php
includes/specials/SpecialResetTokens.php
includes/specials/SpecialRevisiondelete.php
includes/specials/SpecialSearch.php

index 9ded68c..d9da9ac 100644 (file)
@@ -20,6 +20,9 @@ production.
 === New features in 1.31 ===
 * Wikimedia\Rdbms\IDatabase->select() and similar methods now support
   joins with parentheses for grouping.
+* As a first pass in standardizing dialog boxes across the MediaWiki product,
+Html class now provides helper methods for messageBox, successBox, errorBox and
+warningBox generation.
 
 === External library changes in 1.31 ===
 
index 0988b05..524fdcd 100644 (file)
@@ -675,6 +675,52 @@ class Html {
                return self::input( $name, $value, 'checkbox', $attribs );
        }
 
+       /**
+        * Return the HTML for a message box.
+        * @since 1.31
+        * @param string $html of contents of box
+        * @param string $className corresponding to box
+        * @param string $heading (optional)
+        * @return string of HTML representing a box.
+        */
+       public static function messageBox( $html, $className, $heading = '' ) {
+               if ( $heading ) {
+                       $html = self::element( 'h2', [], $heading ) . $html;
+               }
+               return self::rawElement( 'div', [ 'class' => $className ], $html );
+       }
+
+       /**
+        * Return a warning box.
+        * @since 1.31
+        * @param string $html of contents of box
+        * @return string of HTML representing a warning box.
+        */
+       public static function warningBox( $html ) {
+               return self::messageBox( $html, 'warningbox' );
+       }
+
+       /**
+        * Return an error box.
+        * @since 1.31
+        * @param string $html of contents of error box
+        * @param string $heading (optional)
+        * @return string of HTML representing an error box.
+        */
+       public static function errorBox( $html, $heading = '' ) {
+               return self::messageBox( $html, 'errorbox', $heading );
+       }
+
+       /**
+        * Return a success box.
+        * @since 1.31
+        * @param string $html of contents of box
+        * @return string of HTML representing a success box.
+        */
+       public static function successBox( $html ) {
+               return self::messageBox( $html, 'successbox' );
+       }
+
        /**
         * Convenience function to produce a radio button (input element with type=radio)
         *
index 8c1f8dc..c633431 100644 (file)
@@ -102,7 +102,7 @@ class MWException extends Exception {
                } else {
                        $logId = WebRequest::getRequestId();
                        $type = static::class;
-                       return "<div class=\"errorbox\">" .
+                       return Html::errorBox(
                        '[' . $logId . '] ' .
                        gmdate( 'Y-m-d H:i:s' ) . ": " .
                        $this->msg( "internalerror-fatal-exception",
@@ -110,7 +110,7 @@ class MWException extends Exception {
                                $type,
                                $logId,
                                MWExceptionHandler::getURL( $this )
-                       ) . "</div>\n" .
+                       ) ) .
                        "<!-- Set \$wgShowExceptionDetails = true; " .
                        "at the bottom of LocalSettings.php to show detailed " .
                        "debugging information. -->";
index df189af..c9dc273 100644 (file)
@@ -590,7 +590,7 @@ class Article implements Page {
                                                        $outputPage->setRobotPolicy( 'noindex,nofollow' );
 
                                                        $errortext = $error->getWikiText( false, 'view-pool-error' );
-                                                       $outputPage->addWikiText( '<div class="errorbox">' . $errortext . '</div>' );
+                                                       $outputPage->addWikiText( Html::errorBox( $errortext ) );
                                                }
                                                # Connection or timeout error
                                                return;
index ee8d841..1ad1ab0 100644 (file)
@@ -96,12 +96,9 @@ class SkinFallbackTemplate extends BaseTemplate {
         * warning message and page content.
         */
        public function execute() {
-               $this->html( 'headelement' ) ?>
-
-               <div class="warningbox">
-                       <?php echo $this->buildHelpfulInformationMessage() ?>
-               </div>
-
+               $this->html( 'headelement' );
+               echo Html::warningBox( $this->buildHelpfulInformationMessage() );
+       ?>
                <form action="<?php $this->text( 'wgScript' ) ?>">
                        <input type="hidden" name="title" value="<?php $this->text( 'searchtitle' ) ?>" />
                        <h3><label for="searchInput"><?php $this->msg( 'search' ) ?></label></h3>
index 476c452..eb0f0aa 100644 (file)
@@ -451,9 +451,8 @@ class SpecialEditTags extends UnlistedSpecialPage {
         */
        protected function failure( $status ) {
                $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
-               $this->getOutput()->addWikiText( '<div class="errorbox">' .
-                       $status->getWikiText( 'tags-edit-failure' ) .
-                       '</div>'
+               $this->getOutput()->addWikiText(
+                       Html::errorBox( $status->getWikiText( 'tags-edit-failure' ) )
                );
                $this->showForm();
        }
index 46d7cf7..02d6d00 100644 (file)
@@ -235,18 +235,18 @@ class MovePageForm extends UnlistedSpecialPage {
                }
 
                if ( count( $err ) ) {
-                       $out->addHTML( "<div class='errorbox'>\n" );
                        $action_desc = $this->msg( 'action-move' )->plain();
-                       $out->addWikiMsg( 'permissionserrorstext-withaction', count( $err ), $action_desc );
+                       $errMsgHtml = $this->msg( 'permissionserrorstext-withaction',
+                               count( $err ), $action_desc )->parseAsBlock();
 
                        if ( count( $err ) == 1 ) {
                                $errMsg = $err[0];
                                $errMsgName = array_shift( $errMsg );
 
                                if ( $errMsgName == 'hookaborted' ) {
-                                       $out->addHTML( "<p>{$errMsg[0]}</p>\n" );
+                                       $errMsgHtml .= "<p>{$errMsg[0]}</p>\n";
                                } else {
-                                       $out->addWikiMsgArray( $errMsgName, $errMsg );
+                                       $errMsgHtml .= $this->msg( $errMsgName, $errMsg )->parseAsBlock();
                                }
                        } else {
                                $errStr = [];
@@ -260,9 +260,9 @@ class MovePageForm extends UnlistedSpecialPage {
                                        }
                                }
 
-                               $out->addHTML( '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n" );
+                               $errMsgHtml .= '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n";
                        }
-                       $out->addHTML( "</div>\n" );
+                       $out->addHTML( Html::errorBox( $errMsgHtml ) );
                }
 
                if ( $this->oldTitle->isProtected( 'move' ) ) {
index 99880de..358a309 100644 (file)
@@ -62,8 +62,9 @@ class SpecialRecentChangesLinked extends SpecialRecentChanges {
                $outputPage = $this->getOutput();
                $title = Title::newFromText( $target );
                if ( !$title || $title->isExternal() ) {
-                       $outputPage->addHTML( '<div class="errorbox">' . $this->msg( 'allpagesbadtitle' )
-                                       ->parse() . '</div>' );
+                       $outputPage->addHTML(
+                               Html::errorBox( $this->msg( 'allpagesbadtitle' )->parse() )
+                       );
 
                        return false;
                }
index 3e89686..964a261 100644 (file)
@@ -74,7 +74,7 @@ class SpecialResetTokens extends FormSpecialPage {
 
        public function onSuccess() {
                $this->getOutput()->wrapWikiMsg(
-                       "<div class='successbox'>\n$1\n</div>",
+                       Html::successBox( '$1' ),
                        'resettokens-done'
                );
        }
index e1d4dd1..8edebf2 100644 (file)
@@ -636,9 +636,10 @@ class SpecialRevisionDelete extends UnlistedSpecialPage {
        protected function failure( $status ) {
                // Messages: revdelete-failure, logdelete-failure
                $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
-               $this->getOutput()->addWikiText( '<div class="errorbox">' .
-                       $status->getWikiText( $this->typeLabels['failure'] ) .
-                       '</div>'
+               $this->getOutput()->addWikiText(
+                       Html::errorBox(
+                               $status->getWikiText( $this->typeLabels['failure'] )
+                       )
                );
                $this->showForm();
        }
index 09210e4..b3a58cb 100644 (file)
@@ -365,16 +365,12 @@ class SpecialSearch extends SpecialPage {
                if ( $hasErrors ) {
                        list( $error, $warning ) = $textStatus->splitByErrorType();
                        if ( $error->getErrors() ) {
-                               $out->addHTML( Html::rawElement(
-                                       'div',
-                                       [ 'class' => 'errorbox' ],
+                               $out->addHTML( Html::errorBox(
                                        $error->getHTML( 'search-error' )
                                ) );
                        }
                        if ( $warning->getErrors() ) {
-                               $out->addHTML( Html::rawElement(
-                                       'div',
-                                       [ 'class' => 'warningbox' ],
+                               $out->addHTML( Html::warningBox(
                                        $warning->getHTML( 'search-warning' )
                                ) );
                        }