MediaWiki  master
HTMLUserTextField.php
Go to the documentation of this file.
1 <?php
2 
4 use Wikimedia\IPUtils;
5 
25  public function __construct( $params ) {
26  $params = wfArrayPlus2d( $params, [
27  'exists' => false,
28  'ipallowed' => false,
29  'iprange' => false,
30  'iprangelimits' => [
31  'IPv4' => '16',
32  'IPv6' => '32',
33  ],
34  ]
35  );
36 
37  parent::__construct( $params );
38  }
39 
40  public function validate( $value, $alldata ) {
41  // If the value is null, reset it to an empty string which is what is expected by the parent.
42  if ( $value === null ) {
43  $value = '';
44  }
45 
46  // If the value is empty, there are no additional checks that can be performed.
47  if ( $value === '' ) {
48  return parent::validate( $value, $alldata );
49  }
50 
51  // check, if a user exists with the given username
52  $user = User::newFromName( $value, false );
53  $rangeError = null;
54 
55  if ( !$user ) {
56  return $this->msg( 'htmlform-user-not-valid', $value );
57  } elseif (
58  // check, if the user exists, if requested
59  ( $this->mParams['exists'] && $user->getId() === 0 ) &&
60  // check, if the username is a valid IP address, otherwise save the error message
61  !( $this->mParams['ipallowed'] && IPUtils::isValid( $value ) ) &&
62  // check, if the username is a valid IP range, otherwise save the error message
63  !( $this->mParams['iprange'] && ( $rangeError = $this->isValidIPRange( $value ) ) === true )
64  ) {
65  if ( is_string( $rangeError ) ) {
66  return $rangeError;
67  }
68  return $this->msg( 'htmlform-user-not-exists', $user->getName() );
69  }
70 
71  return parent::validate( $value, $alldata );
72  }
73 
74  protected function isValidIPRange( $value ) {
75  $cidrIPRanges = $this->mParams['iprangelimits'];
76 
77  if ( !IPUtils::isValidRange( $value ) ) {
78  return false;
79  }
80 
81  list( $ip, $range ) = explode( '/', $value, 2 );
82 
83  if (
84  ( IPUtils::isIPv4( $ip ) && $cidrIPRanges['IPv4'] == 32 ) ||
85  ( IPUtils::isIPv6( $ip ) && $cidrIPRanges['IPv6'] == 128 )
86  ) {
87  // Range block effectively disabled
88  return $this->msg( 'ip_range_toolow' )->parse();
89  }
90 
91  if (
92  ( IPUtils::isIPv4( $ip ) && $range > 32 ) ||
93  ( IPUtils::isIPv6( $ip ) && $range > 128 )
94  ) {
95  // Dodgy range
96  return $this->msg( 'ip_range_invalid' )->parse();
97  }
98 
99  if ( IPUtils::isIPv4( $ip ) && $range < $cidrIPRanges['IPv4'] ) {
100  return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv4'] )->parse();
101  }
102 
103  if ( IPUtils::isIPv6( $ip ) && $range < $cidrIPRanges['IPv6'] ) {
104  return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv6'] )->parse();
105  }
106 
107  return true;
108  }
109 
110  protected function getInputWidget( $params ) {
111  return new UserInputWidget( $params );
112  }
113 
114  protected function shouldInfuseOOUI() {
115  return true;
116  }
117 
118  protected function getOOUIModules() {
119  return [ 'mediawiki.widgets.UserInputWidget' ];
120  }
121 
122  public function getInputHtml( $value ) {
123  // add the required module and css class for user suggestions in non-OOUI mode
124  $this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
125  $this->mClass .= ' mw-autocomplete-user';
126 
127  // return parent html
128  return parent::getInputHTML( $value );
129  }
130 }
HTMLUserTextField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition: HTMLUserTextField.php:118
HTMLUserTextField\shouldInfuseOOUI
shouldInfuseOOUI()
Whether the field should be automatically infused.
Definition: HTMLUserTextField.php:114
wfArrayPlus2d
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
Definition: GlobalFunctions.php:2871
MediaWiki\Widget\UserInputWidget
User input widget.
Definition: UserInputWidget.php:11
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:545
HTMLTextField
<input> field.
Definition: HTMLTextField.php:15
HTMLUserTextField\getInputWidget
getInputWidget( $params)
Stable to override.
Definition: HTMLUserTextField.php:110
HTMLUserTextField
Implements a text input field for user names.
Definition: HTMLUserTextField.php:20
HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition: HTMLFormField.php:87
HTMLUserTextField\getInputHtml
getInputHtml( $value)
Definition: HTMLUserTextField.php:122
HTMLUserTextField\__construct
__construct( $params)
Stable to call Stable to call.type: HTML textfield typesize: field size in characters (defaults to 45...
Definition: HTMLUserTextField.php:25
HTMLUserTextField\isValidIPRange
isValidIPRange( $value)
Definition: HTMLUserTextField.php:74
HTMLUserTextField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLUserTextField.php:40