MediaWiki REL1_35
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
90 public static function elementClean( $element, $attribs = [], $contents = '' ) {
91 if ( $attribs ) {
92 $attribs = array_map( [ 'UtfNormal\Validator', 'cleanUp' ], $attribs );
93 }
94 if ( $contents ) {
95 $contents =
96 MediaWikiServices::getInstance()->getContentLanguage()->normalize( $contents );
97 }
98 return self::element( $element, $attribs, $contents );
99 }
100
108 public static function openElement( $element, $attribs = null ) {
109 return '<' . $element . self::expandAttributes( $attribs ) . '>';
110 }
111
117 public static function closeElement( $element ) {
118 return "</$element>";
119 }
120
130 public static function tags( $element, $attribs, $contents ) {
131 return self::openElement( $element, $attribs ) . $contents . "</$element>";
132 }
133
143 public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
144 global $wgLang;
145 $options = [];
146
147 if ( $selected === null ) {
148 $selected = '';
149 }
150 $data = new XmlSelect( 'month', $id, $selected );
151
152 if ( $allmonths !== null ) {
153 $options[wfMessage( 'monthsall' )->text()] = $allmonths;
154 }
155 for ( $i = 1; $i < 13; $i++ ) {
156 $options[$wgLang->getMonthName( $i )] = $i;
157 }
158 $data->addOptions( $options );
159 $data->setAttribute( 'class', 'mw-month-selector' );
160 return $data->getHTML();
161 }
162
169 public static function dateMenu( $year, $month ) {
170 # Offset overrides year/month selection
171 if ( $month && $month !== -1 ) {
172 $encMonth = intval( $month );
173 } else {
174 $encMonth = '';
175 }
176 if ( $year ) {
177 $encYear = intval( $year );
178 } elseif ( $encMonth ) {
179 $timestamp = MWTimestamp::getInstance();
180 $thisMonth = intval( $timestamp->format( 'n' ) );
181 $thisYear = intval( $timestamp->format( 'Y' ) );
182 if ( intval( $encMonth ) > $thisMonth ) {
183 $thisYear--;
184 }
185 $encYear = $thisYear;
186 } else {
187 $encYear = '';
188 }
189 $inputAttribs = [ 'id' => 'year', 'maxlength' => 4, 'size' => 7 ];
190 return self::label( wfMessage( 'year' )->text(), 'year' ) . ' ' .
191 Html::input( 'year', $encYear, 'number', $inputAttribs ) . ' ' .
192 self::label( wfMessage( 'month' )->text(), 'month' ) . ' ' .
193 self::monthSelector( $encMonth, -1 );
194 }
195
206 public static function languageSelector( $selected, $customisedOnly = true,
207 $inLanguage = null, $overrideAttrs = [], Message $msg = null
208 ) {
209 global $wgLanguageCode;
210
211 $include = $customisedOnly ? 'mwfile' : 'mw';
212 $languages = MediaWikiServices::getInstance()
213 ->getLanguageNameUtils()
214 ->getLanguageNames( $inLanguage, $include );
215
216 // Make sure the site language is in the list;
217 // a custom language code might not have a defined name...
218 if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
219 $languages[$wgLanguageCode] = $wgLanguageCode;
220 // Sort the array again
221 ksort( $languages );
222 }
223
229 $selected = isset( $languages[$selected] ) ? $selected : $wgLanguageCode;
230 $options = "\n";
231 foreach ( $languages as $code => $name ) {
232 $options .= self::option( "$code - $name", $code, $code == $selected ) . "\n";
233 }
234
235 $attrs = [ 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ];
236 $attrs = array_merge( $attrs, $overrideAttrs );
237
238 if ( $msg === null ) {
239 $msg = wfMessage( 'yourlanguage' );
240 }
241 return [
242 self::label( $msg->text(), $attrs['id'] ),
243 self::tags( 'select', $attrs, $options )
244 ];
245 }
246
254 public static function span( $text, $class, $attribs = [] ) {
255 return self::element( 'span', [ 'class' => $class ] + $attribs, $text );
256 }
257
266 public static function wrapClass( $text, $class, $tag = 'span', $attribs = [] ) {
267 return self::tags( $tag, [ 'class' => $class ] + $attribs, $text );
268 }
269
278 public static function input( $name, $size = false, $value = false, $attribs = [] ) {
279 $attributes = [ 'name' => $name ];
280
281 if ( $size ) {
282 $attributes['size'] = $size;
283 }
284
285 if ( $value !== false ) { // maybe 0
286 $attributes['value'] = $value;
287 }
288
289 return self::element( 'input',
290 Html::getTextInputAttributes( $attributes + $attribs ) );
291 }
292
301 public static function password( $name, $size = false, $value = false,
302 $attribs = []
303 ) {
304 return self::input( $name, $size, $value,
305 array_merge( $attribs, [ 'type' => 'password' ] ) );
306 }
307
316 public static function attrib( $name, $present = true ) {
317 return $present ? [ $name => $name ] : [];
318 }
319
327 public static function check( $name, $checked = false, $attribs = [] ) {
328 return self::element( 'input', array_merge(
329 [
330 'name' => $name,
331 'type' => 'checkbox',
332 'value' => 1 ],
333 self::attrib( 'checked', $checked ),
334 $attribs ) );
335 }
336
345 public static function radio( $name, $value, $checked = false, $attribs = [] ) {
346 return self::element( 'input', [
347 'name' => $name,
348 'type' => 'radio',
349 'value' => $value ] + self::attrib( 'checked', $checked ) + $attribs );
350 }
351
362 public static function label( $label, $id, $attribs = [] ) {
363 $a = [ 'for' => $id ];
364
365 foreach ( [ 'class', 'title' ] as $attr ) {
366 if ( isset( $attribs[$attr] ) ) {
367 $a[$attr] = $attribs[$attr];
368 }
369 }
370
371 return self::element( 'label', $a, $label );
372 }
373
384 public static function inputLabel( $label, $name, $id, $size = false,
385 $value = false, $attribs = []
386 ) {
387 list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
388 return $label . "\u{00A0}" . $input;
389 }
390
403 public static function inputLabelSep( $label, $name, $id, $size = false,
404 $value = false, $attribs = []
405 ) {
406 return [
407 self::label( $label, $id, $attribs ),
408 self::input( $name, $size, $value, [ 'id' => $id ] + $attribs )
409 ];
410 }
411
423 public static function checkLabel( $label, $name, $id, $checked = false, $attribs = [] ) {
425 $chkLabel = self::check( $name, $checked, [ 'id' => $id ] + $attribs ) .
426 "\u{00A0}" .
427 self::label( $label, $id, $attribs );
428
430 $chkLabel = self::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
431 $chkLabel . self::closeElement( 'div' );
432 }
433 return $chkLabel;
434 }
435
448 public static function radioLabel( $label, $name, $value, $id,
449 $checked = false, $attribs = []
450 ) {
451 return self::radio( $name, $value, $checked, [ 'id' => $id ] + $attribs ) .
452 "\u{00A0}" .
453 self::label( $label, $id, $attribs );
454 }
455
463 public static function submitButton( $value, $attribs = [] ) {
465 $baseAttrs = [
466 'type' => 'submit',
467 'value' => $value,
468 ];
469 // Done conditionally for time being as it is possible
470 // some submit forms
471 // might need to be mw-ui-destructive (e.g. delete a page)
473 $baseAttrs['class'] = 'mw-ui-button mw-ui-progressive';
474 }
475 // Any custom attributes will take precendence of anything in baseAttrs e.g. override the class
476 $attribs += $baseAttrs;
477 return Html::element( 'input', $attribs );
478 }
479
488 public static function option( $text, $value = null, $selected = false,
489 $attribs = [] ) {
490 if ( $value !== null ) {
491 $attribs['value'] = $value;
492 }
493 if ( $selected ) {
494 $attribs['selected'] = 'selected';
495 }
496 return Html::element( 'option', $attribs, $text );
497 }
498
512 public static function listDropDown( $name = '', $list = '', $other = '',
513 $selected = '', $class = '', $tabindex = null
514 ) {
515 $options = self::listDropDownOptions( $list, [ 'other' => $other ] );
516
517 $xmlSelect = new XmlSelect( $name, $name, $selected );
518 $xmlSelect->addOptions( $options );
519
520 if ( $class ) {
521 $xmlSelect->setAttribute( 'class', $class );
522 }
523 if ( $tabindex ) {
524 $xmlSelect->setAttribute( 'tabindex', $tabindex );
525 }
526
527 return $xmlSelect->getHTML();
528 }
529
543 public static function listDropDownOptions( $list, $params = [] ) {
544 $options = [];
545
546 if ( isset( $params['other'] ) ) {
547 $options[ $params['other'] ] = 'other';
548 }
549
550 $optgroup = false;
551 foreach ( explode( "\n", $list ) as $option ) {
552 $value = trim( $option );
553 if ( $value == '' ) {
554 continue;
555 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
556 # A new group is starting...
557 $value = trim( substr( $value, 1 ) );
558 $optgroup = $value;
559 } elseif ( substr( $value, 0, 2 ) == '**' ) {
560 # groupmember
561 $opt = trim( substr( $value, 2 ) );
562 if ( $optgroup === false ) {
563 $options[$opt] = $opt;
564 } else {
565 $options[$optgroup][$opt] = $opt;
566 }
567 } else {
568 # groupless reason list
569 $optgroup = false;
570 $options[$option] = $option;
571 }
572 }
573
574 return $options;
575 }
576
585 public static function listDropDownOptionsOoui( $options ) {
586 $optionsOoui = [];
587
588 foreach ( $options as $text => $value ) {
589 if ( is_array( $value ) ) {
590 $optionsOoui[] = [ 'optgroup' => (string)$text ];
591 foreach ( $value as $text2 => $value2 ) {
592 $optionsOoui[] = [ 'data' => (string)$value2, 'label' => (string)$text2 ];
593 }
594 } else {
595 $optionsOoui[] = [ 'data' => (string)$value, 'label' => (string)$text ];
596 }
597 }
598
599 return $optionsOoui;
600 }
601
613 public static function fieldset( $legend = false, $content = false, $attribs = [] ) {
614 $s = self::openElement( 'fieldset', $attribs ) . "\n";
615
616 if ( $legend ) {
617 $s .= self::element( 'legend', null, $legend ) . "\n";
618 }
619
620 if ( $content !== false ) {
621 $s .= $content . "\n";
622 $s .= self::closeElement( 'fieldset' ) . "\n";
623 }
624
625 return $s;
626 }
627
639 public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = [] ) {
640 return self::element( 'textarea',
641 Html::getTextInputAttributes(
642 [
643 'name' => $name,
644 'id' => $name,
645 'cols' => $cols,
646 'rows' => $rows
647 ] + $attribs
648 ),
649 $content, false );
650 }
651
663 public static function encodeJsVar( $value, $pretty = false ) {
664 if ( $value instanceof XmlJsCode ) {
665 return $value->value;
666 }
667 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
668 }
669
681 public static function encodeJsCall( $name, $args, $pretty = false ) {
682 foreach ( $args as &$arg ) {
683 $arg = self::encodeJsVar( $arg, $pretty );
684 if ( $arg === false ) {
685 return false;
686 }
687 }
688
689 return "$name(" . ( $pretty
690 ? ( ' ' . implode( ', ', $args ) . ' ' )
691 : implode( ',', $args )
692 ) . ");";
693 }
694
706 private static function isWellFormed( $text ) {
707 $parser = xml_parser_create( "UTF-8" );
708
709 # case folding violates XML standard, turn it off
710 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
711
712 if ( !xml_parse( $parser, $text, true ) ) {
713 // $err = xml_error_string( xml_get_error_code( $parser ) );
714 // $position = xml_get_current_byte_index( $parser );
715 // $fragment = $this->extractFragment( $html, $position );
716 // $this->mXmlError = "$err at byte $position:\n$fragment";
717 xml_parser_free( $parser );
718 return false;
719 }
720
721 xml_parser_free( $parser );
722
723 return true;
724 }
725
734 public static function isWellFormedXmlFragment( $text ) {
735 $html =
736 Sanitizer::hackDocType() .
737 '<html>' .
738 $text .
739 '</html>';
740
741 return self::isWellFormed( $html );
742 }
743
751 public static function escapeTagsOnly( $in ) {
752 return str_replace(
753 [ '"', '>', '<' ],
754 [ '&quot;', '&gt;', '&lt;' ],
755 $in );
756 }
757
769 public static function buildForm( $fields, $submitLabel = null, $submitAttribs = [] ) {
770 $form = '';
771 $form .= "<table><tbody>";
772
773 foreach ( $fields as $labelmsg => $input ) {
774 $id = "mw-$labelmsg";
775 $form .= self::openElement( 'tr', [ 'id' => $id ] );
776
777 // TODO use a <label> here for accessibility purposes - will need
778 // to either not use a table to build the form, or find the ID of
779 // the input somehow.
780
781 $form .= self::tags( 'td', [ 'class' => 'mw-label' ], wfMessage( $labelmsg )->parse() );
782 $form .= self::openElement( 'td', [ 'class' => 'mw-input' ] )
783 . $input . self::closeElement( 'td' );
784 $form .= self::closeElement( 'tr' );
785 }
786
787 if ( $submitLabel ) {
788 $form .= self::openElement( 'tr' );
789 $form .= self::tags( 'td', [], '' );
790 $form .= self::openElement( 'td', [ 'class' => 'mw-submit' ] )
791 . self::submitButton( wfMessage( $submitLabel )->text(), $submitAttribs )
792 . self::closeElement( 'td' );
793 $form .= self::closeElement( 'tr' );
794 }
795
796 $form .= "</tbody></table>";
797
798 return $form;
799 }
800
808 public static function buildTable( $rows, $attribs = [], $headers = null ) {
809 $s = self::openElement( 'table', $attribs );
810
811 if ( is_array( $headers ) ) {
812 $s .= self::openElement( 'thead', $attribs );
813
814 foreach ( $headers as $id => $header ) {
815 $attribs = [];
816
817 if ( is_string( $id ) ) {
818 $attribs['id'] = $id;
819 }
820
821 $s .= self::element( 'th', $attribs, $header );
822 }
823 $s .= self::closeElement( 'thead' );
824 }
825
826 foreach ( $rows as $id => $row ) {
827 $attribs = [];
828
829 if ( is_string( $id ) ) {
830 $attribs['id'] = $id;
831 }
832
833 $s .= self::buildTableRow( $attribs, $row );
834 }
835
836 $s .= self::closeElement( 'table' );
837
838 return $s;
839 }
840
847 public static function buildTableRow( $attribs, $cells ) {
848 $s = self::openElement( 'tr', $attribs );
849
850 foreach ( $cells as $id => $cell ) {
851 $attribs = [];
852
853 if ( is_string( $id ) ) {
854 $attribs['id'] = $id;
855 }
856
857 $s .= self::element( 'td', $attribs, $cell );
858 }
859
860 $s .= self::closeElement( 'tr' );
861
862 return $s;
863 }
864}
$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:781
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:161
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:301
static closeElement( $element)
Shortcut to close an XML element.
Definition Xml.php:117
static textarea( $name, $content, $cols=40, $rows=5, $attribs=[])
Shortcut for creating textareas.
Definition Xml.php:639
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:403
static isWellFormed( $text)
Check if a string is well-formed XML.
Definition Xml.php:706
static listDropDownOptions( $list, $params=[])
Build options for a drop-down box from a textual list.
Definition Xml.php:543
static encodeJsVar( $value, $pretty=false)
Encode a variable of arbitrary type to JavaScript.
Definition Xml.php:663
static listDropDownOptionsOoui( $options)
Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.
Definition Xml.php:585
static isWellFormedXmlFragment( $text)
Check if a string is a well-formed XML fragment.
Definition Xml.php:734
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
Definition Xml.php:327
static dateMenu( $year, $month)
Definition Xml.php:169
static buildForm( $fields, $submitLabel=null, $submitAttribs=[])
Generate a form (without the opening form element).
Definition Xml.php:769
static attrib( $name, $present=true)
Internal function for use in checkboxes and radio buttons and such.
Definition Xml.php:316
static label( $label, $id, $attribs=[])
Convenience function to build an HTML form label.
Definition Xml.php:362
static openElement( $element, $attribs=null)
This opens an XML element.
Definition Xml.php:108
static input( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML text input field.
Definition Xml.php:278
static wrapClass( $text, $class, $tag='span', $attribs=[])
Shortcut to make a specific element with a class attribute.
Definition Xml.php:266
static buildTable( $rows, $attribs=[], $headers=null)
Build a table of data.
Definition Xml.php:808
static submitButton( $value, $attribs=[])
Convenience function to build an HTML submit button When $wgUseMediaWikiUIEverywhere is true it will ...
Definition Xml.php:463
static option( $text, $value=null, $selected=false, $attribs=[])
Convenience function to build an HTML drop-down list item.
Definition Xml.php:488
static checkLabel( $label, $name, $id, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox with a label.
Definition Xml.php:423
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:384
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:206
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:254
static escapeTagsOnly( $in)
Replace " > and < with their respective HTML entities ( ", >, <)
Definition Xml.php:751
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition Xml.php:130
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:345
static buildTableRow( $attribs, $cells)
Build a row for a table.
Definition Xml.php:847
static radioLabel( $label, $name, $value, $id, $checked=false, $attribs=[])
Convenience function to build an HTML radio button with a label.
Definition Xml.php:448
static encodeJsCall( $name, $args, $pretty=false)
Create a call to a JavaScript function.
Definition Xml.php:681
static listDropDown( $name='', $list='', $other='', $selected='', $class='', $tabindex=null)
Build a drop-down box from a textual list.
Definition Xml.php:512
static monthSelector( $selected='', $allmonths=null, $id='month')
Create a date selector.
Definition Xml.php:143
static fieldset( $legend=false, $content=false, $attribs=[])
Shortcut for creating fieldsets.
Definition Xml.php:613
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:90
if( $line===false) $args
Definition mcc.php:124
$content
Definition router.php:76
$header