MediaWiki REL1_33
HTMLMultiSelectField.php
Go to the documentation of this file.
1<?php
2
17 public function __construct( $params ) {
18 parent::__construct( $params );
19
20 // If the disabled-options parameter is not provided, use an empty array
21 if ( isset( $this->mParams['disabled-options'] ) === false ) {
22 $this->mParams['disabled-options'] = [];
23 }
24
25 if ( isset( $params['dropdown'] ) ) {
26 $this->mClass .= ' mw-htmlform-dropdown';
27 }
28
29 if ( isset( $params['flatlist'] ) ) {
30 $this->mClass .= ' mw-htmlform-flatlist';
31 }
32 }
33
34 public function validate( $value, $alldata ) {
35 $p = parent::validate( $value, $alldata );
36
37 if ( $p !== true ) {
38 return $p;
39 }
40
41 if ( !is_array( $value ) ) {
42 return false;
43 }
44
45 # If all options are valid, array_intersect of the valid options
46 # and the provided options will return the provided options.
47 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
48
49 $validValues = array_intersect( $value, $validOptions );
50 if ( count( $validValues ) == count( $value ) ) {
51 return true;
52 } else {
53 return $this->msg( 'htmlform-select-badoption' );
54 }
55 }
56
57 public function getInputHTML( $value ) {
58 if ( isset( $this->mParams['dropdown'] ) ) {
59 $this->mParent->getOutput()->addModules( 'jquery.chosen' );
60 }
61
63 $html = $this->formatOptions( $this->getOptions(), $value );
64
65 return $html;
66 }
67
68 public function formatOptions( $options, $value ) {
69 $html = '';
70
71 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
72
73 foreach ( $options as $label => $info ) {
74 if ( is_array( $info ) ) {
75 $html .= Html::rawElement( 'h1', [], $label ) . "\n";
76 $html .= $this->formatOptions( $info, $value );
77 } else {
78 $thisAttribs = [
79 'id' => "{$this->mID}-$info",
80 'value' => $info,
81 ];
82 if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
83 $thisAttribs['disabled'] = 'disabled';
84 }
85 $checked = in_array( $info, $value, true );
86
87 $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
88
89 $html .= ' ' . Html::rawElement(
90 'div',
91 [ 'class' => 'mw-htmlform-flatlist-item' ],
92 $checkbox
93 );
94 }
95 }
96
97 return $html;
98 }
99
100 protected function getOneCheckbox( $checked, $attribs, $label ) {
101 if ( $this->mParent instanceof OOUIHTMLForm ) {
102 throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
103 } else {
104 $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
105 $checkbox =
106 Xml::check( "{$this->mName}[]", $checked, $attribs ) .
107 "\u{00A0}" .
108 call_user_func( $elementFunc,
109 'label',
110 [ 'for' => $attribs['id'] ],
111 $label
112 );
113 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
114 $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
115 $checkbox .
116 Html::closeElement( 'div' );
117 }
118 return $checkbox;
119 }
120 }
121
126 public function getOptionsOOUI() {
127 // Sections make this difficult. See getInputOOUI().
128 throw new MWException( 'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
129 }
130
141 public function getInputOOUI( $value ) {
142 $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
143
144 $hasSections = false;
145 $optionsOouiSections = [];
146 $options = $this->getOptions();
147 // If the options are supposed to be split into sections, each section becomes a separate
148 // CheckboxMultiselectInputWidget.
149 foreach ( $options as $label => $section ) {
150 if ( is_array( $section ) ) {
151 $optionsOouiSections[ $label ] = Xml::listDropDownOptionsOoui( $section );
152 unset( $options[$label] );
153 $hasSections = true;
154 }
155 }
156 // If anything remains in the array, they are sectionless options. Put them in a separate widget
157 // at the beginning.
158 if ( $options ) {
159 $optionsOouiSections = array_merge(
160 [ '' => Xml::listDropDownOptionsOoui( $options ) ],
161 $optionsOouiSections
162 );
163 }
164
165 $out = [];
166 foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
167 $attr = [];
168 $attr['name'] = "{$this->mName}[]";
169
170 $attr['value'] = $value;
171 $attr['options'] = $optionsOoui;
172
173 foreach ( $attr['options'] as &$option ) {
174 $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
175 }
176 if ( $this->mOptionsLabelsNotFromMessage ) {
177 foreach ( $attr['options'] as &$option ) {
178 $option['label'] = new OOUI\HtmlSnippet( $option['label'] );
179 }
180 }
181
182 $attr += OOUI\Element::configFromHtmlAttributes(
183 $this->getAttributes( [ 'disabled', 'tabindex' ] )
184 );
185
186 if ( $this->mClass !== '' ) {
187 $attr['classes'] = [ $this->mClass ];
188 }
189
190 $widget = new OOUI\CheckboxMultiselectInputWidget( $attr );
191 if ( $sectionLabel ) {
192 $out[] = new OOUI\FieldsetLayout( [
193 'items' => [ $widget ],
194 'label' => new OOUI\HtmlSnippet( $sectionLabel ),
195 ] );
196 } else {
197 $out[] = $widget;
198 }
199 }
200
201 if ( !$hasSections ) {
202 // Directly return the only OOUI\CheckboxMultiselectInputWidget.
203 // This allows it to be made infusable and later tweaked by JS code.
204 return $out[ 0 ];
205 }
206
207 return implode( '', $out );
208 }
209
215 public function loadDataFromRequest( $request ) {
216 $fromRequest = $request->getArray( $this->mName, [] );
217 // Fetch the value in either one of the two following case:
218 // - we have a valid submit attempt (form was just submitted)
219 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
220 if ( $this->isSubmitAttempt( $request ) || $fromRequest ) {
221 // Checkboxes are just not added to the request arrays if they're not checked,
222 // so it's perfectly possible for there not to be an entry at all
223 return $fromRequest;
224 } else {
225 // That's ok, the user has not yet submitted the form, so show the defaults
226 return $this->getDefault();
227 }
228 }
229
230 public function getDefault() {
231 return $this->mDefault ?? [];
232 }
233
234 public function filterDataForSubmit( $data ) {
237
238 $res = [];
239 foreach ( $options as $opt ) {
240 $res["$opt"] = in_array( $opt, $data, true );
241 }
242
243 return $res;
244 }
245
246 protected function needsLabel() {
247 return false;
248 }
249}
The parent class to generate form fields.
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
getOptions()
Fetch the array of options from the field's parameters.
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
getAttributes(array $list)
Returns the given attributes from the parameters.
msg()
Get a translated interface message.
static forceToStringRecursive( $array)
Recursively forces values in an array to strings, because issues arise with integer 0 as a value.
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
getOneCheckbox( $checked, $attribs, $label)
filterDataForSubmit( $data)
Support for separating multi-option preferences into multiple preferences Due to lack of array suppor...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself.
getInputOOUI( $value)
Get the OOUI version of this field.
formatOptions( $options, $value)
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
MediaWiki exception.
Compact stacked vertical format for forms, implemented using OOUI widgets.
$res
Definition database.txt:21
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2843
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:855
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:1999
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2011
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2012
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template $section
Definition hooks.txt:3070
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$params