113 if ( isset( $attrs[
'class'] ) ) {
114 if ( is_array( $attrs[
'class'] ) ) {
115 $attrs[
'class'][] =
'mw-ui-button';
116 $attrs[
'class'] = array_merge( $attrs[
'class'], $modifiers );
118 $attrs[
'class'] = implode(
' ', $attrs[
'class'] );
120 $attrs[
'class'] .=
' mw-ui-button ' . implode(
' ', $modifiers );
124 $attrs[
'class'] =
'mw-ui-button ' . implode(
' ', $modifiers );
140 if ( isset( $attrs[
'class'] ) ) {
141 if ( is_array( $attrs[
'class'] ) ) {
142 $attrs[
'class'][] =
'mw-ui-input';
144 $attrs[
'class'] .=
' mw-ui-input';
147 $attrs[
'class'] =
'mw-ui-input';
165 public static function linkButton( $text, array $attrs, array $modifiers = [] ) {
166 return self::element(
'a',
167 self::buttonAttributes( $attrs, $modifiers ),
185 public static function submitButton( $contents, array $attrs, array $modifiers = [] ) {
186 $attrs[
'type'] =
'submit';
187 $attrs[
'value'] = $contents;
188 return self::element(
'input', self::buttonAttributes( $attrs, $modifiers ) );
209 public static function rawElement( $element, $attribs = [], $contents =
'' ) {
210 $start = self::openElement( $element, $attribs );
211 if ( in_array( $element, self::$voidElements ) ) {
213 return substr( $start, 0, -1 ) .
'/>';
215 return $start . $contents . self::closeElement( $element );
231 public static function element( $element, $attribs = [], $contents =
'' ) {
232 return self::rawElement( $element, $attribs, strtr( $contents ??
'', [
252 $attribs = (array)$attribs;
255 $element = strtolower( $element );
259 if ( strpos( $element,
' ' ) !==
false ) {
260 wfWarn( __METHOD__ .
" given element name with space '$element'" );
264 if ( $element ==
'input' ) {
292 if ( isset( $attribs[
'type'] ) && !in_array( $attribs[
'type'], $validTypes ) ) {
293 unset( $attribs[
'type'] );
300 if ( $element ==
'button' && !isset( $attribs[
'type'] ) ) {
301 $attribs[
'type'] =
'submit';
304 return "<$element" . self::expandAttributes(
305 self::dropDefaults( $element, $attribs ) ) .
'>';
316 $element = strtolower( $element );
318 return "</$element>";
341 static $attribDefaults = [
342 'area' => [
'shape' =>
'rect' ],
344 'formaction' =>
'GET',
345 'formenctype' =>
'application/x-www-form-urlencoded',
353 'autocomplete' =>
'on',
354 'enctype' =>
'application/x-www-form-urlencoded',
357 'formaction' =>
'GET',
360 'keygen' => [
'keytype' =>
'rsa' ],
361 'link' => [
'media' =>
'all' ],
362 'menu' => [
'type' =>
'list' ],
363 'script' => [
'type' =>
'text/javascript' ],
366 'type' =>
'text/css',
368 'textarea' => [
'wrap' =>
'soft' ],
371 $element = strtolower( $element );
373 foreach ( $attribs as $attrib => $value ) {
374 $lcattrib = strtolower( $attrib );
375 if ( is_array( $value ) ) {
376 $value = implode(
' ', $value );
378 $value = strval( $value );
382 if ( isset( $attribDefaults[$element][$lcattrib] )
383 && $attribDefaults[$element][$lcattrib] == $value
385 unset( $attribs[$attrib] );
388 if ( $lcattrib ==
'class' && $value ==
'' ) {
389 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 ) && in_array( strtolower( $value ), self::$boolAttribs ) ) {
496 $key = strtolower( $key );
500 $spaceSeparatedListAttributes = [
510 if ( in_array( $key, $spaceSeparatedListAttributes ) ) {
515 if ( is_array( $value ) ) {
518 foreach ( $value as $k => $v ) {
519 if ( is_string( $v ) ) {
522 if ( !isset( $value[$v] ) ) {
534 $value = implode(
' ', $newValue );
536 $value = explode(
' ', $value );
540 $value = array_diff( $value, [
'',
' ' ] );
543 $value = implode(
' ', array_unique( $value ) );
544 } elseif ( is_array( $value ) ) {
545 throw new MWException(
"HTML attribute $key can not contain a list of values" );
550 if ( in_array( $key, self::$boolAttribs ) ) {
551 $ret .=
" $key=\"\"";
553 $ret .=
" $key=$quote" . Sanitizer::encodeAttribute( $value ) . $quote;
574 if ( $nonce !==
null ) {
575 $attrs[
'nonce'] = $nonce;
577 wfWarn(
"no nonce set on script. CSP will break it" );
580 if ( preg_match(
'/<\/?script/i', $contents ) ) {
581 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
582 $contents =
'/* ERROR: Invalid script */';
585 return self::rawElement(
'script', $attrs, $contents );
597 $attrs = [
'src' => $url ];
598 if ( $nonce !==
null ) {
599 $attrs[
'nonce'] = $nonce;
601 wfWarn(
"no nonce set on script. CSP will break it" );
604 return self::element(
'script', $attrs );
619 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
624 $contents = strtr( $contents, [
628 ']]>' =>
'\5D\5D\3E '
631 if ( preg_match(
'/[<&]/', $contents ) ) {
632 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
635 return self::rawElement(
'style', [
637 ] + $attribs, $contents );
649 return self::element(
'link', [
650 'rel' =>
'stylesheet',
667 public static function input( $name, $value =
'',
$type =
'text', array $attribs = [] ) {
668 $attribs[
'type'] =
$type;
669 $attribs[
'value'] = $value;
670 $attribs[
'name'] = $name;
671 if ( in_array(
$type, [
'text',
'search',
'email',
'password',
'number' ] ) ) {
672 $attribs = self::getTextInputAttributes( $attribs );
674 if ( in_array(
$type, [
'button',
'reset',
'submit' ] ) ) {
675 $attribs = self::buttonAttributes( $attribs );
677 return self::element(
'input', $attribs );
688 public static function check( $name, $checked =
false, array $attribs = [] ) {
689 if ( isset( $attribs[
'value'] ) ) {
690 $value = $attribs[
'value'];
691 unset( $attribs[
'value'] );
697 $attribs[] =
'checked';
700 return self::input( $name, $value,
'checkbox', $attribs );
711 private static function messageBox( $html, $className, $heading =
'' ) {
712 if ( $heading !==
'' ) {
713 $html = self::element(
'h2', [], $heading ) . $html;
715 return self::rawElement(
'div', [
'class' => $className ], $html );
726 public static function warningBox( $html, $className =
'' ) {
727 return self::messageBox( $html, [
'warningbox', $className ] );
739 public static function errorBox( $html, $heading =
'', $className =
'' ) {
740 return self::messageBox( $html, [
'errorbox', $className ], $heading );
751 public static function successBox( $html, $className =
'' ) {
752 return self::messageBox( $html, [
'successbox', $className ] );
763 public static function radio( $name, $checked =
false, array $attribs = [] ) {
764 if ( isset( $attribs[
'value'] ) ) {
765 $value = $attribs[
'value'];
766 unset( $attribs[
'value'] );
772 $attribs[] =
'checked';
775 return self::input( $name, $value,
'radio', $attribs );
786 public static function label( $label, $id, array $attribs = [] ) {
790 return self::element(
'label', $attribs, $label );
802 public static function hidden( $name, $value, array $attribs = [] ) {
803 return self::input( $name, $value,
'hidden', $attribs );
818 public static function textarea( $name, $value =
'', array $attribs = [] ) {
819 $attribs[
'name'] = $name;
821 if ( substr( $value, 0, 1 ) ==
"\n" ) {
826 $spacedValue =
"\n" . $value;
828 $spacedValue = $value;
830 return self::element(
'textarea', self::getTextInputAttributes( $attribs ), $spacedValue );
839 if ( !isset( $params[
'exclude'] ) || !is_array( $params[
'exclude'] ) ) {
840 $params[
'exclude'] = [];
843 if ( $params[
'in-user-lang'] ??
false ) {
847 $lang = MediaWikiServices::getInstance()->getContentLanguage();
851 if ( isset( $params[
'all'] ) ) {
854 $optionsOut[$params[
'all']] =
wfMessage(
'namespacesall' )->text();
857 $options =
$lang->getFormattedNamespaces();
859 foreach ( $options as $nsId => $nsName ) {
860 if ( $nsId <
NS_MAIN || in_array( $nsId, $params[
'exclude'] ) ) {
866 $nsName =
wfMessage(
'blanknamespace' )->text();
867 } elseif ( is_int( $nsId ) ) {
868 $converter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
869 ->getLanguageConverter(
$lang );
870 $nsName = $converter->convertNamespace( $nsId );
872 $optionsOut[$nsId] = $nsName;
895 array $selectAttribs = []
897 ksort( $selectAttribs );
900 if ( isset( $params[
'selected'] ) ) {
903 if ( !is_int( $params[
'selected'] ) && ctype_digit( (
string)$params[
'selected'] ) ) {
904 $params[
'selected'] = (int)$params[
'selected'];
908 $params[
'selected'] =
'';
911 if ( !isset( $params[
'disable'] ) || !is_array( $params[
'disable'] ) ) {
912 $params[
'disable'] = [];
916 $options = self::namespaceSelectorOptions( $params );
920 foreach ( $options as $nsId => $nsName ) {
921 $optionsHtml[] = self::element(
923 'disabled' => in_array( $nsId, $params[
'disable'] ),
925 'selected' => $nsId === $params[
'selected'],
930 if ( !array_key_exists(
'id', $selectAttribs ) ) {
931 $selectAttribs[
'id'] =
'namespace';
934 if ( !array_key_exists(
'name', $selectAttribs ) ) {
935 $selectAttribs[
'name'] =
'namespace';
939 if ( isset( $params[
'label'] ) ) {
940 $ret .= self::element(
942 'for' => $selectAttribs[
'id'] ??
null,
948 $ret .= self::openElement(
'select', $selectAttribs )
950 . implode(
"\n", $optionsHtml )
952 . self::closeElement(
'select' );
975 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
978 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
982 $attribs[
"xmlns:$tag"] = $ns;
985 $ret .=
"<!DOCTYPE html>\n";
992 $ret .= self::openElement(
'html', $attribs );
1007 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1008 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1021 public static function infoBox( $rawHtml, $icon, $alt, $class =
'' ) {
1022 $s = self::openElement(
'div', [
'class' =>
"mw-infobox $class" ] );
1024 $s .= self::openElement(
'div', [
'class' =>
'mw-infobox-left' ] ) .
1025 self::element(
'img',
1031 self::closeElement(
'div' );
1033 $s .= self::openElement(
'div', [
'class' =>
'mw-infobox-right' ] ) .
1035 self::closeElement(
'div' );
1036 $s .= self::element(
'div', [
'style' =>
'clear: left;' ],
' ' );
1038 $s .= self::closeElement(
'div' );
1040 $s .= self::element(
'div', [
'style' =>
'clear: left;' ],
' ' );
1068 public static function srcSet( array $urls ) {
1070 foreach ( $urls as $density => $url ) {
1073 $density = (string)(
float)$density;
1074 $candidates[$density] = $url;
1078 ksort( $candidates, SORT_NUMERIC );
1079 $candidates = array_unique( $candidates );
1082 foreach ( $candidates as $density => $url ) {
1083 $candidates[$density] = $url .
' ' . $density .
'x';
1086 return implode(
", ", $candidates );
$wgMimeType
The default Content-Type header.
$wgUseMediaWikiUIEverywhere
Temporary variable that applies MediaWiki UI wherever it can be supported.
$wgHtml5Version
Defines the value of the version attribute in the <html> tag, if any.
$wgXhtmlNamespaces
Permit other namespaces in addition to the w3.org default.
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.
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 messageBox( $html, $className, $heading='')
Return the HTML for a message box.
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 dropDefaults( $element, array $attribs)
Given an element name and an associative array of element attributes, return an array that is functio...
static infoBox( $rawHtml, $icon, $alt, $class='')
Get HTML for an information message box with an icon.
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 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).
if(!isset( $args[0])) $lang