44 public static function element( $element, $attribs =
null, $contents =
'',
47 $out =
'<' . $element;
48 if ( $attribs !==
null ) {
49 $out .= self::expandAttributes( $attribs );
51 if ( $contents ===
null ) {
53 } elseif ( $allowShortTag && $contents ===
'' ) {
56 $out .=
'>' . htmlspecialchars( $contents, ENT_NOQUOTES ) .
"</$element>";
72 if ( $attribs ===
null ) {
74 } elseif ( is_array( $attribs ) ) {
75 foreach ( $attribs as $name => $val ) {
76 $out .=
" {$name}=\"" . Sanitizer::encodeAttribute( $val ) .
'"';
80 throw new MWException(
'Expected attribute array, got something else in ' . __METHOD__ );
95 public static function elementClean( $element, $attribs = [], $contents =
'' ) {
97 $attribs = array_map( [ UtfNormal\Validator::class,
'cleanUp' ], $attribs );
101 MediaWikiServices::getInstance()->getContentLanguage()->normalize( $contents );
103 return self::element( $element, $attribs, $contents );
114 return '<' . $element . self::expandAttributes( $attribs ) .
'>';
123 return "</$element>";
135 public static function tags( $element, $attribs, $contents ) {
136 return self::openElement( $element, $attribs ) . $contents .
"</$element>";
148 public static function monthSelector( $selected =
'', $allmonths =
null, $id =
'month' ) {
152 $data =
new XmlSelect(
'month', $id, $selected ??
'' );
154 if ( $allmonths !==
null ) {
155 $options[
wfMessage(
'monthsall' )->text()] = $allmonths;
157 for ( $i = 1; $i < 13; $i++ ) {
158 $options[
$wgLang->getMonthName( $i )] = $i;
160 $data->addOptions( $options );
161 $data->setAttribute(
'class',
'mw-month-selector' );
162 return $data->getHTML();
172 # Offset overrides year/month selection
173 if ( $month && $month !== -1 ) {
174 $encMonth = intval( $month );
179 $encYear = intval( $year );
180 } elseif ( $encMonth ) {
181 $timestamp = MWTimestamp::getInstance();
182 $thisMonth = intval( $timestamp->format(
'n' ) );
183 $thisYear = intval( $timestamp->format(
'Y' ) );
184 if ( $encMonth > $thisMonth ) {
187 $encYear = $thisYear;
191 $inputAttribs = [
'id' =>
'year',
'maxlength' => 4,
'size' => 7 ];
192 return self::label(
wfMessage(
'year' )->text(),
'year' ) .
' ' .
193 Html::input(
'year', $encYear,
'number', $inputAttribs ) .
' ' .
194 self::label(
wfMessage(
'month' )->text(),
'month' ) .
' ' .
195 self::monthSelector( $encMonth,
'-1' );
209 $inLanguage =
null, $overrideAttrs = [],
Message $msg =
null
211 $languageCode = MediaWikiServices::getInstance()->getMainConfig()
212 ->get( MainConfigNames::LanguageCode );
214 $include = $customisedOnly ? LanguageNameUtils::SUPPORTED : LanguageNameUtils::DEFINED;
215 $languages = MediaWikiServices::getInstance()
216 ->getLanguageNameUtils()
217 ->getLanguageNames( $inLanguage, $include );
221 if ( !array_key_exists( $languageCode, $languages ) ) {
222 $languages[$languageCode] = $languageCode;
232 $selected = isset( $languages[$selected] ) ? $selected : $languageCode;
234 foreach ( $languages as $code => $name ) {
235 $options .= self::option(
"$code - $name", $code, $code == $selected ) .
"\n";
238 $attrs = [
'id' =>
'wpUserLanguage',
'name' =>
'wpUserLanguage' ];
239 $attrs = array_merge( $attrs, $overrideAttrs );
241 if ( $msg ===
null ) {
245 self::label( $msg->text(), $attrs[
'id'] ),
246 self::tags(
'select', $attrs, $options )
257 public static function span( $text, $class, $attribs = [] ) {
258 return self::element(
'span', [
'class' => $class ] + $attribs, $text );
269 public static function wrapClass( $text, $class, $tag =
'span', $attribs = [] ) {
270 return self::tags( $tag, [
'class' => $class ] + $attribs, $text );
281 public static function input( $name, $size =
false, $value =
false, $attribs = [] ) {
282 $attributes = [
'name' => $name ];
285 $attributes[
'size'] = $size;
288 if ( $value !==
false ) {
289 $attributes[
'value'] = $value;
292 return self::element(
'input',
293 Html::getTextInputAttributes( $attributes + $attribs ) );
304 public static function password( $name, $size =
false, $value =
false,
307 return self::input( $name, $size, $value,
308 array_merge( $attribs, [
'type' =>
'password' ] ) );
319 public static function attrib( $name, $present =
true ) {
320 return $present ? [ $name => $name ] : [];
330 public static function check( $name, $checked =
false, $attribs = [] ) {
331 return self::element(
'input', array_merge(
334 'type' =>
'checkbox',
336 self::attrib(
'checked', $checked ),
348 public static function radio( $name, $value, $checked =
false, $attribs = [] ) {
349 return self::element(
'input', [
352 'value' => $value ] + self::attrib(
'checked', $checked ) + $attribs );
365 public static function label( $label, $id, $attribs = [] ) {
366 $a = [
'for' => $id ];
368 foreach ( [
'class',
'title' ] as $attr ) {
369 if ( isset( $attribs[$attr] ) ) {
370 $a[$attr] = $attribs[$attr];
374 return self::element(
'label', $a, $label );
387 public static function inputLabel( $label, $name, $id, $size =
false,
388 $value =
false, $attribs = []
390 [ $label, $input ] = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
391 return $label .
"\u{00A0}" . $input;
407 $value =
false, $attribs = []
410 self::label( $label, $id, $attribs ),
411 self::input( $name, $size, $value, [
'id' => $id ] + $attribs )
426 public static function checkLabel( $label, $name, $id, $checked =
false, $attribs = [] ) {
427 $useMediaWikiUIEverywhere = MediaWikiServices::getInstance()->getMainConfig()
428 ->get( MainConfigNames::UseMediaWikiUIEverywhere );
429 $chkLabel = self::check( $name, $checked, [
'id' => $id ] + $attribs ) .
431 self::label( $label, $id, $attribs );
433 if ( $useMediaWikiUIEverywhere ) {
434 $chkLabel = self::openElement(
'div', [
'class' =>
'mw-ui-checkbox' ] ) .
435 $chkLabel . self::closeElement(
'div' );
452 public static function radioLabel( $label, $name, $value, $id,
453 $checked =
false, $attribs = []
455 return self::radio( $name, $value, $checked, [
'id' => $id ] + $attribs ) .
457 self::label( $label, $id, $attribs );
468 $useMediaWikiUIEverywhere = MediaWikiServices::getInstance()->getMainConfig()
469 ->get( MainConfigNames::UseMediaWikiUIEverywhere );
477 if ( $useMediaWikiUIEverywhere ) {
478 $baseAttrs[
'class'] =
'mw-ui-button mw-ui-progressive';
481 $attribs += $baseAttrs;
482 return Html::element(
'input', $attribs );
493 public static function option( $text, $value =
null, $selected =
false,
495 if ( $value !==
null ) {
496 $attribs[
'value'] = $value;
499 $attribs[
'selected'] =
'selected';
501 return Html::element(
'option', $attribs, $text );
517 public static function listDropDown( $name =
'', $list =
'', $other =
'',
518 $selected =
'', $class =
'', $tabindex =
null
520 $options = self::listDropDownOptions( $list, [
'other' => $other ] );
522 $xmlSelect =
new XmlSelect( $name, $name, $selected );
523 $xmlSelect->addOptions( $options );
526 $xmlSelect->setAttribute(
'class', $class );
529 $xmlSelect->setAttribute(
'tabindex', $tabindex );
532 return $xmlSelect->getHTML();
551 if ( isset( $params[
'other'] ) ) {
552 $options[ $params[
'other'] ] =
'other';
556 foreach ( explode(
"\n", $list ) as $option ) {
557 $value = trim( $option );
558 if ( $value ==
'' ) {
561 if ( substr( $value, 0, 1 ) ==
'*' && substr( $value, 1, 1 ) !=
'*' ) {
562 # A new group is starting...
563 $value = trim( substr( $value, 1 ) );
564 if ( $value !==
'' &&
566 ( !isset( $params[
'other'] ) || $value !== $params[
'other'] )
572 } elseif ( substr( $value, 0, 2 ) ==
'**' ) {
574 $opt = trim( substr( $value, 2 ) );
575 if ( $optgroup ===
false ) {
576 $options[$opt] = $opt;
578 $options[$optgroup][$opt] = $opt;
581 # groupless reason list
583 $options[$option] = $option;
601 foreach ( $options as $text => $value ) {
602 if ( is_array( $value ) ) {
603 $optionsOoui[] = [
'optgroup' => (string)$text ];
604 foreach ( $value as $text2 => $value2 ) {
605 $optionsOoui[] = [
'data' => (string)$value2,
'label' => (
string)$text2 ];
608 $optionsOoui[] = [
'data' => (string)$value,
'label' => (
string)$text ];
627 $s = self::openElement(
'fieldset', $attribs ) .
"\n";
630 $s .= self::element(
'legend',
null, $legend ) .
"\n";
635 $s .= self::closeElement(
'fieldset' ) .
"\n";
653 return self::element(
'textarea',
654 Html::getTextInputAttributes(
678 return $value->value;
680 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
695 foreach ( $args as &$arg ) {
696 $arg = self::encodeJsVar( $arg, $pretty );
697 if ( $arg ===
false ) {
702 return "$name(" . ( $pretty
703 ? (
' ' . implode(
', ', $args ) .
' ' )
704 : implode(
',', $args )
719 private static function isWellFormed( $text ) {
720 $parser = xml_parser_create(
"UTF-8" );
722 # case folding violates XML standard, turn it off
723 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
725 if ( !xml_parse( $parser, $text,
true ) ) {
730 xml_parser_free( $parser );
734 xml_parser_free( $parser );
749 Sanitizer::hackDocType() .
754 return self::isWellFormed( $html );
767 [
'"',
'>',
'<' ],
782 public static function buildForm( $fields, $submitLabel =
null, $submitAttribs = [] ) {
784 $form .=
"<table><tbody>";
786 foreach ( $fields as $labelmsg => $input ) {
787 $id =
"mw-$labelmsg";
788 $form .= self::openElement(
'tr', [
'id' => $id ] );
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' );
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' );
809 $form .=
"</tbody></table>";
820 public static function buildTable( $rows, $attribs = [], $headers =
null ) {
821 $s = self::openElement(
'table', $attribs );
823 if ( is_array( $headers ) ) {
824 $s .= self::openElement(
'thead', $attribs );
826 foreach ( $headers as $id =>
$header ) {
829 if ( is_string( $id ) ) {
830 $attribs[
'id'] = $id;
833 $s .= self::element(
'th', $attribs,
$header );
835 $s .= self::closeElement(
'thead' );
838 foreach ( $rows as $id => $row ) {
841 if ( is_string( $id ) ) {
842 $attribs[
'id'] = $id;
845 $s .= self::buildTableRow( $attribs, $row );
848 $s .= self::closeElement(
'table' );
860 $s = self::openElement(
'tr', $attribs );
862 foreach ( $cells as $id => $cell ) {
865 if ( is_string( $id ) ) {
866 $attribs[
'id'] = $id;
869 $s .= self::element(
'td', $attribs, $cell );
872 $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(...