]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/htmlform/HTMLFormField.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / htmlform / HTMLFormField.php
1 <?php
2
3 /**
4  * The parent class to generate form fields.  Any field type should
5  * be a subclass of this.
6  */
7 abstract class HTMLFormField {
8         public $mParams;
9
10         protected $mValidationCallback;
11         protected $mFilterCallback;
12         protected $mName;
13         protected $mDir;
14         protected $mLabel; # String label, as HTML. Set on construction.
15         protected $mID;
16         protected $mClass = '';
17         protected $mVFormClass = '';
18         protected $mHelpClass = false;
19         protected $mDefault;
20         protected $mOptions = false;
21         protected $mOptionsLabelsNotFromMessage = false;
22         protected $mHideIf = null;
23
24         /**
25          * @var bool If true will generate an empty div element with no label
26          * @since 1.22
27          */
28         protected $mShowEmptyLabels = true;
29
30         /**
31          * @var HTMLForm|null
32          */
33         public $mParent;
34
35         /**
36          * This function must be implemented to return the HTML to generate
37          * the input object itself.  It should not implement the surrounding
38          * table cells/rows, or labels/help messages.
39          *
40          * @param string $value The value to set the input to; eg a default
41          *     text for a text input.
42          *
43          * @return string Valid HTML.
44          */
45         abstract public function getInputHTML( $value );
46
47         /**
48          * Same as getInputHTML, but returns an OOUI object.
49          * Defaults to false, which getOOUI will interpret as "use the HTML version"
50          *
51          * @param string $value
52          * @return OOUI\Widget|false
53          */
54         public function getInputOOUI( $value ) {
55                 return false;
56         }
57
58         /**
59          * True if this field type is able to display errors; false if validation errors need to be
60          * displayed in the main HTMLForm error area.
61          * @return bool
62          */
63         public function canDisplayErrors() {
64                 return $this->hasVisibleOutput();
65         }
66
67         /**
68          * Get a translated interface message
69          *
70          * This is a wrapper around $this->mParent->msg() if $this->mParent is set
71          * and wfMessage() otherwise.
72          *
73          * Parameters are the same as wfMessage().
74          *
75          * @return Message
76          */
77         public function msg() {
78                 $args = func_get_args();
79
80                 if ( $this->mParent ) {
81                         $callback = [ $this->mParent, 'msg' ];
82                 } else {
83                         $callback = 'wfMessage';
84                 }
85
86                 return call_user_func_array( $callback, $args );
87         }
88
89         /**
90          * If this field has a user-visible output or not. If not,
91          * it will not be rendered
92          *
93          * @return bool
94          */
95         public function hasVisibleOutput() {
96                 return true;
97         }
98
99         /**
100          * Fetch a field value from $alldata for the closest field matching a given
101          * name.
102          *
103          * This is complex because it needs to handle array fields like the user
104          * would expect. The general algorithm is to look for $name as a sibling
105          * of $this, then a sibling of $this's parent, and so on. Keeping in mind
106          * that $name itself might be referencing an array.
107          *
108          * @param array $alldata
109          * @param string $name
110          * @return string
111          */
112         protected function getNearestFieldByName( $alldata, $name ) {
113                 $tmp = $this->mName;
114                 $thisKeys = [];
115                 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
116                         array_unshift( $thisKeys, $m[2] );
117                         $tmp = $m[1];
118                 }
119                 if ( substr( $tmp, 0, 2 ) == 'wp' &&
120                         !array_key_exists( $tmp, $alldata ) &&
121                         array_key_exists( substr( $tmp, 2 ), $alldata )
122                 ) {
123                         // Adjust for name mangling.
124                         $tmp = substr( $tmp, 2 );
125                 }
126                 array_unshift( $thisKeys, $tmp );
127
128                 $tmp = $name;
129                 $nameKeys = [];
130                 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
131                         array_unshift( $nameKeys, $m[2] );
132                         $tmp = $m[1];
133                 }
134                 array_unshift( $nameKeys, $tmp );
135
136                 $testValue = '';
137                 for ( $i = count( $thisKeys ) - 1; $i >= 0; $i-- ) {
138                         $keys = array_merge( array_slice( $thisKeys, 0, $i ), $nameKeys );
139                         $data = $alldata;
140                         while ( $keys ) {
141                                 $key = array_shift( $keys );
142                                 if ( !is_array( $data ) || !array_key_exists( $key, $data ) ) {
143                                         continue 2;
144                                 }
145                                 $data = $data[$key];
146                         }
147                         $testValue = (string)$data;
148                         break;
149                 }
150
151                 return $testValue;
152         }
153
154         /**
155          * Helper function for isHidden to handle recursive data structures.
156          *
157          * @param array $alldata
158          * @param array $params
159          * @return bool
160          * @throws MWException
161          */
162         protected function isHiddenRecurse( array $alldata, array $params ) {
163                 $origParams = $params;
164                 $op = array_shift( $params );
165
166                 try {
167                         switch ( $op ) {
168                                 case 'AND':
169                                         foreach ( $params as $i => $p ) {
170                                                 if ( !is_array( $p ) ) {
171                                                         throw new MWException(
172                                                                 "Expected array, found " . gettype( $p ) . " at index $i"
173                                                         );
174                                                 }
175                                                 if ( !$this->isHiddenRecurse( $alldata, $p ) ) {
176                                                         return false;
177                                                 }
178                                         }
179                                         return true;
180
181                                 case 'OR':
182                                         foreach ( $params as $i => $p ) {
183                                                 if ( !is_array( $p ) ) {
184                                                         throw new MWException(
185                                                                 "Expected array, found " . gettype( $p ) . " at index $i"
186                                                         );
187                                                 }
188                                                 if ( $this->isHiddenRecurse( $alldata, $p ) ) {
189                                                         return true;
190                                                 }
191                                         }
192                                         return false;
193
194                                 case 'NAND':
195                                         foreach ( $params as $i => $p ) {
196                                                 if ( !is_array( $p ) ) {
197                                                         throw new MWException(
198                                                                 "Expected array, found " . gettype( $p ) . " at index $i"
199                                                         );
200                                                 }
201                                                 if ( !$this->isHiddenRecurse( $alldata, $p ) ) {
202                                                         return true;
203                                                 }
204                                         }
205                                         return false;
206
207                                 case 'NOR':
208                                         foreach ( $params as $i => $p ) {
209                                                 if ( !is_array( $p ) ) {
210                                                         throw new MWException(
211                                                                 "Expected array, found " . gettype( $p ) . " at index $i"
212                                                         );
213                                                 }
214                                                 if ( $this->isHiddenRecurse( $alldata, $p ) ) {
215                                                         return false;
216                                                 }
217                                         }
218                                         return true;
219
220                                 case 'NOT':
221                                         if ( count( $params ) !== 1 ) {
222                                                 throw new MWException( "NOT takes exactly one parameter" );
223                                         }
224                                         $p = $params[0];
225                                         if ( !is_array( $p ) ) {
226                                                 throw new MWException(
227                                                         "Expected array, found " . gettype( $p ) . " at index 0"
228                                                 );
229                                         }
230                                         return !$this->isHiddenRecurse( $alldata, $p );
231
232                                 case '===':
233                                 case '!==':
234                                         if ( count( $params ) !== 2 ) {
235                                                 throw new MWException( "$op takes exactly two parameters" );
236                                         }
237                                         list( $field, $value ) = $params;
238                                         if ( !is_string( $field ) || !is_string( $value ) ) {
239                                                 throw new MWException( "Parameters for $op must be strings" );
240                                         }
241                                         $testValue = $this->getNearestFieldByName( $alldata, $field );
242                                         switch ( $op ) {
243                                                 case '===':
244                                                         return ( $value === $testValue );
245                                                 case '!==':
246                                                         return ( $value !== $testValue );
247                                         }
248
249                                 default:
250                                         throw new MWException( "Unknown operation" );
251                         }
252                 } catch ( Exception $ex ) {
253                         throw new MWException(
254                                 "Invalid hide-if specification for $this->mName: " .
255                                 $ex->getMessage() . " in " . var_export( $origParams, true ),
256                                 0, $ex
257                         );
258                 }
259         }
260
261         /**
262          * Test whether this field is supposed to be hidden, based on the values of
263          * the other form fields.
264          *
265          * @since 1.23
266          * @param array $alldata The data collected from the form
267          * @return bool
268          */
269         public function isHidden( $alldata ) {
270                 if ( !$this->mHideIf ) {
271                         return false;
272                 }
273
274                 return $this->isHiddenRecurse( $alldata, $this->mHideIf );
275         }
276
277         /**
278          * Override this function if the control can somehow trigger a form
279          * submission that shouldn't actually submit the HTMLForm.
280          *
281          * @since 1.23
282          * @param string|array $value The value the field was submitted with
283          * @param array $alldata The data collected from the form
284          *
285          * @return bool True to cancel the submission
286          */
287         public function cancelSubmit( $value, $alldata ) {
288                 return false;
289         }
290
291         /**
292          * Override this function to add specific validation checks on the
293          * field input.  Don't forget to call parent::validate() to ensure
294          * that the user-defined callback mValidationCallback is still run
295          *
296          * @param string|array $value The value the field was submitted with
297          * @param array $alldata The data collected from the form
298          *
299          * @return bool|string|Message True on success, or String/Message error to display, or
300          *   false to fail validation without displaying an error.
301          */
302         public function validate( $value, $alldata ) {
303                 if ( $this->isHidden( $alldata ) ) {
304                         return true;
305                 }
306
307                 if ( isset( $this->mParams['required'] )
308                         && $this->mParams['required'] !== false
309                         && $value === ''
310                 ) {
311                         return $this->msg( 'htmlform-required' );
312                 }
313
314                 if ( isset( $this->mValidationCallback ) ) {
315                         return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
316                 }
317
318                 return true;
319         }
320
321         public function filter( $value, $alldata ) {
322                 if ( isset( $this->mFilterCallback ) ) {
323                         $value = call_user_func( $this->mFilterCallback, $value, $alldata, $this->mParent );
324                 }
325
326                 return $value;
327         }
328
329         /**
330          * Should this field have a label, or is there no input element with the
331          * appropriate id for the label to point to?
332          *
333          * @return bool True to output a label, false to suppress
334          */
335         protected function needsLabel() {
336                 return true;
337         }
338
339         /**
340          * Tell the field whether to generate a separate label element if its label
341          * is blank.
342          *
343          * @since 1.22
344          *
345          * @param bool $show Set to false to not generate a label.
346          * @return void
347          */
348         public function setShowEmptyLabel( $show ) {
349                 $this->mShowEmptyLabels = $show;
350         }
351
352         /**
353          * Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to
354          * just view it? This can't normally be distinguished for e.g. checkboxes.
355          *
356          * Returns true if the request has a field for a CSRF token (wpEditToken) or a form identifier
357          * (wpFormIdentifier).
358          *
359          * @param WebRequest $request
360          * @return bool
361          */
362         protected function isSubmitAttempt( WebRequest $request ) {
363                 return $request->getCheck( 'wpEditToken' ) || $request->getCheck( 'wpFormIdentifier' );
364         }
365
366         /**
367          * Get the value that this input has been set to from a posted form,
368          * or the input's default value if it has not been set.
369          *
370          * @param WebRequest $request
371          * @return string The value
372          */
373         public function loadDataFromRequest( $request ) {
374                 if ( $request->getCheck( $this->mName ) ) {
375                         return $request->getText( $this->mName );
376                 } else {
377                         return $this->getDefault();
378                 }
379         }
380
381         /**
382          * Initialise the object
383          *
384          * @param array $params Associative Array. See HTMLForm doc for syntax.
385          *
386          * @since 1.22 The 'label' attribute no longer accepts raw HTML, use 'label-raw' instead
387          * @throws MWException
388          */
389         public function __construct( $params ) {
390                 $this->mParams = $params;
391
392                 if ( isset( $params['parent'] ) && $params['parent'] instanceof HTMLForm ) {
393                         $this->mParent = $params['parent'];
394                 }
395
396                 # Generate the label from a message, if possible
397                 if ( isset( $params['label-message'] ) ) {
398                         $this->mLabel = $this->getMessage( $params['label-message'] )->parse();
399                 } elseif ( isset( $params['label'] ) ) {
400                         if ( $params['label'] === '&#160;' ) {
401                                 // Apparently some things set &nbsp directly and in an odd format
402                                 $this->mLabel = '&#160;';
403                         } else {
404                                 $this->mLabel = htmlspecialchars( $params['label'] );
405                         }
406                 } elseif ( isset( $params['label-raw'] ) ) {
407                         $this->mLabel = $params['label-raw'];
408                 }
409
410                 $this->mName = "wp{$params['fieldname']}";
411                 if ( isset( $params['name'] ) ) {
412                         $this->mName = $params['name'];
413                 }
414
415                 if ( isset( $params['dir'] ) ) {
416                         $this->mDir = $params['dir'];
417                 }
418
419                 $validName = urlencode( $this->mName );
420                 $validName = str_replace( [ '%5B', '%5D' ], [ '[', ']' ], $validName );
421                 if ( $this->mName != $validName && !isset( $params['nodata'] ) ) {
422                         throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__ );
423                 }
424
425                 $this->mID = "mw-input-{$this->mName}";
426
427                 if ( isset( $params['default'] ) ) {
428                         $this->mDefault = $params['default'];
429                 }
430
431                 if ( isset( $params['id'] ) ) {
432                         $id = $params['id'];
433                         $validId = urlencode( $id );
434
435                         if ( $id != $validId ) {
436                                 throw new MWException( "Invalid id '$id' passed to " . __METHOD__ );
437                         }
438
439                         $this->mID = $id;
440                 }
441
442                 if ( isset( $params['cssclass'] ) ) {
443                         $this->mClass = $params['cssclass'];
444                 }
445
446                 if ( isset( $params['csshelpclass'] ) ) {
447                         $this->mHelpClass = $params['csshelpclass'];
448                 }
449
450                 if ( isset( $params['validation-callback'] ) ) {
451                         $this->mValidationCallback = $params['validation-callback'];
452                 }
453
454                 if ( isset( $params['filter-callback'] ) ) {
455                         $this->mFilterCallback = $params['filter-callback'];
456                 }
457
458                 if ( isset( $params['hidelabel'] ) ) {
459                         $this->mShowEmptyLabels = false;
460                 }
461
462                 if ( isset( $params['hide-if'] ) ) {
463                         $this->mHideIf = $params['hide-if'];
464                 }
465         }
466
467         /**
468          * Get the complete table row for the input, including help text,
469          * labels, and whatever.
470          *
471          * @param string $value The value to set the input to.
472          *
473          * @return string Complete HTML table row.
474          */
475         public function getTableRow( $value ) {
476                 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
477                 $inputHtml = $this->getInputHTML( $value );
478                 $fieldType = static::class;
479                 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
480                 $cellAttributes = [];
481                 $rowAttributes = [];
482                 $rowClasses = '';
483
484                 if ( !empty( $this->mParams['vertical-label'] ) ) {
485                         $cellAttributes['colspan'] = 2;
486                         $verticalLabel = true;
487                 } else {
488                         $verticalLabel = false;
489                 }
490
491                 $label = $this->getLabelHtml( $cellAttributes );
492
493                 $field = Html::rawElement(
494                         'td',
495                         [ 'class' => 'mw-input' ] + $cellAttributes,
496                         $inputHtml . "\n$errors"
497                 );
498
499                 if ( $this->mHideIf ) {
500                         $rowAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
501                         $rowClasses .= ' mw-htmlform-hide-if';
502                 }
503
504                 if ( $verticalLabel ) {
505                         $html = Html::rawElement( 'tr',
506                                 $rowAttributes + [ 'class' => "mw-htmlform-vertical-label $rowClasses" ], $label );
507                         $html .= Html::rawElement( 'tr',
508                                 $rowAttributes + [
509                                         'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
510                                 ],
511                                 $field );
512                 } else {
513                         $html =
514                                 Html::rawElement( 'tr',
515                                         $rowAttributes + [
516                                                 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
517                                         ],
518                                         $label . $field );
519                 }
520
521                 return $html . $helptext;
522         }
523
524         /**
525          * Get the complete div for the input, including help text,
526          * labels, and whatever.
527          * @since 1.20
528          *
529          * @param string $value The value to set the input to.
530          *
531          * @return string Complete HTML table row.
532          */
533         public function getDiv( $value ) {
534                 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
535                 $inputHtml = $this->getInputHTML( $value );
536                 $fieldType = static::class;
537                 $helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
538                 $cellAttributes = [];
539                 $label = $this->getLabelHtml( $cellAttributes );
540
541                 $outerDivClass = [
542                         'mw-input',
543                         'mw-htmlform-nolabel' => ( $label === '' )
544                 ];
545
546                 $horizontalLabel = isset( $this->mParams['horizontal-label'] )
547                         ? $this->mParams['horizontal-label'] : false;
548
549                 if ( $horizontalLabel ) {
550                         $field = '&#160;' . $inputHtml . "\n$errors";
551                 } else {
552                         $field = Html::rawElement(
553                                 'div',
554                                 [ 'class' => $outerDivClass ] + $cellAttributes,
555                                 $inputHtml . "\n$errors"
556                         );
557                 }
558                 $divCssClasses = [ "mw-htmlform-field-$fieldType",
559                         $this->mClass, $this->mVFormClass, $errorClass ];
560
561                 $wrapperAttributes = [
562                         'class' => $divCssClasses,
563                 ];
564                 if ( $this->mHideIf ) {
565                         $wrapperAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
566                         $wrapperAttributes['class'][] = ' mw-htmlform-hide-if';
567                 }
568                 $html = Html::rawElement( 'div', $wrapperAttributes, $label . $field );
569                 $html .= $helptext;
570
571                 return $html;
572         }
573
574         /**
575          * Get the OOUI version of the div. Falls back to getDiv by default.
576          * @since 1.26
577          *
578          * @param string $value The value to set the input to.
579          *
580          * @return OOUI\FieldLayout|OOUI\ActionFieldLayout
581          */
582         public function getOOUI( $value ) {
583                 $inputField = $this->getInputOOUI( $value );
584
585                 if ( !$inputField ) {
586                         // This field doesn't have an OOUI implementation yet at all. Fall back to getDiv() to
587                         // generate the whole field, label and errors and all, then wrap it in a Widget.
588                         // It might look weird, but it'll work OK.
589                         return $this->getFieldLayoutOOUI(
590                                 new OOUI\Widget( [ 'content' => new OOUI\HtmlSnippet( $this->getDiv( $value ) ) ] ),
591                                 [ 'infusable' => false, 'align' => 'top' ]
592                         );
593                 }
594
595                 $infusable = true;
596                 if ( is_string( $inputField ) ) {
597                         // We have an OOUI implementation, but it's not proper, and we got a load of HTML.
598                         // Cheat a little and wrap it in a widget. It won't be infusable, though, since client-side
599                         // JavaScript doesn't know how to rebuilt the contents.
600                         $inputField = new OOUI\Widget( [ 'content' => new OOUI\HtmlSnippet( $inputField ) ] );
601                         $infusable = false;
602                 }
603
604                 $fieldType = static::class;
605                 $help = $this->getHelpText();
606                 $errors = $this->getErrorsRaw( $value );
607                 foreach ( $errors as &$error ) {
608                         $error = new OOUI\HtmlSnippet( $error );
609                 }
610
611                 $notices = $this->getNotices();
612                 foreach ( $notices as &$notice ) {
613                         $notice = new OOUI\HtmlSnippet( $notice );
614                 }
615
616                 $config = [
617                         'classes' => [ "mw-htmlform-field-$fieldType", $this->mClass ],
618                         'align' => $this->getLabelAlignOOUI(),
619                         'help' => ( $help !== null && $help !== '' ) ? new OOUI\HtmlSnippet( $help ) : null,
620                         'errors' => $errors,
621                         'notices' => $notices,
622                         'infusable' => $infusable,
623                 ];
624
625                 $preloadModules = false;
626
627                 if ( $infusable && $this->shouldInfuseOOUI() ) {
628                         $preloadModules = true;
629                         $config['classes'][] = 'mw-htmlform-field-autoinfuse';
630                 }
631
632                 // the element could specify, that the label doesn't need to be added
633                 $label = $this->getLabel();
634                 if ( $label ) {
635                         $config['label'] = new OOUI\HtmlSnippet( $label );
636                 }
637
638                 if ( $this->mHideIf ) {
639                         $preloadModules = true;
640                         $config['hideIf'] = $this->mHideIf;
641                 }
642
643                 $config['modules'] = $this->getOOUIModules();
644
645                 if ( $preloadModules ) {
646                         $this->mParent->getOutput()->addModules( 'mediawiki.htmlform.ooui' );
647                         $this->mParent->getOutput()->addModules( $this->getOOUIModules() );
648                 }
649
650                 return $this->getFieldLayoutOOUI( $inputField, $config );
651         }
652
653         /**
654          * Get label alignment when generating field for OOUI.
655          * @return string 'left', 'right', 'top' or 'inline'
656          */
657         protected function getLabelAlignOOUI() {
658                 return 'top';
659         }
660
661         /**
662          * Get a FieldLayout (or subclass thereof) to wrap this field in when using OOUI output.
663          * @param string $inputField
664          * @param array $config
665          * @return OOUI\FieldLayout|OOUI\ActionFieldLayout
666          */
667         protected function getFieldLayoutOOUI( $inputField, $config ) {
668                 if ( isset( $this->mClassWithButton ) ) {
669                         $buttonWidget = $this->mClassWithButton->getInputOOUI( '' );
670                         return new HTMLFormActionFieldLayout( $inputField, $buttonWidget, $config );
671                 }
672                 return new HTMLFormFieldLayout( $inputField, $config );
673         }
674
675         /**
676          * Whether the field should be automatically infused. Note that all OOjs UI HTMLForm fields are
677          * infusable (you can call OO.ui.infuse() on them), but not all are infused by default, since
678          * there is no benefit in doing it e.g. for buttons and it's a small performance hit on page load.
679          *
680          * @return bool
681          */
682         protected function shouldInfuseOOUI() {
683                 // Always infuse fields with help text, since the interface for it is nicer with JS
684                 return $this->getHelpText() !== null;
685         }
686
687         /**
688          * Get the list of extra ResourceLoader modules which must be loaded client-side before it's
689          * possible to infuse this field's OOjs UI widget.
690          *
691          * @return string[]
692          */
693         protected function getOOUIModules() {
694                 return [];
695         }
696
697         /**
698          * Get the complete raw fields for the input, including help text,
699          * labels, and whatever.
700          * @since 1.20
701          *
702          * @param string $value The value to set the input to.
703          *
704          * @return string Complete HTML table row.
705          */
706         public function getRaw( $value ) {
707                 list( $errors, ) = $this->getErrorsAndErrorClass( $value );
708                 $inputHtml = $this->getInputHTML( $value );
709                 $helptext = $this->getHelpTextHtmlRaw( $this->getHelpText() );
710                 $cellAttributes = [];
711                 $label = $this->getLabelHtml( $cellAttributes );
712
713                 $html = "\n$errors";
714                 $html .= $label;
715                 $html .= $inputHtml;
716                 $html .= $helptext;
717
718                 return $html;
719         }
720
721         /**
722          * Get the complete field for the input, including help text,
723          * labels, and whatever. Fall back from 'vform' to 'div' when not overridden.
724          *
725          * @since 1.25
726          * @param string $value The value to set the input to.
727          * @return string Complete HTML field.
728          */
729         public function getVForm( $value ) {
730                 // Ewwww
731                 $this->mVFormClass = ' mw-ui-vform-field';
732                 return $this->getDiv( $value );
733         }
734
735         /**
736          * Get the complete field as an inline element.
737          * @since 1.25
738          * @param string $value The value to set the input to.
739          * @return string Complete HTML inline element
740          */
741         public function getInline( $value ) {
742                 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
743                 $inputHtml = $this->getInputHTML( $value );
744                 $helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
745                 $cellAttributes = [];
746                 $label = $this->getLabelHtml( $cellAttributes );
747
748                 $html = "\n" . $errors .
749                         $label . '&#160;' .
750                         $inputHtml .
751                         $helptext;
752
753                 return $html;
754         }
755
756         /**
757          * Generate help text HTML in table format
758          * @since 1.20
759          *
760          * @param string|null $helptext
761          * @return string
762          */
763         public function getHelpTextHtmlTable( $helptext ) {
764                 if ( is_null( $helptext ) ) {
765                         return '';
766                 }
767
768                 $rowAttributes = [];
769                 if ( $this->mHideIf ) {
770                         $rowAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
771                         $rowAttributes['class'] = 'mw-htmlform-hide-if';
772                 }
773
774                 $tdClasses = [ 'htmlform-tip' ];
775                 if ( $this->mHelpClass !== false ) {
776                         $tdClasses[] = $this->mHelpClass;
777                 }
778                 $row = Html::rawElement( 'td', [ 'colspan' => 2, 'class' => $tdClasses ], $helptext );
779                 $row = Html::rawElement( 'tr', $rowAttributes, $row );
780
781                 return $row;
782         }
783
784         /**
785          * Generate help text HTML in div format
786          * @since 1.20
787          *
788          * @param string|null $helptext
789          *
790          * @return string
791          */
792         public function getHelpTextHtmlDiv( $helptext ) {
793                 if ( is_null( $helptext ) ) {
794                         return '';
795                 }
796
797                 $wrapperAttributes = [
798                         'class' => 'htmlform-tip',
799                 ];
800                 if ( $this->mHelpClass !== false ) {
801                         $wrapperAttributes['class'] .= " {$this->mHelpClass}";
802                 }
803                 if ( $this->mHideIf ) {
804                         $wrapperAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
805                         $wrapperAttributes['class'] .= ' mw-htmlform-hide-if';
806                 }
807                 $div = Html::rawElement( 'div', $wrapperAttributes, $helptext );
808
809                 return $div;
810         }
811
812         /**
813          * Generate help text HTML formatted for raw output
814          * @since 1.20
815          *
816          * @param string|null $helptext
817          * @return string
818          */
819         public function getHelpTextHtmlRaw( $helptext ) {
820                 return $this->getHelpTextHtmlDiv( $helptext );
821         }
822
823         /**
824          * Determine the help text to display
825          * @since 1.20
826          * @return string|null HTML
827          */
828         public function getHelpText() {
829                 $helptext = null;
830
831                 if ( isset( $this->mParams['help-message'] ) ) {
832                         $this->mParams['help-messages'] = [ $this->mParams['help-message'] ];
833                 }
834
835                 if ( isset( $this->mParams['help-messages'] ) ) {
836                         foreach ( $this->mParams['help-messages'] as $msg ) {
837                                 $msg = $this->getMessage( $msg );
838
839                                 if ( $msg->exists() ) {
840                                         if ( is_null( $helptext ) ) {
841                                                 $helptext = '';
842                                         } else {
843                                                 $helptext .= $this->msg( 'word-separator' )->escaped(); // some space
844                                         }
845                                         $helptext .= $msg->parse(); // Append message
846                                 }
847                         }
848                 } elseif ( isset( $this->mParams['help'] ) ) {
849                         $helptext = $this->mParams['help'];
850                 }
851
852                 return $helptext;
853         }
854
855         /**
856          * Determine form errors to display and their classes
857          * @since 1.20
858          *
859          * @param string $value The value of the input
860          * @return array array( $errors, $errorClass )
861          */
862         public function getErrorsAndErrorClass( $value ) {
863                 $errors = $this->validate( $value, $this->mParent->mFieldData );
864
865                 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
866                         $errors = '';
867                         $errorClass = '';
868                 } else {
869                         $errors = self::formatErrors( $errors );
870                         $errorClass = 'mw-htmlform-invalid-input';
871                 }
872
873                 return [ $errors, $errorClass ];
874         }
875
876         /**
877          * Determine form errors to display, returning them in an array.
878          *
879          * @since 1.26
880          * @param string $value The value of the input
881          * @return string[] Array of error HTML strings
882          */
883         public function getErrorsRaw( $value ) {
884                 $errors = $this->validate( $value, $this->mParent->mFieldData );
885
886                 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
887                         $errors = [];
888                 }
889
890                 if ( !is_array( $errors ) ) {
891                         $errors = [ $errors ];
892                 }
893                 foreach ( $errors as &$error ) {
894                         if ( $error instanceof Message ) {
895                                 $error = $error->parse();
896                         }
897                 }
898
899                 return $errors;
900         }
901
902         /**
903          * Determine notices to display for the field.
904          *
905          * @since 1.28
906          * @return string[]
907          */
908         public function getNotices() {
909                 $notices = [];
910
911                 if ( isset( $this->mParams['notice-message'] ) ) {
912                         $notices[] = $this->getMessage( $this->mParams['notice-message'] )->parse();
913                 }
914
915                 if ( isset( $this->mParams['notice-messages'] ) ) {
916                         foreach ( $this->mParams['notice-messages'] as $msg ) {
917                                 $notices[] = $this->getMessage( $msg )->parse();
918                         }
919                 } elseif ( isset( $this->mParams['notice'] ) ) {
920                         $notices[] = $this->mParams['notice'];
921                 }
922
923                 return $notices;
924         }
925
926         /**
927          * @return string HTML
928          */
929         public function getLabel() {
930                 return is_null( $this->mLabel ) ? '' : $this->mLabel;
931         }
932
933         public function getLabelHtml( $cellAttributes = [] ) {
934                 # Don't output a for= attribute for labels with no associated input.
935                 # Kind of hacky here, possibly we don't want these to be <label>s at all.
936                 $for = [];
937
938                 if ( $this->needsLabel() ) {
939                         $for['for'] = $this->mID;
940                 }
941
942                 $labelValue = trim( $this->getLabel() );
943                 $hasLabel = false;
944                 if ( $labelValue !== '&#160;' && $labelValue !== '' ) {
945                         $hasLabel = true;
946                 }
947
948                 $displayFormat = $this->mParent->getDisplayFormat();
949                 $html = '';
950                 $horizontalLabel = isset( $this->mParams['horizontal-label'] )
951                         ? $this->mParams['horizontal-label'] : false;
952
953                 if ( $displayFormat === 'table' ) {
954                         $html =
955                                 Html::rawElement( 'td',
956                                         [ 'class' => 'mw-label' ] + $cellAttributes,
957                                         Html::rawElement( 'label', $for, $labelValue ) );
958                 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
959                         if ( $displayFormat === 'div' && !$horizontalLabel ) {
960                                 $html =
961                                         Html::rawElement( 'div',
962                                                 [ 'class' => 'mw-label' ] + $cellAttributes,
963                                                 Html::rawElement( 'label', $for, $labelValue ) );
964                         } else {
965                                 $html = Html::rawElement( 'label', $for, $labelValue );
966                         }
967                 }
968
969                 return $html;
970         }
971
972         public function getDefault() {
973                 if ( isset( $this->mDefault ) ) {
974                         return $this->mDefault;
975                 } else {
976                         return null;
977                 }
978         }
979
980         /**
981          * Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
982          *
983          * @return array Attributes
984          */
985         public function getTooltipAndAccessKey() {
986                 if ( empty( $this->mParams['tooltip'] ) ) {
987                         return [];
988                 }
989
990                 return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] );
991         }
992
993         /**
994          * Returns the attributes required for the tooltip and accesskey, for OOUI widgets' config.
995          *
996          * @return array Attributes
997          */
998         public function getTooltipAndAccessKeyOOUI() {
999                 if ( empty( $this->mParams['tooltip'] ) ) {
1000                         return [];
1001                 }
1002
1003                 return [
1004                         'title' => Linker::titleAttrib( $this->mParams['tooltip'] ),
1005                         'accessKey' => Linker::accesskey( $this->mParams['tooltip'] ),
1006                 ];
1007         }
1008
1009         /**
1010          * Returns the given attributes from the parameters
1011          *
1012          * @param array $list List of attributes to get
1013          * @return array Attributes
1014          */
1015         public function getAttributes( array $list ) {
1016                 static $boolAttribs = [ 'disabled', 'required', 'autofocus', 'multiple', 'readonly' ];
1017
1018                 $ret = [];
1019                 foreach ( $list as $key ) {
1020                         if ( in_array( $key, $boolAttribs ) ) {
1021                                 if ( !empty( $this->mParams[$key] ) ) {
1022                                         $ret[$key] = '';
1023                                 }
1024                         } elseif ( isset( $this->mParams[$key] ) ) {
1025                                 $ret[$key] = $this->mParams[$key];
1026                         }
1027                 }
1028
1029                 return $ret;
1030         }
1031
1032         /**
1033          * Given an array of msg-key => value mappings, returns an array with keys
1034          * being the message texts. It also forces values to strings.
1035          *
1036          * @param array $options
1037          * @return array
1038          */
1039         private function lookupOptionsKeys( $options ) {
1040                 $ret = [];
1041                 foreach ( $options as $key => $value ) {
1042                         $key = $this->msg( $key )->plain();
1043                         $ret[$key] = is_array( $value )
1044                                 ? $this->lookupOptionsKeys( $value )
1045                                 : strval( $value );
1046                 }
1047                 return $ret;
1048         }
1049
1050         /**
1051          * Recursively forces values in an array to strings, because issues arise
1052          * with integer 0 as a value.
1053          *
1054          * @param array $array
1055          * @return array|string
1056          */
1057         public static function forceToStringRecursive( $array ) {
1058                 if ( is_array( $array ) ) {
1059                         return array_map( [ __CLASS__, 'forceToStringRecursive' ], $array );
1060                 } else {
1061                         return strval( $array );
1062                 }
1063         }
1064
1065         /**
1066          * Fetch the array of options from the field's parameters. In order, this
1067          * checks 'options-messages', 'options', then 'options-message'.
1068          *
1069          * @return array|null Options array
1070          */
1071         public function getOptions() {
1072                 if ( $this->mOptions === false ) {
1073                         if ( array_key_exists( 'options-messages', $this->mParams ) ) {
1074                                 $this->mOptions = $this->lookupOptionsKeys( $this->mParams['options-messages'] );
1075                         } elseif ( array_key_exists( 'options', $this->mParams ) ) {
1076                                 $this->mOptionsLabelsNotFromMessage = true;
1077                                 $this->mOptions = self::forceToStringRecursive( $this->mParams['options'] );
1078                         } elseif ( array_key_exists( 'options-message', $this->mParams ) ) {
1079                                 $message = $this->getMessage( $this->mParams['options-message'] )->inContentLanguage()->plain();
1080                                 $this->mOptions = Xml::listDropDownOptions( $message );
1081                         } else {
1082                                 $this->mOptions = null;
1083                         }
1084                 }
1085
1086                 return $this->mOptions;
1087         }
1088
1089         /**
1090          * Get options and make them into arrays suitable for OOUI.
1091          * @return array Options for inclusion in a select or whatever.
1092          */
1093         public function getOptionsOOUI() {
1094                 $oldoptions = $this->getOptions();
1095
1096                 if ( $oldoptions === null ) {
1097                         return null;
1098                 }
1099
1100                 return Xml::listDropDownOptionsOoui( $oldoptions );
1101         }
1102
1103         /**
1104          * flatten an array of options to a single array, for instance,
1105          * a set of "<options>" inside "<optgroups>".
1106          *
1107          * @param array $options Associative Array with values either Strings or Arrays
1108          * @return array Flattened input
1109          */
1110         public static function flattenOptions( $options ) {
1111                 $flatOpts = [];
1112
1113                 foreach ( $options as $value ) {
1114                         if ( is_array( $value ) ) {
1115                                 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1116                         } else {
1117                                 $flatOpts[] = $value;
1118                         }
1119                 }
1120
1121                 return $flatOpts;
1122         }
1123
1124         /**
1125          * Formats one or more errors as accepted by field validation-callback.
1126          *
1127          * @param string|Message|array $errors Array of strings or Message instances
1128          * @return string HTML
1129          * @since 1.18
1130          */
1131         protected static function formatErrors( $errors ) {
1132                 // Note: If you change the logic in this method, change
1133                 // htmlform.Checker.js to match.
1134
1135                 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1136                         $errors = array_shift( $errors );
1137                 }
1138
1139                 if ( is_array( $errors ) ) {
1140                         $lines = [];
1141                         foreach ( $errors as $error ) {
1142                                 if ( $error instanceof Message ) {
1143                                         $lines[] = Html::rawElement( 'li', [], $error->parse() );
1144                                 } else {
1145                                         $lines[] = Html::rawElement( 'li', [], $error );
1146                                 }
1147                         }
1148
1149                         return Html::rawElement( 'ul', [ 'class' => 'error' ], implode( "\n", $lines ) );
1150                 } else {
1151                         if ( $errors instanceof Message ) {
1152                                 $errors = $errors->parse();
1153                         }
1154
1155                         return Html::rawElement( 'span', [ 'class' => 'error' ], $errors );
1156                 }
1157         }
1158
1159         /**
1160          * Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a
1161          * name + parameters array) into a Message.
1162          * @param mixed $value
1163          * @return Message
1164          */
1165         protected function getMessage( $value ) {
1166                 $message = Message::newFromSpecifier( $value );
1167
1168                 if ( $this->mParent ) {
1169                         $message->setContext( $this->mParent );
1170                 }
1171
1172                 return $message;
1173         }
1174
1175         /**
1176          * Skip this field when collecting data.
1177          * @param WebRequest $request
1178          * @return bool
1179          * @since 1.27
1180          */
1181         public function skipLoadData( $request ) {
1182                 return !empty( $this->mParams['nodata'] );
1183         }
1184
1185         /**
1186          * Whether this field requires the user agent to have JavaScript enabled for the client-side HTML5
1187          * form validation to work correctly.
1188          *
1189          * @return bool
1190          * @since 1.29
1191          */
1192         public function needsJSForHtml5FormValidation() {
1193                 if ( $this->mHideIf ) {
1194                         // This is probably more restrictive than it needs to be, but better safe than sorry
1195                         return true;
1196                 }
1197                 return false;
1198         }
1199 }