Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
HTMLTitlesMultiselectField | |
0.00% |
0 / 58 |
|
0.00% |
0 / 9 |
600 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
loadDataFromRequest | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
validate | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
56 | |||
getInputHTML | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getInputOOUI | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
110 | |||
getInputWidget | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
shouldInfuseOOUI | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getOOUIModules | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getInputCodex | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\HTMLForm\Field; |
4 | |
5 | use MediaWiki\Widget\TitlesMultiselectWidget; |
6 | |
7 | /** |
8 | * Implements a tag multiselect input field for titles. |
9 | * |
10 | * Besides the parameters recognized by HTMLTitleTextField, additional recognized |
11 | * parameters are: |
12 | * default - (optional) Array of titles to use as preset data |
13 | * placeholder - (optional) Custom placeholder message for input |
14 | * |
15 | * The result is the array of titles |
16 | * |
17 | * This widget is a duplication of HTMLUsersMultiselectField, except for: |
18 | * - The configuration variable changed to 'titles' (from 'users') |
19 | * - OOUI modules were adjusted for the TitlesMultiselectWidget |
20 | * - The PHP version instantiates a MediaWiki\Widget\TitlesMultiselectWidget |
21 | * |
22 | * @stable to extend |
23 | * @note This widget is not likely to remain functional in non-OOUI forms. |
24 | */ |
25 | class HTMLTitlesMultiselectField extends HTMLTitleTextField { |
26 | /** |
27 | * @stable to call |
28 | * @inheritDoc |
29 | */ |
30 | public function __construct( $params ) { |
31 | $params += [ |
32 | // This overrides the default from HTMLTitleTextField |
33 | 'required' => false, |
34 | ]; |
35 | |
36 | parent::__construct( $params ); |
37 | } |
38 | |
39 | public function loadDataFromRequest( $request ) { |
40 | $value = $request->getText( $this->mName, $this->getDefault() ?? '' ); |
41 | |
42 | $titlesArray = explode( "\n", $value ); |
43 | // Remove empty lines |
44 | $titlesArray = array_values( array_filter( $titlesArray, static function ( $title ) { |
45 | return trim( $title ) !== ''; |
46 | } ) ); |
47 | // This function is expected to return a string |
48 | return implode( "\n", $titlesArray ); |
49 | } |
50 | |
51 | public function validate( $value, $alldata ) { |
52 | if ( !$this->mParams['exists'] ) { |
53 | return true; |
54 | } |
55 | |
56 | if ( $value === null ) { |
57 | return false; |
58 | } |
59 | |
60 | // $value is a string, because HTMLForm fields store their values as strings |
61 | $titlesArray = explode( "\n", $value ); |
62 | |
63 | if ( isset( $this->mParams['max'] ) && ( count( $titlesArray ) > $this->mParams['max'] ) ) { |
64 | return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] ); |
65 | } |
66 | |
67 | foreach ( $titlesArray as $title ) { |
68 | $result = parent::validate( $title, $alldata ); |
69 | if ( $result !== true ) { |
70 | return $result; |
71 | } |
72 | } |
73 | |
74 | return true; |
75 | } |
76 | |
77 | public function getInputHTML( $value ) { |
78 | $this->mParent->getOutput()->enableOOUI(); |
79 | return $this->getInputOOUI( $value ); |
80 | } |
81 | |
82 | public function getInputOOUI( $value ) { |
83 | $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' ); |
84 | |
85 | $params = [ |
86 | 'id' => $this->mID, |
87 | 'name' => $this->mName, |
88 | 'dir' => $this->mDir, |
89 | ]; |
90 | |
91 | if ( isset( $this->mParams['disabled'] ) ) { |
92 | $params['disabled'] = $this->mParams['disabled']; |
93 | } |
94 | |
95 | if ( isset( $this->mParams['default'] ) ) { |
96 | $params['default'] = $this->mParams['default']; |
97 | } |
98 | |
99 | $params['placeholder'] = $this->mParams['placeholder'] ?? |
100 | $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain(); |
101 | |
102 | if ( isset( $this->mParams['max'] ) ) { |
103 | $params['tagLimit'] = $this->mParams['max']; |
104 | } |
105 | |
106 | if ( isset( $this->mParams['showMissing'] ) ) { |
107 | $params['showMissing'] = $this->mParams['showMissing']; |
108 | } |
109 | if ( isset( $this->mParams['excludeDynamicNamespaces'] ) ) { |
110 | $params['excludeDynamicNamespaces'] = $this->mParams['excludeDynamicNamespaces']; |
111 | } |
112 | if ( isset( $this->mParams['allowEditTags'] ) ) { |
113 | $params['allowEditTags'] = $this->mParams['allowEditTags']; |
114 | } |
115 | |
116 | if ( isset( $this->mParams['input'] ) ) { |
117 | $params['input'] = $this->mParams['input']; |
118 | } |
119 | |
120 | if ( $value !== null ) { |
121 | // $value is a string, but the widget expects an array |
122 | $params['default'] = $value === '' ? [] : explode( "\n", $value ); |
123 | } |
124 | |
125 | // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm |
126 | $params['infusable'] = true; |
127 | $params['classes'] = [ 'mw-htmlform-autoinfuse' ]; |
128 | |
129 | return $this->getInputWidget( $params ); |
130 | } |
131 | |
132 | /** |
133 | * @inheritDoc |
134 | */ |
135 | protected function getInputWidget( $params ) { |
136 | $widget = new TitlesMultiselectWidget( $params ); |
137 | $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] ); |
138 | return $widget; |
139 | } |
140 | |
141 | protected function shouldInfuseOOUI() { |
142 | return true; |
143 | } |
144 | |
145 | protected function getOOUIModules() { |
146 | return [ 'mediawiki.widgets.TitlesMultiselectWidget' ]; |
147 | } |
148 | |
149 | public function getInputCodex( $value, $hasErrors ) { |
150 | // HTMLTextAreaField defaults to 'rows' => 25, which is too big for this field |
151 | // Use 10 instead (but allow $this->mParams to override that value) |
152 | $textAreaField = new HTMLTextAreaField( $this->mParams + [ 'rows' => 10 ] ); |
153 | return $textAreaField->getInputCodex( $value, $hasErrors ); |
154 | } |
155 | |
156 | } |
157 | |
158 | /** @deprecated class alias since 1.42 */ |
159 | class_alias( HTMLTitlesMultiselectField::class, 'HTMLTitlesMultiselectField' ); |