phpcs: Pass ResourceLoader.php
[lhc/web/wiklou.git] / includes / Xml.php
index 66d9cb5..159f711 100644 (file)
@@ -32,11 +32,13 @@ class Xml {
         *
         * @param string $element Element name
         * @param array $attribs Name=>value pairs. Values will be escaped.
-        * @param string $contents NULL to make an open tag only; '' for a contentless closed tag (default)
+        * @param string $contents Null to make an open tag only; '' for a contentless closed tag (default)
         * @param bool $allowShortTag Whether '' in $contents will result in a contentless closed tag
         * @return string
         */
-       public static function element( $element, $attribs = null, $contents = '', $allowShortTag = true ) {
+       public static function element( $element, $attribs = null, $contents = '',
+               $allowShortTag = true
+       ) {
                $out = '<' . $element;
                if ( !is_null( $attribs ) ) {
                        $out .= self::expandAttributes( $attribs );
@@ -83,7 +85,7 @@ class Xml {
         *
         * @param string $element
         * @param array $attribs Name=>value pairs. Values will be escaped.
-        * @param string $contents NULL to make an open tag only; '' for a contentless closed tag (default)
+        * @param string $contents Null to make an open tag only; '' for a contentless closed tag (default)
         * @return string
         */
        public static function elementClean( $element, $attribs = array(), $contents = '' ) {
@@ -132,34 +134,12 @@ class Xml {
                return self::openElement( $element, $attribs ) . $contents . "</$element>";
        }
 
-       /**
-        * Build a drop-down box for selecting a namespace
-        *
-        * @param string $selected Namespace which should be pre-selected
-        * @param string|null $all Value of an item denoting all namespaces, or null to omit
-        * @param string $element_name Value of the "name" attribute of the select tag
-        * @param string $label Optional label to add to the field
-        * @return string
-        * @deprecated since 1.19
-        */
-       public static function namespaceSelector( $selected = '', $all = null, $element_name = 'namespace', $label = null ) {
-               wfDeprecated( __METHOD__, '1.19' );
-               return Html::namespaceSelector( array(
-                       'selected' => $selected,
-                       'all' => $all,
-                       'label' => $label,
-               ), array(
-                       'name' => $element_name,
-                       'id' => 'namespace',
-                       'class' => 'namespaceselector',
-               ) );
-       }
-
        /**
         * Create a date selector
         *
-        * @param string $selected The month which should be selected, default ''
-        * @param string $allmonths Value of a special item denoting all month. Null to not include (default)
+        * @param string $selected The month which should be selected, default ''.
+        * @param string $allmonths Value of a special item denoting all month.
+        *   Null to not include (default).
         * @param string $id Element identifier
         * @return string Html string containing the month selector
         */
@@ -170,12 +150,20 @@ class Xml {
                        $selected = '';
                }
                if ( !is_null( $allmonths ) ) {
-                       $options[] = self::option( wfMessage( 'monthsall' )->text(), $allmonths, $selected === $allmonths );
+                       $options[] = self::option(
+                               wfMessage( 'monthsall' )->text(),
+                               $allmonths,
+                               $selected === $allmonths
+                       );
                }
                for ( $i = 1; $i < 13; $i++ ) {
                        $options[] = self::option( $wgLang->getMonthName( $i ), $i, $selected === $i );
                }
-               return self::openElement( 'select', array( 'id' => $id, 'name' => 'month', 'class' => 'mw-month-selector' ) )
+               return self::openElement( 'select', array(
+                       'id' => $id,
+                       'name' => 'month',
+                       'class' => 'mw-month-selector'
+               ) )
                        . implode( "\n", $options )
                        . self::closeElement( 'select' );
        }
@@ -220,9 +208,11 @@ class Xml {
         * @param string $inLanguage The ISO code of the language to display the select list in (optional)
         * @param array $overrideAttrs Override the attributes of the select tag (since 1.20)
         * @param Message|null $msg Label message key (since 1.20)
-        * @return array containing 2 items: label HTML and select list HTML
+        * @return array Array containing 2 items: label HTML and select list HTML
         */
-       public static function languageSelector( $selected, $customisedOnly = true, $inLanguage = null, $overrideAttrs = array(), Message $msg = null ) {
+       public static function languageSelector( $selected, $customisedOnly = true,
+               $inLanguage = null, $overrideAttrs = array(), Message $msg = null
+       ) {
                global $wgLanguageCode;
 
                $include = $customisedOnly ? 'mwfile' : 'mw';
@@ -302,7 +292,8 @@ class Xml {
                        $attributes['value'] = $value;
                }
 
-               return self::element( 'input', $attributes + $attribs );
+               return self::element( 'input',
+                       Html::getTextInputAttributes( $attributes + $attribs ) );
        }
 
        /**
@@ -313,8 +304,11 @@ class Xml {
         * @param array $attribs Other attributes
         * @return string HTML
         */
-       public static function password( $name, $size = false, $value = false, $attribs = array() ) {
-               return self::input( $name, $size, $value, array_merge( $attribs, array( 'type' => 'password' ) ) );
+       public static function password( $name, $size = false, $value = false,
+               $attribs = array()
+       ) {
+               return self::input( $name, $size, $value,
+                       array_merge( $attribs, array( 'type' => 'password' ) ) );
        }
 
        /**
@@ -395,7 +389,9 @@ class Xml {
         * @param array $attribs Other attributes
         * @return string HTML
         */
-       public static function inputLabel( $label, $name, $id, $size = false, $value = false, $attribs = array() ) {
+       public static function inputLabel( $label, $name, $id, $size = false,
+               $value = false, $attribs = array()
+       ) {
                list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
                return $label . '&#160;' . $input;
        }
@@ -412,7 +408,9 @@ class Xml {
         *
         * @return array
         */
-       public static function inputLabelSep( $label, $name, $id, $size = false, $value = false, $attribs = array() ) {
+       public static function inputLabelSep( $label, $name, $id, $size = false,
+               $value = false, $attribs = array()
+       ) {
                return array(
                        Xml::label( $label, $id, $attribs ),
                        self::input( $name, $size, $value, array( 'id' => $id ) + $attribs )
@@ -431,9 +429,16 @@ class Xml {
         * @return string HTML
         */
        public static function checkLabel( $label, $name, $id, $checked = false, $attribs = array() ) {
-               return self::check( $name, $checked, array( 'id' => $id ) + $attribs ) .
+               global $wgUseMediaWikiUIEverywhere;
+               $chkLabel = self::check( $name, $checked, array( 'id' => $id ) + $attribs ) .
                        '&#160;' .
                        self::label( $label, $id, $attribs );
+
+               if ( $wgUseMediaWikiUIEverywhere ) {
+                       $chkLabel = self::openElement( 'div', array( 'class' => 'mw-ui-checkbox' ) ) .
+                               $chkLabel . self::closeElement( 'div' );
+               }
+               return $chkLabel;
        }
 
        /**
@@ -448,7 +453,9 @@ class Xml {
         *
         * @return string HTML
         */
-       public static function radioLabel( $label, $name, $value, $id, $checked = false, $attribs = array() ) {
+       public static function radioLabel( $label, $name, $value, $id,
+               $checked = false, $attribs = array()
+       ) {
                return self::radio( $name, $value, $checked, array( 'id' => $id ) + $attribs ) .
                        '&#160;' .
                        self::label( $label, $id, $attribs );
@@ -456,12 +463,26 @@ class Xml {
 
        /**
         * Convenience function to build an HTML submit button
+        * When $wgUseMediaWikiUIEverywhere is true it will default to a constructive button
         * @param string $value Label text for the button
         * @param array $attribs Optional custom attributes
         * @return string HTML
         */
        public static function submitButton( $value, $attribs = array() ) {
-               return Html::element( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs );
+               global $wgUseMediaWikiUIEverywhere;
+               $baseAttrs = array(
+                       'type' => 'submit',
+                       'value' => $value,
+               );
+               // Done conditionally for time being as it is possible
+               // some submit forms
+               // might need to be mw-ui-destructive (e.g. delete a page)
+               if ( $wgUseMediaWikiUIEverywhere ) {
+                       $baseAttrs['class'] = 'mw-ui-button mw-ui-constructive';
+               }
+               // Any custom attributes will take precendence of anything in baseAttrs e.g. override the class
+               $attribs = $attribs + $baseAttrs;
+               return Html::element( 'input', $attribs );
        }
 
        /**
@@ -487,14 +508,17 @@ class Xml {
         * Build a drop-down box from a textual list.
         *
         * @param string $name Name and id for the drop-down
-        * @param string $list Correctly formatted text (newline delimited) to be used to generate the options
+        * @param string $list Correctly formatted text (newline delimited) to be
+        *   used to generate the options.
         * @param string $other Text for the "Other reasons" option
         * @param string $selected Option which should be pre-selected
         * @param string $class CSS classes for the drop-down
         * @param int $tabindex Value of the tabindex attribute
         * @return string
         */
-       public static function listDropDown( $name = '', $list = '', $other = '', $selected = '', $class = '', $tabindex = null ) {
+       public static function listDropDown( $name = '', $list = '', $other = '',
+               $selected = '', $class = '', $tabindex = null
+       ) {
                $optgroup = false;
 
                $options = self::option( $other, 'other', $selected === 'other' );
@@ -554,8 +578,10 @@ class Xml {
        /**
         * Shortcut for creating fieldsets.
         *
-        * @param string|bool $legend Legend of the fieldset. If evaluates to false, legend is not added.
-        * @param string $content Pre-escaped content for the fieldset. If false, only open fieldset is returned.
+        * @param string|bool $legend Legend of the fieldset. If evaluates to false,
+        *   legend is not added.
+        * @param string $content Pre-escaped content for the fieldset. If false,
+        *   only open fieldset is returned.
         * @param array $attribs Any attributes to fieldset-element.
         *
         * @return string
@@ -588,12 +614,14 @@ class Xml {
         */
        public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = array() ) {
                return self::element( 'textarea',
-                                       array(
-                                               'name' => $name,
-                                               'id' => $name,
-                                               'cols' => $cols,
-                                               'rows' => $rows
-                                       ) + $attribs,
+                                       Html::getTextInputAttributes(
+                                               array(
+                                                       'name' => $name,
+                                                       'id' => $name,
+                                                       'cols' => $cols,
+                                                       'rows' => $rows
+                                               ) + $attribs
+                                       ),
                                        $content, false );
        }
 
@@ -740,8 +768,11 @@ class Xml {
        /**
         * Generate a form (without the opening form element).
         * Output optionally includes a submit button.
-        * @param array $fields Associative array, key is the name of a message that contains a description for the field, value is an HTML string containing the appropriate input.
-        * @param string $submitLabel The name of a message containing a label for the submit button.
+        * @param array $fields Associative array, key is the name of a message that
+        *   contains a description for the field, value is an HTML string
+        *   containing the appropriate input.
+        * @param string $submitLabel The name of a message containing a label for
+        *   the submit button.
         * @param array $submitAttribs The attributes to add to the submit button
         * @return string HTML form.
         */
@@ -758,14 +789,17 @@ class Xml {
                        // the input somehow.
 
                        $form .= Xml::tags( 'td', array( 'class' => 'mw-label' ), wfMessage( $labelmsg )->parse() );
-                       $form .= Xml::openElement( 'td', array( 'class' => 'mw-input' ) ) . $input . Xml::closeElement( 'td' );
+                       $form .= Xml::openElement( 'td', array( 'class' => 'mw-input' ) )
+                               . $input . Xml::closeElement( 'td' );
                        $form .= Xml::closeElement( 'tr' );
                }
 
                if ( $submitLabel ) {
                        $form .= Xml::openElement( 'tr' );
                        $form .= Xml::tags( 'td', array(), '' );
-                       $form .= Xml::openElement( 'td', array( 'class' => 'mw-submit' ) ) . Xml::submitButton( wfMessage( $submitLabel )->text(), $submitAttribs ) . Xml::closeElement( 'td' );
+                       $form .= Xml::openElement( 'td', array( 'class' => 'mw-submit' ) )
+                               . Xml::submitButton( wfMessage( $submitLabel )->text(), $submitAttribs )
+                               . Xml::closeElement( 'td' );
                        $form .= Xml::closeElement( 'tr' );
                }