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