41 public static function element( $element, $attribs =
null, $contents =
'',
44 $out =
'<' . $element;
45 if ( $attribs !==
null ) {
46 $out .= self::expandAttributes( $attribs );
48 if ( $contents ===
null ) {
50 } elseif ( $allowShortTag && $contents ===
'' ) {
53 $out .=
'>' . htmlspecialchars( $contents, ENT_NOQUOTES ) .
"</$element>";
69 if ( $attribs ===
null ) {
71 } elseif ( is_array( $attribs ) ) {
72 foreach ( $attribs as $name => $val ) {
73 $out .=
" {$name}=\"" . Sanitizer::encodeAttribute( $val ) .
'"';
77 throw new MWException(
'Expected attribute array, got something else in ' . __METHOD__ );
90 public static function elementClean( $element, $attribs = [], $contents =
'' ) {
92 $attribs = array_map( [
'UtfNormal\Validator',
'cleanUp' ], $attribs );
96 MediaWikiServices::getInstance()->getContentLanguage()->normalize( $contents );
98 return self::element( $element, $attribs, $contents );
109 return '<' . $element . self::expandAttributes( $attribs ) .
'>';
118 return "</$element>";
130 public static function tags( $element, $attribs, $contents ) {
131 return self::openElement( $element, $attribs ) . $contents .
"</$element>";
143 public static function monthSelector( $selected =
'', $allmonths =
null, $id =
'month' ) {
147 if ( $selected ===
null ) {
150 $data =
new XmlSelect(
'month', $id, $selected );
152 if ( $allmonths !==
null ) {
153 $options[
wfMessage(
'monthsall' )->text()] = $allmonths;
155 for ( $i = 1; $i < 13; $i++ ) {
156 $options[
$wgLang->getMonthName( $i )] = $i;
158 $data->addOptions( $options );
159 $data->setAttribute(
'class',
'mw-month-selector' );
160 return $data->getHTML();
170 # Offset overrides year/month selection
171 if ( $month && $month !== -1 ) {
172 $encMonth = intval( $month );
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 ) {
185 $encYear = $thisYear;
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 );
207 $inLanguage =
null, $overrideAttrs = [],
Message $msg =
null
211 $include = $customisedOnly ?
'mwfile' :
'mw';
212 $languages = MediaWikiServices::getInstance()
213 ->getLanguageNameUtils()
214 ->getLanguageNames( $inLanguage, $include );
229 $selected = isset( $languages[$selected] ) ? $selected :
$wgLanguageCode;
231 foreach ( $languages as $code => $name ) {
232 $options .= self::option(
"$code - $name", $code, $code == $selected ) .
"\n";
235 $attrs = [
'id' =>
'wpUserLanguage',
'name' =>
'wpUserLanguage' ];
236 $attrs = array_merge( $attrs, $overrideAttrs );
238 if ( $msg ===
null ) {
242 self::label( $msg->text(), $attrs[
'id'] ),
243 self::tags(
'select', $attrs, $options )
254 public static function span( $text, $class, $attribs = [] ) {
255 return self::element(
'span', [
'class' => $class ] + $attribs, $text );
266 public static function wrapClass( $text, $class, $tag =
'span', $attribs = [] ) {
267 return self::tags( $tag, [
'class' => $class ] + $attribs, $text );
278 public static function input( $name, $size =
false, $value =
false, $attribs = [] ) {
279 $attributes = [
'name' => $name ];
282 $attributes[
'size'] = $size;
285 if ( $value !==
false ) {
286 $attributes[
'value'] = $value;
289 return self::element(
'input',
290 Html::getTextInputAttributes( $attributes + $attribs ) );
301 public static function password( $name, $size =
false, $value =
false,
304 return self::input( $name, $size, $value,
305 array_merge( $attribs, [
'type' =>
'password' ] ) );
316 public static function attrib( $name, $present =
true ) {
317 return $present ? [ $name => $name ] : [];
327 public static function check( $name, $checked =
false, $attribs = [] ) {
328 return self::element(
'input', array_merge(
331 'type' =>
'checkbox',
333 self::attrib(
'checked', $checked ),
345 public static function radio( $name, $value, $checked =
false, $attribs = [] ) {
346 return self::element(
'input', [
349 'value' => $value ] + self::attrib(
'checked', $checked ) + $attribs );
362 public static function label( $label, $id, $attribs = [] ) {
363 $a = [
'for' => $id ];
365 foreach ( [
'class',
'title' ] as $attr ) {
366 if ( isset( $attribs[$attr] ) ) {
367 $a[$attr] = $attribs[$attr];
371 return self::element(
'label', $a, $label );
384 public static function inputLabel( $label, $name, $id, $size =
false,
385 $value =
false, $attribs = []
387 list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
388 return $label .
"\u{00A0}" . $input;
404 $value =
false, $attribs = []
407 self::label( $label, $id, $attribs ),
408 self::input( $name, $size, $value, [
'id' => $id ] + $attribs )
423 public static function checkLabel( $label, $name, $id, $checked =
false, $attribs = [] ) {
425 $chkLabel = self::check( $name, $checked, [
'id' => $id ] + $attribs ) .
427 self::label( $label, $id, $attribs );
430 $chkLabel = self::openElement(
'div', [
'class' =>
'mw-ui-checkbox' ] ) .
431 $chkLabel . self::closeElement(
'div' );
448 public static function radioLabel( $label, $name, $value, $id,
449 $checked =
false, $attribs = []
451 return self::radio( $name, $value, $checked, [
'id' => $id ] + $attribs ) .
453 self::label( $label, $id, $attribs );
473 $baseAttrs[
'class'] =
'mw-ui-button mw-ui-progressive';
476 $attribs += $baseAttrs;
477 return Html::element(
'input', $attribs );
488 public static function option( $text, $value =
null, $selected =
false,
490 if ( $value !==
null ) {
491 $attribs[
'value'] = $value;
494 $attribs[
'selected'] =
'selected';
496 return Html::element(
'option', $attribs, $text );
512 public static function listDropDown( $name =
'', $list =
'', $other =
'',
513 $selected =
'', $class =
'', $tabindex =
null
515 $options = self::listDropDownOptions( $list, [
'other' => $other ] );
517 $xmlSelect =
new XmlSelect( $name, $name, $selected );
518 $xmlSelect->addOptions( $options );
521 $xmlSelect->setAttribute(
'class', $class );
524 $xmlSelect->setAttribute(
'tabindex', $tabindex );
527 return $xmlSelect->getHTML();
546 if ( isset( $params[
'other'] ) ) {
547 $options[ $params[
'other'] ] =
'other';
551 foreach ( explode(
"\n", $list ) as $option ) {
552 $value = trim( $option );
553 if ( $value ==
'' ) {
555 } elseif ( substr( $value, 0, 1 ) ==
'*' && substr( $value, 1, 1 ) !=
'*' ) {
556 # A new group is starting...
557 $value = trim( substr( $value, 1 ) );
559 } elseif ( substr( $value, 0, 2 ) ==
'**' ) {
561 $opt = trim( substr( $value, 2 ) );
562 if ( $optgroup ===
false ) {
563 $options[$opt] = $opt;
565 $options[$optgroup][$opt] = $opt;
568 # groupless reason list
570 $options[$option] = $option;
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 ];
595 $optionsOoui[] = [
'data' => (string)$value,
'label' => (
string)$text ];
614 $s = self::openElement(
'fieldset', $attribs ) .
"\n";
617 $s .= self::element(
'legend',
null, $legend ) .
"\n";
622 $s .= self::closeElement(
'fieldset' ) .
"\n";
640 return self::element(
'textarea',
641 Html::getTextInputAttributes(
665 return $value->value;
667 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
682 foreach (
$args as &$arg ) {
683 $arg = self::encodeJsVar( $arg, $pretty );
684 if ( $arg ===
false ) {
689 return "$name(" . ( $pretty
690 ? (
' ' . implode(
', ',
$args ) .
' ' )
691 : implode(
',',
$args )
707 $parser = xml_parser_create(
"UTF-8" );
709 # case folding violates XML standard, turn it off
710 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING,
false );
712 if ( !xml_parse( $parser, $text,
true ) ) {
717 xml_parser_free( $parser );
721 xml_parser_free( $parser );
736 Sanitizer::hackDocType() .
741 return self::isWellFormed( $html );
754 [
'"',
'>',
'<' ],
769 public static function buildForm( $fields, $submitLabel =
null, $submitAttribs = [] ) {
771 $form .=
"<table><tbody>";
773 foreach ( $fields as $labelmsg => $input ) {
774 $id =
"mw-$labelmsg";
775 $form .= self::openElement(
'tr', [
'id' => $id ] );
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' );
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' );
796 $form .=
"</tbody></table>";
808 public static function buildTable( $rows, $attribs = [], $headers =
null ) {
809 $s = self::openElement(
'table', $attribs );
811 if ( is_array( $headers ) ) {
812 $s .= self::openElement(
'thead', $attribs );
814 foreach ( $headers as $id =>
$header ) {
817 if ( is_string( $id ) ) {
818 $attribs[
'id'] = $id;
821 $s .= self::element(
'th', $attribs,
$header );
823 $s .= self::closeElement(
'thead' );
826 foreach ( $rows as $id => $row ) {
829 if ( is_string( $id ) ) {
830 $attribs[
'id'] = $id;
833 $s .= self::buildTableRow( $attribs, $row );
836 $s .= self::closeElement(
'table' );
848 $s = self::openElement(
'tr', $attribs );
850 foreach ( $cells as $id => $cell ) {
853 if ( is_string( $id ) ) {
854 $attribs[
'id'] = $id;
857 $s .= self::element(
'td', $attribs, $cell );
860 $s .= self::closeElement(
'tr' );
$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.
The Message class deals with fetching and processing of interface message into a variety of formats.
A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to interpret a given string a...
Class for generating HTML <select> or <datalist> elements.
Module of static functions for generating XML.
static password( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML password input field.
static closeElement( $element)
Shortcut to close an XML element.
static textarea( $name, $content, $cols=40, $rows=5, $attribs=[])
Shortcut for creating textareas.
static inputLabelSep( $label, $name, $id, $size=false, $value=false, $attribs=[])
Same as Xml::inputLabel() but return input and label in an array.
static isWellFormed( $text)
Check if a string is well-formed XML.
static listDropDownOptions( $list, $params=[])
Build options for a drop-down box from a textual list.
static encodeJsVar( $value, $pretty=false)
Encode a variable of arbitrary type to JavaScript.
static listDropDownOptionsOoui( $options)
Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.
static isWellFormedXmlFragment( $text)
Check if a string is a well-formed XML fragment.
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
static dateMenu( $year, $month)
static buildForm( $fields, $submitLabel=null, $submitAttribs=[])
Generate a form (without the opening form element).
static attrib( $name, $present=true)
Internal function for use in checkboxes and radio buttons and such.
static label( $label, $id, $attribs=[])
Convenience function to build an HTML form label.
static openElement( $element, $attribs=null)
This opens an XML element.
static input( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML text input field.
static wrapClass( $text, $class, $tag='span', $attribs=[])
Shortcut to make a specific element with a class attribute.
static buildTable( $rows, $attribs=[], $headers=null)
Build a table of data.
static submitButton( $value, $attribs=[])
Convenience function to build an HTML submit button When $wgUseMediaWikiUIEverywhere is true it will ...
static option( $text, $value=null, $selected=false, $attribs=[])
Convenience function to build an HTML drop-down list item.
static checkLabel( $label, $name, $id, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox with a label.
static inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML text input field with a label.
static languageSelector( $selected, $customisedOnly=true, $inLanguage=null, $overrideAttrs=[], Message $msg=null)
Construct a language selector appropriate for use in a form or preferences.
static expandAttributes( $attribs)
Given an array of ('attributename' => 'value'), it generates the code to set the XML attributes : att...
static span( $text, $class, $attribs=[])
Shortcut to make a span element.
static escapeTagsOnly( $in)
Replace " > and < with their respective HTML entities ( ", >, <)
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
static radio( $name, $value, $checked=false, $attribs=[])
Convenience function to build an HTML radio button.
static buildTableRow( $attribs, $cells)
Build a row for a table.
static radioLabel( $label, $name, $value, $id, $checked=false, $attribs=[])
Convenience function to build an HTML radio button with a label.
static encodeJsCall( $name, $args, $pretty=false)
Create a call to a JavaScript function.
static listDropDown( $name='', $list='', $other='', $selected='', $class='', $tabindex=null)
Build a drop-down box from a textual list.
static monthSelector( $selected='', $allmonths=null, $id='month')
Create a date selector.
static fieldset( $legend=false, $content=false, $attribs=[])
Shortcut for creating fieldsets.
static elementClean( $element, $attribs=[], $contents='')
Format an XML element as with self::element(), but run text through the content language's normalize(...