From 4e7021a2318ec588e4d0b226c64d25e93ec80bd8 Mon Sep 17 00:00:00 2001 From: jdlrobson Date: Thu, 28 Sep 2017 13:42:32 -0500 Subject: [PATCH] Provide message/warning/error box abstraction 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 | 3 ++ includes/Html.php | 46 +++++++++++++++++++ includes/exception/MWException.php | 4 +- includes/page/Article.php | 2 +- includes/skins/SkinFallbackTemplate.php | 9 ++-- includes/specials/SpecialEditTags.php | 5 +- includes/specials/SpecialMovepage.php | 12 ++--- .../specials/SpecialRecentchangeslinked.php | 5 +- includes/specials/SpecialResetTokens.php | 2 +- includes/specials/SpecialRevisiondelete.php | 7 +-- includes/specials/SpecialSearch.php | 8 +--- 11 files changed, 73 insertions(+), 30 deletions(-) diff --git a/RELEASE-NOTES-1.31 b/RELEASE-NOTES-1.31 index 9ded68c610..d9da9acf85 100644 --- a/RELEASE-NOTES-1.31 +++ b/RELEASE-NOTES-1.31 @@ -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 === diff --git a/includes/Html.php b/includes/Html.php index 0988b0549e..524fdcd7d9 100644 --- a/includes/Html.php +++ b/includes/Html.php @@ -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) * diff --git a/includes/exception/MWException.php b/includes/exception/MWException.php index 8c1f8dc968..c63343189f 100644 --- a/includes/exception/MWException.php +++ b/includes/exception/MWException.php @@ -102,7 +102,7 @@ class MWException extends Exception { } else { $logId = WebRequest::getRequestId(); $type = static::class; - return "
" . + 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 ) - ) . "
\n" . + ) ) . ""; diff --git a/includes/page/Article.php b/includes/page/Article.php index df189af341..c9dc273b47 100644 --- a/includes/page/Article.php +++ b/includes/page/Article.php @@ -590,7 +590,7 @@ class Article implements Page { $outputPage->setRobotPolicy( 'noindex,nofollow' ); $errortext = $error->getWikiText( false, 'view-pool-error' ); - $outputPage->addWikiText( '
' . $errortext . '
' ); + $outputPage->addWikiText( Html::errorBox( $errortext ) ); } # Connection or timeout error return; diff --git a/includes/skins/SkinFallbackTemplate.php b/includes/skins/SkinFallbackTemplate.php index ee8d8417fb..1ad1ab071b 100644 --- a/includes/skins/SkinFallbackTemplate.php +++ b/includes/skins/SkinFallbackTemplate.php @@ -96,12 +96,9 @@ class SkinFallbackTemplate extends BaseTemplate { * warning message and page content. */ public function execute() { - $this->html( 'headelement' ) ?> - -
- buildHelpfulInformationMessage() ?> -
- + $this->html( 'headelement' ); + echo Html::warningBox( $this->buildHelpfulInformationMessage() ); + ?>

diff --git a/includes/specials/SpecialEditTags.php b/includes/specials/SpecialEditTags.php index 476c452ad8..eb0f0aafa4 100644 --- a/includes/specials/SpecialEditTags.php +++ b/includes/specials/SpecialEditTags.php @@ -451,9 +451,8 @@ class SpecialEditTags extends UnlistedSpecialPage { */ protected function failure( $status ) { $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) ); - $this->getOutput()->addWikiText( '
' . - $status->getWikiText( 'tags-edit-failure' ) . - '
' + $this->getOutput()->addWikiText( + Html::errorBox( $status->getWikiText( 'tags-edit-failure' ) ) ); $this->showForm(); } diff --git a/includes/specials/SpecialMovepage.php b/includes/specials/SpecialMovepage.php index 46d7cf7a87..02d6d00232 100644 --- a/includes/specials/SpecialMovepage.php +++ b/includes/specials/SpecialMovepage.php @@ -235,18 +235,18 @@ class MovePageForm extends UnlistedSpecialPage { } if ( count( $err ) ) { - $out->addHTML( "
\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( "

{$errMsg[0]}

\n" ); + $errMsgHtml .= "

{$errMsg[0]}

\n"; } else { - $out->addWikiMsgArray( $errMsgName, $errMsg ); + $errMsgHtml .= $this->msg( $errMsgName, $errMsg )->parseAsBlock(); } } else { $errStr = []; @@ -260,9 +260,9 @@ class MovePageForm extends UnlistedSpecialPage { } } - $out->addHTML( '
  • ' . implode( "
  • \n
  • ", $errStr ) . "
\n" ); + $errMsgHtml .= '
  • ' . implode( "
  • \n
  • ", $errStr ) . "
\n"; } - $out->addHTML( "
\n" ); + $out->addHTML( Html::errorBox( $errMsgHtml ) ); } if ( $this->oldTitle->isProtected( 'move' ) ) { diff --git a/includes/specials/SpecialRecentchangeslinked.php b/includes/specials/SpecialRecentchangeslinked.php index 99880de786..358a309824 100644 --- a/includes/specials/SpecialRecentchangeslinked.php +++ b/includes/specials/SpecialRecentchangeslinked.php @@ -62,8 +62,9 @@ class SpecialRecentChangesLinked extends SpecialRecentChanges { $outputPage = $this->getOutput(); $title = Title::newFromText( $target ); if ( !$title || $title->isExternal() ) { - $outputPage->addHTML( '
' . $this->msg( 'allpagesbadtitle' ) - ->parse() . '
' ); + $outputPage->addHTML( + Html::errorBox( $this->msg( 'allpagesbadtitle' )->parse() ) + ); return false; } diff --git a/includes/specials/SpecialResetTokens.php b/includes/specials/SpecialResetTokens.php index 3e896863b2..964a261a6b 100644 --- a/includes/specials/SpecialResetTokens.php +++ b/includes/specials/SpecialResetTokens.php @@ -74,7 +74,7 @@ class SpecialResetTokens extends FormSpecialPage { public function onSuccess() { $this->getOutput()->wrapWikiMsg( - "
\n$1\n
", + Html::successBox( '$1' ), 'resettokens-done' ); } diff --git a/includes/specials/SpecialRevisiondelete.php b/includes/specials/SpecialRevisiondelete.php index e1d4dd1b5d..8edebf2c7a 100644 --- a/includes/specials/SpecialRevisiondelete.php +++ b/includes/specials/SpecialRevisiondelete.php @@ -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( '
' . - $status->getWikiText( $this->typeLabels['failure'] ) . - '
' + $this->getOutput()->addWikiText( + Html::errorBox( + $status->getWikiText( $this->typeLabels['failure'] ) + ) ); $this->showForm(); } diff --git a/includes/specials/SpecialSearch.php b/includes/specials/SpecialSearch.php index 09210e4379..b3a58cbd91 100644 --- a/includes/specials/SpecialSearch.php +++ b/includes/specials/SpecialSearch.php @@ -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' ) ) ); } -- 2.20.1