Merge "installer: make sqlite installer move the job queue to another DB"
[lhc/web/wiklou.git] / includes / htmlform / fields / HTMLNamespacesMultiselectField.php
1 <?php
2
3 use MediaWiki\Widget\NamespacesMultiselectWidget;
4
5 /**
6 * Implements a tag multiselect input field for namespaces.
7 *
8 * The result is the array of namespaces
9 *
10 * TODO: This widget duplicates a lot from HTMLTitlesMultiselectField,
11 * which itself duplicates HTMLUsersMultiselectField. These classes
12 * should be refactored.
13 *
14 * @note This widget is not likely to remain functional in non-OOUI forms.
15 */
16 class HTMLNamespacesMultiselectField extends HTMLSelectNamespace {
17 public function loadDataFromRequest( $request ) {
18 $value = $request->getText( $this->mName, $this->getDefault() );
19
20 $namespaces = explode( "\n", $value );
21 // Remove empty lines
22 $namespaces = array_values( array_filter( $namespaces, function ( $namespace ) {
23 return trim( $namespace ) !== '';
24 } ) );
25 // This function is expected to return a string
26 return implode( "\n", $namespaces );
27 }
28
29 public function validate( $value, $alldata ) {
30 if ( !$this->mParams['exists'] ) {
31 return true;
32 }
33
34 if ( is_null( $value ) ) {
35 return false;
36 }
37
38 // $value is a string, because HTMLForm fields store their values as strings
39 $namespaces = explode( "\n", $value );
40
41 if ( isset( $this->mParams['max'] ) ) {
42 if ( count( $namespaces ) > $this->mParams['max'] ) {
43 return $this->msg( 'htmlform-int-toohigh', $this->mParams['max'] );
44 }
45 }
46
47 foreach ( $namespaces as $namespace ) {
48 if ( $namespace < 0 ) {
49 return $this->msg( 'htmlform-select-badoption' );
50 }
51
52 $result = parent::validate( $namespace, $alldata );
53 if ( $result !== true ) {
54 return $result;
55 }
56 }
57
58 return true;
59 }
60
61 public function getInputHTML( $value ) {
62 $this->mParent->getOutput()->enableOOUI();
63 return $this->getInputOOUI( $value );
64 }
65
66 public function getInputOOUI( $value ) {
67 $params = [
68 'id' => $this->mID,
69 'name' => $this->mName,
70 'dir' => $this->mDir,
71 ];
72
73 if ( isset( $this->mParams['disabled'] ) ) {
74 $params['disabled'] = $this->mParams['disabled'];
75 }
76
77 if ( isset( $this->mParams['default'] ) ) {
78 $params['default'] = $this->mParams['default'];
79 }
80
81 if ( isset( $this->mParams['placeholder'] ) ) {
82 $params['placeholder'] = $this->mParams['placeholder'];
83 } else {
84 $params['placeholder'] = $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
85 }
86
87 if ( isset( $this->mParams['max'] ) ) {
88 $params['tagLimit'] = $this->mParams['max'];
89 }
90
91 if ( isset( $this->mParams['input'] ) ) {
92 $params['input'] = $this->mParams['input'];
93 }
94
95 if ( !is_null( $value ) ) {
96 // $value is a string, but the widget expects an array
97 $params['default'] = $value === '' ? [] : explode( "\n", $value );
98 }
99
100 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
101 $params['infusable'] = true;
102 $params['classes'] = [ 'mw-htmlform-field-autoinfuse' ];
103 $widget = new NamespacesMultiselectWidget( $params );
104 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
105
106 return $widget;
107 }
108
109 protected function shouldInfuseOOUI() {
110 return true;
111 }
112
113 protected function getOOUIModules() {
114 return [ 'mediawiki.widgets.NamespacesMultiselectWidget' ];
115 }
116
117 }