2015-06-02 23:43:45 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2015-07-30 21:03:19 +00:00
|
|
|
* Class for generating HTML <select> elements.
|
2015-06-02 23:43:45 +00:00
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2024-05-16 10:52:03 +00:00
|
|
|
namespace MediaWiki\Xml;
|
|
|
|
|
|
2023-02-16 19:27:21 +00:00
|
|
|
use MediaWiki\Html\Html;
|
|
|
|
|
|
2015-06-02 23:43:45 +00:00
|
|
|
/**
|
2015-11-08 15:21:58 +00:00
|
|
|
* Class for generating HTML <select> or <datalist> elements.
|
2015-06-02 23:43:45 +00:00
|
|
|
*/
|
|
|
|
|
class XmlSelect {
|
2024-09-14 08:12:18 +00:00
|
|
|
/** @var array[] */
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $options = [];
|
2024-09-14 08:12:18 +00:00
|
|
|
/** @var string|array|false */
|
2015-06-02 23:43:45 +00:00
|
|
|
protected $default = false;
|
2024-09-14 08:12:18 +00:00
|
|
|
/** @var string|array */
|
2015-11-08 15:21:58 +00:00
|
|
|
protected $tagName = 'select';
|
2024-09-14 08:12:18 +00:00
|
|
|
/** @var (string|int)[] */
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $attributes = [];
|
2015-06-02 23:43:45 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-13 22:52:33 +00:00
|
|
|
* @param string|array $default
|
2015-06-02 23:43:45 +00:00
|
|
|
*/
|
|
|
|
|
public function setDefault( $default ) {
|
|
|
|
|
$this->default = $default;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-08 15:21:58 +00:00
|
|
|
/**
|
|
|
|
|
* @param string|array $tagName
|
|
|
|
|
*/
|
|
|
|
|
public function setTagName( $tagName ) {
|
|
|
|
|
$this->tagName = $tagName;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-02 23:43:45 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
2022-03-08 22:57:00 +00:00
|
|
|
* @param string|int $value
|
2015-06-02 23:43:45 +00:00
|
|
|
*/
|
|
|
|
|
public function setAttribute( $name, $value ) {
|
|
|
|
|
$this->attributes[$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
2022-03-08 22:57:00 +00:00
|
|
|
* @return string|int|null
|
2015-06-02 23:43:45 +00:00
|
|
|
*/
|
|
|
|
|
public function getAttribute( $name ) {
|
2018-06-12 20:44:33 +00:00
|
|
|
return $this->attributes[$name] ?? null;
|
2015-06-02 23:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-07-30 21:03:19 +00:00
|
|
|
* @param string $label
|
2022-03-16 23:34:23 +00:00
|
|
|
* @param string|int|float|false $value If not given, assumed equal to $label
|
2015-06-02 23:43:45 +00:00
|
|
|
*/
|
2015-07-30 21:03:19 +00:00
|
|
|
public function addOption( $label, $value = false ) {
|
|
|
|
|
$value = $value !== false ? $value : $label;
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->options[] = [ $label => $value ];
|
2015-06-02 23:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This accepts an array of form
|
|
|
|
|
* label => value
|
|
|
|
|
* label => ( label => value, label => value )
|
|
|
|
|
*
|
|
|
|
|
* @param array $options
|
|
|
|
|
*/
|
|
|
|
|
public function addOptions( $options ) {
|
|
|
|
|
$this->options[] = $options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-07-30 21:03:19 +00:00
|
|
|
* This accepts an array of form:
|
2015-06-02 23:43:45 +00:00
|
|
|
* label => value
|
|
|
|
|
* label => ( label => value, label => value )
|
|
|
|
|
*
|
|
|
|
|
* @param array $options
|
2018-12-17 15:51:19 +00:00
|
|
|
* @param string|array|false $default
|
2015-06-02 23:43:45 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2019-11-30 22:32:44 +00:00
|
|
|
public static function formatOptions( $options, $default = false ) {
|
2015-06-02 23:43:45 +00:00
|
|
|
$data = '';
|
|
|
|
|
|
|
|
|
|
foreach ( $options as $label => $value ) {
|
|
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
|
$contents = self::formatOptions( $value, $default );
|
2016-02-17 09:09:32 +00:00
|
|
|
$data .= Html::rawElement( 'optgroup', [ 'label' => $label ], $contents ) . "\n";
|
2015-06-02 23:43:45 +00:00
|
|
|
} else {
|
2015-09-13 22:52:33 +00:00
|
|
|
// 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";
|
2015-06-02 23:43:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getHTML() {
|
|
|
|
|
$contents = '';
|
|
|
|
|
|
|
|
|
|
foreach ( $this->options as $options ) {
|
|
|
|
|
$contents .= self::formatOptions( $options, $this->default );
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-08 15:21:58 +00:00
|
|
|
return Html::rawElement( $this->tagName, $this->attributes, rtrim( $contents ) );
|
2015-06-02 23:43:45 +00:00
|
|
|
}
|
2020-03-13 03:37:14 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse labels and values out of a comma- and colon-separated list of options, such as is used for
|
|
|
|
|
* expiry and duration lists. Documentation of the format is on translatewiki.net.
|
2021-03-08 16:11:49 +00:00
|
|
|
* @since 1.35
|
2020-03-13 03:37:14 +00:00
|
|
|
* @link https://translatewiki.net/wiki/Template:Doc-mediawiki-options-list
|
|
|
|
|
* @param string $msg The message to parse.
|
|
|
|
|
* @return string[] The options array, where keys are option labels (i.e. translations)
|
|
|
|
|
* and values are option values (i.e. untranslated).
|
|
|
|
|
*/
|
|
|
|
|
public static function parseOptionsMessage( string $msg ): array {
|
|
|
|
|
$options = [];
|
|
|
|
|
foreach ( explode( ',', $msg ) as $option ) {
|
|
|
|
|
// Normalize options that only have one part.
|
|
|
|
|
if ( strpos( $option, ':' ) === false ) {
|
|
|
|
|
$option = "$option:$option";
|
|
|
|
|
}
|
|
|
|
|
// Extract the two parts.
|
|
|
|
|
[ $label, $value ] = explode( ':', $option );
|
2020-11-29 11:37:44 +00:00
|
|
|
$options[ trim( $label ) ] = trim( $value );
|
2020-03-13 03:37:14 +00:00
|
|
|
}
|
|
|
|
|
return $options;
|
|
|
|
|
}
|
2015-06-02 23:43:45 +00:00
|
|
|
}
|
2024-05-16 10:52:03 +00:00
|
|
|
/** @deprecated class alias since 1.43 */
|
|
|
|
|
class_alias( XmlSelect::class, 'XmlSelect' );
|