Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / Xml.php
blob29c240eb1d9a1ede55282e77a9873763be3bc5f7
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 ) { return "</$element>"; }
121 * Same as Xml::element(), but does not escape contents. Handy when the
122 * content you have is already valid xml.
124 * @param string $element element name
125 * @param array $attribs of attributes
126 * @param string $contents content of the element
127 * @return string
129 public static function tags( $element, $attribs = null, $contents ) {
130 return self::openElement( $element, $attribs ) . $contents . "</$element>";
134 * Build a drop-down box for selecting a namespace
136 * @param $selected Mixed: Namespace which should be pre-selected
137 * @param $all Mixed: Value of an item denoting all namespaces, or null to omit
138 * @param $element_name String: value of the "name" attribute of the select tag
139 * @param string $label optional label to add to the field
140 * @return string
141 * @deprecated since 1.19
143 public static function namespaceSelector( $selected = '', $all = null, $element_name = 'namespace', $label = null ) {
144 wfDeprecated( __METHOD__, '1.19' );
145 return Html::namespaceSelector( array(
146 'selected' => $selected,
147 'all' => $all,
148 'label' => $label,
149 ), array(
150 'name' => $element_name,
151 'id' => 'namespace',
152 'class' => 'namespaceselector',
153 ) );
157 * Create a date selector
159 * @param $selected Mixed: the month which should be selected, default ''
160 * @param string $allmonths value of a special item denoting all month. Null to not include (default)
161 * @param string $id Element identifier
162 * @return String: Html string containing the month selector
164 public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
165 global $wgLang;
166 $options = array();
167 if( is_null( $selected ) )
168 $selected = '';
169 if( !is_null( $allmonths ) )
170 $options[] = self::option( wfMessage( 'monthsall' )->text(), $allmonths, $selected === $allmonths );
171 for( $i = 1; $i < 13; $i++ )
172 $options[] = self::option( $wgLang->getMonthName( $i ), $i, $selected === $i );
173 return self::openElement( 'select', array( 'id' => $id, 'name' => 'month', 'class' => 'mw-month-selector' ) )
174 . implode( "\n", $options )
175 . self::closeElement( 'select' );
179 * @param $year Integer
180 * @param $month Integer
181 * @return string Formatted HTML
183 public static function dateMenu( $year, $month ) {
184 # Offset overrides year/month selection
185 if( $month && $month !== -1 ) {
186 $encMonth = intval( $month );
187 } else {
188 $encMonth = '';
190 if( $year ) {
191 $encYear = intval( $year );
192 } elseif( $encMonth ) {
193 $thisMonth = intval( gmdate( 'n' ) );
194 $thisYear = intval( gmdate( 'Y' ) );
195 if( intval( $encMonth ) > $thisMonth ) {
196 $thisYear--;
198 $encYear = $thisYear;
199 } else {
200 $encYear = '';
202 $inputAttribs = array( 'id' => 'year', 'maxlength' => 4, 'size' => 7 );
203 return self::label( wfMessage( 'year' )->text(), 'year' ) . ' ' .
204 Html::input( 'year', $encYear, 'number', $inputAttribs ) . ' ' .
205 self::label( wfMessage( 'month' )->text(), 'month' ) . ' ' .
206 self::monthSelector( $encMonth, -1 );
210 * Construct a language selector appropriate for use in a form or preferences
212 * @param string $selected The language code of the selected language
213 * @param boolean $customisedOnly If true only languages which have some content are listed
214 * @param string $inLanguage The ISO code of the language to display the select list in (optional)
215 * @param array $overrideAttrs Override the attributes of the select tag (since 1.20)
216 * @param Message|null $msg Label message key (since 1.20)
217 * @return array containing 2 items: label HTML and select list HTML
219 public static function languageSelector( $selected, $customisedOnly = true, $inLanguage = null, $overrideAttrs = array(), Message $msg = null ) {
220 global $wgLanguageCode;
222 $include = $customisedOnly ? 'mwfile' : 'mw';
223 $languages = Language::fetchLanguageNames( $inLanguage, $include );
225 // Make sure the site language is in the list;
226 // a custom language code might not have a defined name...
227 if( !array_key_exists( $wgLanguageCode, $languages ) ) {
228 $languages[$wgLanguageCode] = $wgLanguageCode;
231 ksort( $languages );
234 * If a bogus value is set, default to the content language.
235 * Otherwise, no default is selected and the user ends up
236 * with Afrikaans since it's first in the list.
238 $selected = isset( $languages[$selected] ) ? $selected : $wgLanguageCode;
239 $options = "\n";
240 foreach( $languages as $code => $name ) {
241 $options .= Xml::option( "$code - $name", $code, ($code == $selected) ) . "\n";
244 $attrs = array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' );
245 $attrs = array_merge( $attrs, $overrideAttrs );
247 if( $msg === null ) {
248 $msg = wfMessage( 'yourlanguage' );
250 return array(
251 Xml::label( $msg->text(), $attrs['id'] ),
252 Xml::tags( 'select', $attrs, $options )
258 * Shortcut to make a span element
259 * @param string $text content of the element, will be escaped
260 * @param string $class class name of the span element
261 * @param array $attribs other attributes
262 * @return string
264 public static function span( $text, $class, $attribs = array() ) {
265 return self::element( 'span', array( 'class' => $class ) + $attribs, $text );
269 * Shortcut to make a specific element with a class attribute
270 * @param string $text content of the element, will be escaped
271 * @param string $class class name of the span element
272 * @param string $tag element name
273 * @param array $attribs other attributes
274 * @return string
276 public static function wrapClass( $text, $class, $tag = 'span', $attribs = array() ) {
277 return self::tags( $tag, array( 'class' => $class ) + $attribs, $text );
281 * Convenience function to build an HTML text input field
282 * @param string $name value of the name attribute
283 * @param int $size value of the size attribute
284 * @param $value mixed value of the value attribute
285 * @param array $attribs other attributes
286 * @return string HTML
288 public static function input( $name, $size = false, $value = false, $attribs = array() ) {
289 $attributes = array( 'name' => $name );
291 if( $size ) {
292 $attributes['size'] = $size;
295 if( $value !== false ) { // maybe 0
296 $attributes['value'] = $value;
299 return self::element( 'input', $attributes + $attribs );
303 * Convenience function to build an HTML password input field
304 * @param string $name value of the name attribute
305 * @param int $size value of the size attribute
306 * @param $value mixed value of the value attribute
307 * @param array $attribs other attributes
308 * @return string HTML
310 public static function password( $name, $size = false, $value = false, $attribs = array() ) {
311 return self::input( $name, $size, $value, array_merge( $attribs, array( 'type' => 'password' ) ) );
315 * Internal function for use in checkboxes and radio buttons and such.
317 * @param $name string
318 * @param $present bool
320 * @return array
322 public static function attrib( $name, $present = true ) {
323 return $present ? array( $name => $name ) : array();
327 * Convenience function to build an HTML checkbox
328 * @param string $name value of the name attribute
329 * @param bool $checked Whether the checkbox is checked or not
330 * @param array $attribs other attributes
331 * @return string HTML
333 public static function check( $name, $checked = false, $attribs = array() ) {
334 return self::element( 'input', array_merge(
335 array(
336 'name' => $name,
337 'type' => 'checkbox',
338 'value' => 1 ),
339 self::attrib( 'checked', $checked ),
340 $attribs ) );
344 * Convenience function to build an HTML radio button
345 * @param string $name value of the name attribute
346 * @param string $value value of the value attribute
347 * @param bool $checked Whether the checkbox is checked or not
348 * @param array $attribs other attributes
349 * @return string HTML
351 public static function radio( $name, $value, $checked = false, $attribs = array() ) {
352 return self::element( 'input', array(
353 'name' => $name,
354 'type' => 'radio',
355 'value' => $value ) + self::attrib( 'checked', $checked ) + $attribs );
359 * Convenience function to build an HTML form label
360 * @param string $label text of the label
361 * @param $id
362 * @param array $attribs an attribute array. This will usually be
363 * the same array as is passed to the corresponding input element,
364 * so this function will cherry-pick appropriate attributes to
365 * apply to the label as well; only class and title are applied.
366 * @return string HTML
368 public static function label( $label, $id, $attribs = array() ) {
369 $a = array( 'for' => $id );
371 # FIXME avoid copy pasting below:
372 if( isset( $attribs['class'] ) ) {
373 $a['class'] = $attribs['class'];
375 if( isset( $attribs['title'] ) ) {
376 $a['title'] = $attribs['title'];
379 return self::element( 'label', $a, $label );
383 * Convenience function to build an HTML text input field with a label
384 * @param string $label text of the label
385 * @param string $name value of the name attribute
386 * @param string $id id of the input
387 * @param int|Bool $size value of the size attribute
388 * @param string|Bool $value value of the value attribute
389 * @param array $attribs other attributes
390 * @return string HTML
392 public static function inputLabel( $label, $name, $id, $size = false, $value = false, $attribs = array() ) {
393 list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
394 return $label . '&#160;' . $input;
398 * Same as Xml::inputLabel() but return input and label in an array
400 * @param $label String
401 * @param $name String
402 * @param $id String
403 * @param $size Int|Bool
404 * @param $value String|Bool
405 * @param $attribs array
407 * @return array
409 public static function inputLabelSep( $label, $name, $id, $size = false, $value = false, $attribs = array() ) {
410 return array(
411 Xml::label( $label, $id, $attribs ),
412 self::input( $name, $size, $value, array( 'id' => $id ) + $attribs )
417 * Convenience function to build an HTML checkbox with a label
419 * @param $label
420 * @param $name
421 * @param $id
422 * @param $checked bool
423 * @param $attribs array
425 * @return string HTML
427 public static function checkLabel( $label, $name, $id, $checked = false, $attribs = array() ) {
428 return self::check( $name, $checked, array( 'id' => $id ) + $attribs ) .
429 '&#160;' .
430 self::label( $label, $id, $attribs );
434 * Convenience function to build an HTML radio button with a label
436 * @param $label
437 * @param $name
438 * @param $value
439 * @param $id
440 * @param $checked bool
441 * @param $attribs array
443 * @return string HTML
445 public static function radioLabel( $label, $name, $value, $id, $checked = false, $attribs = array() ) {
446 return self::radio( $name, $value, $checked, array( 'id' => $id ) + $attribs ) .
447 '&#160;' .
448 self::label( $label, $id, $attribs );
452 * Convenience function to build an HTML submit button
453 * @param string $value label text for the button
454 * @param array $attribs optional custom attributes
455 * @return string HTML
457 public static function submitButton( $value, $attribs = array() ) {
458 return Html::element( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs );
462 * Convenience function to build an HTML drop-down list item.
463 * @param string $text text for this item. Will be HTML escaped
464 * @param string $value form submission value; if empty, use text
465 * @param $selected boolean: if true, will be the default selected item
466 * @param array $attribs optional additional HTML attributes
467 * @return string HTML
469 public static function option( $text, $value = null, $selected = false,
470 $attribs = array() ) {
471 if( !is_null( $value ) ) {
472 $attribs['value'] = $value;
474 if( $selected ) {
475 $attribs['selected'] = 'selected';
477 return Html::element( 'option', $attribs, $text );
481 * Build a drop-down box from a textual list.
483 * @param $name Mixed: Name and id for the drop-down
484 * @param $list Mixed: Correctly formatted text (newline delimited) to be used to generate the options
485 * @param $other Mixed: Text for the "Other reasons" option
486 * @param $selected Mixed: Option which should be pre-selected
487 * @param $class Mixed: CSS classes for the drop-down
488 * @param $tabindex Mixed: Value of the tabindex attribute
489 * @return string
491 public static function listDropDown( $name = '', $list = '', $other = '', $selected = '', $class = '', $tabindex = null ) {
492 $optgroup = false;
494 $options = self::option( $other, 'other', $selected === 'other' );
496 foreach ( explode( "\n", $list ) as $option ) {
497 $value = trim( $option );
498 if ( $value == '' ) {
499 continue;
500 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
501 // A new group is starting ...
502 $value = trim( substr( $value, 1 ) );
503 if( $optgroup ) $options .= self::closeElement( 'optgroup' );
504 $options .= self::openElement( 'optgroup', array( 'label' => $value ) );
505 $optgroup = true;
506 } elseif ( substr( $value, 0, 2 ) == '**' ) {
507 // groupmember
508 $value = trim( substr( $value, 2 ) );
509 $options .= self::option( $value, $value, $selected === $value );
510 } else {
511 // groupless reason list
512 if( $optgroup ) $options .= self::closeElement( 'optgroup' );
513 $options .= self::option( $value, $value, $selected === $value );
514 $optgroup = false;
518 if( $optgroup ) $options .= self::closeElement( 'optgroup' );
520 $attribs = array();
522 if( $name ) {
523 $attribs['id'] = $name;
524 $attribs['name'] = $name;
527 if( $class ) {
528 $attribs['class'] = $class;
531 if( $tabindex ) {
532 $attribs['tabindex'] = $tabindex;
535 return Xml::openElement( 'select', $attribs )
536 . "\n"
537 . $options
538 . "\n"
539 . Xml::closeElement( 'select' );
543 * Shortcut for creating fieldsets.
545 * @param string|bool $legend Legend of the fieldset. If evaluates to false, legend is not added.
546 * @param string $content Pre-escaped content for the fieldset. If false, only open fieldset is returned.
547 * @param array $attribs Any attributes to fieldset-element.
549 * @return string
551 public static function fieldset( $legend = false, $content = false, $attribs = array() ) {
552 $s = Xml::openElement( 'fieldset', $attribs ) . "\n";
554 if ( $legend ) {
555 $s .= Xml::element( 'legend', null, $legend ) . "\n";
558 if ( $content !== false ) {
559 $s .= $content . "\n";
560 $s .= Xml::closeElement( 'fieldset' ) . "\n";
563 return $s;
567 * Shortcut for creating textareas.
569 * @param string $name The 'name' for the textarea
570 * @param string $content Content for the textarea
571 * @param int $cols The number of columns for the textarea
572 * @param int $rows The number of rows for the textarea
573 * @param array $attribs Any other attributes for the textarea
575 * @return string
577 public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = array() ) {
578 return self::element( 'textarea',
579 array(
580 'name' => $name,
581 'id' => $name,
582 'cols' => $cols,
583 'rows' => $rows
584 ) + $attribs,
585 $content, false );
589 * Returns an escaped string suitable for inclusion in a string literal
590 * for JavaScript source code.
591 * Illegal control characters are assumed not to be present.
593 * @deprecated since 1.21; use Xml::encodeJsVar() or Xml::encodeJsCall() instead
594 * @param string $string to escape
595 * @return String
597 public static function escapeJsString( $string ) {
598 // See ECMA 262 section 7.8.4 for string literal format
599 $pairs = array(
600 "\\" => "\\\\",
601 "\"" => "\\\"",
602 '\'' => '\\\'',
603 "\n" => "\\n",
604 "\r" => "\\r",
606 # To avoid closing the element or CDATA section
607 "<" => "\\x3c",
608 ">" => "\\x3e",
610 # To avoid any complaints about bad entity refs
611 "&" => "\\x26",
613 # Work around https://bugzilla.mozilla.org/show_bug.cgi?id=274152
614 # Encode certain Unicode formatting chars so affected
615 # versions of Gecko don't misinterpret our strings;
616 # this is a common problem with Farsi text.
617 "\xe2\x80\x8c" => "\\u200c", // ZERO WIDTH NON-JOINER
618 "\xe2\x80\x8d" => "\\u200d", // ZERO WIDTH JOINER
621 return strtr( $string, $pairs );
625 * Encode a variable of arbitrary type to JavaScript.
626 * If the value is an XmlJsCode object, pass through the object's value verbatim.
628 * @note Only use this function for generating JavaScript code. If generating output
629 * for a proper JSON parser, just call FormatJson::encode() directly.
631 * @param mixed $value The value being encoded. Can be any type except a resource.
632 * @param bool $pretty If true, add non-significant whitespace to improve readability.
633 * @return string|bool: String if successful; false upon failure
635 public static function encodeJsVar( $value, $pretty = false ) {
636 if ( $value instanceof XmlJsCode ) {
637 return $value->value;
639 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
643 * Create a call to a JavaScript function. The supplied arguments will be
644 * encoded using Xml::encodeJsVar().
646 * @since 1.17
647 * @param string $name The name of the function to call, or a JavaScript expression
648 * which evaluates to a function object which is called.
649 * @param array $args The arguments to pass to the function.
650 * @param bool $pretty If true, add non-significant whitespace to improve readability.
651 * @return string|bool: String if successful; false upon failure
653 public static function encodeJsCall( $name, $args, $pretty = false ) {
654 foreach ( $args as &$arg ) {
655 $arg = Xml::encodeJsVar( $arg, $pretty );
656 if ( $arg === false ) {
657 return false;
661 return "$name(" . ( $pretty
662 ? ( ' ' . implode( ', ', $args ) . ' ' )
663 : implode( ',', $args )
664 ) . ");";
668 * Check if a string is well-formed XML.
669 * Must include the surrounding tag.
671 * @param string $text string to test.
672 * @return bool
674 * @todo Error position reporting return
676 public static function isWellFormed( $text ) {
677 $parser = xml_parser_create( "UTF-8" );
679 # case folding violates XML standard, turn it off
680 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
682 if( !xml_parse( $parser, $text, true ) ) {
683 //$err = xml_error_string( xml_get_error_code( $parser ) );
684 //$position = xml_get_current_byte_index( $parser );
685 //$fragment = $this->extractFragment( $html, $position );
686 //$this->mXmlError = "$err at byte $position:\n$fragment";
687 xml_parser_free( $parser );
688 return false;
691 xml_parser_free( $parser );
693 return true;
697 * Check if a string is a well-formed XML fragment.
698 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
699 * and can use HTML named entities.
701 * @param $text String:
702 * @return bool
704 public static function isWellFormedXmlFragment( $text ) {
705 $html =
706 Sanitizer::hackDocType() .
707 '<html>' .
708 $text .
709 '</html>';
711 return Xml::isWellFormed( $html );
715 * Replace " > and < with their respective HTML entities ( &quot;,
716 * &gt;, &lt;)
718 * @param string $in text that might contain HTML tags.
719 * @return string Escaped string
721 public static function escapeTagsOnly( $in ) {
722 return str_replace(
723 array( '"', '>', '<' ),
724 array( '&quot;', '&gt;', '&lt;' ),
725 $in );
729 * Generate a form (without the opening form element).
730 * Output optionally includes a submit button.
731 * @param array $fields Associative array, key is message corresponding to a description for the field (colon is in the message), value is appropriate input.
732 * @param string $submitLabel A message containing a label for the submit button.
733 * @return string HTML form.
735 public static function buildForm( $fields, $submitLabel = null ) {
736 $form = '';
737 $form .= "<table><tbody>";
739 foreach( $fields as $labelmsg => $input ) {
740 $id = "mw-$labelmsg";
741 $form .= Xml::openElement( 'tr', array( 'id' => $id ) );
742 $form .= Xml::tags( 'td', array( 'class' => 'mw-label' ), wfMessage( $labelmsg )->parse() );
743 $form .= Xml::openElement( 'td', array( 'class' => 'mw-input' ) ) . $input . Xml::closeElement( 'td' );
744 $form .= Xml::closeElement( 'tr' );
747 if( $submitLabel ) {
748 $form .= Xml::openElement( 'tr' );
749 $form .= Xml::tags( 'td', array(), '' );
750 $form .= Xml::openElement( 'td', array( 'class' => 'mw-submit' ) ) . Xml::submitButton( wfMessage( $submitLabel )->text() ) . Xml::closeElement( 'td' );
751 $form .= Xml::closeElement( 'tr' );
754 $form .= "</tbody></table>";
756 return $form;
760 * Build a table of data
761 * @param array $rows An array of arrays of strings, each to be a row in a table
762 * @param array $attribs An array of attributes to apply to the table tag [optional]
763 * @param array $headers An array of strings to use as table headers [optional]
764 * @return string
766 public static function buildTable( $rows, $attribs = array(), $headers = null ) {
767 $s = Xml::openElement( 'table', $attribs );
769 if ( is_array( $headers ) ) {
770 $s .= Xml::openElement( 'thead', $attribs );
772 foreach( $headers as $id => $header ) {
773 $attribs = array();
775 if ( is_string( $id ) ) {
776 $attribs['id'] = $id;
779 $s .= Xml::element( 'th', $attribs, $header );
781 $s .= Xml::closeElement( 'thead' );
784 foreach( $rows as $id => $row ) {
785 $attribs = array();
787 if ( is_string( $id ) ) {
788 $attribs['id'] = $id;
791 $s .= Xml::buildTableRow( $attribs, $row );
794 $s .= Xml::closeElement( 'table' );
796 return $s;
800 * Build a row for a table
801 * @param array $attribs An array of attributes to apply to the tr tag
802 * @param array $cells An array of strings to put in <td>
803 * @return string
805 public static function buildTableRow( $attribs, $cells ) {
806 $s = Xml::openElement( 'tr', $attribs );
808 foreach( $cells as $id => $cell ) {
810 $attribs = array();
812 if ( is_string( $id ) ) {
813 $attribs['id'] = $id;
816 $s .= Xml::element( 'td', $attribs, $cell );
819 $s .= Xml::closeElement( 'tr' );
821 return $s;
825 class XmlSelect {
826 protected $options = array();
827 protected $default = false;
828 protected $attributes = array();
830 public function __construct( $name = false, $id = false, $default = false ) {
831 if ( $name ) {
832 $this->setAttribute( 'name', $name );
835 if ( $id ) {
836 $this->setAttribute( 'id', $id );
839 if ( $default !== false ) {
840 $this->default = $default;
845 * @param $default
847 public function setDefault( $default ) {
848 $this->default = $default;
852 * @param $name string
853 * @param $value
855 public function setAttribute( $name, $value ) {
856 $this->attributes[$name] = $value;
860 * @param $name
861 * @return array|null
863 public function getAttribute( $name ) {
864 if ( isset( $this->attributes[$name] ) ) {
865 return $this->attributes[$name];
866 } else {
867 return null;
872 * @param $name
873 * @param $value bool
875 public function addOption( $name, $value = false ) {
876 // Stab stab stab
877 $value = ($value !== false) ? $value : $name;
879 $this->options[] = array( $name => $value );
883 * This accepts an array of form
884 * label => value
885 * label => ( label => value, label => value )
887 * @param $options
889 public function addOptions( $options ) {
890 $this->options[] = $options;
894 * This accepts an array of form
895 * label => value
896 * label => ( label => value, label => value )
898 * @param $options
899 * @param bool $default
900 * @return string
902 static function formatOptions( $options, $default = false ) {
903 $data = '';
905 foreach( $options as $label => $value ) {
906 if ( is_array( $value ) ) {
907 $contents = self::formatOptions( $value, $default );
908 $data .= Html::rawElement( 'optgroup', array( 'label' => $label ), $contents ) . "\n";
909 } else {
910 $data .= Xml::option( $label, $value, $value === $default ) . "\n";
914 return $data;
918 * @return string
920 public function getHTML() {
921 $contents = '';
923 foreach ( $this->options as $options ) {
924 $contents .= self::formatOptions( $options, $this->default );
927 return Html::rawElement( 'select', $this->attributes, rtrim( $contents ) );
932 * A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to
933 * interpret a given string as being a JavaScript expression, instead of string
934 * data.
936 * Example:
938 * Xml::encodeJsVar( new XmlJsCode( 'a + b' ) );
940 * Returns "a + b".
942 * @note As of 1.21, XmlJsCode objects cannot be nested inside objects or arrays. The sole
943 * exception is the $args argument to Xml::encodeJsCall() because Xml::encodeJsVar() is
944 * called for each individual element in that array.
946 * @since 1.17
948 class XmlJsCode {
949 public $value;
951 function __construct( $value ) {
952 $this->value = $value;