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 ];
211 $canonicalName = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_VALID );
212 if ( $canonicalName !==
false ) {
214 $userType = $this->userNameUtils->isTemp( $value ) ?
'temp' :
'name';
216 $userIdentity = $this->userIdentityLookup->getUserIdentityByName( $canonicalName );
217 if ( $userIdentity ) {
218 return [ $userType, $userIdentity ];
223 new UserIdentityValue( 0, $canonicalName )
229 if ( strpos( $value,
'#' ) !==
false ) {
234 $t = $this->titleParser->parseTitle( $value );
235 }
catch ( MalformedTitleException $_ ) {
238 if ( !$t ||
$t->getNamespace() !==
NS_USER ||
$t->isExternal() ) {
240 $t = $this->titleParser->parseTitle(
"User:$value" );
241 }
catch ( MalformedTitleException $_ ) {
245 if ( !$t ||
$t->getNamespace() !==
NS_USER ||
$t->isExternal() ) {
249 $value =
$t->getText();
252 $b = IPUtils::RE_IP_BYTE;
253 if ( IPUtils::isValid( $value ) ||
258 preg_match(
"/^$b\.$b\.$b\.xxx$/D", $value )
260 $name = IPUtils::sanitizeIP( $value );
266 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
267 return [
'ip', UserIdentityValue::newAnonymous( $name ) ];
271 if ( IPUtils::isValidRange( $value ) ) {
272 $name = IPUtils::sanitizeIP( $value );
274 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
275 return [
'cidr', UserIdentityValue::newAnonymous( $name ) ];
282 public function getParamInfo( $name, array $settings, array $options ) {
283 $info = parent::getParamInfo( $name, $settings, $options );
285 $info[
'subtypes'] = $settings[self::PARAM_ALLOWED_USER_TYPES];
290 public function getHelpInfo( $name, array $settings, array $options ) {
291 $info = parent::getParamInfo( $name, $settings, $options );
293 $isMulti = !empty( $settings[ParamValidator::PARAM_ISMULTI] );
296 foreach ( $settings[self::PARAM_ALLOWED_USER_TYPES] as $st ) {
301 $subtypes[] = MessageValue::new(
"paramvalidator-help-type-user-subtype-$st" );
303 $info[ParamValidator::PARAM_TYPE] = MessageValue::new(
'paramvalidator-help-type-user' )
304 ->params( $isMulti ? 2 : 1 )
305 ->textListParams( $subtypes )
306 ->numParams( count( $subtypes ) );
if(!defined('MW_SETUP_CALLBACK'))