3 use MediaWiki\Widget\UserInputWidget
;
6 * Implements a text input field for user names.
7 * Automatically auto-completes if using the OOUI display format.
9 * FIXME: Does not work for forms that support GET requests.
11 * Optional parameters:
12 * 'exists' - Whether to validate that the user already exists
13 * 'ipallowed' - Whether an IP adress is interpreted as "valid"
14 * 'iprange' - Whether an IP adress range is interpreted as "valid"
15 * 'iprangelimits' - Specifies the valid IP ranges for IPv4 and IPv6 in an array.
16 * defaults to IPv4 => 16; IPv6 => 32.
20 class HTMLUserTextField
extends HTMLTextField
{
21 public function __construct( $params ) {
22 $params = wfArrayPlus2d( $params, [
33 parent
::__construct( $params );
36 public function validate( $value, $alldata ) {
37 // check, if a user exists with the given username
38 $user = User
::newFromName( $value, false );
42 return $this->msg( 'htmlform-user-not-valid', $value );
44 // check, if the user exists, if requested
45 ( $this->mParams
['exists'] && $user->getId() === 0 ) &&
46 // check, if the username is a valid IP address, otherweise save the error message
47 !( $this->mParams
['ipallowed'] && IP
::isValid( $value ) ) &&
48 // check, if the username is a valid IP range, otherwise save the error message
49 !( $this->mParams
['iprange'] && ( $rangeError = $this->isValidIPRange( $value ) ) === true )
51 if ( is_string( $rangeError ) ) {
54 return $this->msg( 'htmlform-user-not-exists', $user->getName() );
57 return parent
::validate( $value, $alldata );
60 protected function isValidIPRange( $value ) {
61 $cidrIPRanges = $this->mParams
['iprangelimits'];
63 if ( !IP
::isValidBlock( $value ) ) {
67 list( $ip, $range ) = explode( '/', $value, 2 );
70 ( IP
::isIPv4( $ip ) && $cidrIPRanges['IPv4'] == 32 ) ||
71 ( IP
::isIPv6( $ip ) && $cidrIPRanges['IPv6'] == 128 )
73 // Range block effectively disabled
74 return $this->msg( 'ip_range_toolow' )->parse();
78 ( IP
::isIPv4( $ip ) && $range > 32 ) ||
79 ( IP
::isIPv6( $ip ) && $range > 128 )
82 return $this->msg( 'ip_range_invalid' )->parse();
85 if ( IP
::isIPv4( $ip ) && $range < $cidrIPRanges['IPv4'] ) {
86 return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv4'] )->parse();
89 if ( IP
::isIPv6( $ip ) && $range < $cidrIPRanges['IPv6'] ) {
90 return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv6'] )->parse();
96 protected function getInputWidget( $params ) {
97 return new UserInputWidget( $params );
100 protected function shouldInfuseOOUI() {
104 protected function getOOUIModules() {
105 return [ 'mediawiki.widgets.UserInputWidget' ];
108 public function getInputHtml( $value ) {
109 // add the required module and css class for user suggestions in non-OOUI mode
110 $this->mParent
->getOutput()->addModules( 'mediawiki.userSuggest' );
111 $this->mClass
.= ' mw-autocomplete-user';
113 // return parent html
114 return parent
::getInputHTML( $value );