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 ) {
89 [ $type, $user ] = $this->processUser( $value );
91 if ( !$user || !in_array( $type, $settings[self::PARAM_ALLOWED_USER_TYPES],
true ) ) {
93 $this->
failure(
'baduser', $name, $value, $settings, $options );
96 return empty( $settings[self::PARAM_RETURN_OBJECT] ) ? $user->getName() : $user;
100 if ( isset( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
102 [
'name',
'ip',
'temp',
'cidr',
'interwiki',
'id' ],
103 $settings[self::PARAM_ALLOWED_USER_TYPES]
106 if ( empty( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
110 return parent::normalizeSettings( $settings );
113 public function checkSettings(
string $name, $settings, array $options, array $ret ): array {
114 $ret = parent::
checkSettings( $name, $settings, $options, $ret );
116 $ret[
'allowedKeys'] = array_merge( $ret[
'allowedKeys'], [
117 self::PARAM_ALLOWED_USER_TYPES, self::PARAM_RETURN_OBJECT,
120 if ( !is_bool( $settings[self::PARAM_RETURN_OBJECT] ??
false ) ) {
122 . gettype( $settings[self::PARAM_RETURN_OBJECT] );
126 if ( isset( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
127 if ( !is_array( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
129 .
'got ' . gettype( $settings[self::PARAM_ALLOWED_USER_TYPES] );
130 } elseif ( $settings[self::PARAM_ALLOWED_USER_TYPES] === [] ) {
134 $settings[self::PARAM_ALLOWED_USER_TYPES],
135 [
'name',
'ip',
'temp',
'cidr',
'interwiki',
'id' ]
139 'PARAM_ALLOWED_USER_TYPES contains invalid values: ' . implode(
', ', $bad );
142 $hasId = in_array(
'id', $settings[self::PARAM_ALLOWED_USER_TYPES],
true );
146 if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
147 ( $hasId || !empty( $settings[self::PARAM_RETURN_OBJECT] ) ) &&
149 ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT1] ?? 100 ) > 10 ||
150 ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT2] ?? 100 ) > 10
153 $ret[
'issues'][] =
'Multi-valued user-type parameters with PARAM_RETURN_OBJECT or allowing IDs '
154 .
'should set low values (<= 10) for PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2.'
155 .
' (Note that "<= 10" is arbitrary. If something hits this, we can investigate a real limit '
156 .
'once we have a real use case to look at.)';
168 private function processUser(
string $value ): array {
170 if ( preg_match(
'/^#(\d+)$/D', $value, $m ) ) {
177 $userId = (int)$m[1];
178 if ( $userId !== 0 ) {
180 $userIdentity = $this->userIdentityLookup->getUserIdentityByUserId( $userId );
181 if ( $userIdentity ) {
182 return [
'id', $userIdentity ];
188 new UserIdentityValue( 0,
"Unknown user" )
193 if ( ExternalUserNames::isExternal( $value ) ) {
194 $name = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_NONE );
201 $user = $name !==
false ?
new UserIdentityValue( 0, $value, UserIdentityValue::LOCAL ) : null;
202 return [
'interwiki', $user ];
206 if ( $this->userNameUtils->isTemp( $value ) ) {
207 $userIdentity = $this->userIdentityLookup->getUserIdentityByName( $value );
208 return [
'temp', $userIdentity ];
217 $canonicalName = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_VALID );
218 if ( $canonicalName !==
false ) {
219 $userIdentity = $this->userIdentityLookup->getUserIdentityByName( $canonicalName );
220 if ( $userIdentity ) {
221 return [
'name', $userIdentity ];
226 new UserIdentityValue( 0, $canonicalName )
232 if ( strpos( $value,
'#' ) !==
false ) {
237 $t = $this->titleParser->parseTitle( $value );
238 }
catch ( MalformedTitleException $_ ) {
241 if ( !$t ||
$t->getNamespace() !==
NS_USER ||
$t->isExternal() ) {
243 $t = $this->titleParser->parseTitle(
"User:$value" );
244 }
catch ( MalformedTitleException $_ ) {
248 if ( !$t ||
$t->getNamespace() !==
NS_USER ||
$t->isExternal() ) {
252 $value =
$t->getText();
255 $b = IPUtils::RE_IP_BYTE;
256 if ( IPUtils::isValid( $value ) ||
261 preg_match(
"/^$b\.$b\.$b\.xxx$/D", $value )
263 $name = IPUtils::sanitizeIP( $value );
269 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
270 return [
'ip', UserIdentityValue::newAnonymous( $name ) ];
274 if ( IPUtils::isValidRange( $value ) ) {
275 $name = IPUtils::sanitizeIP( $value );
277 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
278 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];
293 public function getHelpInfo( $name, array $settings, array $options ) {
294 $info = parent::getParamInfo( $name, $settings, $options );
296 $isMulti = !empty( $settings[ParamValidator::PARAM_ISMULTI] );
299 foreach ( $settings[self::PARAM_ALLOWED_USER_TYPES] as $st ) {
304 $subtypes[] = MessageValue::new(
"paramvalidator-help-type-user-subtype-$st" );
306 $info[ParamValidator::PARAM_TYPE] = MessageValue::new(
'paramvalidator-help-type-user' )
307 ->params( $isMulti ? 2 : 1 )
308 ->textListParams( $subtypes )
309 ->numParams( count( $subtypes ) );
if(!defined('MW_SETUP_CALLBACK'))