60 private $userIdentityLookup;
66 private $userNameUtils;
81 $this->userIdentityLookup = $userIdentityLookup;
82 $this->titleParser = $titleParser;
83 $this->userNameUtils = $userNameUtils;
86 public function validate( $name, $value, array $settings, array $options ) {
87 [ $type, $user ] = $this->processUser( $value );
89 if ( !$user || !in_array( $type, $settings[self::PARAM_ALLOWED_USER_TYPES],
true ) ) {
91 $this->
failure(
'baduser', $name, $value, $settings, $options );
94 return empty( $settings[self::PARAM_RETURN_OBJECT] ) ? $user->getName() : $user;
98 if ( isset( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
100 [
'name',
'ip',
'temp',
'cidr',
'interwiki',
'id' ],
101 $settings[self::PARAM_ALLOWED_USER_TYPES]
104 if ( empty( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
108 return parent::normalizeSettings( $settings );
111 public function checkSettings(
string $name, $settings, array $options, array $ret ): array {
112 $ret = parent::
checkSettings( $name, $settings, $options, $ret );
114 $ret[
'allowedKeys'] = array_merge( $ret[
'allowedKeys'], [
115 self::PARAM_ALLOWED_USER_TYPES, self::PARAM_RETURN_OBJECT,
118 if ( !is_bool( $settings[self::PARAM_RETURN_OBJECT] ??
false ) ) {
120 . gettype( $settings[self::PARAM_RETURN_OBJECT] );
124 if ( isset( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
125 if ( !is_array( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
127 .
'got ' . gettype( $settings[self::PARAM_ALLOWED_USER_TYPES] );
128 } elseif ( $settings[self::PARAM_ALLOWED_USER_TYPES] === [] ) {
132 $settings[self::PARAM_ALLOWED_USER_TYPES],
133 [
'name',
'ip',
'temp',
'cidr',
'interwiki',
'id' ]
137 'PARAM_ALLOWED_USER_TYPES contains invalid values: ' . implode(
', ', $bad );
140 $hasId = in_array(
'id', $settings[self::PARAM_ALLOWED_USER_TYPES],
true );
144 if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
145 ( $hasId || !empty( $settings[self::PARAM_RETURN_OBJECT] ) ) &&
147 ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT1] ?? 100 ) > 10 ||
148 ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT2] ?? 100 ) > 10
151 $ret[
'issues'][] =
'Multi-valued user-type parameters with PARAM_RETURN_OBJECT or allowing IDs '
152 .
'should set low values (<= 10) for PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2.'
153 .
' (Note that "<= 10" is arbitrary. If something hits this, we can investigate a real limit '
154 .
'once we have a real use case to look at.)';
166 private function processUser(
string $value ): array {
168 if ( preg_match(
'/^#(\d+)$/D', $value, $m ) ) {
175 $userId = (int)$m[1];
176 if ( $userId !== 0 ) {
178 $userIdentity = $this->userIdentityLookup->getUserIdentityByUserId( $userId );
179 if ( $userIdentity ) {
180 return [
'id', $userIdentity ];
186 new UserIdentityValue( 0,
"Unknown user" )
191 if ( ExternalUserNames::isExternal( $value ) ) {
192 $name = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_NONE );
199 $user = $name !==
false ?
new UserIdentityValue( 0, $value, UserIdentityValue::LOCAL ) : null;
200 return [
'interwiki', $user ];
204 if ( $this->userNameUtils->isTemp( $value ) ) {
205 $userIdentity = $this->userIdentityLookup->getUserIdentityByName( $value );
206 return [
'temp', $userIdentity ];
215 $canonicalName = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_VALID );
216 if ( $canonicalName !==
false ) {
217 $userIdentity = $this->userIdentityLookup->getUserIdentityByName( $canonicalName );
218 if ( $userIdentity ) {
219 return [
'name', $userIdentity ];
224 new UserIdentityValue( 0, $canonicalName )
230 if ( strpos( $value,
'#' ) !==
false ) {
235 $t = $this->titleParser->parseTitle( $value );
236 }
catch ( MalformedTitleException $_ ) {
239 if ( !$t ||
$t->getNamespace() !==
NS_USER ||
$t->isExternal() ) {
241 $t = $this->titleParser->parseTitle(
"User:$value" );
242 }
catch ( MalformedTitleException $_ ) {
246 if ( !$t ||
$t->getNamespace() !==
NS_USER ||
$t->isExternal() ) {
250 $value =
$t->getText();
253 $b = IPUtils::RE_IP_BYTE;
254 if ( IPUtils::isValid( $value ) ||
259 preg_match(
"/^$b\.$b\.$b\.xxx$/D", $value )
261 $name = IPUtils::sanitizeIP( $value );
267 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
268 return [
'ip', UserIdentityValue::newAnonymous( $name ) ];
272 if ( IPUtils::isValidRange( $value ) ) {
273 $name = IPUtils::sanitizeIP( $value );
275 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
276 return [
'cidr', UserIdentityValue::newAnonymous( $name ) ];
283 public function getParamInfo( $name, array $settings, array $options ) {
284 $info = parent::getParamInfo( $name, $settings, $options );
286 $info[
'subtypes'] = $settings[self::PARAM_ALLOWED_USER_TYPES];
291 public function getHelpInfo( $name, array $settings, array $options ) {
292 $info = parent::getParamInfo( $name, $settings, $options );
294 $isMulti = !empty( $settings[ParamValidator::PARAM_ISMULTI] );
297 foreach ( $settings[self::PARAM_ALLOWED_USER_TYPES] as $st ) {
302 $subtypes[] = MessageValue::new(
"paramvalidator-help-type-user-subtype-$st" );
304 $info[ParamValidator::PARAM_TYPE] = MessageValue::new(
'paramvalidator-help-type-user' )
305 ->params( $isMulti ? 2 : 1 )
306 ->textListParams( $subtypes )
307 ->numParams( count( $subtypes ) );
if(!defined('MW_SETUP_CALLBACK'))