43 public static function element( $element, $attribs =
null, $contents =
'',
46 $out =
'<' . $element;
47 if ( $attribs !==
null ) {
48 $out .= self::expandAttributes( $attribs );
50 if ( $contents ===
null ) {
52 } elseif ( $allowShortTag && $contents ===
'' ) {
55 $out .=
'>' . htmlspecialchars( $contents, ENT_NOQUOTES ) .
"</$element>";
71 if ( $attribs ===
null ) {
73 } elseif ( is_array( $attribs ) ) {
74 foreach ( $attribs as $name => $val ) {
75 $out .=
" {$name}=\"" . Sanitizer::encodeAttribute( $val ) .
'"';
79 throw new MWException(
'Expected attribute array, got something else in ' . __METHOD__ );
94 public static function elementClean( $element, $attribs = [], $contents =
'' ) {
96 $attribs = array_map( [ UtfNormal\Validator::class,
'cleanUp' ], $attribs );
100 MediaWikiServices::getInstance()->getContentLanguage()->normalize( $contents );
102 return self::element( $element, $attribs, $contents );
113 return '<' . $element . self::expandAttributes( $attribs ) .
'>';
122 return "</$element>";
134 public static function tags( $element, $attribs, $contents ) {
135 return self::openElement( $element, $attribs ) . $contents .
"</$element>";
147 public static function monthSelector( $selected =
'', $allmonths =
null, $id =
'month' ) {
151 if ( $selected ===
null ) {
154 $data =
new XmlSelect(
'month', $id, $selected );
156 if ( $allmonths !==
null ) {
157 $options[
wfMessage(
'monthsall' )->text()] = $allmonths;
159 for ( $i = 1; $i < 13; $i++ ) {
160 $options[
$wgLang->getMonthName( $i )] = $i;
162 $data->addOptions( $options );
163 $data->setAttribute(
'class',
'mw-month-selector' );
164 return $data->getHTML();
174 # Offset overrides year/month selection
175 if ( $month && $month !== -1 ) {
176 $encMonth = intval( $month );
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 ) {
189 $encYear = $thisYear;
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' );
211 $inLanguage =
null, $overrideAttrs = [],
Message $msg =
null
213 $languageCode = MediaWikiServices::getInstance()->getMainConfig()
214 ->get( MainConfigNames::LanguageCode );
216 $include = $customisedOnly ? LanguageNameUtils::SUPPORTED : LanguageNameUtils::DEFINED;
217 $languages = MediaWikiServices::getInstance()
218 ->getLanguageNameUtils()
219 ->getLanguageNames( $inLanguage, $include );
223 if ( !array_key_exists( $languageCode, $languages ) ) {
224 $languages[$languageCode] = $languageCode;
234 $selected = isset( $languages[$selected] ) ? $selected : $languageCode;
236 foreach ( $languages as $code => $name ) {
237 $options .= self::option(
"$code - $name", $code, $code == $selected ) .
"\n";
240 $attrs = [
'id' =>
'wpUserLanguage',
'name' =>
'wpUserLanguage' ];
241 $attrs = array_merge( $attrs, $overrideAttrs );
243 if ( $msg ===
null ) {
247 self::label( $msg->text(), $attrs[
'id'] ),
248 self::tags(
'select', $attrs, $options )
259 public static function span( $text, $class, $attribs = [] ) {
260 return self::element(
'span', [
'class' => $class ] + $attribs, $text );
271 public static function wrapClass( $text, $class, $tag =
'span', $attribs = [] ) {
272 return self::tags( $tag, [
'class' => $class ] + $attribs, $text );
283 public static function input( $name, $size =
false, $value =
false, $attribs = [] ) {
284 $attributes = [
'name' => $name ];
287 $attributes[
'size'] = $size;
290 if ( $value !==
false ) {
291 $attributes[
'value'] = $value;
294 return self::element(
'input',
295 Html::getTextInputAttributes( $attributes + $attribs ) );
306 public static function password( $name, $size =
false, $value =
false,
309 return self::input( $name, $size, $value,
310 array_merge( $attribs, [
'type' =>
'password' ] ) );
321 public static function attrib( $name, $present =
true ) {
322 return $present ? [ $name => $name ] : [];
332 public static function check( $name, $checked =
false, $attribs = [] ) {
333 return self::element(
'input', array_merge(
336 'type' =>
'checkbox',
338 self::attrib(
'checked', $checked ),
350 public static function radio( $name, $value, $checked =
false, $attribs = [] ) {
351 return self::element(
'input', [
354 'value' => $value ] + self::attrib(
'checked', $checked ) + $attribs );
367 public static function label( $label, $id, $attribs = [] ) {
368 $a = [
'for' => $id ];
370 foreach ( [
'class',
'title' ] as $attr ) {
371 if ( isset( $attribs[$attr] ) ) {
372 $a[$attr] = $attribs[$attr];
376 return self::element(
'label', $a, $label );
389 public static function inputLabel( $label, $name, $id, $size =
false,
390 $value =
false, $attribs = []
392 list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
393 return $label .
"\u{00A0}" . $input;
409 $value =
false, $attribs = []
412 self::label( $label, $id, $attribs ),
413 self::input( $name, $size, $value, [
'id' => $id ] + $attribs )
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 ) .
433 self::label( $label, $id, $attribs );
435 if ( $useMediaWikiUIEverywhere ) {
436 $chkLabel = self::openElement(
'div', [
'class' =>
'mw-ui-checkbox' ] ) .
437 $chkLabel . self::closeElement(
'div' );
454 public static function radioLabel( $label, $name, $value, $id,
455 $checked =
false, $attribs = []
457 return self::radio( $name, $value, $checked, [
'id' => $id ] + $attribs ) .
459 self::label( $label, $id, $attribs );
470 $useMediaWikiUIEverywhere = MediaWikiServices::getInstance()->getMainConfig()
471 ->get( MainConfigNames::UseMediaWikiUIEverywhere );
479 if ( $useMediaWikiUIEverywhere ) {
480 $baseAttrs[
'class'] =
'mw-ui-button mw-ui-progressive';
483 $attribs += $baseAttrs;
484 return Html::element(
'input', $attribs );
495 public static function option( $text, $value =
null, $selected =
false,
497 if ( $value !==
null ) {
498 $attribs[
'value'] = $value;
501 $attribs[
'selected'] =
'selected';
503 return Html::element(
'option', $attribs, $text );
519 public static function listDropDown( $name =
'', $list =
'', $other =
'',
520 $selected =
'', $class =
'', $tabindex =
null
522 $options = self::listDropDownOptions( $list, [
'other' => $other ] );
524 $xmlSelect =
new XmlSelect( $name, $name, $selected );
525 $xmlSelect->addOptions( $options );
528 $xmlSelect->setAttribute(
'class', $class );
531 $xmlSelect->setAttribute(
'tabindex', $tabindex );
534 return $xmlSelect->getHTML();
553 if ( isset( $params[
'other'] ) ) {
554 $options[ $params[
'other'] ] =
'other';
558 foreach ( explode(
"\n", $list ) as $option ) {
559 $value = trim( $option );
560 if ( $value ==
'' ) {
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 !==
'' &&
568 ( !isset( $params[
'other'] ) || $value !== $params[
'other'] )
574 } elseif ( substr( $value, 0, 2 ) ==
'**' ) {
576 $opt = trim( substr( $value, 2 ) );
577 if ( $optgroup ===
false ) {
578 $options[$opt] = $opt;
580 $options[$optgroup][$opt] = $opt;
583 # groupless reason list
585 $options[$option] = $option;
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 ];
610 $optionsOoui[] = [
'data' => (string)$value,
'label' => (
string)$text ];
629 $s = self::openElement(
'fieldset', $attribs ) .
"\n";
632 $s .= self::element(
'legend',
null, $legend ) .
"\n";
637 $s .= self::closeElement(
'fieldset' ) .
"\n";
655 return self::element(
'textarea',
656 Html::getTextInputAttributes(
680 return $value->value;
682 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
697 foreach (
$args as &$arg ) {
698 $arg = self::encodeJsVar( $arg, $pretty );
699 if ( $arg ===
false ) {
704 return "$name(" . ( $pretty
705 ? (
' ' . implode(
', ',
$args ) .
' ' )
706 : implode(
',',
$args )
721 private static function isWellFormed( $text ) {
722 $parser = xml_parser_create(
"UTF-8" );
724 # case folding violates XML standard, turn it off
725 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
727 if ( !xml_parse( $parser, $text,
true ) ) {
732 xml_parser_free( $parser );
736 xml_parser_free( $parser );
751 Sanitizer::hackDocType() .
756 return self::isWellFormed( $html );
769 [
'"',
'>',
'<' ],
784 public static function buildForm( $fields, $submitLabel =
null, $submitAttribs = [] ) {
786 $form .=
"<table><tbody>";
788 foreach ( $fields as $labelmsg => $input ) {
789 $id =
"mw-$labelmsg";
790 $form .= self::openElement(
'tr', [
'id' => $id ] );
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' );
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' );
811 $form .=
"</tbody></table>";
822 public static function buildTable( $rows, $attribs = [], $headers =
null ) {
823 $s = self::openElement(
'table', $attribs );
825 if ( is_array( $headers ) ) {
826 $s .= self::openElement(
'thead', $attribs );
828 foreach ( $headers as $id =>
$header ) {
831 if ( is_string( $id ) ) {
832 $attribs[
'id'] = $id;
835 $s .= self::element(
'th', $attribs,
$header );
837 $s .= self::closeElement(
'thead' );
840 foreach ( $rows as $id => $row ) {
843 if ( is_string( $id ) ) {
844 $attribs[
'id'] = $id;
847 $s .= self::buildTableRow( $attribs, $row );
850 $s .= self::closeElement(
'table' );
862 $s = self::openElement(
'tr', $attribs );
864 foreach ( $cells as $id => $cell ) {
867 if ( is_string( $id ) ) {
868 $attribs[
'id'] = $id;
871 $s .= self::element(
'td', $attribs, $cell );
874 $s .= self::closeElement(
'tr' );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode $wgLang
A class containing constants representing the names of configuration variables.
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 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)
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(...
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s