Add autocomplete for WhatLinksHere subpages
[lhc/web/wiklou.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 $mLabel; # String label. Set on construction
14 protected $mID;
15 protected $mClass = '';
16 protected $mHelpClass = false;
17 protected $mDefault;
18 protected $mOptions = false;
19 protected $mOptionsLabelsNotFromMessage = false;
20 protected $mHideIf = null;
21
22 /**
23 * @var bool If true will generate an empty div element with no label
24 * @since 1.22
25 */
26 protected $mShowEmptyLabels = true;
27
28 /**
29 * @var HTMLForm
30 */
31 public $mParent;
32
33 /**
34 * This function must be implemented to return the HTML to generate
35 * the input object itself. It should not implement the surrounding
36 * table cells/rows, or labels/help messages.
37 *
38 * @param string $value The value to set the input to; eg a default
39 * text for a text input.
40 *
41 * @return string Valid HTML.
42 */
43 abstract function getInputHTML( $value );
44
45 /**
46 * Get a translated interface message
47 *
48 * This is a wrapper around $this->mParent->msg() if $this->mParent is set
49 * and wfMessage() otherwise.
50 *
51 * Parameters are the same as wfMessage().
52 *
53 * @return Message
54 */
55 function msg() {
56 $args = func_get_args();
57
58 if ( $this->mParent ) {
59 $callback = array( $this->mParent, 'msg' );
60 } else {
61 $callback = 'wfMessage';
62 }
63
64 return call_user_func_array( $callback, $args );
65 }
66
67
68 /**
69 * Fetch a field value from $alldata for the closest field matching a given
70 * name.
71 *
72 * This is complex because it needs to handle array fields like the user
73 * would expect. The general algorithm is to look for $name as a sibling
74 * of $this, then a sibling of $this's parent, and so on. Keeping in mind
75 * that $name itself might be referencing an array.
76 *
77 * @param array $alldata
78 * @param string $name
79 * @return string
80 */
81 protected function getNearestFieldByName( $alldata, $name ) {
82 $tmp = $this->mName;
83 $thisKeys = array();
84 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
85 array_unshift( $thisKeys, $m[2] );
86 $tmp = $m[1];
87 }
88 if ( substr( $tmp, 0, 2 ) == 'wp' &&
89 !isset( $alldata[$tmp] ) &&
90 isset( $alldata[substr( $tmp, 2 )] )
91 ) {
92 // Adjust for name mangling.
93 $tmp = substr( $tmp, 2 );
94 }
95 array_unshift( $thisKeys, $tmp );
96
97 $tmp = $name;
98 $nameKeys = array();
99 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
100 array_unshift( $nameKeys, $m[2] );
101 $tmp = $m[1];
102 }
103 array_unshift( $nameKeys, $tmp );
104
105 $testValue = '';
106 for ( $i = count( $thisKeys ) - 1; $i >= 0; $i-- ) {
107 $keys = array_merge( array_slice( $thisKeys, 0, $i ), $nameKeys );
108 $data = $alldata;
109 while ( $keys ) {
110 $key = array_shift( $keys );
111 if ( !is_array( $data ) || !isset( $data[$key] ) ) {
112 continue 2;
113 }
114 $data = $data[$key];
115 }
116 $testValue = (string)$data;
117 break;
118 }
119
120 return $testValue;
121 }
122
123 /**
124 * Helper function for isHidden to handle recursive data structures.
125 *
126 * @param array $alldata
127 * @param array $params
128 * @return bool
129 * @throws MWException
130 */
131 protected function isHiddenRecurse( array $alldata, array $params ) {
132 $origParams = $params;
133 $op = array_shift( $params );
134
135 try {
136 switch ( $op ) {
137 case 'AND':
138 foreach ( $params as $i => $p ) {
139 if ( !is_array( $p ) ) {
140 throw new MWException(
141 "Expected array, found " . gettype( $p ) . " at index $i"
142 );
143 }
144 if ( !$this->isHiddenRecurse( $alldata, $p ) ) {
145 return false;
146 }
147 }
148 return true;
149
150 case 'OR':
151 foreach ( $params as $p ) {
152 if ( !is_array( $p ) ) {
153 throw new MWException(
154 "Expected array, found " . gettype( $p ) . " at index $i"
155 );
156 }
157 if ( $this->isHiddenRecurse( $alldata, $p ) ) {
158 return true;
159 }
160 }
161 return false;
162
163 case 'NAND':
164 foreach ( $params as $i => $p ) {
165 if ( !is_array( $p ) ) {
166 throw new MWException(
167 "Expected array, found " . gettype( $p ) . " at index $i"
168 );
169 }
170 if ( !$this->isHiddenRecurse( $alldata, $p ) ) {
171 return true;
172 }
173 }
174 return false;
175
176 case 'NOR':
177 foreach ( $params as $p ) {
178 if ( !is_array( $p ) ) {
179 throw new MWException(
180 "Expected array, found " . gettype( $p ) . " at index $i"
181 );
182 }
183 if ( $this->isHiddenRecurse( $alldata, $p ) ) {
184 return false;
185 }
186 }
187 return true;
188
189 case 'NOT':
190 if ( count( $params ) !== 1 ) {
191 throw new MWException( "NOT takes exactly one parameter" );
192 }
193 $p = $params[0];
194 if ( !is_array( $p ) ) {
195 throw new MWException(
196 "Expected array, found " . gettype( $p ) . " at index 0"
197 );
198 }
199 return !$this->isHiddenRecurse( $alldata, $p );
200
201 case '===':
202 case '!==':
203 if ( count( $params ) !== 2 ) {
204 throw new MWException( "$op takes exactly two parameters" );
205 }
206 list( $field, $value ) = $params;
207 if ( !is_string( $field ) || !is_string( $value ) ) {
208 throw new MWException( "Parameters for $op must be strings" );
209 }
210 $testValue = $this->getNearestFieldByName( $alldata, $field );
211 switch ( $op ) {
212 case '===':
213 return ( $value === $testValue );
214 case '!==':
215 return ( $value !== $testValue );
216 }
217
218 default:
219 throw new MWException( "Unknown operation" );
220 }
221 } catch ( MWException $ex ) {
222 throw new MWException(
223 "Invalid hide-if specification for $this->mName: " .
224 $ex->getMessage() . " in " . var_export( $origParams, true ),
225 0, $ex
226 );
227 }
228 }
229
230 /**
231 * Test whether this field is supposed to be hidden, based on the values of
232 * the other form fields.
233 *
234 * @since 1.23
235 * @param array $alldata The data collected from the form
236 * @return bool
237 */
238 function isHidden( $alldata ) {
239 if ( !$this->mHideIf ) {
240 return false;
241 }
242
243 return $this->isHiddenRecurse( $alldata, $this->mHideIf );
244 }
245
246 /**
247 * Override this function if the control can somehow trigger a form
248 * submission that shouldn't actually submit the HTMLForm.
249 *
250 * @since 1.23
251 * @param string|array $value The value the field was submitted with
252 * @param array $alldata The data collected from the form
253 *
254 * @return bool True to cancel the submission
255 */
256 function cancelSubmit( $value, $alldata ) {
257 return false;
258 }
259
260 /**
261 * Override this function to add specific validation checks on the
262 * field input. Don't forget to call parent::validate() to ensure
263 * that the user-defined callback mValidationCallback is still run
264 *
265 * @param string|array $value The value the field was submitted with
266 * @param array $alldata The data collected from the form
267 *
268 * @return bool|string True on success, or String error to display, or
269 * false to fail validation without displaying an error.
270 */
271 function validate( $value, $alldata ) {
272 if ( $this->isHidden( $alldata ) ) {
273 return true;
274 }
275
276 if ( isset( $this->mParams['required'] )
277 && $this->mParams['required'] !== false
278 && $value === ''
279 ) {
280 return $this->msg( 'htmlform-required' )->parse();
281 }
282
283 if ( isset( $this->mValidationCallback ) ) {
284 return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
285 }
286
287 return true;
288 }
289
290 function filter( $value, $alldata ) {
291 if ( isset( $this->mFilterCallback ) ) {
292 $value = call_user_func( $this->mFilterCallback, $value, $alldata, $this->mParent );
293 }
294
295 return $value;
296 }
297
298 /**
299 * Should this field have a label, or is there no input element with the
300 * appropriate id for the label to point to?
301 *
302 * @return bool True to output a label, false to suppress
303 */
304 protected function needsLabel() {
305 return true;
306 }
307
308 /**
309 * Tell the field whether to generate a separate label element if its label
310 * is blank.
311 *
312 * @since 1.22
313 *
314 * @param bool $show Set to false to not generate a label.
315 * @return void
316 */
317 public function setShowEmptyLabel( $show ) {
318 $this->mShowEmptyLabels = $show;
319 }
320
321 /**
322 * Get the value that this input has been set to from a posted form,
323 * or the input's default value if it has not been set.
324 *
325 * @param WebRequest $request
326 * @return string The value
327 */
328 function loadDataFromRequest( $request ) {
329 if ( $request->getCheck( $this->mName ) ) {
330 return $request->getText( $this->mName );
331 } else {
332 return $this->getDefault();
333 }
334 }
335
336 /**
337 * Initialise the object
338 *
339 * @param array $params Associative Array. See HTMLForm doc for syntax.
340 *
341 * @since 1.22 The 'label' attribute no longer accepts raw HTML, use 'label-raw' instead
342 * @throws MWException
343 */
344 function __construct( $params ) {
345 $this->mParams = $params;
346
347 if ( isset( $params['parent'] ) && $params['parent'] instanceof HTMLForm ) {
348 $this->mParent = $params['parent'];
349 }
350
351 # Generate the label from a message, if possible
352 if ( isset( $params['label-message'] ) ) {
353 $msgInfo = $params['label-message'];
354
355 if ( is_array( $msgInfo ) ) {
356 $msg = array_shift( $msgInfo );
357 } else {
358 $msg = $msgInfo;
359 $msgInfo = array();
360 }
361
362 $this->mLabel = $this->msg( $msg, $msgInfo )->parse();
363 } elseif ( isset( $params['label'] ) ) {
364 if ( $params['label'] === '&#160;' ) {
365 // Apparently some things set &nbsp directly and in an odd format
366 $this->mLabel = '&#160;';
367 } else {
368 $this->mLabel = htmlspecialchars( $params['label'] );
369 }
370 } elseif ( isset( $params['label-raw'] ) ) {
371 $this->mLabel = $params['label-raw'];
372 }
373
374 $this->mName = "wp{$params['fieldname']}";
375 if ( isset( $params['name'] ) ) {
376 $this->mName = $params['name'];
377 }
378
379 $validName = Sanitizer::escapeId( $this->mName );
380 $validName = str_replace( array( '.5B', '.5D' ), array( '[', ']' ), $validName );
381 if ( $this->mName != $validName && !isset( $params['nodata'] ) ) {
382 throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__ );
383 }
384
385 $this->mID = "mw-input-{$this->mName}";
386
387 if ( isset( $params['default'] ) ) {
388 $this->mDefault = $params['default'];
389 }
390
391 if ( isset( $params['id'] ) ) {
392 $id = $params['id'];
393 $validId = Sanitizer::escapeId( $id );
394
395 if ( $id != $validId ) {
396 throw new MWException( "Invalid id '$id' passed to " . __METHOD__ );
397 }
398
399 $this->mID = $id;
400 }
401
402 if ( isset( $params['cssclass'] ) ) {
403 $this->mClass = $params['cssclass'];
404 }
405
406 if ( isset( $params['csshelpclass'] ) ) {
407 $this->mHelpClass = $params['csshelpclass'];
408 }
409
410 if ( isset( $params['validation-callback'] ) ) {
411 $this->mValidationCallback = $params['validation-callback'];
412 }
413
414 if ( isset( $params['filter-callback'] ) ) {
415 $this->mFilterCallback = $params['filter-callback'];
416 }
417
418 if ( isset( $params['flatlist'] ) ) {
419 $this->mClass .= ' mw-htmlform-flatlist';
420 }
421
422 if ( isset( $params['hidelabel'] ) ) {
423 $this->mShowEmptyLabels = false;
424 }
425
426 if ( isset( $params['hide-if'] ) ) {
427 $this->mHideIf = $params['hide-if'];
428 }
429 }
430
431 /**
432 * Get the complete table row for the input, including help text,
433 * labels, and whatever.
434 *
435 * @param string $value The value to set the input to.
436 *
437 * @return string Complete HTML table row.
438 */
439 function getTableRow( $value ) {
440 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
441 $inputHtml = $this->getInputHTML( $value );
442 $fieldType = get_class( $this );
443 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
444 $cellAttributes = array();
445 $rowAttributes = array();
446 $rowClasses = '';
447
448 if ( !empty( $this->mParams['vertical-label'] ) ) {
449 $cellAttributes['colspan'] = 2;
450 $verticalLabel = true;
451 } else {
452 $verticalLabel = false;
453 }
454
455 $label = $this->getLabelHtml( $cellAttributes );
456
457 $field = Html::rawElement(
458 'td',
459 array( 'class' => 'mw-input' ) + $cellAttributes,
460 $inputHtml . "\n$errors"
461 );
462
463 if ( $this->mHideIf ) {
464 $rowAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
465 $rowClasses .= ' mw-htmlform-hide-if';
466 }
467
468 if ( $verticalLabel ) {
469 $html = Html::rawElement( 'tr',
470 $rowAttributes + array( 'class' => "mw-htmlform-vertical-label $rowClasses" ), $label );
471 $html .= Html::rawElement( 'tr',
472 $rowAttributes + array(
473 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
474 ),
475 $field );
476 } else {
477 $html =
478 Html::rawElement( 'tr',
479 $rowAttributes + array(
480 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
481 ),
482 $label . $field );
483 }
484
485 return $html . $helptext;
486 }
487
488 /**
489 * Get the complete div for the input, including help text,
490 * labels, and whatever.
491 * @since 1.20
492 *
493 * @param string $value The value to set the input to.
494 *
495 * @return string Complete HTML table row.
496 */
497 public function getDiv( $value ) {
498 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
499 $inputHtml = $this->getInputHTML( $value );
500 $fieldType = get_class( $this );
501 $helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
502 $cellAttributes = array();
503 $label = $this->getLabelHtml( $cellAttributes );
504
505 $outerDivClass = array(
506 'mw-input',
507 'mw-htmlform-nolabel' => ( $label === '' )
508 );
509
510 $field = Html::rawElement(
511 'div',
512 array( 'class' => $outerDivClass ) + $cellAttributes,
513 $inputHtml . "\n$errors"
514 );
515 $divCssClasses = array( "mw-htmlform-field-$fieldType", $this->mClass, $errorClass );
516 if ( $this->mParent->isVForm() ) {
517 $divCssClasses[] = 'mw-ui-vform-field';
518 }
519
520 $wrapperAttributes = array(
521 'class' => $divCssClasses,
522 );
523 if ( $this->mHideIf ) {
524 $wrapperAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
525 $wrapperAttributes['class'][] = ' mw-htmlform-hide-if';
526 }
527 $html = Html::rawElement( 'div', $wrapperAttributes, $label . $field );
528 $html .= $helptext;
529
530 return $html;
531 }
532
533 /**
534 * Get the complete raw fields for the input, including help text,
535 * labels, and whatever.
536 * @since 1.20
537 *
538 * @param string $value The value to set the input to.
539 *
540 * @return string Complete HTML table row.
541 */
542 public function getRaw( $value ) {
543 list( $errors, ) = $this->getErrorsAndErrorClass( $value );
544 $inputHtml = $this->getInputHTML( $value );
545 $helptext = $this->getHelpTextHtmlRaw( $this->getHelpText() );
546 $cellAttributes = array();
547 $label = $this->getLabelHtml( $cellAttributes );
548
549 $html = "\n$errors";
550 $html .= $label;
551 $html .= $inputHtml;
552 $html .= $helptext;
553
554 return $html;
555 }
556
557 /**
558 * Generate help text HTML in table format
559 * @since 1.20
560 *
561 * @param string|null $helptext
562 * @return string
563 */
564 public function getHelpTextHtmlTable( $helptext ) {
565 if ( is_null( $helptext ) ) {
566 return '';
567 }
568
569 $rowAttributes = array();
570 if ( $this->mHideIf ) {
571 $rowAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
572 $rowAttributes['class'] = 'mw-htmlform-hide-if';
573 }
574
575 $tdClasses = array( 'htmlform-tip' );
576 if ( $this->mHelpClass !== false ) {
577 $tdClasses[] = $this->mHelpClass;
578 }
579 $row = Html::rawElement( 'td', array( 'colspan' => 2, 'class' => $tdClasses ), $helptext );
580 $row = Html::rawElement( 'tr', $rowAttributes, $row );
581
582 return $row;
583 }
584
585 /**
586 * Generate help text HTML in div format
587 * @since 1.20
588 *
589 * @param string|null $helptext
590 *
591 * @return string
592 */
593 public function getHelpTextHtmlDiv( $helptext ) {
594 if ( is_null( $helptext ) ) {
595 return '';
596 }
597
598 $wrapperAttributes = array(
599 'class' => 'htmlform-tip',
600 );
601 if ( $this->mHelpClass !== false ) {
602 $wrapperAttributes['class'] .= " {$this->mHelpClass}";
603 }
604 if ( $this->mHideIf ) {
605 $wrapperAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
606 $wrapperAttributes['class'] .= ' mw-htmlform-hide-if';
607 }
608 $div = Html::rawElement( 'div', $wrapperAttributes, $helptext );
609
610 return $div;
611 }
612
613 /**
614 * Generate help text HTML formatted for raw output
615 * @since 1.20
616 *
617 * @param string|null $helptext
618 * @return string
619 */
620 public function getHelpTextHtmlRaw( $helptext ) {
621 return $this->getHelpTextHtmlDiv( $helptext );
622 }
623
624 /**
625 * Determine the help text to display
626 * @since 1.20
627 * @return string
628 */
629 public function getHelpText() {
630 $helptext = null;
631
632 if ( isset( $this->mParams['help-message'] ) ) {
633 $this->mParams['help-messages'] = array( $this->mParams['help-message'] );
634 }
635
636 if ( isset( $this->mParams['help-messages'] ) ) {
637 foreach ( $this->mParams['help-messages'] as $name ) {
638 $helpMessage = (array)$name;
639 $msg = $this->msg( array_shift( $helpMessage ), $helpMessage );
640
641 if ( $msg->exists() ) {
642 if ( is_null( $helptext ) ) {
643 $helptext = '';
644 } else {
645 $helptext .= $this->msg( 'word-separator' )->escaped(); // some space
646 }
647 $helptext .= $msg->parse(); // Append message
648 }
649 }
650 } elseif ( isset( $this->mParams['help'] ) ) {
651 $helptext = $this->mParams['help'];
652 }
653
654 return $helptext;
655 }
656
657 /**
658 * Determine form errors to display and their classes
659 * @since 1.20
660 *
661 * @param string $value The value of the input
662 * @return array
663 */
664 public function getErrorsAndErrorClass( $value ) {
665 $errors = $this->validate( $value, $this->mParent->mFieldData );
666
667 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
668 $errors = '';
669 $errorClass = '';
670 } else {
671 $errors = self::formatErrors( $errors );
672 $errorClass = 'mw-htmlform-invalid-input';
673 }
674
675 return array( $errors, $errorClass );
676 }
677
678 function getLabel() {
679 return is_null( $this->mLabel ) ? '' : $this->mLabel;
680 }
681
682 function getLabelHtml( $cellAttributes = array() ) {
683 # Don't output a for= attribute for labels with no associated input.
684 # Kind of hacky here, possibly we don't want these to be <label>s at all.
685 $for = array();
686
687 if ( $this->needsLabel() ) {
688 $for['for'] = $this->mID;
689 }
690
691 $labelValue = trim( $this->getLabel() );
692 $hasLabel = false;
693 if ( $labelValue !== '&#160;' && $labelValue !== '' ) {
694 $hasLabel = true;
695 }
696
697 $displayFormat = $this->mParent->getDisplayFormat();
698 $html = '';
699
700 if ( $displayFormat === 'table' ) {
701 $html =
702 Html::rawElement( 'td',
703 array( 'class' => 'mw-label' ) + $cellAttributes,
704 Html::rawElement( 'label', $for, $labelValue ) );
705 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
706 if ( $displayFormat === 'div' ) {
707 $html =
708 Html::rawElement( 'div',
709 array( 'class' => 'mw-label' ) + $cellAttributes,
710 Html::rawElement( 'label', $for, $labelValue ) );
711 } else {
712 $html = Html::rawElement( 'label', $for, $labelValue );
713 }
714 }
715
716 return $html;
717 }
718
719 function getDefault() {
720 if ( isset( $this->mDefault ) ) {
721 return $this->mDefault;
722 } else {
723 return null;
724 }
725 }
726
727 /**
728 * Returns the attributes required for the tooltip and accesskey.
729 *
730 * @return array Attributes
731 */
732 public function getTooltipAndAccessKey() {
733 if ( empty( $this->mParams['tooltip'] ) ) {
734 return array();
735 }
736
737 return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] );
738 }
739
740 /**
741 * Returns the given attributes from the parameters
742 *
743 * @param array $list List of attributes to get
744 * @return array Attributes
745 */
746 public function getAttributes( array $list ) {
747 static $boolAttribs = array( 'disabled', 'required', 'autofocus', 'multiple', 'readonly' );
748
749 $ret = array();
750
751 foreach ( $list as $key ) {
752 if ( in_array( $key, $boolAttribs ) ) {
753 if ( !empty( $this->mParams[$key] ) ) {
754 $ret[$key] = '';
755 }
756 } elseif ( isset( $this->mParams[$key] ) ) {
757 $ret[$key] = $this->mParams[$key];
758 }
759 }
760
761 return $ret;
762 }
763
764 /**
765 * Given an array of msg-key => value mappings, returns an array with keys
766 * being the message texts. It also forces values to strings.
767 *
768 * @param array $options
769 * @return array
770 */
771 private function lookupOptionsKeys( $options ) {
772 $ret = array();
773 foreach ( $options as $key => $value ) {
774 $key = $this->msg( $key )->plain();
775 $ret[$key] = is_array( $value )
776 ? $this->lookupOptionsKeys( $value )
777 : strval( $value );
778 }
779 return $ret;
780 }
781
782 /**
783 * Recursively forces values in an array to strings, because issues arise
784 * with integer 0 as a value.
785 *
786 * @param array $array
787 * @return array
788 */
789 static function forceToStringRecursive( $array ) {
790 if ( is_array( $array ) ) {
791 return array_map( array( __CLASS__, 'forceToStringRecursive' ), $array );
792 } else {
793 return strval( $array );
794 }
795 }
796
797 /**
798 * Fetch the array of options from the field's parameters. In order, this
799 * checks 'options-messages', 'options', then 'options-message'.
800 *
801 * @return array|null Options array
802 */
803 public function getOptions() {
804 if ( $this->mOptions === false ) {
805 if ( array_key_exists( 'options-messages', $this->mParams ) ) {
806 $this->mOptions = $this->lookupOptionsKeys( $this->mParams['options-messages'] );
807 } elseif ( array_key_exists( 'options', $this->mParams ) ) {
808 $this->mOptionsLabelsNotFromMessage = true;
809 $this->mOptions = self::forceToStringRecursive( $this->mParams['options'] );
810 } elseif ( array_key_exists( 'options-message', $this->mParams ) ) {
811 /** @todo This is copied from Xml::listDropDown(), deprecate/avoid duplication? */
812 $message = $this->msg( $this->mParams['options-message'] )->inContentLanguage()->plain();
813
814 $optgroup = false;
815 $this->mOptions = array();
816 foreach ( explode( "\n", $message ) as $option ) {
817 $value = trim( $option );
818 if ( $value == '' ) {
819 continue;
820 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
821 # A new group is starting...
822 $value = trim( substr( $value, 1 ) );
823 $optgroup = $value;
824 } elseif ( substr( $value, 0, 2 ) == '**' ) {
825 # groupmember
826 $opt = trim( substr( $value, 2 ) );
827 if ( $optgroup === false ) {
828 $this->mOptions[$opt] = $opt;
829 } else {
830 $this->mOptions[$optgroup][$opt] = $opt;
831 }
832 } else {
833 # groupless reason list
834 $optgroup = false;
835 $this->mOptions[$option] = $option;
836 }
837 }
838 } else {
839 $this->mOptions = null;
840 }
841 }
842
843 return $this->mOptions;
844 }
845
846 /**
847 * flatten an array of options to a single array, for instance,
848 * a set of "<options>" inside "<optgroups>".
849 *
850 * @param array $options Associative Array with values either Strings or Arrays
851 * @return array Flattened input
852 */
853 public static function flattenOptions( $options ) {
854 $flatOpts = array();
855
856 foreach ( $options as $value ) {
857 if ( is_array( $value ) ) {
858 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
859 } else {
860 $flatOpts[] = $value;
861 }
862 }
863
864 return $flatOpts;
865 }
866
867 /**
868 * Formats one or more errors as accepted by field validation-callback.
869 *
870 * @param string|Message|array $errors Array of strings or Message instances
871 * @return string HTML
872 * @since 1.18
873 */
874 protected static function formatErrors( $errors ) {
875 if ( is_array( $errors ) && count( $errors ) === 1 ) {
876 $errors = array_shift( $errors );
877 }
878
879 if ( is_array( $errors ) ) {
880 $lines = array();
881 foreach ( $errors as $error ) {
882 if ( $error instanceof Message ) {
883 $lines[] = Html::rawElement( 'li', array(), $error->parse() );
884 } else {
885 $lines[] = Html::rawElement( 'li', array(), $error );
886 }
887 }
888
889 return Html::rawElement( 'ul', array( 'class' => 'error' ), implode( "\n", $lines ) );
890 } else {
891 if ( $errors instanceof Message ) {
892 $errors = $errors->parse();
893 }
894
895 return Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
896 }
897 }
898 }