MediaWiki master
HTMLUserTextField.php
Go to the documentation of this file.
1<?php
2
4
8use Wikimedia\IPUtils;
9
29 public function __construct( $params ) {
31 'exists' => false,
32 'external' => false,
33 'ipallowed' => false,
34 'iprange' => false,
35 'iprangelimits' => [
36 'IPv4' => 0,
37 'IPv6' => 0,
38 ],
39 ]
40 );
41
42 parent::__construct( $params );
43 }
44
45 public function validate( $value, $alldata ) {
46 // If the value is null, reset it to an empty string which is what is expected by the parent.
47 if ( $value === null ) {
48 $value = '';
49 }
50
51 // If the value is empty, there are no additional checks that can be performed.
52 if ( $value === '' ) {
53 return parent::validate( $value, $alldata );
54 }
55
56 // check if the input is a valid username
57 $user = MediaWikiServices::getInstance()->getUserFactory()->newFromName( $value );
58 if ( $user ) {
59 // check if the user exists, if requested
60 if ( $this->mParams['exists'] && !(
61 $user->isRegistered() &&
62 // Treat hidden users as unregistered if current user can't view them (T309894)
63 !( $user->isHidden() && !( $this->mParent && $this->mParent->getUser()->isAllowed( 'hideuser' ) ) )
64 ) ) {
65 return $this->msg( 'htmlform-user-not-exists', $user->getName() );
66 }
67 } else {
68 // not a valid username
69 $valid = false;
70 // check if the input is a valid external user
71 if ( $this->mParams['external'] && ExternalUserNames::isExternal( $value ) ) {
72 $valid = true;
73 }
74 // check if the input is a valid IP address
75 if ( $this->mParams['ipallowed'] && IPUtils::isValid( $value ) ) {
76 $valid = true;
77 }
78 // check if the input is a valid IP range
79 if ( $this->mParams['iprange'] ) {
80 $rangeError = $this->isValidIPRange( $value );
81 if ( $rangeError === true ) {
82 $valid = true;
83 } elseif ( $rangeError !== false ) {
84 return $rangeError;
85 }
86 }
87 if ( !$valid ) {
88 return $this->msg( 'htmlform-user-not-valid', $value );
89 }
90 }
91
92 return parent::validate( $value, $alldata );
93 }
94
95 protected function isValidIPRange( $value ) {
96 $cidrIPRanges = $this->mParams['iprangelimits'];
97
98 if ( !IPUtils::isValidRange( $value ) ) {
99 return false;
100 }
101
102 [ $ip, $range ] = explode( '/', $value, 2 );
103
104 if (
105 ( IPUtils::isIPv4( $ip ) && $cidrIPRanges['IPv4'] == 32 ) ||
106 ( IPUtils::isIPv6( $ip ) && $cidrIPRanges['IPv6'] == 128 )
107 ) {
108 // Range block effectively disabled
109 return $this->msg( 'ip_range_toolow' );
110 }
111
112 if (
113 ( IPUtils::isIPv4( $ip ) && $range > 32 ) ||
114 ( IPUtils::isIPv6( $ip ) && $range > 128 )
115 ) {
116 // Dodgy range
117 return $this->msg( 'ip_range_invalid' );
118 }
119
120 if ( IPUtils::isIPv4( $ip ) && $range < $cidrIPRanges['IPv4'] ) {
121 return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv4'] );
122 }
123
124 if ( IPUtils::isIPv6( $ip ) && $range < $cidrIPRanges['IPv6'] ) {
125 return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv6'] );
126 }
127
128 return true;
129 }
130
131 protected function getInputWidget( $params ) {
132 return new UserInputWidget( $params );
133 }
134
135 protected function shouldInfuseOOUI() {
136 return true;
137 }
138
139 protected function getOOUIModules() {
140 return [ 'mediawiki.widgets.UserInputWidget' ];
141 }
142
143 public function getInputHtml( $value ) {
144 // add the required module and css class for user suggestions in non-OOUI mode
145 $this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
146 $this->mClass .= ' mw-autocomplete-user';
147
148 // return parent html
149 return parent::getInputHTML( $value );
150 }
151}
152
154class_alias( HTMLUserTextField::class, 'HTMLUserTextField' );
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
array $params
The job parameters.
Implements a text input field for user names.
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
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...
msg( $key,... $params)
Get a translated interface message.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Class to parse and build external user names.