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