MediaWiki REL1_32
HTMLCheckMatrix.php
Go to the documentation of this file.
1<?php
2
26 static private $requiredParams = [
27 // Required by underlying HTMLFormField
28 'fieldname',
29 // Required by HTMLCheckMatrix
30 'rows',
31 'columns'
32 ];
33
34 public function __construct( $params ) {
35 $missing = array_diff( self::$requiredParams, array_keys( $params ) );
36 if ( $missing ) {
37 throw new HTMLFormFieldRequiredOptionsException( $this, $missing );
38 }
39 parent::__construct( $params );
40 }
41
42 public function validate( $value, $alldata ) {
43 $rows = $this->mParams['rows'];
44 $columns = $this->mParams['columns'];
45
46 // Make sure user-defined validation callback is run
47 $p = parent::validate( $value, $alldata );
48 if ( $p !== true ) {
49 return $p;
50 }
51
52 // Make sure submitted value is an array
53 if ( !is_array( $value ) ) {
54 return false;
55 }
56
57 // If all options are valid, array_intersect of the valid options
58 // and the provided options will return the provided options.
59 $validOptions = [];
60 foreach ( $rows as $rowTag ) {
61 foreach ( $columns as $columnTag ) {
62 $validOptions[] = $columnTag . '-' . $rowTag;
63 }
64 }
65 $validValues = array_intersect( $value, $validOptions );
66 if ( count( $validValues ) == count( $value ) ) {
67 return true;
68 } else {
69 return $this->msg( 'htmlform-select-badoption' );
70 }
71 }
72
83 public function getInputHTML( $value ) {
84 $html = '';
85 $tableContents = '';
86 $rows = $this->mParams['rows'];
87 $columns = $this->mParams['columns'];
88
89 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
90
91 // Build the column headers
92 $headerContents = Html::rawElement( 'td', [], "\u{00A0}" );
93 foreach ( $columns as $columnLabel => $columnTag ) {
94 $headerContents .= Html::rawElement( 'td', [], $columnLabel );
95 }
96 $tableContents .= Html::rawElement( 'tr', [], "\n$headerContents\n" );
97
98 $tooltipClass = 'mw-icon-question';
99 if ( isset( $this->mParams['tooltip-class'] ) ) {
100 $tooltipClass = $this->mParams['tooltip-class'];
101 }
102
103 // Build the options matrix
104 foreach ( $rows as $rowLabel => $rowTag ) {
105 // Append tooltip if configured
106 if ( isset( $this->mParams['tooltips'][$rowLabel] ) ) {
107 $tooltipAttribs = [
108 'class' => "mw-htmlform-tooltip $tooltipClass",
109 'title' => $this->mParams['tooltips'][$rowLabel],
110 'aria-label' => $this->mParams['tooltips'][$rowLabel]
111 ];
112 $rowLabel .= ' ' . Html::element( 'span', $tooltipAttribs, '' );
113 }
114 $rowContents = Html::rawElement( 'td', [], $rowLabel );
115 foreach ( $columns as $columnTag ) {
116 $thisTag = "$columnTag-$rowTag";
117 // Construct the checkbox
118 $thisAttribs = [
119 'id' => "{$this->mID}-$thisTag",
120 'value' => $thisTag,
121 ];
122 $checked = in_array( $thisTag, (array)$value, true );
123 if ( $this->isTagForcedOff( $thisTag ) ) {
124 $checked = false;
125 $thisAttribs['disabled'] = 1;
126 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-off';
127 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
128 $checked = true;
129 $thisAttribs['disabled'] = 1;
130 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-on';
131 }
132
133 $checkbox = $this->getOneCheckboxHTML( $checked, $attribs + $thisAttribs );
134
135 $rowContents .= Html::rawElement(
136 'td',
137 [],
138 $checkbox
139 );
140 }
141 $tableContents .= Html::rawElement( 'tr', [], "\n$rowContents\n" );
142 }
143
144 // Put it all in a table
145 $html .= Html::rawElement( 'table',
146 [ 'class' => 'mw-htmlform-matrix' ],
147 Html::rawElement( 'tbody', [], "\n$tableContents\n" ) ) . "\n";
148
149 return $html;
150 }
151
152 public function getInputOOUI( $value ) {
153 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
154
156 [
157 'name' => $this->mName,
158 'infusable' => true,
159 'id' => $this->mID,
160 'rows' => $this->mParams['rows'],
161 'columns' => $this->mParams['columns'],
162 'tooltips' => $this->mParams['tooltips'] ?? [],
163 'forcedOff' => $this->mParams['force-options-off'] ?? [],
164 'forcedOn' => $this->mParams['force-options-on'] ?? [],
165 'values' => $value,
166 ] + OOUI\Element::configFromHtmlAttributes( $attribs )
167 );
168 }
169
170 protected function getOneCheckboxHTML( $checked, $attribs ) {
171 $checkbox = Xml::check( "{$this->mName}[]", $checked, $attribs );
172 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
173 $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
174 $checkbox .
175 Html::element( 'label', [ 'for' => $attribs['id'] ] ) .
176 Html::closeElement( 'div' );
177 }
178 return $checkbox;
179 }
180
181 protected function isTagForcedOff( $tag ) {
182 return isset( $this->mParams['force-options-off'] )
183 && in_array( $tag, $this->mParams['force-options-off'] );
184 }
185
186 protected function isTagForcedOn( $tag ) {
187 return isset( $this->mParams['force-options-on'] )
188 && in_array( $tag, $this->mParams['force-options-on'] );
189 }
190
202 public function getTableRow( $value ) {
203 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
204 $inputHtml = $this->getInputHTML( $value );
205 $fieldType = static::class;
206 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
207 $cellAttributes = [ 'colspan' => 2 ];
208
209 $hideClass = '';
210 $hideAttributes = [];
211 if ( $this->mHideIf ) {
212 $hideAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
213 $hideClass = 'mw-htmlform-hide-if';
214 }
215
216 $label = $this->getLabelHtml( $cellAttributes );
217
218 $field = Html::rawElement(
219 'td',
220 [ 'class' => 'mw-input' ] + $cellAttributes,
221 $inputHtml . "\n$errors"
222 );
223
224 $html = Html::rawElement( 'tr',
225 [ 'class' => "mw-htmlform-vertical-label $hideClass" ] + $hideAttributes,
226 $label );
227 $html .= Html::rawElement( 'tr',
228 [ 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $hideClass" ] +
229 $hideAttributes,
230 $field );
231
232 return $html . $helptext;
233 }
234
240 public function loadDataFromRequest( $request ) {
241 if ( $this->isSubmitAttempt( $request ) ) {
242 // Checkboxes are just not added to the request arrays if they're not checked,
243 // so it's perfectly possible for there not to be an entry at all
244 return $request->getArray( $this->mName, [] );
245 } else {
246 // That's ok, the user has not yet submitted the form, so show the defaults
247 return $this->getDefault();
248 }
249 }
250
251 public function getDefault() {
252 return $this->mDefault ?? [];
253 }
254
255 public function filterDataForSubmit( $data ) {
256 $columns = HTMLFormField::flattenOptions( $this->mParams['columns'] );
257 $rows = HTMLFormField::flattenOptions( $this->mParams['rows'] );
258 $res = [];
259 foreach ( $columns as $column ) {
260 foreach ( $rows as $row ) {
261 // Make sure option hasn't been forced
262 $thisTag = "$column-$row";
263 if ( $this->isTagForcedOff( $thisTag ) ) {
264 $res[$thisTag] = false;
265 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
266 $res[$thisTag] = true;
267 } else {
268 $res[$thisTag] = in_array( $thisTag, $data );
269 }
270 }
271 }
272
273 return $res;
274 }
275
276 protected function getOOUIModules() {
277 return [ 'mediawiki.widgets.CheckMatrixWidget' ];
278 }
279
280 protected function shouldInfuseOOUI() {
281 return true;
282 }
283}
A checkbox matrix Operates similarly to HTMLMultiSelectField, but instead of using an array of option...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
getOneCheckboxHTML( $checked, $attribs)
shouldInfuseOOUI()
Whether the field should be automatically infused.
__construct( $params)
Initialise the object.
getInputHTML( $value)
Build a table containing a matrix of checkbox options.
getTableRow( $value)
Get the complete table row for the input, including help text, labels, and whatever.
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
loadDataFromRequest( $request)
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
filterDataForSubmit( $data)
Support for separating multi-option preferences into multiple preferences Due to lack of array suppor...
The parent class to generate form fields.
getErrorsAndErrorClass( $value)
Determine form errors to display and their classes.
getHelpTextHtmlTable( $helptext)
Generate help text HTML in table format.
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
getHelpText()
Determine the help text to display.
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
getLabelHtml( $cellAttributes=[])
getAttributes(array $list)
Returns the given attributes from the parameters.
msg()
Get a translated interface message.
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
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
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 $rows
Definition hooks.txt:2857
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:2880
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:2062
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:2063
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
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$params