59 private $userIdentityLookup;
65 private $userNameUtils;
80 $this->userIdentityLookup = $userIdentityLookup;
81 $this->titleParser = $titleParser;
82 $this->userNameUtils = $userNameUtils;
85 public function validate( $name, $value, array $settings, array $options ) {
86 [ $type, $user ] = $this->processUser( $value );
88 if ( !$user || !in_array( $type, $settings[self::PARAM_ALLOWED_USER_TYPES],
true ) ) {
90 $this->
failure(
'baduser', $name, $value, $settings, $options );
93 return empty( $settings[self::PARAM_RETURN_OBJECT] ) ? $user->getName() : $user;
97 if ( isset( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
99 [
'name',
'ip',
'cidr',
'interwiki',
'id' ],
100 $settings[self::PARAM_ALLOWED_USER_TYPES]
103 if ( empty( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
107 return parent::normalizeSettings( $settings );
110 public function checkSettings(
string $name, $settings, array $options, array $ret ): array {
111 $ret = parent::
checkSettings( $name, $settings, $options, $ret );
113 $ret[
'allowedKeys'] = array_merge( $ret[
'allowedKeys'], [
114 self::PARAM_ALLOWED_USER_TYPES, self::PARAM_RETURN_OBJECT,
117 if ( !is_bool( $settings[self::PARAM_RETURN_OBJECT] ??
false ) ) {
119 . gettype( $settings[self::PARAM_RETURN_OBJECT] );
123 if ( isset( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
124 if ( !is_array( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
126 .
'got ' . gettype( $settings[self::PARAM_ALLOWED_USER_TYPES] );
127 } elseif ( $settings[self::PARAM_ALLOWED_USER_TYPES] === [] ) {
131 $settings[self::PARAM_ALLOWED_USER_TYPES],
132 [
'name',
'ip',
'cidr',
'interwiki',
'id' ]
136 'PARAM_ALLOWED_USER_TYPES contains invalid values: ' . implode(
', ', $bad );
139 $hasId = in_array(
'id', $settings[self::PARAM_ALLOWED_USER_TYPES],
true );
143 if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
144 ( $hasId || !empty( $settings[self::PARAM_RETURN_OBJECT] ) ) &&
146 ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT1] ?? 100 ) > 10 ||
147 ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT2] ?? 100 ) > 10
150 $ret[
'issues'][] =
'Multi-valued user-type parameters with PARAM_RETURN_OBJECT or allowing IDs '
151 .
'should set low values (<= 10) for PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2.'
152 .
' (Note that "<= 10" is arbitrary. If something hits this, we can investigate a real limit '
153 .
'once we have a real use case to look at.)';
165 private function processUser(
string $value ): array {
167 if ( preg_match(
'/^#(\d+)$/D', $value, $m ) ) {
174 $userId = (int)$m[1];
175 if ( $userId !== 0 ) {
177 $userIdentity = $this->userIdentityLookup->getUserIdentityByUserId( $userId );
178 if ( $userIdentity ) {
179 return [
'id', $userIdentity ];
185 new UserIdentityValue( 0,
"Unknown user" )
190 if ( ExternalUserNames::isExternal( $value ) ) {
191 $name = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_NONE );
198 $user = $name !==
false ?
new UserIdentityValue( 0, $value, UserIdentityValue::LOCAL ) : null;
199 return [
'interwiki', $user ];
208 $canonicalName = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_VALID );
209 if ( $canonicalName !==
false ) {
210 $userIdentity = $this->userIdentityLookup->getUserIdentityByName( $canonicalName );
211 if ( $userIdentity ) {
212 return [
'name', $userIdentity ];
217 new UserIdentityValue( 0, $canonicalName )
223 if ( strpos( $value,
'#' ) !==
false ) {
228 $t = $this->titleParser->parseTitle( $value );
229 }
catch ( MalformedTitleException $_ ) {
232 if ( !$t ||
$t->getNamespace() !==
NS_USER ||
$t->isExternal() ) {
234 $t = $this->titleParser->parseTitle(
"User:$value" );
235 }
catch ( MalformedTitleException $_ ) {
239 if ( !$t ||
$t->getNamespace() !==
NS_USER ||
$t->isExternal() ) {
243 $value =
$t->getText();
246 $b = IPUtils::RE_IP_BYTE;
247 if ( IPUtils::isValid( $value ) ||
252 preg_match(
"/^$b\.$b\.$b\.xxx$/D", $value )
254 $name = IPUtils::sanitizeIP( $value );
260 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
261 return [
'ip', UserIdentityValue::newAnonymous( $name ) ];
265 if ( IPUtils::isValidRange( $value ) ) {
266 $name = IPUtils::sanitizeIP( $value );
268 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
269 return [
'cidr', UserIdentityValue::newAnonymous( $name ) ];
276 public function getParamInfo( $name, array $settings, array $options ) {
277 $info = parent::getParamInfo( $name, $settings, $options );
279 $info[
'subtypes'] = $settings[self::PARAM_ALLOWED_USER_TYPES];
284 public function getHelpInfo( $name, array $settings, array $options ) {
285 $info = parent::getParamInfo( $name, $settings, $options );
287 $isMulti = !empty( $settings[ParamValidator::PARAM_ISMULTI] );
290 foreach ( $settings[self::PARAM_ALLOWED_USER_TYPES] as $st ) {
294 $subtypes[] = MessageValue::new(
"paramvalidator-help-type-user-subtype-$st" );
296 $info[ParamValidator::PARAM_TYPE] = MessageValue::new(
'paramvalidator-help-type-user' )
297 ->params( $isMulti ? 2 : 1 )
298 ->textListParams( $subtypes )
299 ->numParams( count( $subtypes ) );
if(!defined('MW_SETUP_CALLBACK'))