Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / Html.php
index 506b9cc..890beb0 100644 (file)
@@ -49,7 +49,7 @@
  */
 class Html {
        // List of void elements from HTML5, section 8.1.2 as of 2011-08-12
-       private static $voidElements = array(
+       private static $voidElements = [
                'area',
                'base',
                'br',
@@ -66,11 +66,11 @@ class Html {
                'source',
                'track',
                'wbr',
-       );
+       ];
 
        // Boolean attributes, which may have the value omitted entirely.  Manually
        // collected from the HTML5 spec as of 2011-08-12.
-       private static $boolAttribs = array(
+       private static $boolAttribs = [
                'async',
                'autofocus',
                'autoplay',
@@ -99,7 +99,7 @@ class Html {
                'typemustmatch',
                // HTML5 Microdata
                'itemscope',
-       );
+       ];
 
        /**
         * Modifies a set of attributes meant for button elements
@@ -109,7 +109,7 @@ class Html {
         * @see https://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
         * @return array $attrs A modified attribute array
         */
-       public static function buttonAttributes( array $attrs, array $modifiers = array() ) {
+       public static function buttonAttributes( array $attrs, array $modifiers = [] ) {
                global $wgUseMediaWikiUIEverywhere;
                if ( $wgUseMediaWikiUIEverywhere ) {
                        if ( isset( $attrs['class'] ) ) {
@@ -165,7 +165,7 @@ class Html {
         * @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
         * @return string Raw HTML
         */
-       public static function linkButton( $contents, array $attrs, array $modifiers = array() ) {
+       public static function linkButton( $contents, array $attrs, array $modifiers = [] ) {
                return self::element( 'a',
                        self::buttonAttributes( $attrs, $modifiers ),
                        $contents
@@ -185,7 +185,7 @@ class Html {
         * @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
         * @return string Raw HTML
         */
-       public static function submitButton( $contents, array $attrs, array $modifiers = array() ) {
+       public static function submitButton( $contents, array $attrs, array $modifiers = [] ) {
                $attrs['type'] = 'submit';
                $attrs['value'] = $contents;
                return self::element( 'input', self::buttonAttributes( $attrs, $modifiers ) );
@@ -210,13 +210,13 @@ class Html {
         *   escaped!
         * @return string Raw HTML
         */
-       public static function rawElement( $element, $attribs = array(), $contents = '' ) {
+       public static function rawElement( $element, $attribs = [], $contents = '' ) {
                global $wgWellFormedXml;
                $start = self::openElement( $element, $attribs );
                if ( in_array( $element, self::$voidElements ) ) {
                        if ( $wgWellFormedXml ) {
                                // Silly XML.
-                               return substr( $start, 0, -1 ) . ' />';
+                               return substr( $start, 0, -1 ) . '/>';
                        }
                        return $start;
                } else {
@@ -234,13 +234,13 @@ class Html {
         *
         * @return string
         */
-       public static function element( $element, $attribs = array(), $contents = '' ) {
-               return self::rawElement( $element, $attribs, strtr( $contents, array(
+       public static function element( $element, $attribs = [], $contents = '' ) {
+               return self::rawElement( $element, $attribs, strtr( $contents, [
                        // There's no point in escaping quotes, >, etc. in the contents of
                        // elements.
                        '&' => '&',
                        '<' => '&lt;'
-               ) ) );
+               ] ) );
        }
 
        /**
@@ -252,7 +252,7 @@ class Html {
         *
         * @return string
         */
-       public static function openElement( $element, $attribs = array() ) {
+       public static function openElement( $element, $attribs = [] ) {
                $attribs = (array)$attribs;
                // This is not required in HTML5, but let's do it anyway, for
                // consistency and better compression.
@@ -260,7 +260,7 @@ class Html {
 
                // Remove invalid input types
                if ( $element == 'input' ) {
-                       $validTypes = array(
+                       $validTypes = [
                                'hidden',
                                'text',
                                'password',
@@ -286,7 +286,7 @@ class Html {
                                'search',
                                'tel',
                                'color',
-                       );
+                       ];
                        if ( isset( $attribs['type'] ) && !in_array( $attribs['type'], $validTypes ) ) {
                                unset( $attribs['type'] );
                        }
@@ -336,38 +336,36 @@ class Html {
        private static function dropDefaults( $element, array $attribs ) {
                // Whenever altering this array, please provide a covering test case
                // in HtmlTest::provideElementsWithAttributesHavingDefaultValues
-               static $attribDefaults = array(
-                       'area' => array( 'shape' => 'rect' ),
-                       'button' => array(
+               static $attribDefaults = [
+                       'area' => [ 'shape' => 'rect' ],
+                       'button' => [
                                'formaction' => 'GET',
                                'formenctype' => 'application/x-www-form-urlencoded',
-                       ),
-                       'canvas' => array(
+                       ],
+                       'canvas' => [
                                'height' => '150',
                                'width' => '300',
-                       ),
-                       'command' => array( 'type' => 'command' ),
-                       'form' => array(
+                       ],
+                       'command' => [ 'type' => 'command' ],
+                       'form' => [
                                'action' => 'GET',
                                'autocomplete' => 'on',
                                'enctype' => 'application/x-www-form-urlencoded',
-                       ),
-                       'input' => array(
+                       ],
+                       'input' => [
                                'formaction' => 'GET',
                                'type' => 'text',
-                       ),
-                       'keygen' => array( 'keytype' => 'rsa' ),
-                       'link' => array( 'media' => 'all' ),
-                       'menu' => array( 'type' => 'list' ),
-                       // Note: the use of text/javascript here instead of other JavaScript
-                       // MIME types follows the HTML5 spec.
-                       'script' => array( 'type' => 'text/javascript' ),
-                       'style' => array(
+                       ],
+                       'keygen' => [ 'keytype' => 'rsa' ],
+                       'link' => [ 'media' => 'all' ],
+                       'menu' => [ 'type' => 'list' ],
+                       'script' => [ 'type' => 'text/javascript' ],
+                       'style' => [
                                'media' => 'all',
                                'type' => 'text/css',
-                       ),
-                       'textarea' => array( 'wrap' => 'soft' ),
-               );
+                       ],
+                       'textarea' => [ 'wrap' => 'soft' ],
+               ];
 
                $element = strtolower( $element );
 
@@ -511,21 +509,21 @@ class Html {
                        // numbers to be entered in 'type="number"' fields, allow
                        // the special case 'step="any"'.
 
-                       if ( in_array( $key, array( 'max', 'min', 'pattern', 'required' ) )
+                       if ( in_array( $key, [ 'max', 'min', 'pattern', 'required' ] )
                                || $key === 'step' && $value !== 'any' ) {
                                continue;
                        }
 
                        // http://www.w3.org/TR/html401/index/attributes.html ("space-separated")
                        // http://www.w3.org/TR/html5/index.html#attributes-1 ("space-separated")
-                       $spaceSeparatedListAttributes = array(
+                       $spaceSeparatedListAttributes = [
                                'class', // html4, html5
                                'accesskey', // as of html5, multiple space-separated values allowed
                                // html4-spec doesn't document rel= as space-separated
                                // but has been used like that and is now documented as such
                                // in the html5-spec.
                                'rel',
-                       );
+                       ];
 
                        // Specific features for attributes that allow a list of space-separated values
                        if ( in_array( $key, $spaceSeparatedListAttributes ) ) {
@@ -535,7 +533,7 @@ class Html {
                                // values. Implode/explode to get those into the main array as well.
                                if ( is_array( $value ) ) {
                                        // If input wasn't an array, we can skip this step
-                                       $newValue = array();
+                                       $newValue = [];
                                        foreach ( $value as $k => $v ) {
                                                if ( is_string( $v ) ) {
                                                        // String values should be normal `array( 'foo' )`
@@ -558,7 +556,7 @@ class Html {
 
                                // Normalize spacing by fixing up cases where people used
                                // more than 1 space and/or a trailing/leading space
-                               $value = array_diff( $value, array( '', ' ' ) );
+                               $value = array_diff( $value, [ '', ' ' ] );
 
                                // Remove duplicates and create the string
                                $value = implode( ' ', array_unique( $value ) );
@@ -603,14 +601,14 @@ class Html {
                                // The only difference between this transform and the one by
                                // Sanitizer::encodeAttribute() is '<' is only encoded here if
                                // $wgWellFormedXml is set, and ' is not encoded.
-                               $map = array(
+                               $map = [
                                        '&' => '&amp;',
                                        '"' => '&quot;',
                                        '>' => '&gt;',
                                        "\n" => '&#10;',
                                        "\r" => '&#13;',
                                        "\t" => '&#9;'
-                               );
+                               ];
                                if ( $wgWellFormedXml ) {
                                        // This is allowed per spec: <http://www.w3.org/TR/xml/#NT-AttValue>
                                        // But reportedly it breaks some XML tools?
@@ -635,7 +633,7 @@ class Html {
        public static function inlineScript( $contents ) {
                global $wgWellFormedXml;
 
-               $attrs = array();
+               $attrs = [];
 
                if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) {
                        $contents = "/*<![CDATA[*/$contents/*]]>*/";
@@ -652,7 +650,7 @@ class Html {
         * @return string Raw HTML
         */
        public static function linkedScript( $url ) {
-               $attrs = array( 'src' => $url );
+               $attrs = [ 'src' => $url ];
 
                return self::element( 'script', $attrs );
        }
@@ -673,10 +671,9 @@ class Html {
                        $contents = "/*<![CDATA[*/$contents/*]]>*/";
                }
 
-               return self::rawElement( 'style', array(
-                       'type' => 'text/css',
+               return self::rawElement( 'style', [
                        'media' => $media,
-               ), $contents );
+               ], $contents );
        }
 
        /**
@@ -688,12 +685,11 @@ class Html {
         * @return string Raw HTML
         */
        public static function linkedStyle( $url, $media = 'all' ) {
-               return self::element( 'link', array(
+               return self::element( 'link', [
                        'rel' => 'stylesheet',
                        'href' => $url,
-                       'type' => 'text/css',
                        'media' => $media,
-               ) );
+               ] );
        }
 
        /**
@@ -707,14 +703,14 @@ class Html {
         *   attributes, passed to Html::element()
         * @return string Raw HTML
         */
-       public static function input( $name, $value = '', $type = 'text', array $attribs = array() ) {
+       public static function input( $name, $value = '', $type = 'text', array $attribs = [] ) {
                $attribs['type'] = $type;
                $attribs['value'] = $value;
                $attribs['name'] = $name;
-               if ( in_array( $type, array( 'text', 'search', 'email', 'password', 'number' ) ) ) {
+               if ( in_array( $type, [ 'text', 'search', 'email', 'password', 'number' ] ) ) {
                        $attribs = self::getTextInputAttributes( $attribs );
                }
-               if ( in_array( $type, array( 'button', 'reset', 'submit' ) ) ) {
+               if ( in_array( $type, [ 'button', 'reset', 'submit' ] ) ) {
                        $attribs = self::buttonAttributes( $attribs );
                }
                return self::element( 'input', $attribs );
@@ -728,7 +724,7 @@ class Html {
         * @param array $attribs Array of additional attributes
         * @return string Raw HTML
         */
-       public static function check( $name, $checked = false, array $attribs = array() ) {
+       public static function check( $name, $checked = false, array $attribs = [] ) {
                if ( isset( $attribs['value'] ) ) {
                        $value = $attribs['value'];
                        unset( $attribs['value'] );
@@ -744,14 +740,14 @@ class Html {
        }
 
        /**
-        * Convenience function to produce a checkbox (input element with type=checkbox)
+        * Convenience function to produce a radio button (input element with type=radio)
         *
         * @param string $name Name attribute
-        * @param bool $checked Whether the checkbox is checked or not
+        * @param bool $checked Whether the radio button is checked or not
         * @param array $attribs Array of additional attributes
         * @return string Raw HTML
         */
-       public static function radio( $name, $checked = false, array $attribs = array() ) {
+       public static function radio( $name, $checked = false, array $attribs = [] ) {
                if ( isset( $attribs['value'] ) ) {
                        $value = $attribs['value'];
                        unset( $attribs['value'] );
@@ -774,10 +770,10 @@ class Html {
         * @param array $attribs Additional attributes
         * @return string Raw HTML
         */
-       public static function label( $label, $id, array $attribs = array() ) {
-               $attribs += array(
+       public static function label( $label, $id, array $attribs = [] ) {
+               $attribs += [
                        'for' => $id
-               );
+               ];
                return self::element( 'label', $attribs, $label );
        }
 
@@ -790,7 +786,7 @@ class Html {
         *   attributes, passed to Html::element()
         * @return string Raw HTML
         */
-       public static function hidden( $name, $value, array $attribs = array() ) {
+       public static function hidden( $name, $value, array $attribs = [] ) {
                return self::input( $name, $value, 'hidden', $attribs );
        }
 
@@ -806,7 +802,7 @@ class Html {
         *   attributes, passed to Html::element()
         * @return string Raw HTML
         */
-       public static function textarea( $name, $value = '', array $attribs = array() ) {
+       public static function textarea( $name, $value = '', array $attribs = [] ) {
                $attribs['name'] = $name;
 
                if ( substr( $value, 0, 1 ) == "\n" ) {
@@ -826,13 +822,13 @@ class Html {
         * @param array $params See Html::namespaceSelector()
         * @return array
         */
-       public static function namespaceSelectorOptions( array $params = array() ) {
+       public static function namespaceSelectorOptions( array $params = [] ) {
                global $wgContLang;
 
-               $options = array();
+               $options = [];
 
                if ( !isset( $params['exclude'] ) || !is_array( $params['exclude'] ) ) {
-                       $params['exclude'] = array();
+                       $params['exclude'] = [];
                }
 
                if ( isset( $params['all'] ) ) {
@@ -843,7 +839,7 @@ class Html {
                // Add all namespaces as options (in the content language)
                $options += $wgContLang->getFormattedNamespaces();
 
-               $optionsOut = array();
+               $optionsOut = [];
                // Filter out namespaces below 0 and massage labels
                foreach ( $options as $nsId => $nsName ) {
                        if ( $nsId < NS_MAIN || in_array( $nsId, $params['exclude'] ) ) {
@@ -878,8 +874,8 @@ class Html {
         * - name: [optional], default: 'namespace'.
         * @return string HTML code to select a namespace.
         */
-       public static function namespaceSelector( array $params = array(),
-               array $selectAttribs = array()
+       public static function namespaceSelector( array $params = [],
+               array $selectAttribs = []
        ) {
                ksort( $selectAttribs );
 
@@ -898,21 +894,21 @@ class Html {
                }
 
                if ( !isset( $params['disable'] ) || !is_array( $params['disable'] ) ) {
-                       $params['disable'] = array();
+                       $params['disable'] = [];
                }
 
                // Associative array between option-values and option-labels
                $options = self::namespaceSelectorOptions( $params );
 
                // Convert $options to HTML
-               $optionsHtml = array();
+               $optionsHtml = [];
                foreach ( $options as $nsId => $nsName ) {
                        $optionsHtml[] = self::element(
-                               'option', array(
+                               'option', [
                                        'disabled' => in_array( $nsId, $params['disable'] ),
                                        'value' => $nsId,
                                        'selected' => $nsId === $params['selected'],
-                               ), $nsName
+                               ], $nsName
                        );
                }
 
@@ -927,9 +923,9 @@ class Html {
                $ret = '';
                if ( isset( $params['label'] ) ) {
                        $ret .= self::element(
-                               'label', array(
+                               'label', [
                                        'for' => isset( $selectAttribs['id'] ) ? $selectAttribs['id'] : null,
-                               ), $params['label']
+                               ], $params['label']
                        ) . '&#160;';
                }
 
@@ -951,7 +947,7 @@ class Html {
         *   attributes, passed to Html::element() of html tag.
         * @return string Raw HTML
         */
-       public static function htmlHeader( array $attribs = array() ) {
+       public static function htmlHeader( array $attribs = [] ) {
                $ret = '';
 
                global $wgHtml5Version, $wgMimeType, $wgXhtmlNamespaces;
@@ -1015,25 +1011,25 @@ class Html {
         * @return string
         */
        static function infoBox( $text, $icon, $alt, $class = '' ) {
-               $s = self::openElement( 'div', array( 'class' => "mw-infobox $class" ) );
+               $s = self::openElement( 'div', [ 'class' => "mw-infobox $class" ] );
 
-               $s .= self::openElement( 'div', array( 'class' => 'mw-infobox-left' ) ) .
+               $s .= self::openElement( 'div', [ 'class' => 'mw-infobox-left' ] ) .
                                self::element( 'img',
-                                       array(
+                                       [
                                                'src' => $icon,
                                                'alt' => $alt,
-                                       )
+                                       ]
                                ) .
                                self::closeElement( 'div' );
 
-               $s .= self::openElement( 'div', array( 'class' => 'mw-infobox-right' ) ) .
+               $s .= self::openElement( 'div', [ 'class' => 'mw-infobox-right' ] ) .
                                $text .
                                self::closeElement( 'div' );
-               $s .= self::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
+               $s .= self::element( 'div', [ 'style' => 'clear: left;' ], ' ' );
 
                $s .= self::closeElement( 'div' );
 
-               $s .= self::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
+               $s .= self::element( 'div', [ 'style' => 'clear: left;' ], ' ' );
 
                return $s;
        }
@@ -1062,7 +1058,7 @@ class Html {
         * @return string
         */
        static function srcSet( array $urls ) {
-               $candidates = array();
+               $candidates = [];
                foreach ( $urls as $density => $url ) {
                        // Cast density to float to strip 'x'.
                        $candidates[] = $url . ' ' . (float)$density . 'x';