MediaWiki  master
UserFactory.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\User;
24 
25 use IDBAccessObject;
26 use User;
27 
36 class UserFactory implements IDBAccessObject {
37 
41  private $userNameUtils;
42 
47  $this->userNameUtils = $userNameUtils;
48  }
49 
60  public function newFromName(
61  string $name,
62  string $validate = UserNameUtils::RIGOR_VALID
63  ) {
64  return User::newFromName( $name, $validate );
65  }
66 
75  public function newAnonymous( $ip = null ) : User {
76  if ( $ip ) {
77  $validIp = $this->userNameUtils->isIP( $ip );
78  if ( $validIp ) {
79  $user = $this->newFromName( $ip, UserNameUtils::RIGOR_NONE );
80  } else {
81  throw new \InvalidArgumentException( 'Invalid IP address' );
82  }
83  } else {
84  $user = new User();
85  }
86  return $user;
87  }
88 
97  public function newFromId( int $id ) : User {
98  return User::newFromId( $id );
99  }
100 
109  public function newFromActorId( int $actorId ) : User {
110  return User::newFromActorId( $actorId );
111  }
112 
121  public function newFromUserIdentity( UserIdentity $userIdentity ) : User {
122  if ( $userIdentity instanceof User ) {
123  return $userIdentity;
124  }
125 
126  return $this->newFromAnyId(
127  $userIdentity->getId() === 0 ? null : $userIdentity->getId(),
128  $userIdentity->getName() === '' ? null : $userIdentity->getName(),
129  $userIdentity->getActorId() === 0 ? null : $userIdentity->getActorId()
130  );
131  }
132 
144  public function newFromAnyId(
145  ?int $userId,
146  ?string $userName,
147  ?int $actorId,
148  $dbDomain = false
149  ) : User {
150  return User::newFromAnyId( $userId, $userName, $actorId, $dbDomain );
151  }
152 
162  public function newFromConfirmationCode(
163  string $confirmationCode,
164  int $flags = self::READ_NORMAL
165  ) {
166  return User::newFromConfirmationCode( $confirmationCode, $flags );
167  }
168 
169 }
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:562
MediaWiki\User\UserFactory\newFromUserIdentity
newFromUserIdentity(UserIdentity $userIdentity)
Definition: UserFactory.php:121
MediaWiki\User\UserFactory\newFromId
newFromId(int $id)
Definition: UserFactory.php:97
MediaWiki\User\UserFactory\__construct
__construct(UserNameUtils $userNameUtils)
Definition: UserFactory.php:46
MediaWiki\User\UserNameUtils\RIGOR_NONE
const RIGOR_NONE
Definition: UserNameUtils.php:53
MediaWiki\User\UserNameUtils\RIGOR_VALID
const RIGOR_VALID
Definition: UserNameUtils.php:52
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:538
MediaWiki\User\UserFactory\newFromConfirmationCode
newFromConfirmationCode(string $confirmationCode, int $flags=self::READ_NORMAL)
Definition: UserFactory.php:162
IDBAccessObject
Interface for database access objects.
Definition: IDBAccessObject.php:57
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
MediaWiki\User\UserIdentity\getActorId
getActorId()
User\isIP
static isIP( $name)
Does the string match an anonymous IP address?
Definition: User.php:948
MediaWiki\User\UserIdentity\getName
getName()
MediaWiki\User\UserFactory\newFromActorId
newFromActorId(int $actorId)
Definition: UserFactory.php:109
User\newFromAnyId
static newFromAnyId( $userId, $userName, $actorId, $dbDomain=false)
Static factory method for creation from an ID, name, and/or actor ID.
Definition: User.php:613
MediaWiki\User\UserFactory\newFromName
newFromName(string $name, string $validate=UserNameUtils::RIGOR_VALID)
Definition: UserFactory.php:60
MediaWiki\User\UserFactory\$userNameUtils
UserNameUtils $userNameUtils
Definition: UserFactory.php:41
MediaWiki\User
Definition: DefaultOptionsLookup.php:21
MediaWiki\User\UserIdentity\getId
getId()
MediaWiki\User\UserNameUtils
UserNameUtils service.
Definition: UserNameUtils.php:42
User\newFromActorId
static newFromActorId( $id)
Static factory method for creation from a given actor ID.
Definition: User.php:577
MediaWiki\User\UserFactory\newFromAnyId
newFromAnyId(?int $userId, ?string $userName, ?int $actorId, $dbDomain=false)
Definition: UserFactory.php:144
User\newFromConfirmationCode
static newFromConfirmationCode( $code, $flags=0)
Factory method to fetch whichever user has a given email confirmation code.
Definition: User.php:667
MediaWiki\User\UserFactory\newAnonymous
newAnonymous( $ip=null)
Returns a new anonymous User based on ip.
Definition: UserFactory.php:75
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
MediaWiki\User\UserFactory
Creates User objects.
Definition: UserFactory.php:36