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