Move XmlSelect to its own file
authorGeoffrey Mon <geofbot@gmail.com>
Tue, 2 Jun 2015 23:43:45 +0000 (19:43 -0400)
committerLegoktm <legoktm.wikipedia@gmail.com>
Wed, 3 Jun 2015 03:26:01 +0000 (03:26 +0000)
Move the XmlSelect class to its own file to make it easier to find
and utilize.  Helps prevent the use of unnecessary
Html::openElement, Html::element, etc.

Bug: T93234
Change-Id: I66119a2d0eda15569de06c493a0ee302f21deb3f

autoload.php
includes/Xml.php
includes/XmlSelect.php [new file with mode: 0644]

index 460d874..b74e304 100644 (file)
@@ -1367,7 +1367,7 @@ $wgAutoloadLocalClasses = array(
        'Xml' => __DIR__ . '/includes/Xml.php',
        'XmlDumpWriter' => __DIR__ . '/includes/Export.php',
        'XmlJsCode' => __DIR__ . '/includes/Xml.php',
-       'XmlSelect' => __DIR__ . '/includes/Xml.php',
+       'XmlSelect' => __DIR__ . '/includes/XmlSelect.php',
        'XmlTypeCheck' => __DIR__ . '/includes/libs/XmlTypeCheck.php',
        'ZhConverter' => __DIR__ . '/languages/classes/LanguageZh.php',
        'ZipDirectoryReader' => __DIR__ . '/includes/utils/ZipDirectoryReader.php',
index f0bd70b..c356c6d 100644 (file)
@@ -871,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
diff --git a/includes/XmlSelect.php b/includes/XmlSelect.php
new file mode 100644 (file)
index 0000000..1cd04ae
--- /dev/null
@@ -0,0 +1,130 @@
+<?php
+/**
+ * Class to generate XML <select>.
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * Module of static functions for generating XML <select> elements
+ */
+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 ) );
+       }
+}