thumb_handler.php doesn't seem to extract path_info correctly
[mediawiki.git] / includes / Xml.php
blob223061a1a724de75341d7fbc40886dde614194e0
1 <?php
2 /**
3 * Methods to generate XML.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Module of static functions for generating XML
26 class Xml {
27 /**
28 * Format an XML element with given attributes and, optionally, text content.
29 * Element and attribute names are assumed to be ready for literal inclusion.
30 * Strings are assumed to not contain XML-illegal characters; special
31 * characters (<, >, &) are escaped but illegals are not touched.
33 * @param string $element element name
34 * @param array $attribs Name=>value pairs. Values will be escaped.
35 * @param string $contents NULL to make an open tag only; '' for a contentless closed tag (default)
36 * @param bool $allowShortTag whether '' in $contents will result in a contentless closed tag
37 * @return string
39 public static function element( $element, $attribs = null, $contents = '', $allowShortTag = true ) {
40 $out = '<' . $element;
41 if ( !is_null( $attribs ) ) {
42 $out .= self::expandAttributes( $attribs );
44 if ( is_null( $contents ) ) {
45 $out .= '>';
46 } else {
47 if ( $allowShortTag && $contents === '' ) {
48 $out .= ' />';
49 } else {
50 $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
53 return $out;
56 /**
57 * Given an array of ('attributename' => 'value'), it generates the code
58 * to set the XML attributes : attributename="value".
59 * The values are passed to Sanitizer::encodeAttribute.
60 * Return null if no attributes given.
61 * @param array $attribs of attributes for an XML element
62 * @throws MWException
63 * @return null|string
65 public static function expandAttributes( $attribs ) {
66 $out = '';
67 if ( is_null( $attribs ) ) {
68 return null;
69 } elseif ( is_array( $attribs ) ) {
70 foreach ( $attribs as $name => $val ) {
71 $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
73 return $out;
74 } else {
75 throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
79 /**
80 * Format an XML element as with self::element(), but run text through the
81 * $wgContLang->normalize() validator first to ensure that no invalid UTF-8
82 * is passed.
84 * @param $element String:
85 * @param array $attribs Name=>value pairs. Values will be escaped.
86 * @param string $contents NULL to make an open tag only; '' for a contentless closed tag (default)
87 * @return string
89 public static function elementClean( $element, $attribs = array(), $contents = '' ) {
90 global $wgContLang;
91 if ( $attribs ) {
92 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
94 if ( $contents ) {
95 wfProfileIn( __METHOD__ . '-norm' );
96 $contents = $wgContLang->normalize( $contents );
97 wfProfileOut( __METHOD__ . '-norm' );
99 return self::element( $element, $attribs, $contents );
103 * This opens an XML element
105 * @param string $element name of the element
106 * @param array $attribs of attributes, see Xml::expandAttributes()
107 * @return string
109 public static function openElement( $element, $attribs = null ) {
110 return '<' . $element . self::expandAttributes( $attribs ) . '>';
114 * Shortcut to close an XML element
115 * @param string $element element name
116 * @return string
118 public static function closeElement( $element ) {
119 return "</$element>";
123 * Same as Xml::element(), but does not escape contents. Handy when the
124 * content you have is already valid xml.
126 * @param string $element element name
127 * @param array $attribs of attributes
128 * @param string $contents content of the element
129 * @return string
131 public static function tags( $element, $attribs = null, $contents ) {
132 return self::openElement( $element, $attribs ) . $contents . "</$element>";
136 * Build a drop-down box for selecting a namespace
138 * @param $selected Mixed: Namespace which should be pre-selected
139 * @param $all Mixed: Value of an item denoting all namespaces, or null to omit
140 * @param $element_name String: value of the "name" attribute of the select tag
141 * @param string $label optional label to add to the field
142 * @return string
143 * @deprecated since 1.19
145 public static function namespaceSelector( $selected = '', $all = null, $element_name = 'namespace', $label = null ) {
146 wfDeprecated( __METHOD__, '1.19' );
147 return Html::namespaceSelector( array(
148 'selected' => $selected,
149 'all' => $all,
150 'label' => $label,
151 ), array(
152 'name' => $element_name,
153 'id' => 'namespace',
154 'class' => 'namespaceselector',
155 ) );
159 * Create a date selector
161 * @param $selected Mixed: the month which should be selected, default ''
162 * @param string $allmonths value of a special item denoting all month. Null to not include (default)
163 * @param string $id Element identifier
164 * @return String: Html string containing the month selector
166 public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
167 global $wgLang;
168 $options = array();
169 if ( is_null( $selected ) ) {
170 $selected = '';
172 if ( !is_null( $allmonths ) ) {
173 $options[] = self::option( wfMessage( 'monthsall' )->text(), $allmonths, $selected === $allmonths );
175 for ( $i = 1; $i < 13; $i++ ) {
176 $options[] = self::option( $wgLang->getMonthName( $i ), $i, $selected === $i );
178 return self::openElement( 'select', array( 'id' => $id, 'name' => 'month', 'class' => 'mw-month-selector' ) )
179 . implode( "\n", $options )
180 . self::closeElement( 'select' );
184 * @param $year Integer
185 * @param $month Integer
186 * @return string Formatted HTML
188 public static function dateMenu( $year, $month ) {
189 # Offset overrides year/month selection
190 if ( $month && $month !== -1 ) {
191 $encMonth = intval( $month );
192 } else {
193 $encMonth = '';
195 if ( $year ) {
196 $encYear = intval( $year );
197 } elseif ( $encMonth ) {
198 $thisMonth = intval( gmdate( 'n' ) );
199 $thisYear = intval( gmdate( 'Y' ) );
200 if ( intval( $encMonth ) > $thisMonth ) {
201 $thisYear--;
203 $encYear = $thisYear;
204 } else {
205 $encYear = '';
207 $inputAttribs = array( 'id' => 'year', 'maxlength' => 4, 'size' => 7 );
208 return self::label( wfMessage( 'year' )->text(), 'year' ) . ' ' .
209 Html::input( 'year', $encYear, 'number', $inputAttribs ) . ' ' .
210 self::label( wfMessage( 'month' )->text(), 'month' ) . ' ' .
211 self::monthSelector( $encMonth, -1 );
215 * Construct a language selector appropriate for use in a form or preferences
217 * @param string $selected The language code of the selected language
218 * @param boolean $customisedOnly If true only languages which have some content are listed
219 * @param string $inLanguage The ISO code of the language to display the select list in (optional)
220 * @param array $overrideAttrs Override the attributes of the select tag (since 1.20)
221 * @param Message|null $msg Label message key (since 1.20)
222 * @return array containing 2 items: label HTML and select list HTML
224 public static function languageSelector( $selected, $customisedOnly = true, $inLanguage = null, $overrideAttrs = array(), Message $msg = null ) {
225 global $wgLanguageCode;
227 $include = $customisedOnly ? 'mwfile' : 'mw';
228 $languages = Language::fetchLanguageNames( $inLanguage, $include );
230 // Make sure the site language is in the list;
231 // a custom language code might not have a defined name...
232 if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
233 $languages[$wgLanguageCode] = $wgLanguageCode;
236 ksort( $languages );
239 * If a bogus value is set, default to the content language.
240 * Otherwise, no default is selected and the user ends up
241 * with Afrikaans since it's first in the list.
243 $selected = isset( $languages[$selected] ) ? $selected : $wgLanguageCode;
244 $options = "\n";
245 foreach ( $languages as $code => $name ) {
246 $options .= Xml::option( "$code - $name", $code, $code == $selected ) . "\n";
249 $attrs = array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' );
250 $attrs = array_merge( $attrs, $overrideAttrs );
252 if ( $msg === null ) {
253 $msg = wfMessage( 'yourlanguage' );
255 return array(
256 Xml::label( $msg->text(), $attrs['id'] ),
257 Xml::tags( 'select', $attrs, $options )
263 * Shortcut to make a span element
264 * @param string $text content of the element, will be escaped
265 * @param string $class class name of the span element
266 * @param array $attribs other attributes
267 * @return string
269 public static function span( $text, $class, $attribs = array() ) {
270 return self::element( 'span', array( 'class' => $class ) + $attribs, $text );
274 * Shortcut to make a specific element with a class attribute
275 * @param string $text content of the element, will be escaped
276 * @param string $class class name of the span element
277 * @param string $tag element name
278 * @param array $attribs other attributes
279 * @return string
281 public static function wrapClass( $text, $class, $tag = 'span', $attribs = array() ) {
282 return self::tags( $tag, array( 'class' => $class ) + $attribs, $text );
286 * Convenience function to build an HTML text input field
287 * @param string $name value of the name attribute
288 * @param int $size value of the size attribute
289 * @param $value mixed value of the value attribute
290 * @param array $attribs other attributes
291 * @return string HTML
293 public static function input( $name, $size = false, $value = false, $attribs = array() ) {
294 $attributes = array( 'name' => $name );
296 if ( $size ) {
297 $attributes['size'] = $size;
300 if ( $value !== false ) { // maybe 0
301 $attributes['value'] = $value;
304 return self::element( 'input', $attributes + $attribs );
308 * Convenience function to build an HTML password input field
309 * @param string $name value of the name attribute
310 * @param int $size value of the size attribute
311 * @param $value mixed value of the value attribute
312 * @param array $attribs other attributes
313 * @return string HTML
315 public static function password( $name, $size = false, $value = false, $attribs = array() ) {
316 return self::input( $name, $size, $value, array_merge( $attribs, array( 'type' => 'password' ) ) );
320 * Internal function for use in checkboxes and radio buttons and such.
322 * @param $name string
323 * @param $present bool
325 * @return array
327 public static function attrib( $name, $present = true ) {
328 return $present ? array( $name => $name ) : array();
332 * Convenience function to build an HTML checkbox
333 * @param string $name value of the name attribute
334 * @param bool $checked Whether the checkbox is checked or not
335 * @param array $attribs other attributes
336 * @return string HTML
338 public static function check( $name, $checked = false, $attribs = array() ) {
339 return self::element( 'input', array_merge(
340 array(
341 'name' => $name,
342 'type' => 'checkbox',
343 'value' => 1 ),
344 self::attrib( 'checked', $checked ),
345 $attribs ) );
349 * Convenience function to build an HTML radio button
350 * @param string $name value of the name attribute
351 * @param string $value value of the value attribute
352 * @param bool $checked Whether the checkbox is checked or not
353 * @param array $attribs other attributes
354 * @return string HTML
356 public static function radio( $name, $value, $checked = false, $attribs = array() ) {
357 return self::element( 'input', array(
358 'name' => $name,
359 'type' => 'radio',
360 'value' => $value ) + self::attrib( 'checked', $checked ) + $attribs );
364 * Convenience function to build an HTML form label
365 * @param string $label text of the label
366 * @param $id
367 * @param array $attribs an attribute array. This will usually be
368 * the same array as is passed to the corresponding input element,
369 * so this function will cherry-pick appropriate attributes to
370 * apply to the label as well; only class and title are applied.
371 * @return string HTML
373 public static function label( $label, $id, $attribs = array() ) {
374 $a = array( 'for' => $id );
376 # FIXME avoid copy pasting below:
377 if ( isset( $attribs['class'] ) ) {
378 $a['class'] = $attribs['class'];
380 if ( isset( $attribs['title'] ) ) {
381 $a['title'] = $attribs['title'];
384 return self::element( 'label', $a, $label );
388 * Convenience function to build an HTML text input field with a label
389 * @param string $label text of the label
390 * @param string $name value of the name attribute
391 * @param string $id id of the input
392 * @param int|Bool $size value of the size attribute
393 * @param string|Bool $value value of the value attribute
394 * @param array $attribs other attributes
395 * @return string HTML
397 public static function inputLabel( $label, $name, $id, $size = false, $value = false, $attribs = array() ) {
398 list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
399 return $label . '&#160;' . $input;
403 * Same as Xml::inputLabel() but return input and label in an array
405 * @param $label String
406 * @param $name String
407 * @param $id String
408 * @param $size Int|Bool
409 * @param $value String|Bool
410 * @param $attribs array
412 * @return array
414 public static function inputLabelSep( $label, $name, $id, $size = false, $value = false, $attribs = array() ) {
415 return array(
416 Xml::label( $label, $id, $attribs ),
417 self::input( $name, $size, $value, array( 'id' => $id ) + $attribs )
422 * Convenience function to build an HTML checkbox with a label
424 * @param $label
425 * @param $name
426 * @param $id
427 * @param $checked bool
428 * @param $attribs array
430 * @return string HTML
432 public static function checkLabel( $label, $name, $id, $checked = false, $attribs = array() ) {
433 return self::check( $name, $checked, array( 'id' => $id ) + $attribs ) .
434 '&#160;' .
435 self::label( $label, $id, $attribs );
439 * Convenience function to build an HTML radio button with a label
441 * @param $label
442 * @param $name
443 * @param $value
444 * @param $id
445 * @param $checked bool
446 * @param $attribs array
448 * @return string HTML
450 public static function radioLabel( $label, $name, $value, $id, $checked = false, $attribs = array() ) {
451 return self::radio( $name, $value, $checked, array( 'id' => $id ) + $attribs ) .
452 '&#160;' .
453 self::label( $label, $id, $attribs );
457 * Convenience function to build an HTML submit button
458 * @param string $value label text for the button
459 * @param array $attribs optional custom attributes
460 * @return string HTML
462 public static function submitButton( $value, $attribs = array() ) {
463 return Html::element( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs );
467 * Convenience function to build an HTML drop-down list item.
468 * @param string $text text for this item. Will be HTML escaped
469 * @param string $value form submission value; if empty, use text
470 * @param $selected boolean: if true, will be the default selected item
471 * @param array $attribs optional additional HTML attributes
472 * @return string HTML
474 public static function option( $text, $value = null, $selected = false,
475 $attribs = array() ) {
476 if ( !is_null( $value ) ) {
477 $attribs['value'] = $value;
479 if ( $selected ) {
480 $attribs['selected'] = 'selected';
482 return Html::element( 'option', $attribs, $text );
486 * Build a drop-down box from a textual list.
488 * @param $name Mixed: Name and id for the drop-down
489 * @param $list Mixed: Correctly formatted text (newline delimited) to be used to generate the options
490 * @param $other Mixed: Text for the "Other reasons" option
491 * @param $selected Mixed: Option which should be pre-selected
492 * @param $class Mixed: CSS classes for the drop-down
493 * @param $tabindex Mixed: Value of the tabindex attribute
494 * @return string
496 public static function listDropDown( $name = '', $list = '', $other = '', $selected = '', $class = '', $tabindex = null ) {
497 $optgroup = false;
499 $options = self::option( $other, 'other', $selected === 'other' );
501 foreach ( explode( "\n", $list ) as $option ) {
502 $value = trim( $option );
503 if ( $value == '' ) {
504 continue;
505 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
506 // A new group is starting ...
507 $value = trim( substr( $value, 1 ) );
508 if ( $optgroup ) {
509 $options .= self::closeElement( 'optgroup' );
511 $options .= self::openElement( 'optgroup', array( 'label' => $value ) );
512 $optgroup = true;
513 } elseif ( substr( $value, 0, 2 ) == '**' ) {
514 // groupmember
515 $value = trim( substr( $value, 2 ) );
516 $options .= self::option( $value, $value, $selected === $value );
517 } else {
518 // groupless reason list
519 if ( $optgroup ) {
520 $options .= self::closeElement( 'optgroup' );
522 $options .= self::option( $value, $value, $selected === $value );
523 $optgroup = false;
527 if ( $optgroup ) {
528 $options .= self::closeElement( 'optgroup' );
531 $attribs = array();
533 if ( $name ) {
534 $attribs['id'] = $name;
535 $attribs['name'] = $name;
538 if ( $class ) {
539 $attribs['class'] = $class;
542 if ( $tabindex ) {
543 $attribs['tabindex'] = $tabindex;
546 return Xml::openElement( 'select', $attribs )
547 . "\n"
548 . $options
549 . "\n"
550 . Xml::closeElement( 'select' );
554 * Shortcut for creating fieldsets.
556 * @param string|bool $legend Legend of the fieldset. If evaluates to false, legend is not added.
557 * @param string $content Pre-escaped content for the fieldset. If false, only open fieldset is returned.
558 * @param array $attribs Any attributes to fieldset-element.
560 * @return string
562 public static function fieldset( $legend = false, $content = false, $attribs = array() ) {
563 $s = Xml::openElement( 'fieldset', $attribs ) . "\n";
565 if ( $legend ) {
566 $s .= Xml::element( 'legend', null, $legend ) . "\n";
569 if ( $content !== false ) {
570 $s .= $content . "\n";
571 $s .= Xml::closeElement( 'fieldset' ) . "\n";
574 return $s;
578 * Shortcut for creating textareas.
580 * @param string $name The 'name' for the textarea
581 * @param string $content Content for the textarea
582 * @param int $cols The number of columns for the textarea
583 * @param int $rows The number of rows for the textarea
584 * @param array $attribs Any other attributes for the textarea
586 * @return string
588 public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = array() ) {
589 return self::element( 'textarea',
590 array(
591 'name' => $name,
592 'id' => $name,
593 'cols' => $cols,
594 'rows' => $rows
595 ) + $attribs,
596 $content, false );
600 * Returns an escaped string suitable for inclusion in a string literal
601 * for JavaScript source code.
602 * Illegal control characters are assumed not to be present.
604 * @deprecated since 1.21; use Xml::encodeJsVar() or Xml::encodeJsCall() instead
605 * @param string $string to escape
606 * @return String
608 public static function escapeJsString( $string ) {
609 // See ECMA 262 section 7.8.4 for string literal format
610 $pairs = array(
611 "\\" => "\\\\",
612 "\"" => "\\\"",
613 '\'' => '\\\'',
614 "\n" => "\\n",
615 "\r" => "\\r",
617 # To avoid closing the element or CDATA section
618 "<" => "\\x3c",
619 ">" => "\\x3e",
621 # To avoid any complaints about bad entity refs
622 "&" => "\\x26",
624 # Work around https://bugzilla.mozilla.org/show_bug.cgi?id=274152
625 # Encode certain Unicode formatting chars so affected
626 # versions of Gecko don't misinterpret our strings;
627 # this is a common problem with Farsi text.
628 "\xe2\x80\x8c" => "\\u200c", // ZERO WIDTH NON-JOINER
629 "\xe2\x80\x8d" => "\\u200d", // ZERO WIDTH JOINER
632 return strtr( $string, $pairs );
636 * Encode a variable of arbitrary type to JavaScript.
637 * If the value is an XmlJsCode object, pass through the object's value verbatim.
639 * @note Only use this function for generating JavaScript code. If generating output
640 * for a proper JSON parser, just call FormatJson::encode() directly.
642 * @param mixed $value The value being encoded. Can be any type except a resource.
643 * @param bool $pretty If true, add non-significant whitespace to improve readability.
644 * @return string|bool: String if successful; false upon failure
646 public static function encodeJsVar( $value, $pretty = false ) {
647 if ( $value instanceof XmlJsCode ) {
648 return $value->value;
650 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
654 * Create a call to a JavaScript function. The supplied arguments will be
655 * encoded using Xml::encodeJsVar().
657 * @since 1.17
658 * @param string $name The name of the function to call, or a JavaScript expression
659 * which evaluates to a function object which is called.
660 * @param array $args The arguments to pass to the function.
661 * @param bool $pretty If true, add non-significant whitespace to improve readability.
662 * @return string|bool: String if successful; false upon failure
664 public static function encodeJsCall( $name, $args, $pretty = false ) {
665 foreach ( $args as &$arg ) {
666 $arg = Xml::encodeJsVar( $arg, $pretty );
667 if ( $arg === false ) {
668 return false;
672 return "$name(" . ( $pretty
673 ? ( ' ' . implode( ', ', $args ) . ' ' )
674 : implode( ',', $args )
675 ) . ");";
679 * Check if a string is well-formed XML.
680 * Must include the surrounding tag.
682 * @param string $text string to test.
683 * @return bool
685 * @todo Error position reporting return
687 public static function isWellFormed( $text ) {
688 $parser = xml_parser_create( "UTF-8" );
690 # case folding violates XML standard, turn it off
691 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
693 if ( !xml_parse( $parser, $text, true ) ) {
694 //$err = xml_error_string( xml_get_error_code( $parser ) );
695 //$position = xml_get_current_byte_index( $parser );
696 //$fragment = $this->extractFragment( $html, $position );
697 //$this->mXmlError = "$err at byte $position:\n$fragment";
698 xml_parser_free( $parser );
699 return false;
702 xml_parser_free( $parser );
704 return true;
708 * Check if a string is a well-formed XML fragment.
709 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
710 * and can use HTML named entities.
712 * @param $text String:
713 * @return bool
715 public static function isWellFormedXmlFragment( $text ) {
716 $html =
717 Sanitizer::hackDocType() .
718 '<html>' .
719 $text .
720 '</html>';
722 return Xml::isWellFormed( $html );
726 * Replace " > and < with their respective HTML entities ( &quot;,
727 * &gt;, &lt;)
729 * @param string $in text that might contain HTML tags.
730 * @return string Escaped string
732 public static function escapeTagsOnly( $in ) {
733 return str_replace(
734 array( '"', '>', '<' ),
735 array( '&quot;', '&gt;', '&lt;' ),
736 $in );
740 * Generate a form (without the opening form element).
741 * Output optionally includes a submit button.
742 * @param array $fields Associative array, key is the name of a message that contains a description for the field, value is an HTML string containing the appropriate input.
743 * @param string $submitLabel The name of a message containing a label for the submit button.
744 * @return string HTML form.
746 public static function buildForm( $fields, $submitLabel = null ) {
747 $form = '';
748 $form .= "<table><tbody>";
750 foreach ( $fields as $labelmsg => $input ) {
751 $id = "mw-$labelmsg";
752 $form .= Xml::openElement( 'tr', array( 'id' => $id ) );
754 // TODO use a <label> here for accessibility purposes - will need
755 // to either not use a table to build the form, or find the ID of
756 // the input somehow.
758 $form .= Xml::tags( 'td', array( 'class' => 'mw-label' ), wfMessage( $labelmsg )->parse() );
759 $form .= Xml::openElement( 'td', array( 'class' => 'mw-input' ) ) . $input . Xml::closeElement( 'td' );
760 $form .= Xml::closeElement( 'tr' );
763 if ( $submitLabel ) {
764 $form .= Xml::openElement( 'tr' );
765 $form .= Xml::tags( 'td', array(), '' );
766 $form .= Xml::openElement( 'td', array( 'class' => 'mw-submit' ) ) . Xml::submitButton( wfMessage( $submitLabel )->text() ) . Xml::closeElement( 'td' );
767 $form .= Xml::closeElement( 'tr' );
770 $form .= "</tbody></table>";
772 return $form;
776 * Build a table of data
777 * @param array $rows An array of arrays of strings, each to be a row in a table
778 * @param array $attribs An array of attributes to apply to the table tag [optional]
779 * @param array $headers An array of strings to use as table headers [optional]
780 * @return string
782 public static function buildTable( $rows, $attribs = array(), $headers = null ) {
783 $s = Xml::openElement( 'table', $attribs );
785 if ( is_array( $headers ) ) {
786 $s .= Xml::openElement( 'thead', $attribs );
788 foreach ( $headers as $id => $header ) {
789 $attribs = array();
791 if ( is_string( $id ) ) {
792 $attribs['id'] = $id;
795 $s .= Xml::element( 'th', $attribs, $header );
797 $s .= Xml::closeElement( 'thead' );
800 foreach ( $rows as $id => $row ) {
801 $attribs = array();
803 if ( is_string( $id ) ) {
804 $attribs['id'] = $id;
807 $s .= Xml::buildTableRow( $attribs, $row );
810 $s .= Xml::closeElement( 'table' );
812 return $s;
816 * Build a row for a table
817 * @param array $attribs An array of attributes to apply to the tr tag
818 * @param array $cells An array of strings to put in <td>
819 * @return string
821 public static function buildTableRow( $attribs, $cells ) {
822 $s = Xml::openElement( 'tr', $attribs );
824 foreach ( $cells as $id => $cell ) {
825 $attribs = array();
827 if ( is_string( $id ) ) {
828 $attribs['id'] = $id;
831 $s .= Xml::element( 'td', $attribs, $cell );
834 $s .= Xml::closeElement( 'tr' );
836 return $s;
840 class XmlSelect {
841 protected $options = array();
842 protected $default = false;
843 protected $attributes = array();
845 public function __construct( $name = false, $id = false, $default = false ) {
846 if ( $name ) {
847 $this->setAttribute( 'name', $name );
850 if ( $id ) {
851 $this->setAttribute( 'id', $id );
854 if ( $default !== false ) {
855 $this->default = $default;
860 * @param $default
862 public function setDefault( $default ) {
863 $this->default = $default;
867 * @param $name string
868 * @param $value
870 public function setAttribute( $name, $value ) {
871 $this->attributes[$name] = $value;
875 * @param $name
876 * @return array|null
878 public function getAttribute( $name ) {
879 if ( isset( $this->attributes[$name] ) ) {
880 return $this->attributes[$name];
881 } else {
882 return null;
887 * @param $name
888 * @param $value bool
890 public function addOption( $name, $value = false ) {
891 // Stab stab stab
892 $value = $value !== false ? $value : $name;
894 $this->options[] = array( $name => $value );
898 * This accepts an array of form
899 * label => value
900 * label => ( label => value, label => value )
902 * @param $options
904 public function addOptions( $options ) {
905 $this->options[] = $options;
909 * This accepts an array of form
910 * label => value
911 * label => ( label => value, label => value )
913 * @param $options
914 * @param bool $default
915 * @return string
917 static function formatOptions( $options, $default = false ) {
918 $data = '';
920 foreach ( $options as $label => $value ) {
921 if ( is_array( $value ) ) {
922 $contents = self::formatOptions( $value, $default );
923 $data .= Html::rawElement( 'optgroup', array( 'label' => $label ), $contents ) . "\n";
924 } else {
925 $data .= Xml::option( $label, $value, $value === $default ) . "\n";
929 return $data;
933 * @return string
935 public function getHTML() {
936 $contents = '';
938 foreach ( $this->options as $options ) {
939 $contents .= self::formatOptions( $options, $this->default );
942 return Html::rawElement( 'select', $this->attributes, rtrim( $contents ) );
947 * A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to
948 * interpret a given string as being a JavaScript expression, instead of string
949 * data.
951 * Example:
953 * Xml::encodeJsVar( new XmlJsCode( 'a + b' ) );
955 * Returns "a + b".
957 * @note As of 1.21, XmlJsCode objects cannot be nested inside objects or arrays. The sole
958 * exception is the $args argument to Xml::encodeJsCall() because Xml::encodeJsVar() is
959 * called for each individual element in that array.
961 * @since 1.17
963 class XmlJsCode {
964 public $value;
966 function __construct( $value ) {
967 $this->value = $value;