MediaWiki REL1_34
HTMLFormField.php
Go to the documentation of this file.
1<?php
2
7abstract class HTMLFormField {
9 public $mParams;
10
13 protected $mName;
14 protected $mDir;
15 protected $mLabel; # String label, as HTML. Set on construction.
16 protected $mID;
17 protected $mClass = '';
18 protected $mVFormClass = '';
19 protected $mHelpClass = false;
20 protected $mDefault;
24 protected $mOptions = false;
26 protected $mHideIf = null;
27
32 protected $mShowEmptyLabels = true;
33
37 public $mParent;
38
49 abstract public function getInputHTML( $value );
50
58 public function getInputOOUI( $value ) {
59 return false;
60 }
61
67 public function canDisplayErrors() {
68 return $this->hasVisibleOutput();
69 }
70
83 public function msg( $key, ...$params ) {
84 if ( $this->mParent ) {
85 return $this->mParent->msg( $key, ...$params );
86 }
87 return wfMessage( $key, ...$params );
88 }
89
96 public function hasVisibleOutput() {
97 return true;
98 }
99
113 protected function getNearestFieldByName( $alldata, $name ) {
114 $tmp = $this->mName;
115 $thisKeys = [];
116 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
117 array_unshift( $thisKeys, $m[2] );
118 $tmp = $m[1];
119 }
120 if ( substr( $tmp, 0, 2 ) == 'wp' &&
121 !array_key_exists( $tmp, $alldata ) &&
122 array_key_exists( substr( $tmp, 2 ), $alldata )
123 ) {
124 // Adjust for name mangling.
125 $tmp = substr( $tmp, 2 );
126 }
127 array_unshift( $thisKeys, $tmp );
128
129 $tmp = $name;
130 $nameKeys = [];
131 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
132 array_unshift( $nameKeys, $m[2] );
133 $tmp = $m[1];
134 }
135 array_unshift( $nameKeys, $tmp );
136
137 $testValue = '';
138 for ( $i = count( $thisKeys ) - 1; $i >= 0; $i-- ) {
139 $keys = array_merge( array_slice( $thisKeys, 0, $i ), $nameKeys );
140 $data = $alldata;
141 foreach ( $keys as $key ) {
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
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
269 public function isHidden( $alldata ) {
270 if ( !$this->mHideIf ) {
271 return false;
272 }
273
274 return $this->isHiddenRecurse( $alldata, $this->mHideIf );
275 }
276
287 public function cancelSubmit( $value, $alldata ) {
288 return false;
289 }
290
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 ( $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 = ( $this->mFilterCallback )( $value, $alldata, $this->mParent );
324 }
325
326 return $value;
327 }
328
335 protected function needsLabel() {
336 return true;
337 }
338
348 public function setShowEmptyLabel( $show ) {
349 $this->mShowEmptyLabels = $show;
350 }
351
362 protected function isSubmitAttempt( WebRequest $request ) {
363 return $request->getCheck( 'wpEditToken' ) || $request->getCheck( 'wpFormIdentifier' );
364 }
365
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
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;' || $params['label'] === "\u{00A0}" ) {
401 // Apparently some things set &nbsp directly and in an odd format
402 $this->mLabel = "\u{00A0}";
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
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
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 = $this->mParams['horizontal-label'] ?? false;
547
548 if ( $horizontalLabel ) {
549 $field = "\u{00A0}" . $inputHtml . "\n$errors";
550 } else {
551 $field = Html::rawElement(
552 'div',
553 [ 'class' => $outerDivClass ] + $cellAttributes,
554 $inputHtml . "\n$errors"
555 );
556 }
557 $divCssClasses = [ "mw-htmlform-field-$fieldType",
558 $this->mClass, $this->mVFormClass, $errorClass ];
559
560 $wrapperAttributes = [
561 'class' => $divCssClasses,
562 ];
563 if ( $this->mHideIf ) {
564 $wrapperAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
565 $wrapperAttributes['class'][] = ' mw-htmlform-hide-if';
566 }
567 $html = Html::rawElement( 'div', $wrapperAttributes, $label . $field );
568 $html .= $helptext;
569
570 return $html;
571 }
572
581 public function getOOUI( $value ) {
582 $inputField = $this->getInputOOUI( $value );
583
584 if ( !$inputField ) {
585 // This field doesn't have an OOUI implementation yet at all. Fall back to getDiv() to
586 // generate the whole field, label and errors and all, then wrap it in a Widget.
587 // It might look weird, but it'll work OK.
588 return $this->getFieldLayoutOOUI(
589 new OOUI\Widget( [ 'content' => new OOUI\HtmlSnippet( $this->getDiv( $value ) ) ] ),
590 [ 'align' => 'top' ]
591 );
592 }
593
594 $infusable = true;
595 if ( is_string( $inputField ) ) {
596 // We have an OOUI implementation, but it's not proper, and we got a load of HTML.
597 // Cheat a little and wrap it in a widget. It won't be infusable, though, since client-side
598 // JavaScript doesn't know how to rebuilt the contents.
599 $inputField = new OOUI\Widget( [ 'content' => new OOUI\HtmlSnippet( $inputField ) ] );
600 $infusable = false;
601 }
602
603 $fieldType = static::class;
604 $help = $this->getHelpText();
605 $errors = $this->getErrorsRaw( $value );
606 foreach ( $errors as &$error ) {
607 $error = new OOUI\HtmlSnippet( $error );
608 }
609
610 $config = [
611 'classes' => [ "mw-htmlform-field-$fieldType", $this->mClass ],
612 'align' => $this->getLabelAlignOOUI(),
613 'help' => ( $help !== null && $help !== '' ) ? new OOUI\HtmlSnippet( $help ) : null,
614 'errors' => $errors,
615 'infusable' => $infusable,
616 'helpInline' => $this->isHelpInline(),
617 ];
618
619 $preloadModules = false;
620
621 if ( $infusable && $this->shouldInfuseOOUI() ) {
622 $preloadModules = true;
623 $config['classes'][] = 'mw-htmlform-field-autoinfuse';
624 }
625
626 // the element could specify, that the label doesn't need to be added
627 $label = $this->getLabel();
628 if ( $label && $label !== "\u{00A0}" && $label !== '&#160;' ) {
629 $config['label'] = new OOUI\HtmlSnippet( $label );
630 }
631
632 if ( $this->mHideIf ) {
633 $preloadModules = true;
634 $config['hideIf'] = $this->mHideIf;
635 }
636
637 $config['modules'] = $this->getOOUIModules();
638
639 if ( $preloadModules ) {
640 $this->mParent->getOutput()->addModules( 'mediawiki.htmlform.ooui' );
641 $this->mParent->getOutput()->addModules( $this->getOOUIModules() );
642 }
643
644 return $this->getFieldLayoutOOUI( $inputField, $config );
645 }
646
651 protected function getLabelAlignOOUI() {
652 return 'top';
653 }
654
662 protected function getFieldLayoutOOUI( $inputField, $config ) {
663 if ( isset( $this->mClassWithButton ) ) {
664 $buttonWidget = $this->mClassWithButton->getInputOOUI( '' );
665 return new HTMLFormActionFieldLayout( $inputField, $buttonWidget, $config );
666 }
667 return new HTMLFormFieldLayout( $inputField, $config );
668 }
669
677 protected function shouldInfuseOOUI() {
678 // Always infuse fields with popup help text, since the interface for it is nicer with JS
679 return $this->getHelpText() !== null && !$this->isHelpInline();
680 }
681
688 protected function getOOUIModules() {
689 return [];
690 }
691
701 public function getRaw( $value ) {
702 list( $errors, ) = $this->getErrorsAndErrorClass( $value );
703 $inputHtml = $this->getInputHTML( $value );
704 $helptext = $this->getHelpTextHtmlRaw( $this->getHelpText() );
705 $cellAttributes = [];
706 $label = $this->getLabelHtml( $cellAttributes );
707
708 $html = "\n$errors";
709 $html .= $label;
710 $html .= $inputHtml;
711 $html .= $helptext;
712
713 return $html;
714 }
715
724 public function getVForm( $value ) {
725 // Ewwww
726 $this->mVFormClass = ' mw-ui-vform-field';
727 return $this->getDiv( $value );
728 }
729
736 public function getInline( $value ) {
737 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
738 $inputHtml = $this->getInputHTML( $value );
739 $helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
740 $cellAttributes = [];
741 $label = $this->getLabelHtml( $cellAttributes );
742
743 $html = "\n" . $errors .
744 $label . "\u{00A0}" .
745 $inputHtml .
746 $helptext;
747
748 return $html;
749 }
750
758 public function getHelpTextHtmlTable( $helptext ) {
759 if ( is_null( $helptext ) ) {
760 return '';
761 }
762
763 $rowAttributes = [];
764 if ( $this->mHideIf ) {
765 $rowAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
766 $rowAttributes['class'] = 'mw-htmlform-hide-if';
767 }
768
769 $tdClasses = [ 'htmlform-tip' ];
770 if ( $this->mHelpClass !== false ) {
771 $tdClasses[] = $this->mHelpClass;
772 }
773 $row = Html::rawElement( 'td', [ 'colspan' => 2, 'class' => $tdClasses ], $helptext );
774 $row = Html::rawElement( 'tr', $rowAttributes, $row );
775
776 return $row;
777 }
778
787 public function getHelpTextHtmlDiv( $helptext ) {
788 if ( is_null( $helptext ) ) {
789 return '';
790 }
791
792 $wrapperAttributes = [
793 'class' => 'htmlform-tip',
794 ];
795 if ( $this->mHelpClass !== false ) {
796 $wrapperAttributes['class'] .= " {$this->mHelpClass}";
797 }
798 if ( $this->mHideIf ) {
799 $wrapperAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
800 $wrapperAttributes['class'] .= ' mw-htmlform-hide-if';
801 }
802 $div = Html::rawElement( 'div', $wrapperAttributes, $helptext );
803
804 return $div;
805 }
806
814 public function getHelpTextHtmlRaw( $helptext ) {
815 return $this->getHelpTextHtmlDiv( $helptext );
816 }
817
823 public function getHelpText() {
824 $helptext = null;
825
826 if ( isset( $this->mParams['help-message'] ) ) {
827 $this->mParams['help-messages'] = [ $this->mParams['help-message'] ];
828 }
829
830 if ( isset( $this->mParams['help-messages'] ) ) {
831 foreach ( $this->mParams['help-messages'] as $msg ) {
832 $msg = $this->getMessage( $msg );
833
834 if ( $msg->exists() ) {
835 if ( is_null( $helptext ) ) {
836 $helptext = '';
837 } else {
838 $helptext .= $this->msg( 'word-separator' )->escaped(); // some space
839 }
840 $helptext .= $msg->parse(); // Append message
841 }
842 }
843 } elseif ( isset( $this->mParams['help'] ) ) {
844 $helptext = $this->mParams['help'];
845 }
846
847 return $helptext;
848 }
849
858 public function isHelpInline() {
859 return $this->mParams['help-inline'] ?? true;
860 }
861
874 public function getErrorsAndErrorClass( $value ) {
875 $errors = $this->validate( $value, $this->mParent->mFieldData );
876
877 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
878 $errors = '';
879 $errorClass = '';
880 } else {
881 $errors = self::formatErrors( $errors );
882 $errorClass = 'mw-htmlform-invalid-input';
883 }
884
885 return [ $errors, $errorClass ];
886 }
887
895 public function getErrorsRaw( $value ) {
896 $errors = $this->validate( $value, $this->mParent->mFieldData );
897
898 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
899 $errors = [];
900 }
901
902 if ( !is_array( $errors ) ) {
903 $errors = [ $errors ];
904 }
905 foreach ( $errors as &$error ) {
906 if ( $error instanceof Message ) {
907 $error = $error->parse();
908 }
909 }
910
911 return $errors;
912 }
913
917 public function getLabel() {
918 return $this->mLabel ?? '';
919 }
920
921 public function getLabelHtml( $cellAttributes = [] ) {
922 # Don't output a for= attribute for labels with no associated input.
923 # Kind of hacky here, possibly we don't want these to be <label>s at all.
924 $for = [];
925
926 if ( $this->needsLabel() ) {
927 $for['for'] = $this->mID;
928 }
929
930 $labelValue = trim( $this->getLabel() );
931 $hasLabel = false;
932 if ( $labelValue !== "\u{00A0}" && $labelValue !== '&#160;' && $labelValue !== '' ) {
933 $hasLabel = true;
934 }
935
936 $displayFormat = $this->mParent->getDisplayFormat();
937 $html = '';
938 $horizontalLabel = $this->mParams['horizontal-label'] ?? false;
939
940 if ( $displayFormat === 'table' ) {
941 $html =
942 Html::rawElement( 'td',
943 [ 'class' => 'mw-label' ] + $cellAttributes,
944 Html::rawElement( 'label', $for, $labelValue ) );
945 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
946 if ( $displayFormat === 'div' && !$horizontalLabel ) {
947 $html =
948 Html::rawElement( 'div',
949 [ 'class' => 'mw-label' ] + $cellAttributes,
950 Html::rawElement( 'label', $for, $labelValue ) );
951 } else {
952 $html = Html::rawElement( 'label', $for, $labelValue );
953 }
954 }
955
956 return $html;
957 }
958
959 public function getDefault() {
960 return $this->mDefault ?? null;
961 }
962
968 public function getTooltipAndAccessKey() {
969 if ( empty( $this->mParams['tooltip'] ) ) {
970 return [];
971 }
972
973 return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] );
974 }
975
981 public function getTooltipAndAccessKeyOOUI() {
982 if ( empty( $this->mParams['tooltip'] ) ) {
983 return [];
984 }
985
986 return [
987 'title' => Linker::titleAttrib( $this->mParams['tooltip'] ),
988 'accessKey' => Linker::accesskey( $this->mParams['tooltip'] ),
989 ];
990 }
991
998 public function getAttributes( array $list ) {
999 static $boolAttribs = [ 'disabled', 'required', 'autofocus', 'multiple', 'readonly' ];
1000
1001 $ret = [];
1002 foreach ( $list as $key ) {
1003 if ( in_array( $key, $boolAttribs ) ) {
1004 if ( !empty( $this->mParams[$key] ) ) {
1005 $ret[$key] = '';
1006 }
1007 } elseif ( isset( $this->mParams[$key] ) ) {
1008 $ret[$key] = $this->mParams[$key];
1009 }
1010 }
1011
1012 return $ret;
1013 }
1014
1022 private function lookupOptionsKeys( $options ) {
1023 $ret = [];
1024 foreach ( $options as $key => $value ) {
1025 $key = $this->msg( $key )->plain();
1026 $ret[$key] = is_array( $value )
1027 ? $this->lookupOptionsKeys( $value )
1028 : strval( $value );
1029 }
1030 return $ret;
1031 }
1032
1040 public static function forceToStringRecursive( $array ) {
1041 if ( is_array( $array ) ) {
1042 return array_map( [ __CLASS__, 'forceToStringRecursive' ], $array );
1043 } else {
1044 return strval( $array );
1045 }
1046 }
1047
1054 public function getOptions() {
1055 if ( $this->mOptions === false ) {
1056 if ( array_key_exists( 'options-messages', $this->mParams ) ) {
1057 $this->mOptions = $this->lookupOptionsKeys( $this->mParams['options-messages'] );
1058 } elseif ( array_key_exists( 'options', $this->mParams ) ) {
1059 $this->mOptionsLabelsNotFromMessage = true;
1060 $this->mOptions = self::forceToStringRecursive( $this->mParams['options'] );
1061 } elseif ( array_key_exists( 'options-message', $this->mParams ) ) {
1062 $message = $this->getMessage( $this->mParams['options-message'] )->inContentLanguage()->plain();
1063 $this->mOptions = Xml::listDropDownOptions( $message );
1064 } else {
1065 $this->mOptions = null;
1066 }
1067 }
1068
1069 return $this->mOptions;
1070 }
1071
1076 public function getOptionsOOUI() {
1077 $oldoptions = $this->getOptions();
1078
1079 if ( $oldoptions === null ) {
1080 return null;
1081 }
1082
1083 return Xml::listDropDownOptionsOoui( $oldoptions );
1084 }
1085
1093 public static function flattenOptions( $options ) {
1094 $flatOpts = [];
1095
1096 foreach ( $options as $value ) {
1097 if ( is_array( $value ) ) {
1098 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1099 } else {
1100 $flatOpts[] = $value;
1101 }
1102 }
1103
1104 return $flatOpts;
1105 }
1106
1120 protected static function formatErrors( $errors ) {
1121 // Note: If you change the logic in this method, change
1122 // htmlform.Checker.js to match.
1123
1124 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1125 $errors = array_shift( $errors );
1126 }
1127
1128 if ( is_array( $errors ) ) {
1129 $lines = [];
1130 foreach ( $errors as $error ) {
1131 if ( $error instanceof Message ) {
1132 $lines[] = Html::rawElement( 'li', [], $error->parse() );
1133 } else {
1134 $lines[] = Html::rawElement( 'li', [], $error );
1135 }
1136 }
1137
1138 return Html::rawElement( 'ul', [ 'class' => 'error' ], implode( "\n", $lines ) );
1139 } else {
1140 if ( $errors instanceof Message ) {
1141 $errors = $errors->parse();
1142 }
1143
1144 return Html::rawElement( 'span', [ 'class' => 'error' ], $errors );
1145 }
1146 }
1147
1154 protected function getMessage( $value ) {
1155 $message = Message::newFromSpecifier( $value );
1156
1157 if ( $this->mParent ) {
1158 $message->setContext( $this->mParent );
1159 }
1160
1161 return $message;
1162 }
1163
1170 public function skipLoadData( $request ) {
1171 return !empty( $this->mParams['nodata'] );
1172 }
1173
1182 if ( $this->mHideIf ) {
1183 // This is probably more restrictive than it needs to be, but better safe than sorry
1184 return true;
1185 }
1186 return false;
1187 }
1188}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
The parent class to generate form fields.
getHelpTextHtmlDiv( $helptext)
Generate help text HTML in div format.
needsJSForHtml5FormValidation()
Whether this field requires the user agent to have JavaScript enabled for the client-side HTML5 form ...
HTMLForm null $mParent
getLabelAlignOOUI()
Get label alignment when generating field for OOUI.
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
getHelpTextHtmlRaw( $helptext)
Generate help text HTML formatted for raw output.
isHelpInline()
Determine if the help text should be displayed inline.
getFieldLayoutOOUI( $inputField, $config)
Get a FieldLayout (or subclass thereof) to wrap this field in when using OOUI output.
isHidden( $alldata)
Test whether this field is supposed to be hidden, based on the values of the other form fields.
filter( $value, $alldata)
isHiddenRecurse(array $alldata, array $params)
Helper function for isHidden to handle recursive data structures.
bool $mShowEmptyLabels
If true will generate an empty div element with no label.
shouldInfuseOOUI()
Whether the field should be automatically infused.
__construct( $params)
Initialise the object.
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
skipLoadData( $request)
Skip this field when collecting data.
getVForm( $value)
Get the complete field for the input, including help text, labels, and whatever.
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself.
getErrorsAndErrorClass( $value)
Determine form errors to display and their classes.
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
getOOUI( $value)
Get the OOUI version of the div.
lookupOptionsKeys( $options)
Given an array of msg-key => value mappings, returns an array with keys being the message texts.
canDisplayErrors()
True if this field type is able to display errors; false if validation errors need to be displayed in...
getHelpTextHtmlTable( $helptext)
Generate help text HTML in table format.
array array[] $mParams
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
getRaw( $value)
Get the complete raw fields for the input, including help text, labels, and whatever.
getTooltipAndAccessKeyOOUI()
Returns the attributes required for the tooltip and accesskey, for OOUI widgets' config.
getErrorsRaw( $value)
Determine form errors to display, returning them in an array.
getHelpText()
Determine the help text to display.
getOptions()
Fetch the array of options from the field's parameters.
getTableRow( $value)
Get the complete table row for the input, including help text, labels, and whatever.
getInline( $value)
Get the complete field as an inline element.
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
array bool null $mOptions
msg( $key,... $params)
Get a translated interface message.
getLabelHtml( $cellAttributes=[])
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
getAttributes(array $list)
Returns the given attributes from the parameters.
getNearestFieldByName( $alldata, $name)
Fetch a field value from $alldata for the closest field matching a given name.
setShowEmptyLabel( $show)
Tell the field whether to generate a separate label element if its label is blank.
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
getDiv( $value)
Get the complete div for the input, including help text, labels, and whatever.
cancelSubmit( $value, $alldata)
Override this function if the control can somehow trigger a form submission that shouldn't actually s...
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
hasVisibleOutput()
If this field has a user-visible output or not.
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
static formatErrors( $errors)
Formats one or more errors as accepted by field validation-callback.
static forceToStringRecursive( $array)
Recursively forces values in an array to strings, because issues arise with integer 0 as a value.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:131
static titleAttrib( $name, $options=null, array $msgParams=[])
Given the id of an interface element, constructs the appropriate title attribute from the system mess...
Definition Linker.php:2026
static accesskey( $name)
Given the id of an interface element, constructs the appropriate accesskey attribute from the system ...
Definition Linker.php:2074
static tooltipAndAccesskeyAttribs( $name, array $msgParams=[], $options=null)
Returns the attributes for the tooltip and access key.
Definition Linker.php:2195
MediaWiki exception.
The Message class provides methods which fulfil two basic services:
Definition Message.php:162
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
Definition Message.php:427
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
getCheck( $name)
Return true if the named value is set in the input, whatever that value is (even "0").
$help
Definition mcc.php:32
$lines
Definition router.php:61