Merge "add basic print styles to CologneBlue"
[lhc/web/wiklou.git] / includes / Xml.php
1 <?php
2 /**
3 * Methods to generate XML.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * Module of static functions for generating XML
25 */
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.
32 *
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
38 */
39 public static function element( $element, $attribs = null, $contents = '', $allowShortTag = true ) {
40 $out = '<' . $element;
41 if ( !is_null( $attribs ) ) {
42 $out .= self::expandAttributes( $attribs );
43 }
44 if ( is_null( $contents ) ) {
45 $out .= '>';
46 } else {
47 if ( $allowShortTag && $contents === '' ) {
48 $out .= ' />';
49 } else {
50 $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
51 }
52 }
53 return $out;
54 }
55
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
64 */
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 ) . '"';
72 }
73 return $out;
74 } else {
75 throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
76 }
77 }
78
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.
83 *
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
88 */
89 public static function elementClean( $element, $attribs = array(), $contents = '' ) {
90 global $wgContLang;
91 if ( $attribs ) {
92 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
93 }
94 if ( $contents ) {
95 wfProfileIn( __METHOD__ . '-norm' );
96 $contents = $wgContLang->normalize( $contents );
97 wfProfileOut( __METHOD__ . '-norm' );
98 }
99 return self::element( $element, $attribs, $contents );
100 }
101
102 /**
103 * This opens an XML element
104 *
105 * @param string $element name of the element
106 * @param array $attribs of attributes, see Xml::expandAttributes()
107 * @return string
108 */
109 public static function openElement( $element, $attribs = null ) {
110 return '<' . $element . self::expandAttributes( $attribs ) . '>';
111 }
112
113 /**
114 * Shortcut to close an XML element
115 * @param string $element element name
116 * @return string
117 */
118 public static function closeElement( $element ) {
119 return "</$element>";
120 }
121
122 /**
123 * Same as Xml::element(), but does not escape contents. Handy when the
124 * content you have is already valid xml.
125 *
126 * @param string $element element name
127 * @param array $attribs of attributes
128 * @param string $contents content of the element
129 * @return string
130 */
131 public static function tags( $element, $attribs = null, $contents ) {
132 return self::openElement( $element, $attribs ) . $contents . "</$element>";
133 }
134
135 /**
136 * Build a drop-down box for selecting a namespace
137 *
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
144 */
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 ) );
156 }
157
158 /**
159 * Create a date selector
160 *
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
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 }
172 if ( !is_null( $allmonths ) ) {
173 $options[] = self::option( wfMessage( 'monthsall' )->text(), $allmonths, $selected === $allmonths );
174 }
175 for ( $i = 1; $i < 13; $i++ ) {
176 $options[] = self::option( $wgLang->getMonthName( $i ), $i, $selected === $i );
177 }
178 return self::openElement( 'select', array( 'id' => $id, 'name' => 'month', 'class' => 'mw-month-selector' ) )
179 . implode( "\n", $options )
180 . self::closeElement( 'select' );
181 }
182
183 /**
184 * @param $year Integer
185 * @param $month Integer
186 * @return string Formatted HTML
187 */
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 = '';
194 }
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--;
202 }
203 $encYear = $thisYear;
204 } else {
205 $encYear = '';
206 }
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 );
212 }
213
214 /**
215 * Construct a language selector appropriate for use in a form or preferences
216 *
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
223 */
224 public static function languageSelector( $selected, $customisedOnly = true, $inLanguage = null, $overrideAttrs = array(), Message $msg = null ) {
225 global $wgLanguageCode;
226
227 $include = $customisedOnly ? 'mwfile' : 'mw';
228 $languages = Language::fetchLanguageNames( $inLanguage, $include );
229
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;
234 }
235
236 ksort( $languages );
237
238 /**
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.
242 */
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";
247 }
248
249 $attrs = array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' );
250 $attrs = array_merge( $attrs, $overrideAttrs );
251
252 if ( $msg === null ) {
253 $msg = wfMessage( 'yourlanguage' );
254 }
255 return array(
256 Xml::label( $msg->text(), $attrs['id'] ),
257 Xml::tags( 'select', $attrs, $options )
258 );
259
260 }
261
262 /**
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
268 */
269 public static function span( $text, $class, $attribs = array() ) {
270 return self::element( 'span', array( 'class' => $class ) + $attribs, $text );
271 }
272
273 /**
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
280 */
281 public static function wrapClass( $text, $class, $tag = 'span', $attribs = array() ) {
282 return self::tags( $tag, array( 'class' => $class ) + $attribs, $text );
283 }
284
285 /**
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
292 */
293 public static function input( $name, $size = false, $value = false, $attribs = array() ) {
294 $attributes = array( 'name' => $name );
295
296 if ( $size ) {
297 $attributes['size'] = $size;
298 }
299
300 if ( $value !== false ) { // maybe 0
301 $attributes['value'] = $value;
302 }
303
304 return self::element( 'input', $attributes + $attribs );
305 }
306
307 /**
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
314 */
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' ) ) );
317 }
318
319 /**
320 * Internal function for use in checkboxes and radio buttons and such.
321 *
322 * @param $name string
323 * @param $present bool
324 *
325 * @return array
326 */
327 public static function attrib( $name, $present = true ) {
328 return $present ? array( $name => $name ) : array();
329 }
330
331 /**
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
337 */
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 ) );
346 }
347
348 /**
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
355 */
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 );
361 }
362
363 /**
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
372 */
373 public static function label( $label, $id, $attribs = array() ) {
374 $a = array( 'for' => $id );
375
376 # FIXME avoid copy pasting below:
377 if ( isset( $attribs['class'] ) ) {
378 $a['class'] = $attribs['class'];
379 }
380 if ( isset( $attribs['title'] ) ) {
381 $a['title'] = $attribs['title'];
382 }
383
384 return self::element( 'label', $a, $label );
385 }
386
387 /**
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
396 */
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;
400 }
401
402 /**
403 * Same as Xml::inputLabel() but return input and label in an array
404 *
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
411 *
412 * @return array
413 */
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 )
418 );
419 }
420
421 /**
422 * Convenience function to build an HTML checkbox with a label
423 *
424 * @param $label
425 * @param $name
426 * @param $id
427 * @param $checked bool
428 * @param $attribs array
429 *
430 * @return string HTML
431 */
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 );
436 }
437
438 /**
439 * Convenience function to build an HTML radio button with a label
440 *
441 * @param $label
442 * @param $name
443 * @param $value
444 * @param $id
445 * @param $checked bool
446 * @param $attribs array
447 *
448 * @return string HTML
449 */
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 );
454 }
455
456 /**
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
461 */
462 public static function submitButton( $value, $attribs = array() ) {
463 return Html::element( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs );
464 }
465
466 /**
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
473 */
474 public static function option( $text, $value = null, $selected = false,
475 $attribs = array() ) {
476 if ( !is_null( $value ) ) {
477 $attribs['value'] = $value;
478 }
479 if ( $selected ) {
480 $attribs['selected'] = 'selected';
481 }
482 return Html::element( 'option', $attribs, $text );
483 }
484
485 /**
486 * Build a drop-down box from a textual list.
487 *
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
495 */
496 public static function listDropDown( $name = '', $list = '', $other = '', $selected = '', $class = '', $tabindex = null ) {
497 $optgroup = false;
498
499 $options = self::option( $other, 'other', $selected === 'other' );
500
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' );
510 }
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' );
521 }
522 $options .= self::option( $value, $value, $selected === $value );
523 $optgroup = false;
524 }
525 }
526
527 if ( $optgroup ) {
528 $options .= self::closeElement( 'optgroup' );
529 }
530
531 $attribs = array();
532
533 if ( $name ) {
534 $attribs['id'] = $name;
535 $attribs['name'] = $name;
536 }
537
538 if ( $class ) {
539 $attribs['class'] = $class;
540 }
541
542 if ( $tabindex ) {
543 $attribs['tabindex'] = $tabindex;
544 }
545
546 return Xml::openElement( 'select', $attribs )
547 . "\n"
548 . $options
549 . "\n"
550 . Xml::closeElement( 'select' );
551 }
552
553 /**
554 * Shortcut for creating fieldsets.
555 *
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.
559 *
560 * @return string
561 */
562 public static function fieldset( $legend = false, $content = false, $attribs = array() ) {
563 $s = Xml::openElement( 'fieldset', $attribs ) . "\n";
564
565 if ( $legend ) {
566 $s .= Xml::element( 'legend', null, $legend ) . "\n";
567 }
568
569 if ( $content !== false ) {
570 $s .= $content . "\n";
571 $s .= Xml::closeElement( 'fieldset' ) . "\n";
572 }
573
574 return $s;
575 }
576
577 /**
578 * Shortcut for creating textareas.
579 *
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
585 *
586 * @return string
587 */
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 );
597 }
598
599 /**
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.
603 *
604 * @deprecated since 1.21; use Xml::encodeJsVar() or Xml::encodeJsCall() instead
605 * @param string $string to escape
606 * @return String
607 */
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",
616
617 # To avoid closing the element or CDATA section
618 "<" => "\\x3c",
619 ">" => "\\x3e",
620
621 # To avoid any complaints about bad entity refs
622 "&" => "\\x26",
623
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
630 );
631
632 return strtr( $string, $pairs );
633 }
634
635 /**
636 * Encode a variable of arbitrary type to JavaScript.
637 * If the value is an XmlJsCode object, pass through the object's value verbatim.
638 *
639 * @note Only use this function for generating JavaScript code. If generating output
640 * for a proper JSON parser, just call FormatJson::encode() directly.
641 *
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
645 */
646 public static function encodeJsVar( $value, $pretty = false ) {
647 if ( $value instanceof XmlJsCode ) {
648 return $value->value;
649 }
650 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
651 }
652
653 /**
654 * Create a call to a JavaScript function. The supplied arguments will be
655 * encoded using Xml::encodeJsVar().
656 *
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
663 */
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;
669 }
670 }
671
672 return "$name(" . ( $pretty
673 ? ( ' ' . implode( ', ', $args ) . ' ' )
674 : implode( ',', $args )
675 ) . ");";
676 }
677
678 /**
679 * Check if a string is well-formed XML.
680 * Must include the surrounding tag.
681 *
682 * @param string $text string to test.
683 * @return bool
684 *
685 * @todo Error position reporting return
686 */
687 public static function isWellFormed( $text ) {
688 $parser = xml_parser_create( "UTF-8" );
689
690 # case folding violates XML standard, turn it off
691 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
692
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;
700 }
701
702 xml_parser_free( $parser );
703
704 return true;
705 }
706
707 /**
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.
711 *
712 * @param $text String:
713 * @return bool
714 */
715 public static function isWellFormedXmlFragment( $text ) {
716 $html =
717 Sanitizer::hackDocType() .
718 '<html>' .
719 $text .
720 '</html>';
721
722 return Xml::isWellFormed( $html );
723 }
724
725 /**
726 * Replace " > and < with their respective HTML entities ( &quot;,
727 * &gt;, &lt;)
728 *
729 * @param string $in text that might contain HTML tags.
730 * @return string Escaped string
731 */
732 public static function escapeTagsOnly( $in ) {
733 return str_replace(
734 array( '"', '>', '<' ),
735 array( '&quot;', '&gt;', '&lt;' ),
736 $in );
737 }
738
739 /**
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.
745 */
746 public static function buildForm( $fields, $submitLabel = null ) {
747 $form = '';
748 $form .= "<table><tbody>";
749
750 foreach ( $fields as $labelmsg => $input ) {
751 $id = "mw-$labelmsg";
752 $form .= Xml::openElement( 'tr', array( 'id' => $id ) );
753
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.
757
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' );
761 }
762
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' );
768 }
769
770 $form .= "</tbody></table>";
771
772 return $form;
773 }
774
775 /**
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
781 */
782 public static function buildTable( $rows, $attribs = array(), $headers = null ) {
783 $s = Xml::openElement( 'table', $attribs );
784
785 if ( is_array( $headers ) ) {
786 $s .= Xml::openElement( 'thead', $attribs );
787
788 foreach ( $headers as $id => $header ) {
789 $attribs = array();
790
791 if ( is_string( $id ) ) {
792 $attribs['id'] = $id;
793 }
794
795 $s .= Xml::element( 'th', $attribs, $header );
796 }
797 $s .= Xml::closeElement( 'thead' );
798 }
799
800 foreach ( $rows as $id => $row ) {
801 $attribs = array();
802
803 if ( is_string( $id ) ) {
804 $attribs['id'] = $id;
805 }
806
807 $s .= Xml::buildTableRow( $attribs, $row );
808 }
809
810 $s .= Xml::closeElement( 'table' );
811
812 return $s;
813 }
814
815 /**
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
820 */
821 public static function buildTableRow( $attribs, $cells ) {
822 $s = Xml::openElement( 'tr', $attribs );
823
824 foreach ( $cells as $id => $cell ) {
825 $attribs = array();
826
827 if ( is_string( $id ) ) {
828 $attribs['id'] = $id;
829 }
830
831 $s .= Xml::element( 'td', $attribs, $cell );
832 }
833
834 $s .= Xml::closeElement( 'tr' );
835
836 return $s;
837 }
838 }
839
840 class XmlSelect {
841 protected $options = array();
842 protected $default = false;
843 protected $attributes = array();
844
845 public function __construct( $name = false, $id = false, $default = false ) {
846 if ( $name ) {
847 $this->setAttribute( 'name', $name );
848 }
849
850 if ( $id ) {
851 $this->setAttribute( 'id', $id );
852 }
853
854 if ( $default !== false ) {
855 $this->default = $default;
856 }
857 }
858
859 /**
860 * @param $default
861 */
862 public function setDefault( $default ) {
863 $this->default = $default;
864 }
865
866 /**
867 * @param $name string
868 * @param $value
869 */
870 public function setAttribute( $name, $value ) {
871 $this->attributes[$name] = $value;
872 }
873
874 /**
875 * @param $name
876 * @return array|null
877 */
878 public function getAttribute( $name ) {
879 if ( isset( $this->attributes[$name] ) ) {
880 return $this->attributes[$name];
881 } else {
882 return null;
883 }
884 }
885
886 /**
887 * @param $name
888 * @param $value bool
889 */
890 public function addOption( $name, $value = false ) {
891 // Stab stab stab
892 $value = ($value !== false) ? $value : $name;
893
894 $this->options[] = array( $name => $value );
895 }
896
897 /**
898 * This accepts an array of form
899 * label => value
900 * label => ( label => value, label => value )
901 *
902 * @param $options
903 */
904 public function addOptions( $options ) {
905 $this->options[] = $options;
906 }
907
908 /**
909 * This accepts an array of form
910 * label => value
911 * label => ( label => value, label => value )
912 *
913 * @param $options
914 * @param bool $default
915 * @return string
916 */
917 static function formatOptions( $options, $default = false ) {
918 $data = '';
919
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";
926 }
927 }
928
929 return $data;
930 }
931
932 /**
933 * @return string
934 */
935 public function getHTML() {
936 $contents = '';
937
938 foreach ( $this->options as $options ) {
939 $contents .= self::formatOptions( $options, $this->default );
940 }
941
942 return Html::rawElement( 'select', $this->attributes, rtrim( $contents ) );
943 }
944 }
945
946 /**
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.
950 *
951 * Example:
952 *
953 * Xml::encodeJsVar( new XmlJsCode( 'a + b' ) );
954 *
955 * Returns "a + b".
956 *
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.
960 *
961 * @since 1.17
962 */
963 class XmlJsCode {
964 public $value;
965
966 function __construct( $value ) {
967 $this->value = $value;
968 }
969 }