MediaWiki REL1_41
Xml.php
Go to the documentation of this file.
1<?php
29
33class Xml {
50 public static function element( $element, $attribs = null, $contents = '',
51 $allowShortTag = true
52 ) {
53 $out = '<' . $element;
54 if ( $attribs !== null ) {
55 $out .= self::expandAttributes( $attribs );
56 }
57 if ( $contents === null ) {
58 $out .= '>';
59 } elseif ( $allowShortTag && $contents === '' ) {
60 $out .= ' />';
61 } else {
62 $out .= '>' . htmlspecialchars( $contents, ENT_NOQUOTES ) . "</$element>";
63 }
64 return $out;
65 }
66
75 public static function expandAttributes( ?array $attribs ) {
76 if ( $attribs === null ) {
77 return null;
78 }
79 $out = '';
80 foreach ( $attribs as $name => $val ) {
81 $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
82 }
83 return $out;
84 }
85
97 public static function elementClean( $element, $attribs = [], $contents = '' ) {
98 if ( $attribs ) {
99 $attribs = array_map( [ UtfNormal\Validator::class, 'cleanUp' ], $attribs );
100 }
101 if ( $contents ) {
102 $contents =
103 MediaWikiServices::getInstance()->getContentLanguage()->normalize( $contents );
104 }
105 return self::element( $element, $attribs, $contents );
106 }
107
115 public static function openElement( $element, $attribs = null ) {
116 return '<' . $element . self::expandAttributes( $attribs ) . '>';
117 }
118
124 public static function closeElement( $element ) {
125 return "</$element>";
126 }
127
141 public static function tags( $element, $attribs, $contents ) {
142 return self::openElement( $element, $attribs ) . $contents . "</$element>";
143 }
144
154 public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
155 global $wgLang;
156 $options = [];
157
158 $data = new XmlSelect( 'month', $id, $selected ?? '' );
159
160 if ( $allmonths !== null ) {
161 $options[wfMessage( 'monthsall' )->text()] = $allmonths;
162 }
163 for ( $i = 1; $i < 13; $i++ ) {
164 $options[$wgLang->getMonthName( $i )] = $i;
165 }
166 $data->addOptions( $options );
167 $data->setAttribute( 'class', 'mw-month-selector' );
168 return $data->getHTML();
169 }
170
177 public static function dateMenu( $year, $month ) {
178 # Offset overrides year/month selection
179 if ( $month && $month !== -1 ) {
180 $encMonth = intval( $month );
181 } else {
182 $encMonth = '';
183 }
184 if ( $year ) {
185 $encYear = intval( $year );
186 } elseif ( $encMonth ) {
187 $timestamp = MWTimestamp::getInstance();
188 $thisMonth = intval( $timestamp->format( 'n' ) );
189 $thisYear = intval( $timestamp->format( 'Y' ) );
190 if ( $encMonth > $thisMonth ) {
191 $thisYear--;
192 }
193 $encYear = $thisYear;
194 } else {
195 $encYear = '';
196 }
197 $inputAttribs = [ 'id' => 'year', 'maxlength' => 4, 'size' => 7 ];
198 return self::label( wfMessage( 'year' )->text(), 'year' ) . ' ' .
199 Html::input( 'year', $encYear, 'number', $inputAttribs ) . ' ' .
200 self::label( wfMessage( 'month' )->text(), 'month' ) . ' ' .
201 self::monthSelector( $encMonth, '-1' );
202 }
203
214 public static function languageSelector( $selected, $customisedOnly = true,
215 $inLanguage = null, $overrideAttrs = [], Message $msg = null
216 ) {
217 $languageCode = MediaWikiServices::getInstance()->getMainConfig()
218 ->get( MainConfigNames::LanguageCode );
219
220 $include = $customisedOnly ? LanguageNameUtils::SUPPORTED : LanguageNameUtils::DEFINED;
221 $languages = MediaWikiServices::getInstance()
222 ->getLanguageNameUtils()
223 ->getLanguageNames( $inLanguage, $include );
224
225 // Make sure the site language is in the list;
226 // a custom language code might not have a defined name...
227 if ( !array_key_exists( $languageCode, $languages ) ) {
228 $languages[$languageCode] = $languageCode;
229 // Sort the array again
230 ksort( $languages );
231 }
232
238 $selected = isset( $languages[$selected] ) ? $selected : $languageCode;
239 $options = "\n";
240 foreach ( $languages as $code => $name ) {
241 $options .= self::option( "$code - $name", $code, $code == $selected ) . "\n";
242 }
243
244 $attrs = [ 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ];
245 $attrs = array_merge( $attrs, $overrideAttrs );
246
247 if ( $msg === null ) {
248 $msg = wfMessage( 'yourlanguage' );
249 }
250 return [
251 self::label( $msg->text(), $attrs['id'] ),
252 self::tags( 'select', $attrs, $options )
253 ];
254 }
255
263 public static function span( $text, $class, $attribs = [] ) {
264 return self::element( 'span', [ 'class' => $class ] + $attribs, $text );
265 }
266
275 public static function wrapClass( $text, $class, $tag = 'span', $attribs = [] ) {
276 return self::tags( $tag, [ 'class' => $class ] + $attribs, $text );
277 }
278
287 public static function input( $name, $size = false, $value = false, $attribs = [] ) {
288 $attributes = [ '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',
299 Html::getTextInputAttributes( $attributes + $attribs ) );
300 }
301
310 public static function password( $name, $size = false, $value = false,
311 $attribs = []
312 ) {
313 return self::input( $name, $size, $value,
314 array_merge( $attribs, [ 'type' => 'password' ] ) );
315 }
316
325 public static function attrib( $name, $present = true ) {
326 return $present ? [ $name => $name ] : [];
327 }
328
336 public static function check( $name, $checked = false, $attribs = [] ) {
337 return self::element( 'input', array_merge(
338 [
339 'name' => $name,
340 'type' => 'checkbox',
341 'value' => 1 ],
342 self::attrib( 'checked', $checked ),
343 $attribs ) );
344 }
345
354 public static function radio( $name, $value, $checked = false, $attribs = [] ) {
355 return self::element( 'input', [
356 'name' => $name,
357 'type' => 'radio',
358 'value' => $value ] + self::attrib( 'checked', $checked ) + $attribs );
359 }
360
371 public static function label( $label, $id, $attribs = [] ) {
372 $a = [ 'for' => $id ];
373
374 foreach ( [ 'class', 'title' ] as $attr ) {
375 if ( isset( $attribs[$attr] ) ) {
376 $a[$attr] = $attribs[$attr];
377 }
378 }
379
380 return self::element( 'label', $a, $label );
381 }
382
393 public static function inputLabel( $label, $name, $id, $size = false,
394 $value = false, $attribs = []
395 ) {
396 [ $label, $input ] = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
397 return $label . "\u{00A0}" . $input;
398 }
399
412 public static function inputLabelSep( $label, $name, $id, $size = false,
413 $value = false, $attribs = []
414 ) {
415 return [
416 self::label( $label, $id, $attribs ),
417 self::input( $name, $size, $value, [ 'id' => $id ] + $attribs )
418 ];
419 }
420
432 public static function checkLabel( $label, $name, $id, $checked = false, $attribs = [] ) {
433 $useMediaWikiUIEverywhere = MediaWikiServices::getInstance()->getMainConfig()
434 ->get( MainConfigNames::UseMediaWikiUIEverywhere );
435 $chkLabel = self::check( $name, $checked, [ 'id' => $id ] + $attribs ) .
436 "\u{00A0}" .
437 self::label( $label, $id, $attribs );
438
439 if ( $useMediaWikiUIEverywhere ) {
440 $chkLabel = self::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
441 $chkLabel . self::closeElement( 'div' );
442 }
443 return $chkLabel;
444 }
445
458 public static function radioLabel( $label, $name, $value, $id,
459 $checked = false, $attribs = []
460 ) {
461 return self::radio( $name, $value, $checked, [ 'id' => $id ] + $attribs ) .
462 "\u{00A0}" .
463 self::label( $label, $id, $attribs );
464 }
465
473 public static function submitButton( $value, $attribs = [] ) {
474 $useMediaWikiUIEverywhere = MediaWikiServices::getInstance()->getMainConfig()
475 ->get( MainConfigNames::UseMediaWikiUIEverywhere );
476 $baseAttrs = [
477 'type' => 'submit',
478 'value' => $value,
479 ];
480 // Done conditionally for time being as it is possible
481 // some submit forms
482 // might need to be mw-ui-destructive (e.g. delete a page)
483 if ( $useMediaWikiUIEverywhere ) {
484 $baseAttrs['class'] = 'mw-ui-button mw-ui-progressive';
485 }
486 // Any custom attributes will take precedence of anything in baseAttrs e.g. override the class
487 $attribs += $baseAttrs;
488 return Html::element( 'input', $attribs );
489 }
490
499 public static function option( $text, $value = null, $selected = false,
500 $attribs = [] ) {
501 if ( $value !== null ) {
502 $attribs['value'] = $value;
503 }
504 if ( $selected ) {
505 $attribs['selected'] = 'selected';
506 }
507 return Html::element( 'option', $attribs, $text );
508 }
509
523 public static function listDropDown( $name = '', $list = '', $other = '',
524 $selected = '', $class = '', $tabindex = null
525 ) {
526 $options = self::listDropDownOptions( $list, [ 'other' => $other ] );
527
528 $xmlSelect = new XmlSelect( $name, $name, $selected );
529 $xmlSelect->addOptions( $options );
530
531 if ( $class ) {
532 $xmlSelect->setAttribute( 'class', $class );
533 }
534 if ( $tabindex ) {
535 $xmlSelect->setAttribute( 'tabindex', $tabindex );
536 }
537
538 return $xmlSelect->getHTML();
539 }
540
554 public static function listDropDownOptions( $list, $params = [] ) {
555 $options = [];
556
557 if ( isset( $params['other'] ) ) {
558 $options[ $params['other'] ] = 'other';
559 }
560
561 $optgroup = false;
562 foreach ( explode( "\n", $list ) as $option ) {
563 $value = trim( $option );
564 if ( $value == '' ) {
565 continue;
566 }
567 if ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
568 # A new group is starting...
569 $value = trim( substr( $value, 1 ) );
570 if ( $value !== '' &&
571 // Do not use the value for 'other' as option group - T251351
572 ( !isset( $params['other'] ) || $value !== $params['other'] )
573 ) {
574 $optgroup = $value;
575 } else {
576 $optgroup = false;
577 }
578 } elseif ( substr( $value, 0, 2 ) == '**' ) {
579 # groupmember
580 $opt = trim( substr( $value, 2 ) );
581 if ( $optgroup === false ) {
582 $options[$opt] = $opt;
583 } else {
584 $options[$optgroup][$opt] = $opt;
585 }
586 } else {
587 # groupless reason list
588 $optgroup = false;
589 $options[$option] = $option;
590 }
591 }
592
593 return $options;
594 }
595
604 public static function listDropDownOptionsOoui( $options ) {
605 $optionsOoui = [];
606
607 foreach ( $options as $text => $value ) {
608 if ( is_array( $value ) ) {
609 $optionsOoui[] = [ 'optgroup' => (string)$text ];
610 foreach ( $value as $text2 => $value2 ) {
611 $optionsOoui[] = [ 'data' => (string)$value2, 'label' => (string)$text2 ];
612 }
613 } else {
614 $optionsOoui[] = [ 'data' => (string)$value, 'label' => (string)$text ];
615 }
616 }
617
618 return $optionsOoui;
619 }
620
632 public static function fieldset( $legend = false, $content = false, $attribs = [] ) {
633 $s = self::openElement( 'fieldset', $attribs ) . "\n";
634
635 if ( $legend ) {
636 $s .= self::element( 'legend', null, $legend ) . "\n";
637 }
638
639 if ( $content !== false ) {
640 $s .= $content . "\n";
641 $s .= self::closeElement( 'fieldset' ) . "\n";
642 }
643
644 return $s;
645 }
646
658 public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = [] ) {
659 return self::element( 'textarea',
660 Html::getTextInputAttributes(
661 [
662 'name' => $name,
663 'id' => $name,
664 'cols' => $cols,
665 'rows' => $rows
666 ] + $attribs
667 ),
668 $content, false );
669 }
670
685 public static function encodeJsVar( $value, $pretty = false ) {
686 return Html::encodeJsVar( $value, $pretty );
687 }
688
704 public static function encodeJsCall( $name, $args, $pretty = false ) {
705 return Html::encodeJsCall( $name, $args, $pretty );
706 }
707
719 private static function isWellFormed( $text ) {
720 $parser = xml_parser_create( "UTF-8" );
721
722 # case folding violates XML standard, turn it off
723 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
724
725 if ( !xml_parse( $parser, $text, true ) ) {
726 // $err = xml_error_string( xml_get_error_code( $parser ) );
727 // $position = xml_get_current_byte_index( $parser );
728 // $fragment = $this->extractFragment( $html, $position );
729 // $this->mXmlError = "$err at byte $position:\n$fragment";
730 xml_parser_free( $parser );
731 return false;
732 }
733
734 xml_parser_free( $parser );
735
736 return true;
737 }
738
747 public static function isWellFormedXmlFragment( $text ) {
748 $html =
749 Sanitizer::hackDocType() .
750 '<html>' .
751 $text .
752 '</html>';
753
754 return self::isWellFormed( $html );
755 }
756
764 public static function escapeTagsOnly( $in ) {
765 return str_replace(
766 [ '"', '>', '<' ],
767 [ '&quot;', '&gt;', '&lt;' ],
768 $in );
769 }
770
782 public static function buildForm( $fields, $submitLabel = null, $submitAttribs = [] ) {
783 $form = '';
784 $form .= "<table><tbody>";
785
786 foreach ( $fields as $labelmsg => $input ) {
787 $id = "mw-$labelmsg";
788 $form .= self::openElement( 'tr', [ 'id' => $id ] );
789
790 // TODO use a <label> here for accessibility purposes - will need
791 // to either not use a table to build the form, or find the ID of
792 // the input somehow.
793
794 $form .= self::tags( 'td', [ 'class' => 'mw-label' ], wfMessage( $labelmsg )->parse() );
795 $form .= self::openElement( 'td', [ 'class' => 'mw-input' ] )
796 . $input . self::closeElement( 'td' );
797 $form .= self::closeElement( 'tr' );
798 }
799
800 if ( $submitLabel ) {
801 $form .= self::openElement( 'tr' );
802 $form .= self::tags( 'td', [], '' );
803 $form .= self::openElement( 'td', [ 'class' => 'mw-submit' ] )
804 . self::submitButton( wfMessage( $submitLabel )->text(), $submitAttribs )
805 . self::closeElement( 'td' );
806 $form .= self::closeElement( 'tr' );
807 }
808
809 $form .= "</tbody></table>";
810
811 return $form;
812 }
813
820 public static function buildTable( $rows, $attribs = [], $headers = null ) {
821 $s = self::openElement( 'table', $attribs );
822
823 if ( is_array( $headers ) ) {
824 $s .= self::openElement( 'thead', $attribs );
825
826 foreach ( $headers as $id => $header ) {
827 $attribs = [];
828
829 if ( is_string( $id ) ) {
830 $attribs['id'] = $id;
831 }
832
833 $s .= self::element( 'th', $attribs, $header );
834 }
835 $s .= self::closeElement( 'thead' );
836 }
837
838 foreach ( $rows as $id => $row ) {
839 $attribs = [];
840
841 if ( is_string( $id ) ) {
842 $attribs['id'] = $id;
843 }
844
845 $s .= self::buildTableRow( $attribs, $row );
846 }
847
848 $s .= self::closeElement( 'table' );
849
850 return $s;
851 }
852
859 public static function buildTableRow( $attribs, $cells ) {
860 $s = self::openElement( 'tr', $attribs );
861
862 foreach ( $cells as $id => $cell ) {
863 $attribs = [];
864
865 if ( is_string( $id ) ) {
866 $attribs['id'] = $id;
867 }
868
869 $s .= self::element( 'td', $attribs, $cell );
870 }
871
872 $s .= self::closeElement( 'tr' );
873
874 return $s;
875 }
876}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode $wgLang
Definition Setup.php:535
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
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.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
Library for creating and parsing MW-style timestamps.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:28
Module of static functions for generating XML.
Definition Xml.php:33
static password( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML password input field.
Definition Xml.php:310
static closeElement( $element)
Shortcut to close an XML element.
Definition Xml.php:124
static textarea( $name, $content, $cols=40, $rows=5, $attribs=[])
Shortcut for creating textareas.
Definition Xml.php:658
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:412
static listDropDownOptions( $list, $params=[])
Build options for a drop-down box from a textual list.
Definition Xml.php:554
static encodeJsVar( $value, $pretty=false)
Encode a variable of arbitrary type to JavaScript.
Definition Xml.php:685
static listDropDownOptionsOoui( $options)
Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.
Definition Xml.php:604
static isWellFormedXmlFragment( $text)
Check if a string is a well-formed XML fragment.
Definition Xml.php:747
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
Definition Xml.php:336
static dateMenu( $year, $month)
Definition Xml.php:177
static buildForm( $fields, $submitLabel=null, $submitAttribs=[])
Generate a form (without the opening form element).
Definition Xml.php:782
static attrib( $name, $present=true)
Internal function for use in checkboxes and radio buttons and such.
Definition Xml.php:325
static label( $label, $id, $attribs=[])
Convenience function to build an HTML form label.
Definition Xml.php:371
static openElement( $element, $attribs=null)
This opens an XML element.
Definition Xml.php:115
static input( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML text input field.
Definition Xml.php:287
static wrapClass( $text, $class, $tag='span', $attribs=[])
Shortcut to make a specific element with a class attribute.
Definition Xml.php:275
static buildTable( $rows, $attribs=[], $headers=null)
Definition Xml.php:820
static submitButton( $value, $attribs=[])
Convenience function to build an HTML submit button When $wgUseMediaWikiUIEverywhere is true it will ...
Definition Xml.php:473
static option( $text, $value=null, $selected=false, $attribs=[])
Convenience function to build an HTML drop-down list item.
Definition Xml.php:499
static checkLabel( $label, $name, $id, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox with a label.
Definition Xml.php:432
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:393
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:214
static span( $text, $class, $attribs=[])
Shortcut to make a span element.
Definition Xml.php:263
static escapeTagsOnly( $in)
Replace " > and < with their respective HTML entities ( ", >, <)
Definition Xml.php:764
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition Xml.php:141
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition Xml.php:50
static radio( $name, $value, $checked=false, $attribs=[])
Convenience function to build an HTML radio button.
Definition Xml.php:354
static buildTableRow( $attribs, $cells)
Build a row for a table.
Definition Xml.php:859
static radioLabel( $label, $name, $value, $id, $checked=false, $attribs=[])
Convenience function to build an HTML radio button with a label.
Definition Xml.php:458
static encodeJsCall( $name, $args, $pretty=false)
Create a call to a JavaScript function.
Definition Xml.php:704
static listDropDown( $name='', $list='', $other='', $selected='', $class='', $tabindex=null)
Build a drop-down box from a textual list.
Definition Xml.php:523
static expandAttributes(?array $attribs)
Given an array of ('attributename' => 'value'), it generates the code to set the XML attributes : att...
Definition Xml.php:75
static monthSelector( $selected='', $allmonths=null, $id='month')
Create a date selector.
Definition Xml.php:154
static fieldset( $legend=false, $content=false, $attribs=[])
Shortcut for creating fieldsets.
Definition Xml.php:632
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:97
$content
Definition router.php:76
$header