Merge "language: Use item 'fallbackSequence' instead of duplicating logic"
[lhc/web/wiklou.git] / includes / Xml.php
index 7761ecc..c356c6d 100644 (file)
@@ -94,9 +94,7 @@ class Xml {
                        $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
                }
                if ( $contents ) {
-                       wfProfileIn( __METHOD__ . '-norm' );
                        $contents = $wgContLang->normalize( $contents );
-                       wfProfileOut( __METHOD__ . '-norm' );
                }
                return self::element( $element, $attribs, $contents );
        }
@@ -134,31 +132,6 @@ 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
         *
@@ -317,7 +290,8 @@ class Xml {
                        $attributes['value'] = $value;
                }
 
-               return self::element( 'input', $attributes + $attribs );
+               return self::element( 'input',
+                       Html::getTextInputAttributes( $attributes + $attribs ) );
        }
 
        /**
@@ -392,12 +366,10 @@ class Xml {
        public static function label( $label, $id, $attribs = array() ) {
                $a = array( 'for' => $id );
 
-               # FIXME avoid copy pasting below:
-               if ( isset( $attribs['class'] ) ) {
-                               $a['class'] = $attribs['class'];
-               }
-               if ( isset( $attribs['title'] ) ) {
-                               $a['title'] = $attribs['title'];
+               foreach ( array( 'class', 'title' ) as $attr ) {
+                       if ( isset( $attribs[$attr] ) ) {
+                               $a[$attr] = $attribs[$attr];
+                       }
                }
 
                return self::element( 'label', $a, $label );
@@ -453,9 +425,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;
        }
 
        /**
@@ -480,12 +459,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 );
        }
 
        /**
@@ -617,12 +610,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 );
        }
 
@@ -708,13 +703,15 @@ class Xml {
        /**
         * Check if a string is well-formed XML.
         * Must include the surrounding tag.
+        * This function is a DoS vector if an attacker can define
+        * entities in $text.
         *
         * @param string $text String to test.
         * @return bool
         *
         * @todo Error position reporting return
         */
-       public static function isWellFormed( $text ) {
+       private static function isWellFormed( $text ) {
                $parser = xml_parser_create( "UTF-8" );
 
                # case folding violates XML standard, turn it off
@@ -874,112 +871,6 @@ class Xml {
        }
 }
 
-class XmlSelect {
-       protected $options = array();
-       protected $default = false;
-       protected $attributes = array();
-
-       public function __construct( $name = false, $id = false, $default = false ) {
-               if ( $name ) {
-                       $this->setAttribute( 'name', $name );
-               }
-
-               if ( $id ) {
-                       $this->setAttribute( 'id', $id );
-               }
-
-               if ( $default !== false ) {
-                       $this->default = $default;
-               }
-       }
-
-       /**
-        * @param string $default
-        */
-       public function setDefault( $default ) {
-               $this->default = $default;
-       }
-
-       /**
-        * @param string $name
-        * @param array $value
-        */
-       public function setAttribute( $name, $value ) {
-               $this->attributes[$name] = $value;
-       }
-
-       /**
-        * @param string $name
-        * @return array|null
-        */
-       public function getAttribute( $name ) {
-               if ( isset( $this->attributes[$name] ) ) {
-                       return $this->attributes[$name];
-               } else {
-                       return null;
-               }
-       }
-
-       /**
-        * @param string $name
-        * @param bool $value
-        */
-       public function addOption( $name, $value = false ) {
-               // Stab stab stab
-               $value = $value !== false ? $value : $name;
-
-               $this->options[] = array( $name => $value );
-       }
-
-       /**
-        * This accepts an array of form
-        * label => value
-        * label => ( label => value, label => value )
-        *
-        * @param array $options
-        */
-       public function addOptions( $options ) {
-               $this->options[] = $options;
-       }
-
-       /**
-        * This accepts an array of form
-        * label => value
-        * label => ( label => value, label => value )
-        *
-        * @param array $options
-        * @param bool $default
-        * @return string
-        */
-       static function formatOptions( $options, $default = false ) {
-               $data = '';
-
-               foreach ( $options as $label => $value ) {
-                       if ( is_array( $value ) ) {
-                               $contents = self::formatOptions( $value, $default );
-                               $data .= Html::rawElement( 'optgroup', array( 'label' => $label ), $contents ) . "\n";
-                       } else {
-                               $data .= Xml::option( $label, $value, $value === $default ) . "\n";
-                       }
-               }
-
-               return $data;
-       }
-
-       /**
-        * @return string
-        */
-       public function getHTML() {
-               $contents = '';
-
-               foreach ( $this->options as $options ) {
-                       $contents .= self::formatOptions( $options, $this->default );
-               }
-
-               return Html::rawElement( 'select', $this->attributes, rtrim( $contents ) );
-       }
-}
-
 /**
  * A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to
  * interpret a given string as being a JavaScript expression, instead of string