60 private $userIdentityLookup;
66 private $userNameUtils;
81 $this->userIdentityLookup = $userIdentityLookup;
82 $this->titleParser = $titleParser;
83 $this->userNameUtils = $userNameUtils;
87 public function validate( $name, $value, array $settings, array $options ) {
90 [ $type, $user ] = $this->processUser( $value );
92 if ( !$user || !in_array( $type, $settings[self::PARAM_ALLOWED_USER_TYPES],
true ) ) {
94 $this->
failure(
'baduser', $name, $value, $settings, $options );
97 return empty( $settings[self::PARAM_RETURN_OBJECT] ) ? $user->getName() : $user;
102 if ( isset( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
104 [
'name',
'ip',
'temp',
'cidr',
'interwiki',
'id' ],
105 $settings[self::PARAM_ALLOWED_USER_TYPES]
108 if ( empty( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
112 return parent::normalizeSettings( $settings );
116 public function checkSettings(
string $name, $settings, array $options, array $ret ): array {
117 $ret = parent::
checkSettings( $name, $settings, $options, $ret );
122 if ( !is_bool( $settings[self::PARAM_RETURN_OBJECT] ??
false ) ) {
124 . gettype( $settings[self::PARAM_RETURN_OBJECT] );
128 if ( isset( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
129 if ( !is_array( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
131 .
'got ' . gettype( $settings[self::PARAM_ALLOWED_USER_TYPES] );
132 } elseif ( $settings[self::PARAM_ALLOWED_USER_TYPES] === [] ) {
136 $settings[self::PARAM_ALLOWED_USER_TYPES],
137 [
'name',
'ip',
'temp',
'cidr',
'interwiki',
'id' ]
141 'PARAM_ALLOWED_USER_TYPES contains invalid values: ' . implode(
', ', $bad );
144 $hasId = in_array(
'id', $settings[self::PARAM_ALLOWED_USER_TYPES],
true );
148 if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
149 ( $hasId || !empty( $settings[self::PARAM_RETURN_OBJECT] ) ) &&
151 ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT1] ?? 100 ) > 10 ||
152 ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT2] ?? 100 ) > 10
155 $ret[
'issues'][] =
'Multi-valued user-type parameters with PARAM_RETURN_OBJECT or allowing IDs '
156 .
'should set low values (<= 10) for PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2.'
157 .
' (Note that "<= 10" is arbitrary. If something hits this, we can investigate a real limit '
158 .
'once we have a real use case to look at.)';
170 private function processUser(
string $value ): array {
172 if ( preg_match(
'/^#(\d+)$/D', $value, $m ) ) {
179 $userId = (int)$m[1];
180 if ( $userId !== 0 ) {
182 $userIdentity = $this->userIdentityLookup->getUserIdentityByUserId( $userId );
183 if ( $userIdentity ) {
184 return [
'id', $userIdentity ];
190 new UserIdentityValue( 0,
"Unknown user" )
195 if ( ExternalUserNames::isExternal( $value ) ) {
196 $name = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_NONE );
203 $user = $name !==
false ?
new UserIdentityValue( 0, $value, UserIdentityValue::LOCAL ) : null;
204 return [
'interwiki', $user ];
213 $canonicalName = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_VALID );
214 if ( $canonicalName !==
false ) {
216 $userType = $this->userNameUtils->isTemp( $value ) ?
'temp' :
'name';
218 $userIdentity = $this->userIdentityLookup->getUserIdentityByName( $canonicalName );
219 if ( $userIdentity ) {
220 return [ $userType, $userIdentity ];
225 new UserIdentityValue( 0, $canonicalName )
231 if ( str_contains( $value,
'#' ) ) {
236 $t = $this->titleParser->parseTitle( $value );
237 }
catch ( MalformedTitleException ) {
240 if ( !$t ||
$t->getNamespace() !==
NS_USER ||
$t->isExternal() ) {
242 $t = $this->titleParser->parseTitle(
"User:$value" );
243 }
catch ( MalformedTitleException ) {
247 if ( !$t ||
$t->getNamespace() !==
NS_USER ||
$t->isExternal() ) {
251 $value =
$t->getText();
254 $b = IPUtils::RE_IP_BYTE;
255 if ( IPUtils::isValid( $value ) ||
260 preg_match(
"/^$b\.$b\.$b\.xxx$/D", $value )
262 $name = IPUtils::sanitizeIP( $value );
268 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
269 return [
'ip', UserIdentityValue::newAnonymous( $name ) ];
273 if ( IPUtils::isValidRange( $value ) ) {
274 $name = IPUtils::sanitizeIP( $value );
276 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
277 return [
'cidr', UserIdentityValue::newAnonymous( $name ) ];
285 public function getParamInfo( $name, array $settings, array $options ) {
286 $info = parent::getParamInfo( $name, $settings, $options );
288 $info[
'subtypes'] = $settings[self::PARAM_ALLOWED_USER_TYPES];
294 public function getHelpInfo( $name, array $settings, array $options ) {
295 $info = parent::getParamInfo( $name, $settings, $options );
297 $isMulti = !empty( $settings[ParamValidator::PARAM_ISMULTI] );
300 foreach ( $settings[self::PARAM_ALLOWED_USER_TYPES] as $st ) {
305 $subtypes[] = MessageValue::new(
"paramvalidator-help-type-user-subtype-$st" );
307 $info[ParamValidator::PARAM_TYPE] = MessageValue::new(
'paramvalidator-help-type-user' )
308 ->params( $isMulti ? 2 : 1 )
309 ->textListParams( $subtypes )
310 ->numParams( count( $subtypes ) );
if(!defined('MW_SETUP_CALLBACK'))