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