3 use MediaWiki\Widget\NamespacesMultiselectWidget
;
4 use MediaWiki\MediaWikiServices
;
7 * Implements a tag multiselect input field for namespaces.
9 * The result is the array of namespaces
11 * TODO: This widget duplicates a lot from HTMLTitlesMultiselectField,
12 * which itself duplicates HTMLUsersMultiselectField. These classes
13 * should be refactored.
15 * @note This widget is not likely to remain functional in non-OOUI forms.
17 class HTMLNamespacesMultiselectField
extends HTMLSelectNamespace
{
18 public function loadDataFromRequest( $request ) {
19 $value = $request->getText( $this->mName
, $this->getDefault() );
21 $namespaces = explode( "\n", $value );
23 $namespaces = array_values( array_filter( $namespaces, function ( $namespace ) {
24 return trim( $namespace ) !== '';
26 // This function is expected to return a string
27 return implode( "\n", $namespaces );
30 public function validate( $value, $alldata ) {
31 if ( !$this->mParams
['exists'] ||
$value === '' ) {
35 if ( is_null( $value ) ) {
39 // $value is a string, because HTMLForm fields store their values as strings
40 $namespaces = explode( "\n", $value );
42 if ( isset( $this->mParams
['max'] ) && ( count( $namespaces ) > $this->mParams
['max'] ) ) {
43 return $this->msg( 'htmlform-int-toohigh', $this->mParams
['max'] );
46 foreach ( $namespaces as $namespace ) {
49 !MediaWikiServices
::getInstance()->getNamespaceInfo()->exists( $namespace )
51 return $this->msg( 'htmlform-select-badoption' );
54 $result = parent
::validate( $namespace, $alldata );
55 if ( $result !== true ) {
63 public function getInputHTML( $value ) {
64 $this->mParent
->getOutput()->enableOOUI();
65 return $this->getInputOOUI( $value );
68 public function getInputOOUI( $value ) {
71 'name' => $this->mName
,
75 if ( isset( $this->mParams
['disabled'] ) ) {
76 $params['disabled'] = $this->mParams
['disabled'];
79 if ( isset( $this->mParams
['default'] ) ) {
80 $params['default'] = $this->mParams
['default'];
83 if ( isset( $this->mParams
['placeholder'] ) ) {
84 $params['placeholder'] = $this->mParams
['placeholder'];
86 $params['placeholder'] = $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
89 if ( isset( $this->mParams
['max'] ) ) {
90 $params['tagLimit'] = $this->mParams
['max'];
93 if ( isset( $this->mParams
['input'] ) ) {
94 $params['input'] = $this->mParams
['input'];
97 if ( !is_null( $value ) ) {
98 // $value is a string, but the widget expects an array
99 $params['default'] = $value === '' ?
[] : explode( "\n", $value );
102 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
103 $params['infusable'] = true;
104 $params['classes'] = [ 'mw-htmlform-field-autoinfuse' ];
105 $widget = new NamespacesMultiselectWidget( $params );
106 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
111 protected function shouldInfuseOOUI() {
115 protected function getOOUIModules() {
116 return [ 'mediawiki.widgets.NamespacesMultiselectWidget' ];