32use UtfNormal\Validator;
54 public static function element( $element, $attribs =
null, $contents =
'',
57 $out =
'<' . $element;
58 if ( $attribs !==
null ) {
61 if ( $contents ===
null ) {
63 } elseif ( $allowShortTag && $contents ===
'' ) {
66 $out .=
'>' . htmlspecialchars( $contents, ENT_NOQUOTES ) .
"</$element>";
80 if ( $attribs ===
null ) {
84 foreach ( $attribs as $name => $val ) {
85 $out .=
" {$name}=\"" . Sanitizer::encodeAttribute( $val ) .
'"';
101 public static function elementClean( $element, $attribs = [], $contents =
'' ) {
103 $attribs = array_map( [ Validator::class,
'cleanUp' ], $attribs );
129 return "</$element>";
145 public static function tags( $element, $attribs, $contents ) {
160 public static function monthSelector( $selected =
'', $allmonths =
null, $id =
'month' ) {
166 $data =
new XmlSelect(
'month', $id, $selected ??
'' );
168 if ( $allmonths !==
null ) {
169 $options[
wfMessage(
'monthsall' )->text()] = $allmonths;
171 for ( $i = 1; $i < 13; $i++ ) {
172 $options[
$wgLang->getMonthName( $i )] = $i;
174 $data->addOptions( $options );
175 $data->setAttribute(
'class',
'mw-month-selector' );
176 return $data->getHTML();
189 # Offset overrides year/month selection
190 if ( $month && $month !== -1 ) {
191 $encMonth = intval( $month );
196 $encYear = intval( $year );
197 } elseif ( $encMonth ) {
198 $timestamp = MWTimestamp::getInstance();
199 $thisMonth = intval( $timestamp->format(
'n' ) );
200 $thisYear = intval( $timestamp->format(
'Y' ) );
201 if ( $encMonth > $thisMonth ) {
204 $encYear = $thisYear;
208 $inputAttribs = [
'id' =>
'year',
'maxlength' => 4,
'size' => 7 ];
210 Html::input(
'year', $encYear,
'number', $inputAttribs ) .
' ' .
228 $inLanguage =
null, $overrideAttrs = [], ?
Message $msg =
null
234 $include = $customisedOnly ? LanguageNameUtils::SUPPORTED : LanguageNameUtils::DEFINED;
236 ->getLanguageNameUtils()
237 ->getLanguageNames( $inLanguage, $include );
241 if ( !array_key_exists( $languageCode, $languages ) ) {
242 $languages[$languageCode] = $languageCode;
252 $selected = isset( $languages[$selected] ) ? $selected : $languageCode;
254 foreach ( $languages as $code => $name ) {
255 $options .=
self::option(
"$code - $name", $code, $code == $selected ) .
"\n";
258 $attrs = [
'id' =>
'wpUserLanguage',
'name' =>
'wpUserLanguage' ];
259 $attrs = array_merge( $attrs, $overrideAttrs );
277 public static function span( $text, $class, $attribs = [] ) {
280 return self::element(
'span', [
'class' => $class ] + $attribs, $text );
294 public static function wrapClass( $text, $class, $tag =
'span', $attribs = [] ) {
296 return self::tags( $tag, [
'class' => $class ] + $attribs, $text );
309 public static function input( $name, $size =
false, $value =
false, $attribs = [] ) {
312 $attributes = [
'name' => $name ];
315 $attributes[
'size'] = $size;
318 if ( $value !==
false ) {
319 $attributes[
'value'] = $value;
335 public static function password( $name, $size =
false, $value =
false,
341 array_merge( $attribs, [
'type' =>
'password' ] ) );
354 public static function attrib( $name, $present =
true ) {
355 return $present ? [ $name => $name ] : [];
367 public static function check( $name, $checked =
false, $attribs = [] ) {
373 'type' =>
'checkbox',
375 self::attrib(
'checked', $checked ),
389 public static function radio( $name, $value, $checked =
false, $attribs = [] ) {
395 'value' => $value ] + self::attrib(
'checked', $checked ) + $attribs );
410 public static function label( $label, $id, $attribs = [] ) {
411 $a = [
'for' => $id ];
413 foreach ( [
'class',
'title' ] as $attr ) {
414 if ( isset( $attribs[$attr] ) ) {
415 $a[$attr] = $attribs[$attr];
434 public static function inputLabel( $label, $name, $id, $size =
false,
435 $value =
false, $attribs = []
440 return $label .
"\u{00A0}" . $input;
457 $value =
false, $attribs = []
463 self::input( $name, $size, $value, [
'id' => $id ] + $attribs )
479 public static function checkLabel( $label, $name, $id, $checked =
false, $attribs = [] ) {
482 return self::check( $name, $checked, [
'id' => $id ] + $attribs ) .
500 public static function radioLabel( $label, $name, $value, $id,
501 $checked =
false, $attribs = []
505 return self::radio( $name, $value, $checked, [
'id' => $id ] + $attribs ) .
539 public static function option( $text, $value =
null, $selected =
false,
541 if ( $value !==
null ) {
542 $attribs[
'value'] = $value;
545 $attribs[
'selected'] =
'selected';
565 public static function listDropdown( $name =
'', $list =
'', $other =
'',
566 $selected =
'', $class =
'', $tabindex =
null
570 $xmlSelect =
new XmlSelect( $name, $name, $selected );
571 $xmlSelect->addOptions( $options );
574 $xmlSelect->setAttribute(
'class', $class );
577 $xmlSelect->setAttribute(
'tabindex', $tabindex );
580 return $xmlSelect->getHTML();
601 if ( isset( $params[
'other'] ) ) {
602 $options[ $params[
'other'] ] =
'other';
606 foreach ( explode(
"\n", $list ) as $option ) {
607 $value = trim( $option );
608 if ( $value ==
'' ) {
611 if ( substr( $value, 0, 1 ) ==
'*' && substr( $value, 1, 1 ) !=
'*' ) {
612 # A new group is starting...
613 $value = trim( substr( $value, 1 ) );
614 if ( $value !==
'' &&
616 ( !isset( $params[
'other'] ) || $value !== $params[
'other'] )
622 } elseif ( substr( $value, 0, 2 ) ==
'**' ) {
624 $opt = trim( substr( $value, 2 ) );
625 if ( $optgroup ===
false ) {
626 $options[$opt] = $opt;
628 $options[$optgroup][$opt] = $opt;
631 # groupless reason list
633 $options[$option] = $option;
653 foreach ( $options as $text => $value ) {
654 if ( is_array( $value ) ) {
655 $optionsOoui[] = [
'optgroup' => (string)$text ];
656 foreach ( $value as $text2 => $value2 ) {
657 $optionsOoui[] = [
'data' => (string)$value2,
'label' => (
string)$text2 ];
660 $optionsOoui[] = [
'data' => (string)$value,
'label' => (
string)$text ];
679 public static function fieldset( $legend =
false, $content =
false, $attribs = [] ) {
686 if ( $content !==
false ) {
687 $s .= $content .
"\n";
706 public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = [] ) {
737 return Html::encodeJsVar( $value, $pretty );
759 return Html::encodeJsCall( $name, $args, $pretty );
773 private static function isWellFormed( $text ) {
774 $parser = xml_parser_create(
"UTF-8" );
776 # case folding violates XML standard, turn it off
777 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
779 if ( !xml_parse( $parser, $text,
true ) ) {
784 xml_parser_free( $parser );
788 xml_parser_free( $parser );
803 Sanitizer::hackDocType() .
808 return self::isWellFormed( $html );
821 [
'"',
'>',
'<' ],
838 public static function buildForm( $fields, $submitLabel =
null, $submitAttribs = [] ) {
840 $form .=
"<table><tbody>";
842 foreach ( $fields as $labelmsg => $input ) {
843 $id =
"mw-$labelmsg";
856 if ( $submitLabel ) {
864 'value' =>
wfMessage( $submitLabel )->text(),
871 $form .=
"</tbody></table>";
884 public static function buildTable( $rows, $attribs = [], $headers =
null ) {
889 if ( is_array( $headers ) ) {
892 foreach ( $headers as $id =>
$header ) {
895 if ( is_string( $id ) ) {
896 $attribs[
'id'] = $id;
904 foreach ( $rows as $id => $row ) {
907 if ( is_string( $id ) ) {
908 $attribs[
'id'] = $id;
932 foreach ( $cells as $id => $cell ) {
935 if ( is_string( $id ) ) {
936 $attribs[
'id'] = $id;
948class_alias( Xml::class,
'Xml' );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgLang
A class containing constants representing the names of configuration variables.
const LanguageCode
Name constant for the LanguageCode setting, for use with Config::get()