MediaWiki REL1_39
Xml.php
Go to the documentation of this file.
1<?php
26
30class Xml {
43 public static function element( $element, $attribs = null, $contents = '',
44 $allowShortTag = true
45 ) {
46 $out = '<' . $element;
47 if ( $attribs !== null ) {
48 $out .= self::expandAttributes( $attribs );
49 }
50 if ( $contents === null ) {
51 $out .= '>';
52 } elseif ( $allowShortTag && $contents === '' ) {
53 $out .= ' />';
54 } else {
55 $out .= '>' . htmlspecialchars( $contents, ENT_NOQUOTES ) . "</$element>";
56 }
57 return $out;
58 }
59
69 public static function expandAttributes( $attribs ) {
70 $out = '';
71 if ( $attribs === null ) {
72 return null;
73 } elseif ( is_array( $attribs ) ) {
74 foreach ( $attribs as $name => $val ) {
75 $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
76 }
77 return $out;
78 } else {
79 throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
80 }
81 }
82
94 public static function elementClean( $element, $attribs = [], $contents = '' ) {
95 if ( $attribs ) {
96 $attribs = array_map( [ UtfNormal\Validator::class, 'cleanUp' ], $attribs );
97 }
98 if ( $contents ) {
99 $contents =
100 MediaWikiServices::getInstance()->getContentLanguage()->normalize( $contents );
101 }
102 return self::element( $element, $attribs, $contents );
103 }
104
112 public static function openElement( $element, $attribs = null ) {
113 return '<' . $element . self::expandAttributes( $attribs ) . '>';
114 }
115
121 public static function closeElement( $element ) {
122 return "</$element>";
123 }
124
134 public static function tags( $element, $attribs, $contents ) {
135 return self::openElement( $element, $attribs ) . $contents . "</$element>";
136 }
137
147 public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
148 global $wgLang;
149 $options = [];
150
151 if ( $selected === null ) {
152 $selected = '';
153 }
154 $data = new XmlSelect( 'month', $id, $selected );
155
156 if ( $allmonths !== null ) {
157 $options[wfMessage( 'monthsall' )->text()] = $allmonths;
158 }
159 for ( $i = 1; $i < 13; $i++ ) {
160 $options[$wgLang->getMonthName( $i )] = $i;
161 }
162 $data->addOptions( $options );
163 $data->setAttribute( 'class', 'mw-month-selector' );
164 return $data->getHTML();
165 }
166
173 public static function dateMenu( $year, $month ) {
174 # Offset overrides year/month selection
175 if ( $month && $month !== -1 ) {
176 $encMonth = intval( $month );
177 } else {
178 $encMonth = '';
179 }
180 if ( $year ) {
181 $encYear = intval( $year );
182 } elseif ( $encMonth ) {
183 $timestamp = MWTimestamp::getInstance();
184 $thisMonth = intval( $timestamp->format( 'n' ) );
185 $thisYear = intval( $timestamp->format( 'Y' ) );
186 if ( $encMonth > $thisMonth ) {
187 $thisYear--;
188 }
189 $encYear = $thisYear;
190 } else {
191 $encYear = '';
192 }
193 $inputAttribs = [ 'id' => 'year', 'maxlength' => 4, 'size' => 7 ];
194 return self::label( wfMessage( 'year' )->text(), 'year' ) . ' ' .
195 Html::input( 'year', $encYear, 'number', $inputAttribs ) . ' ' .
196 self::label( wfMessage( 'month' )->text(), 'month' ) . ' ' .
197 self::monthSelector( $encMonth, '-1' );
198 }
199
210 public static function languageSelector( $selected, $customisedOnly = true,
211 $inLanguage = null, $overrideAttrs = [], Message $msg = null
212 ) {
213 $languageCode = MediaWikiServices::getInstance()->getMainConfig()
214 ->get( MainConfigNames::LanguageCode );
215
216 $include = $customisedOnly ? LanguageNameUtils::SUPPORTED : LanguageNameUtils::DEFINED;
217 $languages = MediaWikiServices::getInstance()
218 ->getLanguageNameUtils()
219 ->getLanguageNames( $inLanguage, $include );
220
221 // Make sure the site language is in the list;
222 // a custom language code might not have a defined name...
223 if ( !array_key_exists( $languageCode, $languages ) ) {
224 $languages[$languageCode] = $languageCode;
225 // Sort the array again
226 ksort( $languages );
227 }
228
234 $selected = isset( $languages[$selected] ) ? $selected : $languageCode;
235 $options = "\n";
236 foreach ( $languages as $code => $name ) {
237 $options .= self::option( "$code - $name", $code, $code == $selected ) . "\n";
238 }
239
240 $attrs = [ 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ];
241 $attrs = array_merge( $attrs, $overrideAttrs );
242
243 if ( $msg === null ) {
244 $msg = wfMessage( 'yourlanguage' );
245 }
246 return [
247 self::label( $msg->text(), $attrs['id'] ),
248 self::tags( 'select', $attrs, $options )
249 ];
250 }
251
259 public static function span( $text, $class, $attribs = [] ) {
260 return self::element( 'span', [ 'class' => $class ] + $attribs, $text );
261 }
262
271 public static function wrapClass( $text, $class, $tag = 'span', $attribs = [] ) {
272 return self::tags( $tag, [ 'class' => $class ] + $attribs, $text );
273 }
274
283 public static function input( $name, $size = false, $value = false, $attribs = [] ) {
284 $attributes = [ 'name' => $name ];
285
286 if ( $size ) {
287 $attributes['size'] = $size;
288 }
289
290 if ( $value !== false ) { // maybe 0
291 $attributes['value'] = $value;
292 }
293
294 return self::element( 'input',
295 Html::getTextInputAttributes( $attributes + $attribs ) );
296 }
297
306 public static function password( $name, $size = false, $value = false,
307 $attribs = []
308 ) {
309 return self::input( $name, $size, $value,
310 array_merge( $attribs, [ 'type' => 'password' ] ) );
311 }
312
321 public static function attrib( $name, $present = true ) {
322 return $present ? [ $name => $name ] : [];
323 }
324
332 public static function check( $name, $checked = false, $attribs = [] ) {
333 return self::element( 'input', array_merge(
334 [
335 'name' => $name,
336 'type' => 'checkbox',
337 'value' => 1 ],
338 self::attrib( 'checked', $checked ),
339 $attribs ) );
340 }
341
350 public static function radio( $name, $value, $checked = false, $attribs = [] ) {
351 return self::element( 'input', [
352 'name' => $name,
353 'type' => 'radio',
354 'value' => $value ] + self::attrib( 'checked', $checked ) + $attribs );
355 }
356
367 public static function label( $label, $id, $attribs = [] ) {
368 $a = [ 'for' => $id ];
369
370 foreach ( [ 'class', 'title' ] as $attr ) {
371 if ( isset( $attribs[$attr] ) ) {
372 $a[$attr] = $attribs[$attr];
373 }
374 }
375
376 return self::element( 'label', $a, $label );
377 }
378
389 public static function inputLabel( $label, $name, $id, $size = false,
390 $value = false, $attribs = []
391 ) {
392 list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
393 return $label . "\u{00A0}" . $input;
394 }
395
408 public static function inputLabelSep( $label, $name, $id, $size = false,
409 $value = false, $attribs = []
410 ) {
411 return [
412 self::label( $label, $id, $attribs ),
413 self::input( $name, $size, $value, [ 'id' => $id ] + $attribs )
414 ];
415 }
416
428 public static function checkLabel( $label, $name, $id, $checked = false, $attribs = [] ) {
429 $useMediaWikiUIEverywhere = MediaWikiServices::getInstance()->getMainConfig()
430 ->get( MainConfigNames::UseMediaWikiUIEverywhere );
431 $chkLabel = self::check( $name, $checked, [ 'id' => $id ] + $attribs ) .
432 "\u{00A0}" .
433 self::label( $label, $id, $attribs );
434
435 if ( $useMediaWikiUIEverywhere ) {
436 $chkLabel = self::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
437 $chkLabel . self::closeElement( 'div' );
438 }
439 return $chkLabel;
440 }
441
454 public static function radioLabel( $label, $name, $value, $id,
455 $checked = false, $attribs = []
456 ) {
457 return self::radio( $name, $value, $checked, [ 'id' => $id ] + $attribs ) .
458 "\u{00A0}" .
459 self::label( $label, $id, $attribs );
460 }
461
469 public static function submitButton( $value, $attribs = [] ) {
470 $useMediaWikiUIEverywhere = MediaWikiServices::getInstance()->getMainConfig()
471 ->get( MainConfigNames::UseMediaWikiUIEverywhere );
472 $baseAttrs = [
473 'type' => 'submit',
474 'value' => $value,
475 ];
476 // Done conditionally for time being as it is possible
477 // some submit forms
478 // might need to be mw-ui-destructive (e.g. delete a page)
479 if ( $useMediaWikiUIEverywhere ) {
480 $baseAttrs['class'] = 'mw-ui-button mw-ui-progressive';
481 }
482 // Any custom attributes will take precedence of anything in baseAttrs e.g. override the class
483 $attribs += $baseAttrs;
484 return Html::element( 'input', $attribs );
485 }
486
495 public static function option( $text, $value = null, $selected = false,
496 $attribs = [] ) {
497 if ( $value !== null ) {
498 $attribs['value'] = $value;
499 }
500 if ( $selected ) {
501 $attribs['selected'] = 'selected';
502 }
503 return Html::element( 'option', $attribs, $text );
504 }
505
519 public static function listDropDown( $name = '', $list = '', $other = '',
520 $selected = '', $class = '', $tabindex = null
521 ) {
522 $options = self::listDropDownOptions( $list, [ 'other' => $other ] );
523
524 $xmlSelect = new XmlSelect( $name, $name, $selected );
525 $xmlSelect->addOptions( $options );
526
527 if ( $class ) {
528 $xmlSelect->setAttribute( 'class', $class );
529 }
530 if ( $tabindex ) {
531 $xmlSelect->setAttribute( 'tabindex', $tabindex );
532 }
533
534 return $xmlSelect->getHTML();
535 }
536
550 public static function listDropDownOptions( $list, $params = [] ) {
551 $options = [];
552
553 if ( isset( $params['other'] ) ) {
554 $options[ $params['other'] ] = 'other';
555 }
556
557 $optgroup = false;
558 foreach ( explode( "\n", $list ) as $option ) {
559 $value = trim( $option );
560 if ( $value == '' ) {
561 continue;
562 }
563 if ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
564 # A new group is starting...
565 $value = trim( substr( $value, 1 ) );
566 if ( $value !== '' &&
567 // Do not use the value for 'other' as option group - T251351
568 ( !isset( $params['other'] ) || $value !== $params['other'] )
569 ) {
570 $optgroup = $value;
571 } else {
572 $optgroup = false;
573 }
574 } elseif ( substr( $value, 0, 2 ) == '**' ) {
575 # groupmember
576 $opt = trim( substr( $value, 2 ) );
577 if ( $optgroup === false ) {
578 $options[$opt] = $opt;
579 } else {
580 $options[$optgroup][$opt] = $opt;
581 }
582 } else {
583 # groupless reason list
584 $optgroup = false;
585 $options[$option] = $option;
586 }
587 }
588
589 return $options;
590 }
591
600 public static function listDropDownOptionsOoui( $options ) {
601 $optionsOoui = [];
602
603 foreach ( $options as $text => $value ) {
604 if ( is_array( $value ) ) {
605 $optionsOoui[] = [ 'optgroup' => (string)$text ];
606 foreach ( $value as $text2 => $value2 ) {
607 $optionsOoui[] = [ 'data' => (string)$value2, 'label' => (string)$text2 ];
608 }
609 } else {
610 $optionsOoui[] = [ 'data' => (string)$value, 'label' => (string)$text ];
611 }
612 }
613
614 return $optionsOoui;
615 }
616
628 public static function fieldset( $legend = false, $content = false, $attribs = [] ) {
629 $s = self::openElement( 'fieldset', $attribs ) . "\n";
630
631 if ( $legend ) {
632 $s .= self::element( 'legend', null, $legend ) . "\n";
633 }
634
635 if ( $content !== false ) {
636 $s .= $content . "\n";
637 $s .= self::closeElement( 'fieldset' ) . "\n";
638 }
639
640 return $s;
641 }
642
654 public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = [] ) {
655 return self::element( 'textarea',
656 Html::getTextInputAttributes(
657 [
658 'name' => $name,
659 'id' => $name,
660 'cols' => $cols,
661 'rows' => $rows
662 ] + $attribs
663 ),
664 $content, false );
665 }
666
678 public static function encodeJsVar( $value, $pretty = false ) {
679 if ( $value instanceof XmlJsCode ) {
680 return $value->value;
681 }
682 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
683 }
684
696 public static function encodeJsCall( $name, $args, $pretty = false ) {
697 foreach ( $args as &$arg ) {
698 $arg = self::encodeJsVar( $arg, $pretty );
699 if ( $arg === false ) {
700 return false;
701 }
702 }
703
704 return "$name(" . ( $pretty
705 ? ( ' ' . implode( ', ', $args ) . ' ' )
706 : implode( ',', $args )
707 ) . ");";
708 }
709
721 private static function isWellFormed( $text ) {
722 $parser = xml_parser_create( "UTF-8" );
723
724 # case folding violates XML standard, turn it off
725 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
726
727 if ( !xml_parse( $parser, $text, true ) ) {
728 // $err = xml_error_string( xml_get_error_code( $parser ) );
729 // $position = xml_get_current_byte_index( $parser );
730 // $fragment = $this->extractFragment( $html, $position );
731 // $this->mXmlError = "$err at byte $position:\n$fragment";
732 xml_parser_free( $parser );
733 return false;
734 }
735
736 xml_parser_free( $parser );
737
738 return true;
739 }
740
749 public static function isWellFormedXmlFragment( $text ) {
750 $html =
751 Sanitizer::hackDocType() .
752 '<html>' .
753 $text .
754 '</html>';
755
756 return self::isWellFormed( $html );
757 }
758
766 public static function escapeTagsOnly( $in ) {
767 return str_replace(
768 [ '"', '>', '<' ],
769 [ '&quot;', '&gt;', '&lt;' ],
770 $in );
771 }
772
784 public static function buildForm( $fields, $submitLabel = null, $submitAttribs = [] ) {
785 $form = '';
786 $form .= "<table><tbody>";
787
788 foreach ( $fields as $labelmsg => $input ) {
789 $id = "mw-$labelmsg";
790 $form .= self::openElement( 'tr', [ 'id' => $id ] );
791
792 // TODO use a <label> here for accessibility purposes - will need
793 // to either not use a table to build the form, or find the ID of
794 // the input somehow.
795
796 $form .= self::tags( 'td', [ 'class' => 'mw-label' ], wfMessage( $labelmsg )->parse() );
797 $form .= self::openElement( 'td', [ 'class' => 'mw-input' ] )
798 . $input . self::closeElement( 'td' );
799 $form .= self::closeElement( 'tr' );
800 }
801
802 if ( $submitLabel ) {
803 $form .= self::openElement( 'tr' );
804 $form .= self::tags( 'td', [], '' );
805 $form .= self::openElement( 'td', [ 'class' => 'mw-submit' ] )
806 . self::submitButton( wfMessage( $submitLabel )->text(), $submitAttribs )
807 . self::closeElement( 'td' );
808 $form .= self::closeElement( 'tr' );
809 }
810
811 $form .= "</tbody></table>";
812
813 return $form;
814 }
815
822 public static function buildTable( $rows, $attribs = [], $headers = null ) {
823 $s = self::openElement( 'table', $attribs );
824
825 if ( is_array( $headers ) ) {
826 $s .= self::openElement( 'thead', $attribs );
827
828 foreach ( $headers as $id => $header ) {
829 $attribs = [];
830
831 if ( is_string( $id ) ) {
832 $attribs['id'] = $id;
833 }
834
835 $s .= self::element( 'th', $attribs, $header );
836 }
837 $s .= self::closeElement( 'thead' );
838 }
839
840 foreach ( $rows as $id => $row ) {
841 $attribs = [];
842
843 if ( is_string( $id ) ) {
844 $attribs['id'] = $id;
845 }
846
847 $s .= self::buildTableRow( $attribs, $row );
848 }
849
850 $s .= self::closeElement( 'table' );
851
852 return $s;
853 }
854
861 public static function buildTableRow( $attribs, $cells ) {
862 $s = self::openElement( 'tr', $attribs );
863
864 foreach ( $cells as $id => $cell ) {
865 $attribs = [];
866
867 if ( is_string( $id ) ) {
868 $attribs['id'] = $id;
869 }
870
871 $s .= self::element( 'td', $attribs, $cell );
872 }
873
874 $s .= self::closeElement( 'tr' );
875
876 return $s;
877 }
878}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode $wgLang
Definition Setup.php:497
MediaWiki exception.
A service that provides utilities to do with language names and codes.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:140
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:30
static password( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML password input field.
Definition Xml.php:306
static closeElement( $element)
Shortcut to close an XML element.
Definition Xml.php:121
static textarea( $name, $content, $cols=40, $rows=5, $attribs=[])
Shortcut for creating textareas.
Definition Xml.php:654
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:408
static listDropDownOptions( $list, $params=[])
Build options for a drop-down box from a textual list.
Definition Xml.php:550
static encodeJsVar( $value, $pretty=false)
Encode a variable of arbitrary type to JavaScript.
Definition Xml.php:678
static listDropDownOptionsOoui( $options)
Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.
Definition Xml.php:600
static isWellFormedXmlFragment( $text)
Check if a string is a well-formed XML fragment.
Definition Xml.php:749
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
Definition Xml.php:332
static dateMenu( $year, $month)
Definition Xml.php:173
static buildForm( $fields, $submitLabel=null, $submitAttribs=[])
Generate a form (without the opening form element).
Definition Xml.php:784
static attrib( $name, $present=true)
Internal function for use in checkboxes and radio buttons and such.
Definition Xml.php:321
static label( $label, $id, $attribs=[])
Convenience function to build an HTML form label.
Definition Xml.php:367
static openElement( $element, $attribs=null)
This opens an XML element.
Definition Xml.php:112
static input( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML text input field.
Definition Xml.php:283
static wrapClass( $text, $class, $tag='span', $attribs=[])
Shortcut to make a specific element with a class attribute.
Definition Xml.php:271
static buildTable( $rows, $attribs=[], $headers=null)
Definition Xml.php:822
static submitButton( $value, $attribs=[])
Convenience function to build an HTML submit button When $wgUseMediaWikiUIEverywhere is true it will ...
Definition Xml.php:469
static option( $text, $value=null, $selected=false, $attribs=[])
Convenience function to build an HTML drop-down list item.
Definition Xml.php:495
static checkLabel( $label, $name, $id, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox with a label.
Definition Xml.php:428
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:389
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:210
static expandAttributes( $attribs)
Given an array of ('attributename' => 'value'), it generates the code to set the XML attributes : att...
Definition Xml.php:69
static span( $text, $class, $attribs=[])
Shortcut to make a span element.
Definition Xml.php:259
static escapeTagsOnly( $in)
Replace " > and < with their respective HTML entities ( ", >, <)
Definition Xml.php:766
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition Xml.php:134
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition Xml.php:43
static radio( $name, $value, $checked=false, $attribs=[])
Convenience function to build an HTML radio button.
Definition Xml.php:350
static buildTableRow( $attribs, $cells)
Build a row for a table.
Definition Xml.php:861
static radioLabel( $label, $name, $value, $id, $checked=false, $attribs=[])
Convenience function to build an HTML radio button with a label.
Definition Xml.php:454
static encodeJsCall( $name, $args, $pretty=false)
Create a call to a JavaScript function.
Definition Xml.php:696
static listDropDown( $name='', $list='', $other='', $selected='', $class='', $tabindex=null)
Build a drop-down box from a textual list.
Definition Xml.php:519
static monthSelector( $selected='', $allmonths=null, $id='month')
Create a date selector.
Definition Xml.php:147
static fieldset( $legend=false, $content=false, $attribs=[])
Shortcut for creating fieldsets.
Definition Xml.php:628
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:94
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