33use UnexpectedValueException;
59 private static $voidElements = [
82 private static $boolAttribs = [
91 'formnovalidate' =>
true,
108 'typemustmatch' =>
true,
146 public static function addClass( &$classes,
string $class ): void {
147 $classes = (array)$classes;
149 foreach ( $classes as $key => $val ) {
151 ( is_int( $key ) && is_string( $val ) ) ||
152 ( is_string( $key ) && is_bool( $val ) )
157 wfWarn( __METHOD__ .
": Argument doesn't look like a class array: " . var_export( $classes,
true ) );
173 public static function linkButton( $text, array $attrs, array $modifiers = [] ) {
191 public static function submitButton( $contents, array $attrs = [], array $modifiers = [] ) {
192 $attrs[
'type'] =
'submit';
193 $attrs[
'value'] = $contents;
219 public static function rawElement( $element, $attribs = [], $contents =
'' ) {
220 $start = self::openElement( $element, $attribs );
221 if ( isset( self::$voidElements[$element] ) ) {
224 $contents = Sanitizer::escapeCombiningChar( $contents ??
'' );
225 return $start . $contents . self::closeElement( $element );
245 public static function element( $element, $attribs = [], $contents =
'' ) {
246 return self::rawElement(
249 strtr( $contents ??
'', [
270 $attribs = (array)$attribs;
273 $element = strtolower( $element );
277 if ( str_contains( $element,
' ' ) ) {
278 wfWarn( __METHOD__ .
" given element name with space '$element'" );
282 if ( $element ==
'input' ) {
297 'datetime-local' =>
true,
310 if ( isset( $attribs[
'type'] ) && !isset( $validTypes[$attribs[
'type']] ) ) {
311 unset( $attribs[
'type'] );
318 if ( $element ==
'button' && !isset( $attribs[
'type'] ) ) {
319 $attribs[
'type'] =
'submit';
322 return "<$element" . self::expandAttributes(
323 self::dropDefaults( $element, $attribs ) ) .
'>';
334 $element = strtolower( $element );
336 return "</$element>";
356 private static function dropDefaults( $element, array $attribs ) {
359 static $attribDefaults = [
360 'area' => [
'shape' =>
'rect' ],
362 'formaction' =>
'GET',
363 'formenctype' =>
'application/x-www-form-urlencoded',
371 'autocomplete' =>
'on',
372 'enctype' =>
'application/x-www-form-urlencoded',
375 'formaction' =>
'GET',
378 'keygen' => [
'keytype' =>
'rsa' ],
379 'link' => [
'media' =>
'all' ],
380 'menu' => [
'type' =>
'list' ],
381 'script' => [
'type' =>
'text/javascript' ],
384 'type' =>
'text/css',
386 'textarea' => [
'wrap' =>
'soft' ],
389 foreach ( $attribs as $attrib => $value ) {
390 if ( $attrib ===
'class' ) {
391 if ( $value ===
'' || $value === [] || $value === [
'' ] ) {
392 unset( $attribs[$attrib] );
394 } elseif ( isset( $attribDefaults[$element][$attrib] ) ) {
395 if ( is_array( $value ) ) {
396 $value = implode(
' ', $value );
398 $value = strval( $value );
400 if ( $attribDefaults[$element][$attrib] == $value ) {
401 unset( $attribs[$attrib] );
407 if ( $element ===
'link'
408 && isset( $attribs[
'type'] ) && strval( $attribs[
'type'] ) ==
'text/css'
410 unset( $attribs[
'type'] );
412 if ( $element ===
'input' ) {
413 $type = $attribs[
'type'] ??
null;
414 $value = $attribs[
'value'] ??
null;
415 if ( $type ===
'checkbox' || $type ===
'radio' ) {
419 if ( $value ===
'on' ) {
420 unset( $attribs[
'value'] );
422 } elseif ( $type ===
'submit' ) {
430 if ( $value ===
'' ) {
431 unset( $attribs[
'value'] );
435 if ( $element ===
'select' && isset( $attribs[
'size'] ) ) {
436 $multiple = ( $attribs[
'multiple'] ?? false ) !==
false ||
437 in_array(
'multiple', $attribs );
438 $default = $multiple ? 4 : 1;
439 if ( (
int)$attribs[
'size'] === $default ) {
440 unset( $attribs[
'size'] );
460 if ( is_array( $classes ) ) {
463 foreach ( $classes as $k => $v ) {
464 if ( is_string( $v ) ) {
467 if ( !isset( $classes[$v] ) ) {
471 foreach ( explode(
' ', $v ) as $part ) {
474 if ( $part !==
'' && $part !==
' ' ) {
475 $arrayValue[] = $part;
486 $arrayValue = explode(
' ', $classes );
489 $arrayValue = array_diff( $arrayValue, [
'',
' ' ] );
493 return implode(
' ', array_unique( $arrayValue ) );
536 foreach ( $attribs as $key => $value ) {
538 if ( $value ===
false || $value ===
null ) {
544 if ( is_int( $key ) && isset( self::$boolAttribs[strtolower( $value )] ) ) {
550 $key = strtolower( $key );
554 $spaceSeparatedListAttributes = [
564 if ( isset( $spaceSeparatedListAttributes[$key] ) ) {
566 $value = self::expandClassList( $value );
573 } elseif ( is_array( $value ) ) {
574 throw new UnexpectedValueException(
"HTML attribute $key can not contain a list of values" );
577 if ( isset( self::$boolAttribs[$key] ) ) {
578 $ret .=
" $key=\"\"";
583 $encValue = htmlspecialchars( $value, ENT_QUOTES );
587 $encValue = strtr( $encValue, [
592 $ret .=
" $key=\"$encValue\"";
612 if ( preg_match(
'/<\/?script/i', $contents ) ) {
613 wfLogWarning( __METHOD__ .
': Illegal character sequence found in inline script.' );
614 $contents =
'/* ERROR: Invalid script */';
617 return self::rawElement(
'script', [], $contents );
629 $attrs = [
'src' =>
$url ];
630 if ( $nonce !==
null ) {
631 $attrs[
'nonce'] = $nonce;
632 } elseif ( ContentSecurityPolicy::isNonceRequired( MediaWikiServices::getInstance()->getMainConfig() ) ) {
633 wfWarn(
"no nonce set on script. CSP will break it" );
636 return self::element(
'script', $attrs );
651 public static function inlineStyle( $contents, $media =
'all', $attribs = [] ) {
656 $contents = strtr( $contents, [
660 ']]>' =>
'\5D\5D\3E '
663 if ( preg_match(
'/[<&]/', $contents ) ) {
664 $contents =
"/*<![CDATA[*/$contents/*]]>*/";
667 return self::rawElement(
'style', [
669 ] + $attribs, $contents );
681 return self::element(
'link', [
682 'rel' =>
'stylesheet',
699 public static function input( $name, $value =
'', $type =
'text', array $attribs = [] ) {
700 $attribs[
'type'] = $type;
701 $attribs[
'value'] = $value;
702 $attribs[
'name'] = $name;
703 return self::element(
'input', $attribs );
714 public static function check( $name, $checked =
false, array $attribs = [] ) {
715 if ( isset( $attribs[
'value'] ) ) {
716 $value = $attribs[
'value'];
717 unset( $attribs[
'value'] );
723 $attribs[] =
'checked';
726 return self::input( $name, $value,
'checkbox', $attribs );
739 private static function messageBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
740 if ( $heading !==
'' ) {
741 $html = self::element(
'h2', [], $heading ) . $html;
743 self::addClass( $className,
'cdx-message' );
744 self::addClass( $className,
'cdx-message--block' );
745 return self::rawElement(
'div', [
'class' => $className ],
746 self::element(
'span', [
'class' => [
750 self::rawElement(
'div', [
751 'class' =>
'cdx-message__content'
771 public static function noticeBox( $html, $className, $heading =
'', $iconClassName =
'' ) {
772 return self::messageBox( $html, [
773 'cdx-message--notice',
775 ], $heading, $iconClassName );
792 public static function warningBox( $html, $className =
'' ) {
793 return self::messageBox( $html, [
794 'cdx-message--warning', $className ] );
812 public static function errorBox( $html, $heading =
'', $className =
'' ) {
813 return self::messageBox( $html, [
814 'cdx-message--error', $className ], $heading );
831 public static function successBox( $html, $className =
'' ) {
832 return self::messageBox( $html, [
833 'cdx-message--success', $className ] );
844 public static function radio( $name, $checked =
false, array $attribs = [] ) {
845 if ( isset( $attribs[
'value'] ) ) {
846 $value = $attribs[
'value'];
847 unset( $attribs[
'value'] );
853 $attribs[] =
'checked';
856 return self::input( $name, $value,
'radio', $attribs );
867 public static function label( $label, $id, array $attribs = [] ) {
871 return self::element(
'label', $attribs, $label );
883 public static function hidden( $name, $value, array $attribs = [] ) {
884 return self::input( $name, $value,
'hidden', $attribs );
899 public static function textarea( $name, $value =
'', array $attribs = [] ) {
900 $attribs[
'name'] = $name;
902 if ( str_starts_with( $value ??
'',
"\n" ) ) {
907 $spacedValue =
"\n" . $value;
909 $spacedValue = $value;
911 return self::element(
'textarea', $attribs, $spacedValue );
920 if ( !isset( $params[
'exclude'] ) || !is_array( $params[
'exclude'] ) ) {
921 $params[
'exclude'] = [];
924 if ( $params[
'in-user-lang'] ??
false ) {
928 $lang = MediaWikiServices::getInstance()->getContentLanguage();
932 if ( isset( $params[
'all'] ) ) {
935 $optionsOut[$params[
'all']] =
wfMessage(
'namespacesall' )->text();
938 $options = $lang->getFormattedNamespaces();
940 foreach ( $options as $nsId => $nsName ) {
941 if ( $nsId <
NS_MAIN || in_array( $nsId, $params[
'exclude'] ) ) {
945 isset( $params[
'include'] ) &&
946 is_array( $params[
'include'] ) &&
947 !in_array( $nsId, $params[
'include'] )
955 $nsName =
wfMessage(
'blanknamespace' )->text();
956 } elseif ( is_int( $nsId ) ) {
957 $converter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
958 ->getLanguageConverter( $lang );
959 $nsName = $converter->convertNamespace( $nsId );
961 $optionsOut[$nsId] = $nsName;
985 array $selectAttribs = []
987 ksort( $selectAttribs );
990 if ( isset( $params[
'selected'] ) ) {
993 if ( !is_int( $params[
'selected'] ) && ctype_digit( (
string)$params[
'selected'] ) ) {
994 $params[
'selected'] = (int)$params[
'selected'];
998 $params[
'selected'] =
'';
1001 if ( !isset( $params[
'disable'] ) || !is_array( $params[
'disable'] ) ) {
1002 $params[
'disable'] = [];
1006 $options = self::namespaceSelectorOptions( $params );
1010 foreach ( $options as $nsId => $nsName ) {
1011 $optionsHtml[] = self::element(
1014 'disabled' => in_array( $nsId, $params[
'disable'] ),
1016 'selected' => $nsId === $params[
'selected'],
1022 $selectAttribs[
'id'] ??=
'namespace';
1023 $selectAttribs[
'name'] ??=
'namespace';
1026 if ( isset( $params[
'label'] ) ) {
1027 $ret .= self::element(
1029 'for' => $selectAttribs[
'id'],
1035 $ret .= self::openElement(
'select', $selectAttribs )
1037 . implode(
"\n", $optionsHtml )
1039 . self::closeElement(
'select' );
1054 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
1055 $html5Version = $mainConfig->get( MainConfigNames::Html5Version );
1056 $mimeType = $mainConfig->get( MainConfigNames::MimeType );
1057 $xhtmlNamespaces = $mainConfig->get( MainConfigNames::XhtmlNamespaces );
1059 $isXHTML = self::isXmlMimeType( $mimeType );
1064 $ret .=
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
1067 $attribs[
'xmlns'] =
'http://www.w3.org/1999/xhtml';
1070 foreach ( $xhtmlNamespaces as $tag => $ns ) {
1071 $attribs[
"xmlns:$tag"] = $ns;
1074 $ret .=
"<!DOCTYPE html>\n";
1077 if ( $html5Version ) {
1078 $attribs[
'version'] = $html5Version;
1081 $ret .= self::openElement(
'html', $attribs );
1096 # * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
1097 return (
bool)preg_match(
'!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
1123 public static function srcSet( array $urls ) {
1125 foreach ( $urls as $density =>
$url ) {
1128 $density = (string)(
float)$density;
1129 $candidates[$density] =
$url;
1133 ksort( $candidates, SORT_NUMERIC );
1134 $candidates = array_unique( $candidates );
1137 foreach ( $candidates as $density =>
$url ) {
1138 $candidates[$density] =
$url .
' ' . $density .
'x';
1141 return implode(
", ", $candidates );
1160 return $value->value;
1162 return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
1180 $encodedArgs = self::encodeJsList( $args, $pretty );
1181 if ( $encodedArgs ===
false ) {
1184 return "$name($encodedArgs);";
1197 foreach ( $args as &$arg ) {
1198 $arg = self::encodeJsVar( $arg, $pretty );
1199 if ( $arg ===
false ) {
1204 return ' ' . implode(
', ', $args ) .
' ';
1206 return implode(
',', $args );
1226 if ( isset( $params[
'other'] ) ) {
1227 $options[ $params[
'other'] ] =
'other';
1231 foreach ( explode(
"\n", $list ) as $option ) {
1232 $value = trim( $option );
1233 if ( $value ==
'' ) {
1236 if ( str_starts_with( $value,
'*' ) && !str_starts_with( $value,
'**' ) ) {
1237 # A new group is starting...
1238 $value = trim( substr( $value, 1 ) );
1239 if ( $value !==
'' &&
1241 ( !isset( $params[
'other'] ) || $value !== $params[
'other'] )
1247 } elseif ( str_starts_with( $value,
'**' ) ) {
1249 $opt = trim( substr( $value, 2 ) );
1250 if ( $optgroup ===
false ) {
1251 $options[$opt] = $opt;
1253 $options[$optgroup][$opt] = $opt;
1256 # groupless reason list
1258 $options[$option] = $option;
1276 foreach ( $options as $text => $value ) {
1277 if ( is_array( $value ) ) {
1278 $optionsOoui[] = [
'optgroup' => (string)$text ];
1279 foreach ( $value as $text2 => $value2 ) {
1280 $optionsOoui[] = [
'data' => (string)$value2,
'label' => (
string)$text2 ];
1283 $optionsOoui[] = [
'data' => (string)$value,
'label' => (
string)$text ];
1287 return $optionsOoui;
1301 foreach ( $options as $text => $value ) {
1302 if ( is_array( $value ) ) {
1304 'label' => (string)$text,
1305 'items' => array_map(
static function ( $text2, $value2 ) {
1306 return [
'label' => (string)$text2,
'value' => (
string)$value2 ];
1307 }, array_keys( $value ), $value )
1310 $optionsCodex[] = [
'label' => (string)$text,
'value' => (
string)$value ];
1313 return $optionsCodex;
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.
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
if(!defined('MW_SETUP_CALLBACK'))
A class containing constants representing the names of configuration variables.
Handle sending Content-Security-Policy headers.