MediaWiki REL1_37
Xml.php
Go to the documentation of this file.
1<?php
24
28class Xml {
41 public static function element( $element, $attribs = null, $contents = '',
42 $allowShortTag = true
43 ) {
44 $out = '<' . $element;
45 if ( $attribs !== null ) {
46 $out .= self::expandAttributes( $attribs );
47 }
48 if ( $contents === null ) {
49 $out .= '>';
50 } elseif ( $allowShortTag && $contents === '' ) {
51 $out .= ' />';
52 } else {
53 $out .= '>' . htmlspecialchars( $contents, ENT_NOQUOTES ) . "</$element>";
54 }
55 return $out;
56 }
57
67 public static function expandAttributes( $attribs ) {
68 $out = '';
69 if ( $attribs === null ) {
70 return null;
71 } elseif ( is_array( $attribs ) ) {
72 foreach ( $attribs as $name => $val ) {
73 $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
74 }
75 return $out;
76 } else {
77 throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
78 }
79 }
80
92 public static function elementClean( $element, $attribs = [], $contents = '' ) {
93 if ( $attribs ) {
94 $attribs = array_map( [ UtfNormal\Validator::class, 'cleanUp' ], $attribs );
95 }
96 if ( $contents ) {
97 $contents =
98 MediaWikiServices::getInstance()->getContentLanguage()->normalize( $contents );
99 }
100 return self::element( $element, $attribs, $contents );
101 }
102
110 public static function openElement( $element, $attribs = null ) {
111 return '<' . $element . self::expandAttributes( $attribs ) . '>';
112 }
113
119 public static function closeElement( $element ) {
120 return "</$element>";
121 }
122
132 public static function tags( $element, $attribs, $contents ) {
133 return self::openElement( $element, $attribs ) . $contents . "</$element>";
134 }
135
145 public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
146 global $wgLang;
147 $options = [];
148
149 if ( $selected === null ) {
150 $selected = '';
151 }
152 $data = new XmlSelect( 'month', $id, $selected );
153
154 if ( $allmonths !== null ) {
155 $options[wfMessage( 'monthsall' )->text()] = $allmonths;
156 }
157 for ( $i = 1; $i < 13; $i++ ) {
158 $options[$wgLang->getMonthName( $i )] = $i;
159 }
160 $data->addOptions( $options );
161 $data->setAttribute( 'class', 'mw-month-selector' );
162 return $data->getHTML();
163 }
164
171 public static function dateMenu( $year, $month ) {
172 # Offset overrides year/month selection
173 if ( $month && $month !== -1 ) {
174 $encMonth = intval( $month );
175 } else {
176 $encMonth = '';
177 }
178 if ( $year ) {
179 $encYear = intval( $year );
180 } elseif ( $encMonth ) {
181 $timestamp = MWTimestamp::getInstance();
182 $thisMonth = intval( $timestamp->format( 'n' ) );
183 $thisYear = intval( $timestamp->format( 'Y' ) );
184 if ( intval( $encMonth ) > $thisMonth ) {
185 $thisYear--;
186 }
187 $encYear = $thisYear;
188 } else {
189 $encYear = '';
190 }
191 $inputAttribs = [ 'id' => 'year', 'maxlength' => 4, 'size' => 7 ];
192 return self::label( wfMessage( 'year' )->text(), 'year' ) . ' ' .
193 Html::input( 'year', $encYear, 'number', $inputAttribs ) . ' ' .
194 self::label( wfMessage( 'month' )->text(), 'month' ) . ' ' .
195 self::monthSelector( $encMonth, -1 );
196 }
197
208 public static function languageSelector( $selected, $customisedOnly = true,
209 $inLanguage = null, $overrideAttrs = [], Message $msg = null
210 ) {
211 global $wgLanguageCode;
212
213 $include = $customisedOnly ? 'mwfile' : 'mw';
214 $languages = MediaWikiServices::getInstance()
215 ->getLanguageNameUtils()
216 ->getLanguageNames( $inLanguage, $include );
217
218 // Make sure the site language is in the list;
219 // a custom language code might not have a defined name...
220 if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
221 $languages[$wgLanguageCode] = $wgLanguageCode;
222 // Sort the array again
223 ksort( $languages );
224 }
225
231 $selected = isset( $languages[$selected] ) ? $selected : $wgLanguageCode;
232 $options = "\n";
233 foreach ( $languages as $code => $name ) {
234 $options .= self::option( "$code - $name", $code, $code == $selected ) . "\n";
235 }
236
237 $attrs = [ 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ];
238 $attrs = array_merge( $attrs, $overrideAttrs );
239
240 if ( $msg === null ) {
241 $msg = wfMessage( 'yourlanguage' );
242 }
243 return [
244 self::label( $msg->text(), $attrs['id'] ),
245 self::tags( 'select', $attrs, $options )
246 ];
247 }
248
256 public static function span( $text, $class, $attribs = [] ) {
257 return self::element( 'span', [ 'class' => $class ] + $attribs, $text );
258 }
259
268 public static function wrapClass( $text, $class, $tag = 'span', $attribs = [] ) {
269 return self::tags( $tag, [ 'class' => $class ] + $attribs, $text );
270 }
271
280 public static function input( $name, $size = false, $value = false, $attribs = [] ) {
281 $attributes = [ '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',
292 Html::getTextInputAttributes( $attributes + $attribs ) );
293 }
294
303 public static function password( $name, $size = false, $value = false,
304 $attribs = []
305 ) {
306 return self::input( $name, $size, $value,
307 array_merge( $attribs, [ 'type' => 'password' ] ) );
308 }
309
318 public static function attrib( $name, $present = true ) {
319 return $present ? [ $name => $name ] : [];
320 }
321
329 public static function check( $name, $checked = false, $attribs = [] ) {
330 return self::element( 'input', array_merge(
331 [
332 'name' => $name,
333 'type' => 'checkbox',
334 'value' => 1 ],
335 self::attrib( 'checked', $checked ),
336 $attribs ) );
337 }
338
347 public static function radio( $name, $value, $checked = false, $attribs = [] ) {
348 return self::element( 'input', [
349 'name' => $name,
350 'type' => 'radio',
351 'value' => $value ] + self::attrib( 'checked', $checked ) + $attribs );
352 }
353
364 public static function label( $label, $id, $attribs = [] ) {
365 $a = [ 'for' => $id ];
366
367 foreach ( [ 'class', 'title' ] as $attr ) {
368 if ( isset( $attribs[$attr] ) ) {
369 $a[$attr] = $attribs[$attr];
370 }
371 }
372
373 return self::element( 'label', $a, $label );
374 }
375
386 public static function inputLabel( $label, $name, $id, $size = false,
387 $value = false, $attribs = []
388 ) {
389 list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
390 return $label . "\u{00A0}" . $input;
391 }
392
405 public static function inputLabelSep( $label, $name, $id, $size = false,
406 $value = false, $attribs = []
407 ) {
408 return [
409 self::label( $label, $id, $attribs ),
410 self::input( $name, $size, $value, [ 'id' => $id ] + $attribs )
411 ];
412 }
413
425 public static function checkLabel( $label, $name, $id, $checked = false, $attribs = [] ) {
427 $chkLabel = self::check( $name, $checked, [ 'id' => $id ] + $attribs ) .
428 "\u{00A0}" .
429 self::label( $label, $id, $attribs );
430
432 $chkLabel = self::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
433 $chkLabel . self::closeElement( 'div' );
434 }
435 return $chkLabel;
436 }
437
450 public static function radioLabel( $label, $name, $value, $id,
451 $checked = false, $attribs = []
452 ) {
453 return self::radio( $name, $value, $checked, [ 'id' => $id ] + $attribs ) .
454 "\u{00A0}" .
455 self::label( $label, $id, $attribs );
456 }
457
465 public static function submitButton( $value, $attribs = [] ) {
467 $baseAttrs = [
468 'type' => 'submit',
469 'value' => $value,
470 ];
471 // Done conditionally for time being as it is possible
472 // some submit forms
473 // might need to be mw-ui-destructive (e.g. delete a page)
475 $baseAttrs['class'] = 'mw-ui-button mw-ui-progressive';
476 }
477 // Any custom attributes will take precendence of anything in baseAttrs e.g. override the class
478 $attribs += $baseAttrs;
479 return Html::element( 'input', $attribs );
480 }
481
490 public static function option( $text, $value = null, $selected = false,
491 $attribs = [] ) {
492 if ( $value !== null ) {
493 $attribs['value'] = $value;
494 }
495 if ( $selected ) {
496 $attribs['selected'] = 'selected';
497 }
498 return Html::element( 'option', $attribs, $text );
499 }
500
514 public static function listDropDown( $name = '', $list = '', $other = '',
515 $selected = '', $class = '', $tabindex = null
516 ) {
517 $options = self::listDropDownOptions( $list, [ 'other' => $other ] );
518
519 $xmlSelect = new XmlSelect( $name, $name, $selected );
520 $xmlSelect->addOptions( $options );
521
522 if ( $class ) {
523 $xmlSelect->setAttribute( 'class', $class );
524 }
525 if ( $tabindex ) {
526 $xmlSelect->setAttribute( 'tabindex', $tabindex );
527 }
528
529 return $xmlSelect->getHTML();
530 }
531
545 public static function listDropDownOptions( $list, $params = [] ) {
546 $options = [];
547
548 if ( isset( $params['other'] ) ) {
549 $options[ $params['other'] ] = 'other';
550 }
551
552 $optgroup = false;
553 foreach ( explode( "\n", $list ) as $option ) {
554 $value = trim( $option );
555 if ( $value == '' ) {
556 continue;
557 }
558 if ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
559 # A new group is starting...
560 $value = trim( substr( $value, 1 ) );
561 if ( $value !== '' &&
562 // Do not use the value for 'other' as option group - T251351
563 ( !isset( $params['other'] ) || $value !== $params['other'] )
564 ) {
565 $optgroup = $value;
566 } else {
567 $optgroup = false;
568 }
569 } elseif ( substr( $value, 0, 2 ) == '**' ) {
570 # groupmember
571 $opt = trim( substr( $value, 2 ) );
572 if ( $optgroup === false ) {
573 $options[$opt] = $opt;
574 } else {
575 $options[$optgroup][$opt] = $opt;
576 }
577 } else {
578 # groupless reason list
579 $optgroup = false;
580 $options[$option] = $option;
581 }
582 }
583
584 return $options;
585 }
586
595 public static function listDropDownOptionsOoui( $options ) {
596 $optionsOoui = [];
597
598 foreach ( $options as $text => $value ) {
599 if ( is_array( $value ) ) {
600 $optionsOoui[] = [ 'optgroup' => (string)$text ];
601 foreach ( $value as $text2 => $value2 ) {
602 $optionsOoui[] = [ 'data' => (string)$value2, 'label' => (string)$text2 ];
603 }
604 } else {
605 $optionsOoui[] = [ 'data' => (string)$value, 'label' => (string)$text ];
606 }
607 }
608
609 return $optionsOoui;
610 }
611
623 public static function fieldset( $legend = false, $content = false, $attribs = [] ) {
624 $s = self::openElement( 'fieldset', $attribs ) . "\n";
625
626 if ( $legend ) {
627 $s .= self::element( 'legend', null, $legend ) . "\n";
628 }
629
630 if ( $content !== false ) {
631 $s .= $content . "\n";
632 $s .= self::closeElement( 'fieldset' ) . "\n";
633 }
634
635 return $s;
636 }
637
649 public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = [] ) {
650 return self::element( 'textarea',
651 Html::getTextInputAttributes(
652 [
653 'name' => $name,
654 'id' => $name,
655 'cols' => $cols,
656 'rows' => $rows
657 ] + $attribs
658 ),
659 $content, false );
660 }
661
673 public static function encodeJsVar( $value, $pretty = false ) {
674 if ( $value instanceof XmlJsCode ) {
675 return $value->value;
676 }
677 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
678 }
679
691 public static function encodeJsCall( $name, $args, $pretty = false ) {
692 foreach ( $args as &$arg ) {
693 $arg = self::encodeJsVar( $arg, $pretty );
694 if ( $arg === false ) {
695 return false;
696 }
697 }
698
699 return "$name(" . ( $pretty
700 ? ( ' ' . implode( ', ', $args ) . ' ' )
701 : implode( ',', $args )
702 ) . ");";
703 }
704
716 private static function isWellFormed( $text ) {
717 $parser = xml_parser_create( "UTF-8" );
718
719 # case folding violates XML standard, turn it off
720 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
721
722 if ( !xml_parse( $parser, $text, true ) ) {
723 // $err = xml_error_string( xml_get_error_code( $parser ) );
724 // $position = xml_get_current_byte_index( $parser );
725 // $fragment = $this->extractFragment( $html, $position );
726 // $this->mXmlError = "$err at byte $position:\n$fragment";
727 xml_parser_free( $parser );
728 return false;
729 }
730
731 xml_parser_free( $parser );
732
733 return true;
734 }
735
744 public static function isWellFormedXmlFragment( $text ) {
745 $html =
746 Sanitizer::hackDocType() .
747 '<html>' .
748 $text .
749 '</html>';
750
751 return self::isWellFormed( $html );
752 }
753
761 public static function escapeTagsOnly( $in ) {
762 return str_replace(
763 [ '"', '>', '<' ],
764 [ '&quot;', '&gt;', '&lt;' ],
765 $in );
766 }
767
779 public static function buildForm( $fields, $submitLabel = null, $submitAttribs = [] ) {
780 $form = '';
781 $form .= "<table><tbody>";
782
783 foreach ( $fields as $labelmsg => $input ) {
784 $id = "mw-$labelmsg";
785 $form .= self::openElement( 'tr', [ 'id' => $id ] );
786
787 // TODO use a <label> here for accessibility purposes - will need
788 // to either not use a table to build the form, or find the ID of
789 // the input somehow.
790
791 $form .= self::tags( 'td', [ 'class' => 'mw-label' ], wfMessage( $labelmsg )->parse() );
792 $form .= self::openElement( 'td', [ 'class' => 'mw-input' ] )
793 . $input . self::closeElement( 'td' );
794 $form .= self::closeElement( 'tr' );
795 }
796
797 if ( $submitLabel ) {
798 $form .= self::openElement( 'tr' );
799 $form .= self::tags( 'td', [], '' );
800 $form .= self::openElement( 'td', [ 'class' => 'mw-submit' ] )
801 . self::submitButton( wfMessage( $submitLabel )->text(), $submitAttribs )
802 . self::closeElement( 'td' );
803 $form .= self::closeElement( 'tr' );
804 }
805
806 $form .= "</tbody></table>";
807
808 return $form;
809 }
810
817 public static function buildTable( $rows, $attribs = [], $headers = null ) {
818 $s = self::openElement( 'table', $attribs );
819
820 if ( is_array( $headers ) ) {
821 $s .= self::openElement( 'thead', $attribs );
822
823 foreach ( $headers as $id => $header ) {
824 $attribs = [];
825
826 if ( is_string( $id ) ) {
827 $attribs['id'] = $id;
828 }
829
830 $s .= self::element( 'th', $attribs, $header );
831 }
832 $s .= self::closeElement( 'thead' );
833 }
834
835 foreach ( $rows as $id => $row ) {
836 $attribs = [];
837
838 if ( is_string( $id ) ) {
839 $attribs['id'] = $id;
840 }
841
842 $s .= self::buildTableRow( $attribs, $row );
843 }
844
845 $s .= self::closeElement( 'table' );
846
847 return $s;
848 }
849
856 public static function buildTableRow( $attribs, $cells ) {
857 $s = self::openElement( 'tr', $attribs );
858
859 foreach ( $cells as $id => $cell ) {
860 $attribs = [];
861
862 if ( is_string( $id ) ) {
863 $attribs['id'] = $id;
864 }
865
866 $s .= self::element( 'td', $attribs, $cell );
867 }
868
869 $s .= self::closeElement( 'tr' );
870
871 return $s;
872 }
873}
$wgLanguageCode
Site language code.
$wgUseMediaWikiUIEverywhere
Temporary variable that applies MediaWiki UI wherever it can be supported.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
$wgLang
Definition Setup.php:831
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:138
A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to interpret a given string a...
Definition XmlJsCode.php:40
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:26
Module of static functions for generating XML.
Definition Xml.php:28
static password( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML password input field.
Definition Xml.php:303
static closeElement( $element)
Shortcut to close an XML element.
Definition Xml.php:119
static textarea( $name, $content, $cols=40, $rows=5, $attribs=[])
Shortcut for creating textareas.
Definition Xml.php:649
static inputLabelSep( $label, $name, $id, $size=false, $value=false, $attribs=[])
Same as Xml::inputLabel() but return input and label in an array.
Definition Xml.php:405
static isWellFormed( $text)
Check if a string is well-formed XML.
Definition Xml.php:716
static listDropDownOptions( $list, $params=[])
Build options for a drop-down box from a textual list.
Definition Xml.php:545
static encodeJsVar( $value, $pretty=false)
Encode a variable of arbitrary type to JavaScript.
Definition Xml.php:673
static listDropDownOptionsOoui( $options)
Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.
Definition Xml.php:595
static isWellFormedXmlFragment( $text)
Check if a string is a well-formed XML fragment.
Definition Xml.php:744
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
Definition Xml.php:329
static dateMenu( $year, $month)
Definition Xml.php:171
static buildForm( $fields, $submitLabel=null, $submitAttribs=[])
Generate a form (without the opening form element).
Definition Xml.php:779
static attrib( $name, $present=true)
Internal function for use in checkboxes and radio buttons and such.
Definition Xml.php:318
static label( $label, $id, $attribs=[])
Convenience function to build an HTML form label.
Definition Xml.php:364
static openElement( $element, $attribs=null)
This opens an XML element.
Definition Xml.php:110
static input( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML text input field.
Definition Xml.php:280
static wrapClass( $text, $class, $tag='span', $attribs=[])
Shortcut to make a specific element with a class attribute.
Definition Xml.php:268
static buildTable( $rows, $attribs=[], $headers=null)
Definition Xml.php:817
static submitButton( $value, $attribs=[])
Convenience function to build an HTML submit button When $wgUseMediaWikiUIEverywhere is true it will ...
Definition Xml.php:465
static option( $text, $value=null, $selected=false, $attribs=[])
Convenience function to build an HTML drop-down list item.
Definition Xml.php:490
static checkLabel( $label, $name, $id, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox with a label.
Definition Xml.php:425
static inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML text input field with a label.
Definition Xml.php:386
static languageSelector( $selected, $customisedOnly=true, $inLanguage=null, $overrideAttrs=[], Message $msg=null)
Construct a language selector appropriate for use in a form or preferences.
Definition Xml.php:208
static expandAttributes( $attribs)
Given an array of ('attributename' => 'value'), it generates the code to set the XML attributes : att...
Definition Xml.php:67
static span( $text, $class, $attribs=[])
Shortcut to make a span element.
Definition Xml.php:256
static escapeTagsOnly( $in)
Replace " > and < with their respective HTML entities ( ", >, <)
Definition Xml.php:761
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition Xml.php:132
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition Xml.php:41
static radio( $name, $value, $checked=false, $attribs=[])
Convenience function to build an HTML radio button.
Definition Xml.php:347
static buildTableRow( $attribs, $cells)
Build a row for a table.
Definition Xml.php:856
static radioLabel( $label, $name, $value, $id, $checked=false, $attribs=[])
Convenience function to build an HTML radio button with a label.
Definition Xml.php:450
static encodeJsCall( $name, $args, $pretty=false)
Create a call to a JavaScript function.
Definition Xml.php:691
static listDropDown( $name='', $list='', $other='', $selected='', $class='', $tabindex=null)
Build a drop-down box from a textual list.
Definition Xml.php:514
static monthSelector( $selected='', $allmonths=null, $id='month')
Create a date selector.
Definition Xml.php:145
static fieldset( $legend=false, $content=false, $attribs=[])
Shortcut for creating fieldsets.
Definition Xml.php:623
static elementClean( $element, $attribs=[], $contents='')
Format an XML element as with self::element(), but run text through the content language's normalize(...
Definition Xml.php:92
if( $line===false) $args
Definition mcc.php:124
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s
$content
Definition router.php:76
$header