3 namespace MediaWiki\HTMLForm\Field
;
5 use MediaWiki\MediaWikiServices
;
6 use MediaWiki\User\ExternalUserNames
;
7 use MediaWiki\Widget\UserInputWidget
;
11 * Implements a text input field for user names.
12 * Automatically auto-completes if using the OOUI display format.
14 * Optional parameters:
15 * 'exists' - Whether to validate that the user already exists
16 * 'external' - Whether an external user (imported actor) is interpreted as "valid"
17 * 'ipallowed' - Whether an IP address is interpreted as "valid"
18 * 'usemodwiki-ipallowed' - Whether an IP address in the usemod wiki format (e.g. 300.300.300.xxx) is accepted. The
19 * 'ipallowed' parameter must be set to true if this parameter is set to true.
20 * 'iprange' - Whether an IP address range is interpreted as "valid"
21 * 'iprangelimits' - Specifies the valid IP ranges for IPv4 and IPv6 in an array.
22 * 'excludenamed' - Whether to exclude named users or not.
23 * 'excludetemp' - Whether to exclude temporary users or not.
28 class HTMLUserTextField
extends HTMLTextField
{
33 public function __construct( $params ) {
34 $params = wfArrayPlus2d( $params, [
38 'usemodwiki-ipallowed' => false,
44 'excludenamed' => false,
45 'excludetemp' => false,
49 parent
::__construct( $params );
52 public function validate( $value, $alldata ) {
53 // If the value is null, reset it to an empty string which is what is expected by the parent.
56 // If the value is empty, there are no additional checks that can be performed.
57 if ( $value === '' ) {
58 return parent
::validate( $value, $alldata );
61 // check if the input is a valid username
62 $user = MediaWikiServices
::getInstance()->getUserFactory()->newFromName( $value );
64 // check if the user exists, if requested
65 if ( $this->mParams
['exists'] && !(
66 $user->isRegistered() &&
67 // Treat hidden users as unregistered if current user can't view them (T309894)
68 !( $user->isHidden() && !( $this->mParent
&& $this->mParent
->getUser()->isAllowed( 'hideuser' ) ) )
70 return $this->msg( 'htmlform-user-not-exists', $user->getName() );
73 // check if the user account type matches the account type filter
74 $excludeNamed = $this->mParams
['excludenamed'] ??
null;
75 $excludeTemp = $this->mParams
['excludetemp'] ??
null;
76 if ( ( $excludeTemp && $user->isTemp() ) ||
( $excludeNamed && $user->isNamed() ) ) {
77 return $this->msg( 'htmlform-user-not-valid', $user->getName() );
80 // not a valid username
82 // check if the input is a valid external user
83 if ( $this->mParams
['external'] && ExternalUserNames
::isExternal( $value ) ) {
86 // check if the input is a valid IP address, optionally also checking for usemod wiki IPs
87 if ( $this->mParams
['ipallowed'] ) {
88 $b = IPUtils
::RE_IP_BYTE
;
89 if ( IPUtils
::isValid( $value ) ) {
91 } elseif ( $this->mParams
['usemodwiki-ipallowed'] && preg_match( "/^$b\.$b\.$b\.xxx$/", $value ) ) {
95 // check if the input is a valid IP range
96 if ( $this->mParams
['iprange'] ) {
97 $rangeError = $this->isValidIPRange( $value );
98 if ( $rangeError === true ) {
100 } elseif ( $rangeError !== false ) {
105 return $this->msg( 'htmlform-user-not-valid', $value );
109 return parent
::validate( $value, $alldata );
112 protected function isValidIPRange( $value ) {
113 $cidrIPRanges = $this->mParams
['iprangelimits'];
115 if ( !IPUtils
::isValidRange( $value ) ) {
119 [ $ip, $range ] = explode( '/', $value, 2 );
122 ( IPUtils
::isIPv4( $ip ) && $cidrIPRanges['IPv4'] == 32 ) ||
123 ( IPUtils
::isIPv6( $ip ) && $cidrIPRanges['IPv6'] == 128 )
125 // Range block effectively disabled
126 return $this->msg( 'ip_range_toolow' );
130 ( IPUtils
::isIPv4( $ip ) && $range > 32 ) ||
131 ( IPUtils
::isIPv6( $ip ) && $range > 128 )
134 return $this->msg( 'ip_range_invalid' );
137 if ( IPUtils
::isIPv4( $ip ) && $range < $cidrIPRanges['IPv4'] ) {
138 return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv4'] );
141 if ( IPUtils
::isIPv6( $ip ) && $range < $cidrIPRanges['IPv6'] ) {
142 return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv6'] );
148 protected function getInputWidget( $params ) {
149 if ( isset( $this->mParams
['excludenamed'] ) ) {
150 $params['excludenamed'] = $this->mParams
['excludenamed'];
153 if ( isset( $this->mParams
['excludetemp'] ) ) {
154 $params['excludetemp'] = $this->mParams
['excludetemp'];
157 return new UserInputWidget( $params );
160 protected function shouldInfuseOOUI() {
164 protected function getOOUIModules() {
165 return [ 'mediawiki.widgets.UserInputWidget' ];
168 public function getInputHtml( $value ) {
169 // add the required module and css class for user suggestions in non-OOUI mode
170 $this->mParent
->getOutput()->addModules( 'mediawiki.userSuggest' );
171 $this->mClass
.= ' mw-autocomplete-user';
173 // return parent html
174 return parent
::getInputHTML( $value );
178 /** @deprecated class alias since 1.42 */
179 class_alias( HTMLUserTextField
::class, 'HTMLUserTextField' );