From 8007e25308a3084b0dcc3b0ede11604a652bda77 Mon Sep 17 00:00:00 2001 From: Geoffrey Mon Date: Tue, 2 Jun 2015 19:43:45 -0400 Subject: [PATCH 1/1] Move XmlSelect to its own file 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 | 2 +- includes/Xml.php | 106 --------------------------------- includes/XmlSelect.php | 130 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 107 deletions(-) create mode 100644 includes/XmlSelect.php diff --git a/autoload.php b/autoload.php index 460d8749b1..b74e304085 100644 --- a/autoload.php +++ b/autoload.php @@ -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', diff --git a/includes/Xml.php b/includes/Xml.php index f0bd70b23b..c356c6db61 100644 --- a/includes/Xml.php +++ b/includes/Xml.php @@ -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 index 0000000000..1cd04ae10e --- /dev/null +++ b/includes/XmlSelect.php @@ -0,0 +1,130 @@ +. + * + * 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