Revert r113650 and reapply r113619 and r113649 with one modification: User::createNew...
[lhc/web/wiklou.git] / includes / Xml.php
1 <?php
2
3 /**
4 * Module of static functions for generating XML
5 */
6
7 class Xml {
8 /**
9 * Format an XML element with given attributes and, optionally, text content.
10 * Element and attribute names are assumed to be ready for literal inclusion.
11 * Strings are assumed to not contain XML-illegal characters; special
12 * characters (<, >, &) are escaped but illegals are not touched.
13 *
14 * @param $element String: element name
15 * @param $attribs Array: Name=>value pairs. Values will be escaped.
16 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
17 * @param $allowShortTag Bool: whether '' in $contents will result in a contentless closed tag
18 * @return string
19 */
20 public static function element( $element, $attribs = null, $contents = '', $allowShortTag = true ) {
21 $out = '<' . $element;
22 if( !is_null( $attribs ) ) {
23 $out .= self::expandAttributes( $attribs );
24 }
25 if( is_null( $contents ) ) {
26 $out .= '>';
27 } else {
28 if( $allowShortTag && $contents === '' ) {
29 $out .= ' />';
30 } else {
31 $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
32 }
33 }
34 return $out;
35 }
36
37 /**
38 * Given an array of ('attributename' => 'value'), it generates the code
39 * to set the XML attributes : attributename="value".
40 * The values are passed to Sanitizer::encodeAttribute.
41 * Return null if no attributes given.
42 * @param $attribs Array of attributes for an XML element
43 * @return null|string
44 */
45 public static function expandAttributes( $attribs ) {
46 $out = '';
47 if( is_null( $attribs ) ) {
48 return null;
49 } elseif( is_array( $attribs ) ) {
50 foreach( $attribs as $name => $val ) {
51 $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
52 }
53 return $out;
54 } else {
55 throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
56 }
57 }
58
59 /**
60 * Format an XML element as with self::element(), but run text through the
61 * $wgContLang->normalize() validator first to ensure that no invalid UTF-8
62 * is passed.
63 *
64 * @param $element String:
65 * @param $attribs Array: Name=>value pairs. Values will be escaped.
66 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
67 * @return string
68 */
69 public static function elementClean( $element, $attribs = array(), $contents = '') {
70 global $wgContLang;
71 if( $attribs ) {
72 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
73 }
74 if( $contents ) {
75 wfProfileIn( __METHOD__ . '-norm' );
76 $contents = $wgContLang->normalize( $contents );
77 wfProfileOut( __METHOD__ . '-norm' );
78 }
79 return self::element( $element, $attribs, $contents );
80 }
81
82 /**
83 * This opens an XML element
84 *
85 * @param $element String name of the element
86 * @param $attribs array of attributes, see Xml::expandAttributes()
87 * @return string
88 */
89 public static function openElement( $element, $attribs = null ) {
90 return '<' . $element . self::expandAttributes( $attribs ) . '>';
91 }
92
93 /**
94 * Shortcut to close an XML element
95 * @param $element String element name
96 * @return string
97 */
98 public static function closeElement( $element ) { return "</$element>"; }
99
100 /**
101 * Same as Xml::element(), but does not escape contents. Handy when the
102 * content you have is already valid xml.
103 *
104 * @param $element String element name
105 * @param $attribs array of attributes
106 * @param $contents String content of the element
107 * @return string
108 */
109 public static function tags( $element, $attribs = null, $contents ) {
110 return self::openElement( $element, $attribs ) . $contents . "</$element>";
111 }
112
113 /**
114 * Build a drop-down box for selecting a namespace
115 *
116 * @param $selected Mixed: Namespace which should be pre-selected
117 * @param $all Mixed: Value of an item denoting all namespaces, or null to omit
118 * @param $element_name String: value of the "name" attribute of the select tag
119 * @param $label String: optional label to add to the field
120 * @return string
121 * @deprecated since 1.19
122 */
123 public static function namespaceSelector( $selected = '', $all = null, $element_name = 'namespace', $label = null ) {
124 wfDeprecated( __METHOD__, '1.19' );
125 return Html::namespaceSelector( array(
126 'selected' => $selected,
127 'all' => $all,
128 'label' => $label,
129 ), array(
130 'name' => $element_name,
131 'id' => 'namespace',
132 'class' => 'namespaceselector',
133 ) );
134 }
135
136 /**
137 * Create a date selector
138 *
139 * @param $selected Mixed: the month which should be selected, default ''
140 * @param $allmonths String: value of a special item denoting all month. Null to not include (default)
141 * @param $id String: Element identifier
142 * @return String: Html string containing the month selector
143 */
144 public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
145 global $wgLang;
146 $options = array();
147 if( is_null( $selected ) )
148 $selected = '';
149 if( !is_null( $allmonths ) )
150 $options[] = self::option( wfMsg( 'monthsall' ), $allmonths, $selected === $allmonths );
151 for( $i = 1; $i < 13; $i++ )
152 $options[] = self::option( $wgLang->getMonthName( $i ), $i, $selected === $i );
153 return self::openElement( 'select', array( 'id' => $id, 'name' => 'month', 'class' => 'mw-month-selector' ) )
154 . implode( "\n", $options )
155 . self::closeElement( 'select' );
156 }
157
158 /**
159 * @param $year Integer
160 * @param $month Integer
161 * @return string Formatted HTML
162 */
163 public static function dateMenu( $year, $month ) {
164 # Offset overrides year/month selection
165 if( $month && $month !== -1 ) {
166 $encMonth = intval( $month );
167 } else {
168 $encMonth = '';
169 }
170 if( $year ) {
171 $encYear = intval( $year );
172 } elseif( $encMonth ) {
173 $thisMonth = intval( gmdate( 'n' ) );
174 $thisYear = intval( gmdate( 'Y' ) );
175 if( intval($encMonth) > $thisMonth ) {
176 $thisYear--;
177 }
178 $encYear = $thisYear;
179 } else {
180 $encYear = '';
181 }
182 return Xml::label( wfMsg( 'year' ), 'year' ) . ' '.
183 Xml::input( 'year', 4, $encYear, array('id' => 'year', 'maxlength' => 4) ) . ' '.
184 Xml::label( wfMsg( 'month' ), 'month' ) . ' '.
185 Xml::monthSelector( $encMonth, -1 );
186 }
187
188 /**
189 * Construct a language selector appropriate for use in a form or preferences
190 *
191 * @param string $selected The language code of the selected language
192 * @param boolean $customisedOnly If true only languages which have some content are listed
193 * @param string $inLanguage The ISO code of the language to display the select list in (optional)
194 * @return array containing 2 items: label HTML and select list HTML
195 */
196 public static function languageSelector( $selected, $customisedOnly = true, $inLanguage = null ) {
197 global $wgLanguageCode;
198
199 $languages = Language::fetchLanguageNames( $inLanguage, $customisedOnly ? 'mwfile' : 'mw' );
200
201 // Make sure the site language is in the list; a custom language code might not have a
202 // defined name...
203 if( !array_key_exists( $wgLanguageCode, $languages ) ) {
204 $languages[$wgLanguageCode] = $wgLanguageCode;
205 }
206
207 ksort( $languages );
208
209 /**
210 * If a bogus value is set, default to the content language.
211 * Otherwise, no default is selected and the user ends up
212 * with an Afrikaans interface since it's first in the list.
213 */
214 $selected = isset( $languages[$selected] ) ? $selected : $wgLanguageCode;
215 $options = "\n";
216 foreach( $languages as $code => $name ) {
217 $options .= Xml::option( "$code - $name", $code, ($code == $selected) ) . "\n";
218 }
219
220 return array(
221 Xml::label( wfMsg('yourlanguage'), 'wpUserLanguage' ),
222 Xml::tags( 'select',
223 array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ),
224 $options
225 )
226 );
227
228 }
229
230 /**
231 * Shortcut to make a span element
232 * @param $text String content of the element, will be escaped
233 * @param $class String class name of the span element
234 * @param $attribs array other attributes
235 * @return string
236 */
237 public static function span( $text, $class, $attribs = array() ) {
238 return self::element( 'span', array( 'class' => $class ) + $attribs, $text );
239 }
240
241 /**
242 * Shortcut to make a specific element with a class attribute
243 * @param $text string content of the element, will be escaped
244 * @param $class string class name of the span element
245 * @param $tag string element name
246 * @param $attribs array other attributes
247 * @return string
248 */
249 public static function wrapClass( $text, $class, $tag = 'span', $attribs = array() ) {
250 return self::tags( $tag, array( 'class' => $class ) + $attribs, $text );
251 }
252
253 /**
254 * Convenience function to build an HTML text input field
255 * @param $name String value of the name attribute
256 * @param $size int value of the size attribute
257 * @param $value mixed value of the value attribute
258 * @param $attribs array other attributes
259 * @return string HTML
260 */
261 public static function input( $name, $size = false, $value = false, $attribs = array() ) {
262 $attributes = array( 'name' => $name );
263
264 if( $size ) {
265 $attributes['size'] = $size;
266 }
267
268 if( $value !== false ) { // maybe 0
269 $attributes['value'] = $value;
270 }
271
272 return self::element( 'input', $attributes + $attribs );
273 }
274
275 /**
276 * Convenience function to build an HTML password input field
277 * @param $name string value of the name attribute
278 * @param $size int value of the size attribute
279 * @param $value mixed value of the value attribute
280 * @param $attribs array other attributes
281 * @return string HTML
282 */
283 public static function password( $name, $size = false, $value = false, $attribs = array() ) {
284 return self::input( $name, $size, $value, array_merge( $attribs, array( 'type' => 'password' ) ) );
285 }
286
287 /**
288 * Internal function for use in checkboxes and radio buttons and such.
289 *
290 * @param $name string
291 * @param $present bool
292 *
293 * @return array
294 */
295 public static function attrib( $name, $present = true ) {
296 return $present ? array( $name => $name ) : array();
297 }
298
299 /**
300 * Convenience function to build an HTML checkbox
301 * @param $name String value of the name attribute
302 * @param $checked Bool Whether the checkbox is checked or not
303 * @param $attribs Array other attributes
304 * @return string HTML
305 */
306 public static function check( $name, $checked = false, $attribs=array() ) {
307 return self::element( 'input', array_merge(
308 array(
309 'name' => $name,
310 'type' => 'checkbox',
311 'value' => 1 ),
312 self::attrib( 'checked', $checked ),
313 $attribs ) );
314 }
315
316 /**
317 * Convenience function to build an HTML radio button
318 * @param $name String value of the name attribute
319 * @param $value String value of the value attribute
320 * @param $checked Bool Whether the checkbox is checked or not
321 * @param $attribs Array other attributes
322 * @return string HTML
323 */
324 public static function radio( $name, $value, $checked = false, $attribs = array() ) {
325 return self::element( 'input', array(
326 'name' => $name,
327 'type' => 'radio',
328 'value' => $value ) + self::attrib( 'checked', $checked ) + $attribs );
329 }
330
331 /**
332 * Convenience function to build an HTML form label
333 * @param $label String text of the label
334 * @param $id
335 * @param $attribs Array an attribute array. This will usuall be
336 * the same array as is passed to the corresponding input element,
337 * so this function will cherry-pick appropriate attributes to
338 * apply to the label as well; only class and title are applied.
339 * @return string HTML
340 */
341 public static function label( $label, $id, $attribs = array() ) {
342 $a = array( 'for' => $id );
343
344 # FIXME avoid copy pasting below:
345 if( isset( $attribs['class'] ) ){
346 $a['class'] = $attribs['class'];
347 }
348 if( isset( $attribs['title'] ) ){
349 $a['title'] = $attribs['title'];
350 }
351
352 return self::element( 'label', $a, $label );
353 }
354
355 /**
356 * Convenience function to build an HTML text input field with a label
357 * @param $label String text of the label
358 * @param $name String value of the name attribute
359 * @param $id String id of the input
360 * @param $size Int|Bool value of the size attribute
361 * @param $value String|Bool value of the value attribute
362 * @param $attribs array other attributes
363 * @return string HTML
364 */
365 public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs = array() ) {
366 list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
367 return $label . '&#160;' . $input;
368 }
369
370 /**
371 * Same as Xml::inputLabel() but return input and label in an array
372 *
373 * @param $label String
374 * @param $name String
375 * @param $id String
376 * @param $size Int|Bool
377 * @param $value String|Bool
378 * @param $attribs array
379 *
380 * @return array
381 */
382 public static function inputLabelSep( $label, $name, $id, $size = false, $value = false, $attribs = array() ) {
383 return array(
384 Xml::label( $label, $id, $attribs ),
385 self::input( $name, $size, $value, array( 'id' => $id ) + $attribs )
386 );
387 }
388
389 /**
390 * Convenience function to build an HTML checkbox with a label
391 *
392 * @param $label
393 * @param $name
394 * @param $id
395 * @param $checked bool
396 * @param $attribs array
397 *
398 * @return string HTML
399 */
400 public static function checkLabel( $label, $name, $id, $checked = false, $attribs = array() ) {
401 return self::check( $name, $checked, array( 'id' => $id ) + $attribs ) .
402 '&#160;' .
403 self::label( $label, $id, $attribs );
404 }
405
406 /**
407 * Convenience function to build an HTML radio button with a label
408 *
409 * @param $label
410 * @param $name
411 * @param $value
412 * @param $id
413 * @param $checked bool
414 * @param $attribs array
415 *
416 * @return string HTML
417 */
418 public static function radioLabel( $label, $name, $value, $id, $checked = false, $attribs = array() ) {
419 return self::radio( $name, $value, $checked, array( 'id' => $id ) + $attribs ) .
420 '&#160;' .
421 self::label( $label, $id, $attribs );
422 }
423
424 /**
425 * Convenience function to build an HTML submit button
426 * @param $value String: label text for the button
427 * @param $attribs Array: optional custom attributes
428 * @return string HTML
429 */
430 public static function submitButton( $value, $attribs = array() ) {
431 return Html::element( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs );
432 }
433
434 /**
435 * Convenience function to build an HTML drop-down list item.
436 * @param $text String: text for this item
437 * @param $value String: form submission value; if empty, use text
438 * @param $selected boolean: if true, will be the default selected item
439 * @param $attribs array: optional additional HTML attributes
440 * @return string HTML
441 */
442 public static function option( $text, $value=null, $selected = false,
443 $attribs = array() ) {
444 if( !is_null( $value ) ) {
445 $attribs['value'] = $value;
446 }
447 if( $selected ) {
448 $attribs['selected'] = 'selected';
449 }
450 return Html::element( 'option', $attribs, $text );
451 }
452
453 /**
454 * Build a drop-down box from a textual list.
455 *
456 * @param $name Mixed: Name and id for the drop-down
457 * @param $list Mixed: Correctly formatted text (newline delimited) to be used to generate the options
458 * @param $other Mixed: Text for the "Other reasons" option
459 * @param $selected Mixed: Option which should be pre-selected
460 * @param $class Mixed: CSS classes for the drop-down
461 * @param $tabindex Mixed: Value of the tabindex attribute
462 * @return string
463 */
464 public static function listDropDown( $name= '', $list = '', $other = '', $selected = '', $class = '', $tabindex = null ) {
465 $optgroup = false;
466
467 $options = self::option( $other, 'other', $selected === 'other' );
468
469 foreach ( explode( "\n", $list ) as $option) {
470 $value = trim( $option );
471 if ( $value == '' ) {
472 continue;
473 } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
474 // A new group is starting ...
475 $value = trim( substr( $value, 1 ) );
476 if( $optgroup ) $options .= self::closeElement('optgroup');
477 $options .= self::openElement( 'optgroup', array( 'label' => $value ) );
478 $optgroup = true;
479 } elseif ( substr( $value, 0, 2) == '**' ) {
480 // groupmember
481 $value = trim( substr( $value, 2 ) );
482 $options .= self::option( $value, $value, $selected === $value );
483 } else {
484 // groupless reason list
485 if( $optgroup ) $options .= self::closeElement('optgroup');
486 $options .= self::option( $value, $value, $selected === $value );
487 $optgroup = false;
488 }
489 }
490
491 if( $optgroup ) $options .= self::closeElement('optgroup');
492
493 $attribs = array();
494
495 if( $name ) {
496 $attribs['id'] = $name;
497 $attribs['name'] = $name;
498 }
499
500 if( $class ) {
501 $attribs['class'] = $class;
502 }
503
504 if( $tabindex ) {
505 $attribs['tabindex'] = $tabindex;
506 }
507
508 return Xml::openElement( 'select', $attribs )
509 . "\n"
510 . $options
511 . "\n"
512 . Xml::closeElement( 'select' );
513 }
514
515 /**
516 * Shortcut for creating fieldsets.
517 *
518 * @param $legend string|bool Legend of the fieldset. If evaluates to false, legend is not added.
519 * @param $content string Pre-escaped content for the fieldset. If false, only open fieldset is returned.
520 * @param $attribs array Any attributes to fieldset-element.
521 *
522 * @return string
523 */
524 public static function fieldset( $legend = false, $content = false, $attribs = array() ) {
525 $s = Xml::openElement( 'fieldset', $attribs ) . "\n";
526
527 if ( $legend ) {
528 $s .= Xml::element( 'legend', null, $legend ) . "\n";
529 }
530
531 if ( $content !== false ) {
532 $s .= $content . "\n";
533 $s .= Xml::closeElement( 'fieldset' ) . "\n";
534 }
535
536 return $s;
537 }
538
539 /**
540 * Shortcut for creating textareas.
541 *
542 * @param $name string The 'name' for the textarea
543 * @param $content string Content for the textarea
544 * @param $cols int The number of columns for the textarea
545 * @param $rows int The number of rows for the textarea
546 * @param $attribs array Any other attributes for the textarea
547 *
548 * @return string
549 */
550 public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = array() ) {
551 return self::element( 'textarea',
552 array( 'name' => $name,
553 'id' => $name,
554 'cols' => $cols,
555 'rows' => $rows
556 ) + $attribs,
557 $content, false );
558 }
559
560 /**
561 * Returns an escaped string suitable for inclusion in a string literal
562 * for JavaScript source code.
563 * Illegal control characters are assumed not to be present.
564 *
565 * @param $string String to escape
566 * @return String
567 */
568 public static function escapeJsString( $string ) {
569 // See ECMA 262 section 7.8.4 for string literal format
570 $pairs = array(
571 "\\" => "\\\\",
572 "\"" => "\\\"",
573 '\'' => '\\\'',
574 "\n" => "\\n",
575 "\r" => "\\r",
576
577 # To avoid closing the element or CDATA section
578 "<" => "\\x3c",
579 ">" => "\\x3e",
580
581 # To avoid any complaints about bad entity refs
582 "&" => "\\x26",
583
584 # Work around https://bugzilla.mozilla.org/show_bug.cgi?id=274152
585 # Encode certain Unicode formatting chars so affected
586 # versions of Gecko don't misinterpret our strings;
587 # this is a common problem with Farsi text.
588 "\xe2\x80\x8c" => "\\u200c", // ZERO WIDTH NON-JOINER
589 "\xe2\x80\x8d" => "\\u200d", // ZERO WIDTH JOINER
590 );
591
592 return strtr( $string, $pairs );
593 }
594
595 /**
596 * Encode a variable of unknown type to JavaScript.
597 * Arrays are converted to JS arrays, objects are converted to JS associative
598 * arrays (objects). So cast your PHP associative arrays to objects before
599 * passing them to here.
600 *
601 * @param $value
602 *
603 * @return string
604 */
605 public static function encodeJsVar( $value ) {
606 if ( is_bool( $value ) ) {
607 $s = $value ? 'true' : 'false';
608 } elseif ( is_null( $value ) ) {
609 $s = 'null';
610 } elseif ( is_int( $value ) || is_float( $value ) ) {
611 $s = strval($value);
612 } elseif ( is_array( $value ) && // Make sure it's not associative.
613 array_keys($value) === range( 0, count($value) - 1 ) ||
614 count($value) == 0
615 ) {
616 $s = '[';
617 foreach ( $value as $elt ) {
618 if ( $s != '[' ) {
619 $s .= ',';
620 }
621 $s .= self::encodeJsVar( $elt );
622 }
623 $s .= ']';
624 } elseif ( $value instanceof XmlJsCode ) {
625 $s = $value->value;
626 } elseif ( is_object( $value ) || is_array( $value ) ) {
627 // Objects and associative arrays
628 $s = '{';
629 foreach ( (array)$value as $name => $elt ) {
630 if ( $s != '{' ) {
631 $s .= ',';
632 }
633
634 $s .= '"' . self::escapeJsString( $name ) . '":' .
635 self::encodeJsVar( $elt );
636 }
637 $s .= '}';
638 } else {
639 $s = '"' . self::escapeJsString( $value ) . '"';
640 }
641 return $s;
642 }
643
644 /**
645 * Create a call to a JavaScript function. The supplied arguments will be
646 * encoded using Xml::encodeJsVar().
647 *
648 * @param $name String The name of the function to call, or a JavaScript expression
649 * which evaluates to a function object which is called.
650 * @param $args Array of arguments to pass to the function.
651 *
652 * @since 1.17
653 *
654 * @return string
655 */
656 public static function encodeJsCall( $name, $args ) {
657 $s = "$name(";
658 $first = true;
659
660 foreach ( $args as $arg ) {
661 if ( $first ) {
662 $first = false;
663 } else {
664 $s .= ', ';
665 }
666
667 $s .= Xml::encodeJsVar( $arg );
668 }
669
670 $s .= ");\n";
671
672 return $s;
673 }
674
675 /**
676 * Check if a string is well-formed XML.
677 * Must include the surrounding tag.
678 *
679 * @param $text String: string to test.
680 * @return bool
681 *
682 * @todo Error position reporting return
683 */
684 public static function isWellFormed( $text ) {
685 $parser = xml_parser_create( "UTF-8" );
686
687 # case folding violates XML standard, turn it off
688 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
689
690 if( !xml_parse( $parser, $text, true ) ) {
691 //$err = xml_error_string( xml_get_error_code( $parser ) );
692 //$position = xml_get_current_byte_index( $parser );
693 //$fragment = $this->extractFragment( $html, $position );
694 //$this->mXmlError = "$err at byte $position:\n$fragment";
695 xml_parser_free( $parser );
696 return false;
697 }
698
699 xml_parser_free( $parser );
700
701 return true;
702 }
703
704 /**
705 * Check if a string is a well-formed XML fragment.
706 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
707 * and can use HTML named entities.
708 *
709 * @param $text String:
710 * @return bool
711 */
712 public static function isWellFormedXmlFragment( $text ) {
713 $html =
714 Sanitizer::hackDocType() .
715 '<html>' .
716 $text .
717 '</html>';
718
719 return Xml::isWellFormed( $html );
720 }
721
722 /**
723 * Replace " > and < with their respective HTML entities ( &quot;,
724 * &gt;, &lt;)
725 *
726 * @param $in String: text that might contain HTML tags.
727 * @return string Escaped string
728 */
729 public static function escapeTagsOnly( $in ) {
730 return str_replace(
731 array( '"', '>', '<' ),
732 array( '&quot;', '&gt;', '&lt;' ),
733 $in );
734 }
735
736 /**
737 * Generate a form (without the opening form element).
738 * Output optionally includes a submit button.
739 * @param $fields Array Associative array, key is message corresponding to a description for the field (colon is in the message), value is appropriate input.
740 * @param $submitLabel String A message containing a label for the submit button.
741 * @return string HTML form.
742 */
743 public static function buildForm( $fields, $submitLabel = null ) {
744 $form = '';
745 $form .= "<table><tbody>";
746
747 foreach( $fields as $labelmsg => $input ) {
748 $id = "mw-$labelmsg";
749 $form .= Xml::openElement( 'tr', array( 'id' => $id ) );
750 $form .= Xml::tags( 'td', array('class' => 'mw-label'), wfMsgExt( $labelmsg, array('parseinline') ) );
751 $form .= Xml::openElement( 'td', array( 'class' => 'mw-input' ) ) . $input . Xml::closeElement( 'td' );
752 $form .= Xml::closeElement( 'tr' );
753 }
754
755 if( $submitLabel ) {
756 $form .= Xml::openElement( 'tr' );
757 $form .= Xml::tags( 'td', array(), '' );
758 $form .= Xml::openElement( 'td', array( 'class' => 'mw-submit' ) ) . Xml::submitButton( wfMsg( $submitLabel ) ) . Xml::closeElement( 'td' );
759 $form .= Xml::closeElement( 'tr' );
760 }
761
762 $form .= "</tbody></table>";
763
764 return $form;
765 }
766
767 /**
768 * Build a table of data
769 * @param $rows array An array of arrays of strings, each to be a row in a table
770 * @param $attribs array An array of attributes to apply to the table tag [optional]
771 * @param $headers array An array of strings to use as table headers [optional]
772 * @return string
773 */
774 public static function buildTable( $rows, $attribs = array(), $headers = null ) {
775 $s = Xml::openElement( 'table', $attribs );
776
777 if ( is_array( $headers ) ) {
778 $s .= Xml::openElement( 'thead', $attribs );
779
780 foreach( $headers as $id => $header ) {
781 $attribs = array();
782
783 if ( is_string( $id ) ) {
784 $attribs['id'] = $id;
785 }
786
787 $s .= Xml::element( 'th', $attribs, $header );
788 }
789 $s .= Xml::closeElement( 'thead' );
790 }
791
792 foreach( $rows as $id => $row ) {
793 $attribs = array();
794
795 if ( is_string( $id ) ) {
796 $attribs['id'] = $id;
797 }
798
799 $s .= Xml::buildTableRow( $attribs, $row );
800 }
801
802 $s .= Xml::closeElement( 'table' );
803
804 return $s;
805 }
806
807 /**
808 * Build a row for a table
809 * @param $attribs array An array of attributes to apply to the tr tag
810 * @param $cells array An array of strings to put in <td>
811 * @return string
812 */
813 public static function buildTableRow( $attribs, $cells ) {
814 $s = Xml::openElement( 'tr', $attribs );
815
816 foreach( $cells as $id => $cell ) {
817
818 $attribs = array();
819
820 if ( is_string( $id ) ) {
821 $attribs['id'] = $id;
822 }
823
824 $s .= Xml::element( 'td', $attribs, $cell );
825 }
826
827 $s .= Xml::closeElement( 'tr' );
828
829 return $s;
830 }
831 }
832
833 class XmlSelect {
834 protected $options = array();
835 protected $default = false;
836 protected $attributes = array();
837
838 public function __construct( $name = false, $id = false, $default = false ) {
839 if ( $name ) {
840 $this->setAttribute( 'name', $name );
841 }
842
843 if ( $id ) {
844 $this->setAttribute( 'id', $id );
845 }
846
847 if ( $default !== false ) {
848 $this->default = $default;
849 }
850 }
851
852 /**
853 * @param $default
854 */
855 public function setDefault( $default ) {
856 $this->default = $default;
857 }
858
859 /**
860 * @param $name string
861 * @param $value
862 */
863 public function setAttribute( $name, $value ) {
864 $this->attributes[$name] = $value;
865 }
866
867 /**
868 * @param $name
869 * @return array|null
870 */
871 public function getAttribute( $name ) {
872 if ( isset( $this->attributes[$name] ) ) {
873 return $this->attributes[$name];
874 } else {
875 return null;
876 }
877 }
878
879 /**
880 * @param $name
881 * @param $value bool
882 */
883 public function addOption( $name, $value = false ) {
884 // Stab stab stab
885 $value = ($value !== false) ? $value : $name;
886
887 $this->options[] = array( $name => $value );
888 }
889
890 /**
891 * This accepts an array of form
892 * label => value
893 * label => ( label => value, label => value )
894 *
895 * @param $options
896 */
897 public function addOptions( $options ) {
898 $this->options[] = $options;
899 }
900
901 /**
902 * This accepts an array of form
903 * label => value
904 * label => ( label => value, label => value )
905 *
906 * @param $options
907 * @param bool $default
908 * @return string
909 */
910 static function formatOptions( $options, $default = false ) {
911 $data = '';
912
913 foreach( $options as $label => $value ) {
914 if ( is_array( $value ) ) {
915 $contents = self::formatOptions( $value, $default );
916 $data .= Html::rawElement( 'optgroup', array( 'label' => $label ), $contents ) . "\n";
917 } else {
918 $data .= Xml::option( $label, $value, $value === $default ) . "\n";
919 }
920 }
921
922 return $data;
923 }
924
925 /**
926 * @return string
927 */
928 public function getHTML() {
929 $contents = '';
930
931 foreach ( $this->options as $options ) {
932 $contents .= self::formatOptions( $options, $this->default );
933 }
934
935 return Html::rawElement( 'select', $this->attributes, rtrim( $contents ) );
936 }
937 }
938
939 /**
940 * A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to
941 * interpret a given string as being a JavaScript expression, instead of string
942 * data.
943 *
944 * Example:
945 *
946 * Xml::encodeJsVar( new XmlJsCode( 'a + b' ) );
947 *
948 * Returns "a + b".
949 * @since 1.17
950 */
951 class XmlJsCode {
952 public $value;
953
954 function __construct( $value ) {
955 $this->value = $value;
956 }
957 }