Hygiene: Make construction of buttons easier
authorjdlrobson <jdlrobson@gmail.com>
Mon, 29 Sep 2014 23:45:45 +0000 (16:45 -0700)
committerjdlrobson <jdlrobson@gmail.com>
Tue, 30 Sep 2014 21:54:56 +0000 (14:54 -0700)
Stop littering MediaWiki with globals, provide a common
api for generating them similar to how we do text input
attributes before things get out of control.

Adds
* submitButton
* linkButton

Change-Id: I61bb3c358f755ed9f2153d94b744c1a9da02c456

includes/EditPage.php
includes/Html.php
includes/Preferences.php
includes/actions/HistoryAction.php
includes/specials/SpecialBooksources.php
includes/specials/SpecialCategories.php
includes/specials/SpecialContributions.php
includes/specials/SpecialSearch.php
includes/templates/Usercreate.php
includes/templates/Userlogin.php

index 8a55b1d..0674dcd 100644 (file)
@@ -3190,7 +3190,7 @@ HTML
        }
 
        protected function showStandardInputs( &$tabindex = 2 ) {
-               global $wgOut, $wgUseMediaWikiUIEverywhere;
+               global $wgOut;
                $wgOut->addHTML( "<div class='editOptions'>\n" );
 
                if ( $this->section != 'new' ) {
@@ -3222,10 +3222,8 @@ HTML
                        'target' => 'helpwindow',
                        'href' => $edithelpurl,
                );
-               if ( $wgUseMediaWikiUIEverywhere ) {
-                       $attrs['class'] = 'mw-ui-button mw-ui-quiet';
-               }
-               $edithelp = Html::element( 'a', $attrs, wfMessage( 'edithelp' )->text() ) .
+               $edithelp = Html::linkButton( wfMessage( 'edithelp' )->text(),
+                       $attrs, array( 'mw-ui-quiet' ) ) .
                        wfMessage( 'word-separator' )->escaped() .
                        wfMessage( 'newwindow' )->parse();
 
@@ -3268,20 +3266,16 @@ HTML
         * @return string
         */
        public function getCancelLink() {
-               global $wgUseMediaWikiUIEverywhere;
                $cancelParams = array();
                if ( !$this->isConflict && $this->oldid > 0 ) {
                        $cancelParams['oldid'] = $this->oldid;
                }
                $attrs = array( 'id' => 'mw-editform-cancel' );
-               if ( $wgUseMediaWikiUIEverywhere ) {
-                       $attrs['class'] = 'mw-ui-button mw-ui-quiet';
-               }
 
                return Linker::linkKnown(
                        $this->getContextTitle(),
                        wfMessage( 'cancel' )->parse(),
-                       $attrs,
+                       Html::buttonAttributes( $attrs, array( 'mw-ui-quiet' ) ),
                        $cancelParams
                );
        }
@@ -3750,47 +3744,33 @@ HTML
         * @return array
         */
        public function getEditButtons( &$tabindex ) {
-               global $wgUseMediaWikiUIEverywhere;
-
                $buttons = array();
 
                $attribs = array(
                        'id' => 'wpSave',
                        'name' => 'wpSave',
-                       'type' => 'submit',
                        'tabindex' => ++$tabindex,
-                       'value' => wfMessage( 'savearticle' )->text(),
                ) + Linker::tooltipAndAccesskeyAttribs( 'save' );
-               if ( $wgUseMediaWikiUIEverywhere ) {
-                       $attribs['class'] = 'mw-ui-button mw-ui-constructive';
-               }
-               $buttons['save'] = Xml::element( 'input', $attribs, '' );
+               $buttons['save'] = Html::submitButton( wfMessage( 'savearticle' )->text(),
+                       $attribs, array( 'mw-ui-constructive' ) );
 
                ++$tabindex; // use the same for preview and live preview
                $attribs = array(
                        'id' => 'wpPreview',
                        'name' => 'wpPreview',
-                       'type' => 'submit',
                        'tabindex' => $tabindex,
-                       'value' => wfMessage( 'showpreview' )->text(),
                ) + Linker::tooltipAndAccesskeyAttribs( 'preview' );
-               if ( $wgUseMediaWikiUIEverywhere ) {
-                       $attribs['class'] = 'mw-ui-button mw-ui-progressive';
-               }
-               $buttons['preview'] = Xml::element( 'input', $attribs, '' );
+               $buttons['preview'] = Html::submitButton( wfMessage( 'showpreview' )->text(),
+                       $attribs, array( 'mw-ui-progressive' ) );
                $buttons['live'] = '';
 
                $attribs = array(
                        'id' => 'wpDiff',
                        'name' => 'wpDiff',
-                       'type' => 'submit',
                        'tabindex' => ++$tabindex,
-                       'value' => wfMessage( 'showdiff' )->text(),
                ) + Linker::tooltipAndAccesskeyAttribs( 'diff' );
-               if ( $wgUseMediaWikiUIEverywhere ) {
-                       $attribs['class'] = 'mw-ui-button mw-ui-progressive';
-               }
-               $buttons['diff'] = Xml::element( 'input', $attribs, '' );
+               $buttons['diff'] = Html::submitButton( wfMessage( 'showdiff' )->text(),
+                       $attribs, array( 'mw-ui-progressive' ) );
 
                wfRunHooks( 'EditPageBeforeEditButtons', array( &$this, &$buttons, &$tabindex ) );
                return $buttons;
index 1e16e39..fc5916d 100644 (file)
@@ -101,6 +101,34 @@ class Html {
                'itemscope',
        );
 
+       /**
+        * Modifies a set of attributes meant for button elements
+        * and apply a set of default attributes when $wgUseMediaWikiUIEverywhere enabled.
+        * @param array $modifiers to add to the button
+        * @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
+        * @return array $attrs A modified attribute array
+        */
+       public static function buttonAttributes( $attrs, $modifiers = array() ) {
+               global $wgUseMediaWikiUIEverywhere;
+               if ( $wgUseMediaWikiUIEverywhere ) {
+                       if ( isset( $attrs['class'] ) ) {
+                               if ( is_array( $attrs['class'] ) ) {
+                                       $attrs['class'][] = 'mw-ui-button';
+                                       $attrs = array_merge( $attrs, $modifiers );
+                                       // ensure compatibility with Xml
+                                       $attrs['class'] = implode( ' ', $attrs['class'] );
+                               } else {
+                                       $attrs['class'] .= ' mw-ui-button ' . implode( ' ', $modifiers );
+                               }
+                       } else {
+                               $attrs['class'] = array( 'mw-ui-button' );
+                               // ensure compatibility with Xml
+                               $attrs['class'] = implode( ' ', array_merge( $attrs['class'], $modifiers ) );
+                       }
+               }
+               return $attrs;
+       }
+
        /**
         * Modifies a set of attributes meant for text input elements
         * and apply a set of default attributes.
@@ -130,6 +158,43 @@ class Html {
                return $attrs;
        }
 
+       /**
+        * Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enabled).
+        *
+        * @param string $contents The raw HTML contents of the element: *not*
+        *   escaped!
+        * @param array $attrs Associative array of attributes, e.g., array(
+        *   'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for
+        *   further documentation.
+        * @param array $modifiers to add to the button
+        * @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
+        * @return string Raw HTML
+        */
+       public static function linkButton( $contents, $attrs, $modifiers = array() ) {
+               return Html::element( 'a',
+                       self::buttonAttributes( $attrs, $modifiers ),
+                       $contents
+               );
+       }
+
+       /**
+        * Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enabled).
+        *
+        * @param string $contents The raw HTML contents of the element: *not*
+        *   escaped!
+        * @param array $attrs Associative array of attributes, e.g., array(
+        *   'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for
+        *   further documentation.
+        * @param array $modifiers to add to the button
+        * @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
+        * @return string Raw HTML
+        */
+       public static function submitButton( $contents, $attrs, $modifiers = array() ) {
+               $attrs['type'] = 'submit';
+               $attrs['value'] = $contents;
+               return Html::element( 'input', self::buttonAttributes( $attrs, $modifiers ) );
+       }
+
        /**
         * Returns an HTML element in a string.  The major advantage here over
         * manually typing out the HTML is that it will escape all attribute
index 8fa2f9a..582a4a5 100644 (file)
@@ -1518,12 +1518,8 @@ class PreferencesForm extends HTMLForm {
         * @return string
         */
        function getButtons() {
-               global $wgUseMediaWikiUIEverywhere;
 
                $attrs = array( 'id' => 'mw-prefs-restoreprefs' );
-               if ( $wgUseMediaWikiUIEverywhere ) {
-                       $attrs['class'] = 'mw-ui-button mw-ui-quiet';
-               }
 
                if ( !$this->getModifiedUser()->isAllowedAny( 'editmyprivateinfo', 'editmyoptions' ) ) {
                        return '';
@@ -1535,7 +1531,7 @@ class PreferencesForm extends HTMLForm {
                        $t = SpecialPage::getTitleFor( 'Preferences', 'reset' );
 
                        $html .= "\n" . Linker::link( $t, $this->msg( 'restoreprefs' )->escaped(),
-                               $attrs );
+                               Html::buttonAttributes( $attrs, array( 'mw-ui-quiet' ) ) );
 
                        $html = Xml::tags( 'div', array( 'class' => 'mw-prefs-buttons' ), $html );
                }
index 99006ed..dd0c94c 100644 (file)
@@ -182,9 +182,6 @@ class HistoryAction extends FormlessAction {
                // Add the general form
                $action = htmlspecialchars( wfScript() );
                $className = 'historysubmit mw-history-compareselectedversions-button';
-               if ( $config->get( 'UseMediaWikiUIEverywhere' ) ) {
-                       $className .= ' mw-ui-button mw-ui-progressive';
-               }
                $out->addHTML(
                        "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
                        Xml::fieldset(
@@ -200,9 +197,10 @@ class HistoryAction extends FormlessAction {
                        ) . '&#160;' .
                        ( $tagSelector ? ( implode( '&#160;', $tagSelector ) . '&#160;' ) : '' ) .
                        $checkDeleted .
-                       Xml::submitButton(
+                       Html::submitButton(
                                $this->msg( 'allpagessubmit' )->text(),
-                               array( 'class' => $className )
+                               $attrs,
+                               array( 'mw-ui-progressive' )
                        ) . "\n" .
                        '</fieldset></form>'
                );
@@ -492,12 +490,10 @@ class HistoryPager extends ReverseChronologicalPager {
                // Button container stored in $this->buttons for re-use in getEndBody()
                $this->buttons = '<div>';
                $className = 'historysubmit mw-history-compareselectedversions-button';
-               if ( $this->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
-                       $className .= ' mw-ui-button';
-               }
+               $attrs = array( 'class' => $className )
+                       + Linker::tooltipAndAccesskeyAttribs( 'compareselectedversions' );
                $this->buttons .= $this->submitButton( $this->msg( 'compareselectedversions' )->text(),
-                       array( 'class' => $className )
-                               + Linker::tooltipAndAccesskeyAttribs( 'compareselectedversions' )
+                       $attrs
                ) . "\n";
 
                if ( $this->getUser()->isAllowed( 'deleterevision' ) ) {
@@ -568,7 +564,7 @@ class HistoryPager extends ReverseChronologicalPager {
        function submitButton( $message, $attributes = array() ) {
                # Disable submit button if history has 1 revision only
                if ( $this->getNumRows() > 1 ) {
-                       return Xml::submitButton( $message, $attributes );
+                       return Html::submitButton( $message, $attributes );
                } else {
                        return '';
                }
index e6750e1..7395b8e 100644 (file)
@@ -134,16 +134,10 @@ class SpecialBookSources extends SpecialPage {
                        array( 'autofocus' => true, 'class' => 'mw-ui-input-inline' )
                );
 
-               if ( $this->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
-                       $form .= '&#160;' . Xml::submitButton(
-                               $this->msg( 'booksources-search' )->text(),
-                               array( 'class' => 'mw-ui-button mw-ui-progressive' )
-                       ) . "</p>\n";
-               } else {
-                       $form .= '&#160;' . Xml::submitButton(
-                               $this->msg( 'booksources-search' )->text()
-                       ) . "</p>\n";
-               }
+               $form .= '&#160;' . Html::submitButton(
+                       $this->msg( 'booksources-search' )->text(),
+                       array(), array( 'mw-ui-progressive' )
+               ) . "</p>\n";
 
                $form .= Html::closeElement( 'form' ) . "\n";
                $form .= Html::closeElement( 'fieldset' ) . "\n";
index bf6bb35..3a13b7e 100644 (file)
@@ -180,10 +180,6 @@ class CategoryPager extends AlphabeticPager {
        }
 
        public function getStartForm( $from ) {
-               $submitClassName = '';
-               if ( $this->getConfig( 'UseMediaWikiUIEverywhere' ) ) {
-                       $submitClassName = 'mw-ui-button mw-ui-progressive';
-               }
                return Xml::tags(
                        'form',
                        array( 'method' => 'get', 'action' => wfScript() ),
@@ -194,9 +190,9 @@ class CategoryPager extends AlphabeticPager {
                                                $this->msg( 'categoriesfrom' )->text(),
                                                'from', 'from', 20, $from, array( 'class' => 'mw-ui-input-inline' ) ) .
                                                ' ' .
-                                               Xml::submitButton(
+                                               Html::submitButton(
                                                        $this->msg( 'allpagessubmit' )->text(),
-                                                       array( 'class' => $submitClassName )
+                                                       array(), array( 'mw-ui-progressive' )
                                                )
                                )
                );
index 4b4f545..5631c3a 100644 (file)
@@ -603,19 +603,14 @@ class SpecialContributions extends IncludableSpecialPage {
                        $deletedOnlyCheck . $checkLabelTopOnly . $checkLabelNewOnly
                );
 
-               $className = 'mw-submit';
-               if ( $this->getConfig( 'UseMediaWikiUIEverywhere') ) {
-                       $className .= ' mw-ui-button mw-ui-progressive';
-               }
-
                $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ),
                        Xml::dateMenu(
                                $this->opts['year'] === '' ? MWTimestamp::getInstance()->format( 'Y' ) : $this->opts['year'],
                                $this->opts['month']
                        ) . ' ' .
-                               Xml::submitButton(
+                               Html::submitButton(
                                        $this->msg( 'sp-contributions-submit' )->text(),
-                                       array( 'class' => $className )
+                                       array( 'class' => 'mw-submit' ), array( 'mw-ui-progressive' )
                                )
                );
 
index adc248e..81dc77f 100644 (file)
@@ -1068,9 +1068,9 @@ class SpecialSearch extends SpecialPage {
                        'class' => 'mw-ui-input mw-ui-input-inline',
                ) ) . "\n";
                $out .= Html::hidden( 'fulltext', 'Search' ) . "\n";
-               $out .= Xml::submitButton(
+               $out .= Html::submitButton(
                        $this->msg( 'searchbutton' )->text(),
-                       array( 'class' => array( 'mw-ui-button', 'mw-ui-progressive' ) )
+                       array(), array( 'mw-ui-progressive' )
                ) . "\n";
 
                // Results-info
index 01da0bd..d435615 100644 (file)
@@ -254,14 +254,16 @@ class UsercreateTemplate extends BaseTemplate {
                        ?>
                        <div class="mw-ui-vform-field mw-submit">
                                <?php
-                               echo Html::input(
-                                       'wpCreateaccount',
+                               echo Html::submitButton(
                                        $this->getMsg( $this->data['loggedin'] ? 'createacct-another-submit' : 'createacct-submit' ),
-                                       'submit',
-                                       array(
-                                               'class' => "mw-ui-button mw-ui-big mw-ui-block mw-ui-constructive",
+                                       $attrs = array(
                                                'id' => 'wpCreateaccount',
                                                'tabindex' => $tabIndex++
+                                       ),
+                                       array(
+                                               'mw-ui-big',
+                                               'mw-ui-block',
+                                               'mw-ui-constructive',
                                        )
                                );
                                ?>
index 8bba426..99fe218 100644 (file)
@@ -148,11 +148,14 @@ class UserloginTemplate extends BaseTemplate {
 
                        <div class="mw-ui-vform-field">
                                <?php
-                               echo Html::input( 'wpLoginAttempt', $this->getMsg( 'pt-login-button' )->text(), 'submit', array(
+                               $attrs = array(
                                        'id' => 'wpLoginAttempt',
                                        'tabindex' => '6',
-                                       'class' => 'mw-ui-button mw-ui-big mw-ui-block mw-ui-constructive'
-                               ) );
+                               );
+                               $modifiers = array(
+                                       'mw-ui-big', 'mw-ui-block', 'mw-ui-constructive',
+                               );
+                               echo Html::submitButton( $this->getMsg( 'pt-login-button' )->text(), $attrs, $modifiers );
                                ?>
                        </div>