Remove patch-log_search-rename-index.sql from MW 1.16 updaters
[lhc/web/wiklou.git] / includes / XmlSelect.php
index 1cd04ae..89f2f41 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Class to generate XML <select>.
+ * Class for generating HTML <select> elements.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  */
 
 /**
- * Module of static functions for generating XML <select> elements
+ * Class for generating HTML <select> or <datalist> elements.
  */
 class XmlSelect {
-       protected $options = array();
+       protected $options = [];
        protected $default = false;
-       protected $attributes = array();
+       protected $tagName = 'select';
+       protected $attributes = [];
 
        public function __construct( $name = false, $id = false, $default = false ) {
                if ( $name ) {
@@ -43,15 +44,22 @@ class XmlSelect {
        }
 
        /**
-        * @param string $default
+        * @param string|array $default
         */
        public function setDefault( $default ) {
                $this->default = $default;
        }
 
+       /**
+        * @param string|array $tagName
+        */
+       public function setTagName( $tagName ) {
+               $this->tagName = $tagName;
+       }
+
        /**
         * @param string $name
-        * @param array $value
+        * @param string $value
         */
        public function setAttribute( $name, $value ) {
                $this->attributes[$name] = $value;
@@ -59,7 +67,7 @@ class XmlSelect {
 
        /**
         * @param string $name
-        * @return array|null
+        * @return string|null
         */
        public function getAttribute( $name ) {
                if ( isset( $this->attributes[$name] ) ) {
@@ -70,14 +78,12 @@ class XmlSelect {
        }
 
        /**
-        * @param string $name
-        * @param bool $value
+        * @param string $label
+        * @param string $value If not given, assumed equal to $label
         */
-       public function addOption( $name, $value = false ) {
-               // Stab stab stab
-               $value = $value !== false ? $value : $name;
-
-               $this->options[] = array( $name => $value );
+       public function addOption( $label, $value = false ) {
+               $value = $value !== false ? $value : $label;
+               $this->options[] = [ $label => $value ];
        }
 
        /**
@@ -92,12 +98,12 @@ class XmlSelect {
        }
 
        /**
-        * This accepts an array of form
+        * This accepts an array of form:
         * label => value
         * label => ( label => value, label => value )
         *
         * @param array $options
-        * @param bool $default
+        * @param string|array $default
         * @return string
         */
        static function formatOptions( $options, $default = false ) {
@@ -106,9 +112,13 @@ class XmlSelect {
                foreach ( $options as $label => $value ) {
                        if ( is_array( $value ) ) {
                                $contents = self::formatOptions( $value, $default );
-                               $data .= Html::rawElement( 'optgroup', array( 'label' => $label ), $contents ) . "\n";
+                               $data .= Html::rawElement( 'optgroup', [ 'label' => $label ], $contents ) . "\n";
                        } else {
-                               $data .= Xml::option( $label, $value, $value === $default ) . "\n";
+                               // If $default is an array, then the <select> probably has the multiple attribute,
+                               // so we should check if each $value is in $default, rather than checking if
+                               // $value is equal to $default.
+                               $selected = is_array( $default ) ? in_array( $value, $default ) : $value === $default;
+                               $data .= Xml::option( $label, $value, $selected ) . "\n";
                        }
                }
 
@@ -125,6 +135,6 @@ class XmlSelect {
                        $contents .= self::formatOptions( $options, $this->default );
                }
 
-               return Html::rawElement( 'select', $this->attributes, rtrim( $contents ) );
+               return Html::rawElement( $this->tagName, $this->attributes, rtrim( $contents ) );
        }
 }