53 private static $voidElements = [
76 private static $boolAttribs = [
85 'formnovalidate' =>
true,
102 'typemustmatch' =>
true,
114 $useMediaWikiUIEverywhere = MediaWikiServices::getInstance()
115 ->getMainConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere );
116 if ( $useMediaWikiUIEverywhere ) {
117 if ( isset( $attrs[
'class'] ) ) {
118 if ( is_array( $attrs[
'class'] ) ) {
119 $attrs[
'class'][] =
'mw-ui-button';
120 $attrs[
'class'] = array_merge( $attrs[
'class'], $modifiers );
122 $attrs[
'class'] = implode(
' ', $attrs[
'class'] );
124 $attrs[
'class'] .=
' mw-ui-button ' . implode(
' ', $modifiers );
128 $attrs[
'class'] =
'mw-ui-button ' . implode(
' ', $modifiers );
142 $useMediaWikiUIEverywhere = MediaWikiServices::getInstance()
143 ->getMainConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere );
144 if ( $useMediaWikiUIEverywhere ) {
145 if ( isset( $attrs[
'class'] ) ) {
146 if ( is_array( $attrs[
'class'] ) ) {
147 $attrs[
'class'][] =
'mw-ui-input';
149 $attrs[
'class'] .=
' mw-ui-input';
152 $attrs[
'class'] =
'mw-ui-input';
170 public static function linkButton( $text, array $attrs, array $modifiers = [] ) {
171 return self::element(
'a',
172 self::buttonAttributes( $attrs, $modifiers ),
190 public static function submitButton( $contents, array $attrs, array $modifiers = [] ) {
191 $attrs[
'type'] =
'submit';
192 $attrs[
'value'] = $contents;
193 return self::element(
'input', self::buttonAttributes( $attrs, $modifiers ) );
214 public static function rawElement( $element, $attribs = [], $contents =
'' ) {
215 $start = self::openElement( $element, $attribs );
216 if ( isset( self::$voidElements[$element] ) ) {
218 return substr( $start, 0, -1 ) .
'/>';
220 return $start . $contents . self::closeElement( $element );
236 public static function element( $element, $attribs = [], $contents =
'' ) {
237 return self::rawElement( $element, $attribs, strtr( $contents ??
'', [
257 $attribs = (array)$attribs;
260 $element = strtolower( $element );
264 if ( strpos( $element,
' ' ) !==
false ) {
265 wfWarn( __METHOD__ .
" given element name with space '$element'" );
269 if ( $element ==
'input' ) {
284 'datetime-local' =>
true,
297 if ( isset( $attribs[
'type'] ) && !isset( $validTypes[$attribs[
'type']] ) ) {
298 unset( $attribs[
'type'] );
305 if ( $element ==
'button' && !isset( $attribs[
'type'] ) ) {
306 $attribs[
'type'] =
'submit';
309 return "<$element" . self::expandAttributes(
310 self::dropDefaults( $element, $attribs ) ) .
'>';
321 $element = strtolower( $element );
323 return "</$element>";
343 private static function dropDefaults( $element, array $attribs ) {
346 static $attribDefaults = [
347 'area' => [
'shape' =>
'rect' ],
349 'formaction' =>
'GET',
350 'formenctype' =>
'application/x-www-form-urlencoded',
358 'autocomplete' =>
'on',
359 'enctype' =>
'application/x-www-form-urlencoded',
362 'formaction' =>
'GET',
365 'keygen' => [
'keytype' =>
'rsa' ],
366 'link' => [
'media' =>
'all' ],
367 'menu' => [
'type' =>
'list' ],
368 'script' => [
'type' =>
'text/javascript' ],
371 'type' =>
'text/css',
373 'textarea' => [
'wrap' =>
'soft' ],
376 foreach ( $attribs as $attrib => $value ) {
377 if ( $attrib ===
'class' ) {
378 if ( $value ===
'' || $value === [] || $value === [
'' ] ) {
379 unset( $attribs[$attrib] );
381 } elseif ( isset( $attribDefaults[$element][$attrib] ) ) {
382 if ( is_array( $value ) ) {
383 $value = implode(
' ', $value );
385 $value = strval( $value );
387 if ( $attribDefaults[$element][$attrib] == $value ) {
388 unset( $attribs[$attrib] );
394 if ( $element ===
'link'
395 && isset( $attribs[
'type'] ) && strval( $attribs[
'type'] ) ==
'text/css'
397 unset( $attribs[
'type'] );
399 if ( $element ===
'input' ) {
400 $type = $attribs[
'type'] ??
null;
401 $value = $attribs[
'value'] ??
null;
402 if (
$type ===
'checkbox' ||
$type ===
'radio' ) {
406 if ( $value ===
'on' ) {
407 unset( $attribs[
'value'] );
409 } elseif (
$type ===
'submit' ) {
417 if ( $value ===
'' ) {
418 unset( $attribs[
'value'] );
422 if ( $element ===
'select' && isset( $attribs[
'size'] ) ) {
423 if ( in_array(
'multiple', $attribs )
424 || ( isset( $attribs[
'multiple'] ) && $attribs[
'multiple'] !==
false )
427 if ( strval( $attribs[
'size'] ) ==
'4' ) {
428 unset( $attribs[
'size'] );
432 if ( strval( $attribs[
'size'] ) ==
'1' ) {
433 unset( $attribs[
'size'] );
482 foreach ( $attribs as $key => $value ) {
484 if ( $value ===
false || $value ===
null ) {
490 if ( is_int( $key ) && isset( self::$boolAttribs[strtolower( $value )] ) ) {
496 $key = strtolower( $key );
500 $spaceSeparatedListAttributes = [
510 if ( isset( $spaceSeparatedListAttributes[$key] ) ) {
515 if ( is_array( $value ) ) {
518 foreach ( $value as $k => $v ) {
519 if ( is_string( $v ) ) {
522 if ( !isset( $value[$v] ) ) {
526 foreach ( explode(
' ', $v ) as $part ) {
529 if ( $part !==
'' && $part !==
' ' ) {
530 $arrayValue[] = $part;
541 $arrayValue = explode(
' ', $value );
544 $arrayValue = array_diff( $arrayValue, [
'',
' ' ] );
548 $value = implode(
' ', array_unique( $arrayValue ) );
555 } elseif ( is_array( $value ) ) {
556 throw new MWException(
"HTML attribute $key can not contain a list of values" );
559 if ( isset( self::$boolAttribs[$key] ) ) {
560 $ret .=
" $key=\"\"";
565 $encValue = htmlspecialchars( $value, ENT_QUOTES );
569 $encValue = strtr( $encValue, [
574 $ret .=
" $key=\"$encValue\"";
595 if ( $nonce !==
null ) {
596 $attrs[
'nonce'] = $nonce;
598 wfWarn(
"no nonce set on script. CSP will break it" );
601 if ( preg_match(
'/<\/?script/i', $contents ) ) {
602 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
603 $contents =
'/* ERROR: Invalid script */';
606 return self::rawElement(
'script', $attrs, $contents );
618 $attrs = [
'src' => $url ];
619 if ( $nonce !==
null ) {
620 $attrs[
'nonce'] = $nonce;
622 wfWarn(
"no nonce set on script. CSP will break it" );
625 return self::element(
'script', $attrs );
640 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
645 $contents = strtr( $contents, [
649 ']]>' =>
'\5D\5D\3E '
652 if ( preg_match(
'/[<&]/', $contents ) ) {
653 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
656 return self::rawElement(
'style', [
658 ] + $attribs, $contents );
670 return self::element(
'link', [
671 'rel' =>
'stylesheet',
688 public static function input( $name, $value =
'',
$type =
'text', array $attribs = [] ) {
689 $attribs[
'type'] =
$type;
690 $attribs[
'value'] = $value;
691 $attribs[
'name'] = $name;
692 $textInputAttributes = [
699 if ( isset( $textInputAttributes[
$type] ) ) {
700 $attribs = self::getTextInputAttributes( $attribs );
702 $buttonAttributes = [
707 if ( isset( $buttonAttributes[
$type] ) ) {
708 $attribs = self::buttonAttributes( $attribs );
710 return self::element(
'input', $attribs );
721 public static function check( $name, $checked =
false, array $attribs = [] ) {
722 if ( isset( $attribs[
'value'] ) ) {
723 $value = $attribs[
'value'];
724 unset( $attribs[
'value'] );
730 $attribs[] =
'checked';
733 return self::input( $name, $value,
'checkbox', $attribs );
744 private static function messageBox( $html, $className, $heading =
'' ) {
745 if ( $heading !==
'' ) {
746 $html = self::element(
'h2', [], $heading ) . $html;
748 if ( is_array( $className ) ) {
749 $className[] =
'mw-message-box';
751 $className .=
' mw-message-box';
753 return self::rawElement(
'div', [
'class' => $className ], $html );
764 return self::messageBox( $html, [
'mw-message-box-notice', $className ] );
775 public static function warningBox( $html, $className =
'' ) {
776 return self::messageBox( $html, [
'mw-message-box-warning', $className ] );
788 public static function errorBox( $html, $heading =
'', $className =
'' ) {
789 return self::messageBox( $html, [
'mw-message-box-error', $className ], $heading );
800 public static function successBox( $html, $className =
'' ) {
801 return self::messageBox( $html, [
'mw-message-box-success', $className ] );
812 public static function radio( $name, $checked =
false, array $attribs = [] ) {
813 if ( isset( $attribs[
'value'] ) ) {
814 $value = $attribs[
'value'];
815 unset( $attribs[
'value'] );
821 $attribs[] =
'checked';
824 return self::input( $name, $value,
'radio', $attribs );
835 public static function label( $label, $id, array $attribs = [] ) {
839 return self::element(
'label', $attribs, $label );
851 public static function hidden( $name, $value, array $attribs = [] ) {
852 return self::input( $name, $value,
'hidden', $attribs );
867 public static function textarea( $name, $value =
'', array $attribs = [] ) {
868 $attribs[
'name'] = $name;
870 if ( substr( $value, 0, 1 ) ==
"\n" ) {
875 $spacedValue =
"\n" . $value;
877 $spacedValue = $value;
879 return self::element(
'textarea', self::getTextInputAttributes( $attribs ), $spacedValue );
888 if ( !isset( $params[
'exclude'] ) || !is_array( $params[
'exclude'] ) ) {
889 $params[
'exclude'] = [];
892 if ( $params[
'in-user-lang'] ??
false ) {
896 $lang = MediaWikiServices::getInstance()->getContentLanguage();
900 if ( isset( $params[
'all'] ) ) {
903 $optionsOut[$params[
'all']] =
wfMessage(
'namespacesall' )->text();
906 $options =
$lang->getFormattedNamespaces();
908 foreach ( $options as $nsId => $nsName ) {
909 if ( $nsId <
NS_MAIN || in_array( $nsId, $params[
'exclude'] ) ) {
915 $nsName =
wfMessage(
'blanknamespace' )->text();
916 } elseif ( is_int( $nsId ) ) {
917 $converter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
918 ->getLanguageConverter(
$lang );
919 $nsName = $converter->convertNamespace( $nsId );
921 $optionsOut[$nsId] = $nsName;
944 array $selectAttribs = []
946 ksort( $selectAttribs );
949 if ( isset( $params[
'selected'] ) ) {
952 if ( !is_int( $params[
'selected'] ) && ctype_digit( (
string)$params[
'selected'] ) ) {
953 $params[
'selected'] = (int)$params[
'selected'];
957 $params[
'selected'] =
'';
960 if ( !isset( $params[
'disable'] ) || !is_array( $params[
'disable'] ) ) {
961 $params[
'disable'] = [];
965 $options = self::namespaceSelectorOptions( $params );
969 foreach ( $options as $nsId => $nsName ) {
970 $optionsHtml[] = self::element(
972 'disabled' => in_array( $nsId, $params[
'disable'] ),
974 'selected' => $nsId === $params[
'selected'],
979 if ( !array_key_exists(
'id', $selectAttribs ) ) {
980 $selectAttribs[
'id'] =
'namespace';
983 if ( !array_key_exists(
'name', $selectAttribs ) ) {
984 $selectAttribs[
'name'] =
'namespace';
988 if ( isset( $params[
'label'] ) ) {
989 $ret .= self::element(
991 'for' => $selectAttribs[
'id'] ??
null,
997 $ret .= self::openElement(
'select', $selectAttribs )
999 . implode(
"\n", $optionsHtml )
1001 . self::closeElement(
'select' );
1016 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
1017 $html5Version = $mainConfig->get( MainConfigNames::Html5Version );
1018 $mimeType = $mainConfig->get( MainConfigNames::MimeType );
1019 $xhtmlNamespaces = $mainConfig->get( MainConfigNames::XhtmlNamespaces );
1021 $isXHTML = self::isXmlMimeType( $mimeType );
1026 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1029 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1032 foreach ( $xhtmlNamespaces as $tag => $ns ) {
1033 $attribs[
"xmlns:$tag"] = $ns;
1036 $ret .=
"<!DOCTYPE html>\n";
1039 if ( $html5Version ) {
1040 $attribs[
'version'] = $html5Version;
1043 $ret .= self::openElement(
'html', $attribs );
1058 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1059 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1085 public static function srcSet( array $urls ) {
1087 foreach ( $urls as $density => $url ) {
1090 $density = (string)(
float)$density;
1091 $candidates[$density] = $url;
1095 ksort( $candidates, SORT_NUMERIC );
1096 $candidates = array_unique( $candidates );
1099 foreach ( $candidates as $density => $url ) {
1100 $candidates[$density] = $url .
' ' . $density .
'x';
1103 return implode(
", ", $candidates );
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode $wgLang
static isNonceRequired(Config $config)
Should we set nonce attribute.
This class is a collection of static functions that serve two purposes:
static inlineScript( $contents, $nonce=null)
Output an HTML script tag with the given contents.
static label( $label, $id, array $attribs=[])
Convenience function for generating a label for inputs.
static textarea( $name, $value='', array $attribs=[])
Convenience function to produce a <textarea> element.
static namespaceSelectorOptions(array $params=[])
Helper for Html::namespaceSelector().
static linkedScript( $url, $nonce=null)
Output a "<script>" tag linking to the given URL, e.g., "<script src=foo.js></script>".
static buttonAttributes(array $attrs, array $modifiers=[])
Modifies a set of attributes meant for button elements and apply a set of default attributes when $wg...
static linkedStyle( $url, $media='all')
Output a "<link rel=stylesheet>" linking to the given URL for the given media type (if any).
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
static isXmlMimeType( $mimetype)
Determines if the given MIME type is xml.
static htmlHeader(array $attribs=[])
Constructs the opening html-tag with necessary doctypes depending on global variables.
static input( $name, $value='', $type='text', array $attribs=[])
Convenience function to produce an "<input>" element.
static submitButton( $contents, array $attrs, array $modifiers=[])
Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enab...
static getTextInputAttributes(array $attrs)
Modifies a set of attributes meant for text input elements and apply a set of default attributes.
static radio( $name, $checked=false, array $attribs=[])
Convenience function to produce a radio button (input element with type=radio)
static expandAttributes(array $attribs)
Given an associative array of element attributes, generate a string to stick after the element name i...
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
static warningBox( $html, $className='')
Return a warning box.
static namespaceSelector(array $params=[], array $selectAttribs=[])
Build a drop-down box for selecting a namespace.
static noticeBox( $html, $className)
Return the HTML for a notice message box.
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
static linkButton( $text, array $attrs, array $modifiers=[])
Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enab...
static check( $name, $checked=false, array $attribs=[])
Convenience function to produce a checkbox (input element with type=checkbox)
static srcSet(array $urls)
Generate a srcset attribute value.
static successBox( $html, $className='')
Return a success box.
static errorBox( $html, $heading='', $className='')
Return an error box.
static closeElement( $element)
Returns "</$element>".
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
static inlineStyle( $contents, $media='all', $attribs=[])
Output a "<style>" tag with the given contents for the given media type (if any).
A class containing constants representing the names of configuration variables.
if(!isset( $args[0])) $lang