Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / htmlform / fields / HTMLUserTextField.php
blobd19c50c9f71916fb255dab7b177ebf8a116fdbcd
1 <?php
3 namespace MediaWiki\HTMLForm\Field;
5 use MediaWiki\MediaWikiServices;
6 use MediaWiki\User\ExternalUserNames;
7 use MediaWiki\Widget\UserInputWidget;
8 use Wikimedia\IPUtils;
10 /**
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.
25 * @stable to extend
26 * @since 1.26
28 class HTMLUserTextField extends HTMLTextField {
29 /**
30 * @stable to call
31 * @inheritDoc
33 public function __construct( $params ) {
34 $params = wfArrayPlus2d( $params, [
35 'exists' => false,
36 'external' => false,
37 'ipallowed' => false,
38 'usemodwiki-ipallowed' => false,
39 'iprange' => false,
40 'iprangelimits' => [
41 'IPv4' => 0,
42 'IPv6' => 0,
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.
54 $value ??= '';
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 );
63 if ( $user ) {
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' ) ) )
69 ) ) {
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() );
79 } else {
80 // not a valid username
81 $valid = false;
82 // check if the input is a valid external user
83 if ( $this->mParams['external'] && ExternalUserNames::isExternal( $value ) ) {
84 $valid = true;
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 ) ) {
90 $valid = true;
91 } elseif ( $this->mParams['usemodwiki-ipallowed'] && preg_match( "/^$b\.$b\.$b\.xxx$/", $value ) ) {
92 $valid = true;
95 // check if the input is a valid IP range
96 if ( $this->mParams['iprange'] ) {
97 $rangeError = $this->isValidIPRange( $value );
98 if ( $rangeError === true ) {
99 $valid = true;
100 } elseif ( $rangeError !== false ) {
101 return $rangeError;
104 if ( !$valid ) {
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 ) ) {
116 return false;
119 [ $ip, $range ] = explode( '/', $value, 2 );
121 if (
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' );
129 if (
130 ( IPUtils::isIPv4( $ip ) && $range > 32 ) ||
131 ( IPUtils::isIPv6( $ip ) && $range > 128 )
133 // Dodgy range
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'] );
145 return true;
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() {
161 return true;
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' );