Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / htmlform / fields / HTMLNamespacesMultiselectField.php
blob212f91bb177a9f6bd7d16f2d2300caa682931d82
1 <?php
3 namespace MediaWiki\HTMLForm\Field;
5 use MediaWiki\MediaWikiServices;
6 use MediaWiki\Widget\NamespacesMultiselectWidget;
8 /**
9 * Implements a tag multiselect input field for namespaces.
11 * The result is the array of namespaces
13 * TODO: This widget duplicates a lot from HTMLTitlesMultiselectField,
14 * which itself duplicates HTMLUsersMultiselectField. These classes
15 * should be refactored.
17 * @stable to extend
18 * @note This widget is not likely to remain functional in non-OOUI forms.
20 class HTMLNamespacesMultiselectField extends HTMLSelectNamespace {
21 public function loadDataFromRequest( $request ) {
22 $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
24 $namespaces = explode( "\n", $value );
25 // Remove empty lines
26 $namespaces = array_values( array_filter( $namespaces, static function ( $namespace ) {
27 return trim( $namespace ) !== '';
28 } ) );
29 // This function is expected to return a string
30 return implode( "\n", $namespaces );
33 public function validate( $value, $alldata ) {
34 if ( !$this->mParams['exists'] || $value === '' ) {
35 return true;
38 if ( $value === null ) {
39 return false;
42 // $value is a string, because HTMLForm fields store their values as strings
43 $namespaces = explode( "\n", $value );
45 if ( isset( $this->mParams['max'] ) && ( count( $namespaces ) > $this->mParams['max'] ) ) {
46 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
49 foreach ( $namespaces as $namespace ) {
50 if (
51 $namespace < 0 ||
52 !MediaWikiServices::getInstance()->getNamespaceInfo()->exists( (int)$namespace )
53 ) {
54 return $this->msg( 'htmlform-select-badoption' );
57 $result = parent::validate( $namespace, $alldata );
58 if ( $result !== true ) {
59 return $result;
63 return true;
66 public function getInputHTML( $value ) {
67 $this->mParent->getOutput()->enableOOUI();
68 return $this->getInputOOUI( $value );
71 public function getInputOOUI( $value ) {
72 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
74 $params = [
75 'id' => $this->mID,
76 'name' => $this->mName,
77 'dir' => $this->mDir,
80 if ( isset( $this->mParams['disabled'] ) ) {
81 $params['disabled'] = $this->mParams['disabled'];
84 if ( isset( $this->mParams['default'] ) ) {
85 $params['default'] = $this->mParams['default'];
88 $params['placeholder'] = $this->mParams['placeholder'] ??
89 $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
91 if ( isset( $this->mParams['max'] ) ) {
92 $params['tagLimit'] = $this->mParams['max'];
95 if ( isset( $this->mParams['input'] ) ) {
96 $params['input'] = $this->mParams['input'];
99 if ( isset( $this->mParams['allowEditTags'] ) ) {
100 $params['allowEditTags'] = $this->mParams['allowEditTags'];
103 if ( $value !== null ) {
104 // $value is a string, but the widget expects an array
105 $params['default'] = $value === '' ? [] : explode( "\n", $value );
108 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
109 $params['infusable'] = true;
110 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
111 $widget = new NamespacesMultiselectWidget( $params );
112 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
114 return $widget;
117 protected function shouldInfuseOOUI() {
118 return true;
121 protected function getOOUIModules() {
122 return [ 'mediawiki.widgets.NamespacesMultiselectWidget' ];
125 public function getInputCodex( $value, $hasErrors ) {
126 // HTMLTextAreaField defaults to 'rows' => 25, which is too big for this field
127 // Use 10 instead (but allow $this->mParams to override that value)
128 $textAreaField = new HTMLTextAreaField( $this->mParams + [ 'rows' => 10 ] );
129 return $textAreaField->getInputCodex( $value, $hasErrors );
134 /** @deprecated class alias since 1.42 */
135 class_alias( HTMLNamespacesMultiselectField::class, 'HTMLNamespacesMultiselectField' );