MediaWiki  master
UserFactory.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\User;
24 
26 use IDBAccessObject;
27 use InvalidArgumentException;
29 use stdClass;
30 use User;
32 
42 
49  private $loadBalancer;
50 
52  private $userNameUtils;
53 
55  private $lastUserFromIdentity = null;
56 
61  public function __construct(
64  ) {
65  $this->loadBalancer = $loadBalancer;
66  $this->userNameUtils = $userNameUtils;
67  }
68 
87  public function newFromName(
88  string $name,
89  string $validate = self::RIGOR_VALID
90  ) : ?User {
91  // RIGOR_* constants are the same here and in the UserNameUtils class
92  $canonicalName = $this->userNameUtils->getCanonical( $name, $validate );
93  if ( $canonicalName === false ) {
94  return null;
95  }
96 
97  $user = new User();
98  $user->mName = $canonicalName;
99  $user->mFrom = 'name';
100  $user->setItemLoaded( 'name' );
101  return $user;
102  }
103 
112  public function newAnonymous( $ip = null ) : User {
113  if ( $ip ) {
114  $validIp = $this->userNameUtils->isIP( $ip );
115  if ( $validIp ) {
116  $user = $this->newFromName( $ip, self::RIGOR_NONE );
117  } else {
118  throw new InvalidArgumentException( 'Invalid IP address' );
119  }
120  } else {
121  $user = new User();
122  }
123  return $user;
124  }
125 
134  public function newFromId( int $id ) : User {
135  $user = new User();
136  $user->mId = $id;
137  $user->mFrom = 'id';
138  $user->setItemLoaded( 'id' );
139  return $user;
140  }
141 
150  public function newFromActorId( int $actorId ) : User {
151  $user = new User();
152  $user->mActorId = $actorId;
153  $user->mFrom = 'actor';
154  $user->setItemLoaded( 'actor' );
155  return $user;
156  }
157 
166  public function newFromUserIdentity( UserIdentity $userIdentity ) : User {
167  if ( $userIdentity instanceof User ) {
168  return $userIdentity;
169  }
170 
171  // Cache the $userIdentity we converted last. This avoids redundant conversion
172  // in cases where we would be converting the same UserIdentity over and over,
173  // for instance because we need to access data preferences when formatting
174  // timestamps in a listing.
175  if (
176  $this->lastUserFromIdentity
177  && $this->lastUserFromIdentity->getId() == $userIdentity->getId()
178  && $this->lastUserFromIdentity->getName() == $userIdentity->getName()
179  ) {
181  }
182 
183  $this->lastUserFromIdentity = $this->newFromAnyId(
184  $userIdentity->getId() === 0 ? null : $userIdentity->getId(),
185  $userIdentity->getName() === '' ? null : $userIdentity->getName(),
186  null
187  );
188 
190  }
191 
207  public function newFromAnyId(
208  ?int $userId,
209  ?string $userName,
210  ?int $actorId = null,
211  $dbDomain = false
212  ) : User {
213  // Stop-gap solution for the problem described in T222212.
214  // Force the User ID and Actor ID to zero for users loaded from the database
215  // of another wiki, to prevent subtle data corruption and confusing failure modes.
216  if ( $dbDomain !== false ) {
217  $userId = 0;
218  $actorId = 0;
219  }
220 
221  $user = new User;
222  $user->mFrom = 'defaults';
223 
224  if ( $actorId !== null ) {
225  $user->mActorId = $actorId;
226  if ( $actorId !== 0 ) {
227  $user->mFrom = 'actor';
228  }
229  $user->setItemLoaded( 'actor' );
230  }
231 
232  if ( $userName !== null && $userName !== '' ) {
233  $user->mName = $userName;
234  $user->mFrom = 'name';
235  $user->setItemLoaded( 'name' );
236  }
237 
238  if ( $userId !== null ) {
239  $user->mId = $userId;
240  if ( $userId !== 0 ) {
241  $user->mFrom = 'id';
242  }
243  $user->setItemLoaded( 'id' );
244  }
245 
246  if ( $user->mFrom === 'defaults' ) {
247  throw new InvalidArgumentException(
248  'Cannot create a user with no name, no ID, and no actor ID'
249  );
250  }
251 
252  return $user;
253  }
254 
267  public function newFromConfirmationCode(
268  string $confirmationCode,
269  int $flags = self::READ_NORMAL
270  ) {
271  list( $index, $options ) = DBAccessObjectUtils::getDBOptions( $flags );
272 
273  $db = $this->loadBalancer->getConnectionRef( $index );
274 
275  $id = $db->selectField(
276  'user',
277  'user_id',
278  [
279  'user_email_token' => md5( $confirmationCode ),
280  'user_email_token_expires > ' . $db->addQuotes( $db->timestamp() ),
281  ],
282  __METHOD__,
283  $options
284  );
285 
286  if ( !$id ) {
287  return null;
288  }
289 
290  return $this->newFromId( (int)$id );
291  }
292 
302  public function newFromRow( $row, $data = null ) {
303  return User::newFromRow( $row, $data );
304  }
305 
311  public function newFromAuthority( Authority $authority ): User {
312  if ( $authority instanceof User ) {
313  return $authority;
314  }
315  return $this->newFromUserIdentity( $authority->getUser() );
316  }
317 
318 }
MediaWiki\User\UserFactory\newFromUserIdentity
newFromUserIdentity(UserIdentity $userIdentity)
Factory method for creation fom a given UserIdentity, replacing User::newFromIdentity.
Definition: UserFactory.php:166
MediaWiki\User\UserFactory\newFromId
newFromId(int $id)
Factory method for creation from a given user ID, replacing User::newFromId.
Definition: UserFactory.php:134
MediaWiki\User\UserFactory\newFromRow
newFromRow( $row, $data=null)
Definition: UserFactory.php:302
MediaWiki\User\UserFactory\newFromAuthority
newFromAuthority(Authority $authority)
Definition: UserFactory.php:311
MediaWiki\User\UserFactory\newFromConfirmationCode
newFromConfirmationCode(string $confirmationCode, int $flags=self::READ_NORMAL)
Factory method to fetch the user for a given email confirmation code, replacing User::newFromConfirma...
Definition: UserFactory.php:267
MediaWiki\User\UserIdentity\getId
getId( $wikiId=self::LOCAL)
DBAccessObjectUtils\getDBOptions
static getDBOptions( $bitfield)
Get an appropriate DB index, options, and fallback DB index for a query.
Definition: DBAccessObjectUtils.php:52
IDBAccessObject
Interface for database access objects.
Definition: IDBAccessObject.php:57
User\newFromRow
static newFromRow( $row, $data=null)
Create a new user object from a user row.
Definition: User.php:748
MediaWiki\Permissions\Authority\getUser
getUser()
Returns the performer of the actions associated with this authority.
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
User\setItemLoaded
setItemLoaded( $item)
Set that an item has been loaded.
Definition: User.php:1271
User\isIP
static isIP( $name)
Does the string match an anonymous IP address?
Definition: User.php:1012
MediaWiki\User\UserIdentity\getName
getName()
MediaWiki\User\UserFactory\newFromActorId
newFromActorId(int $actorId)
Factory method for creation from a given actor ID, replacing User::newFromActorId.
Definition: UserFactory.php:150
MediaWiki\User\UserFactory\__construct
__construct(ILoadBalancer $loadBalancer, UserNameUtils $userNameUtils)
Definition: UserFactory.php:61
DBAccessObjectUtils
Helper class for DAO classes.
Definition: DBAccessObjectUtils.php:29
MediaWiki\User\UserFactory\$userNameUtils
UserNameUtils $userNameUtils
Definition: UserFactory.php:52
MediaWiki\Permissions\Authority
Definition: Authority.php:30
MediaWiki\User
Definition: ActorNormalization.php:21
MediaWiki\User\UserRigorOptions
Shared interface for rigor levels when dealing with User methods.
Definition: UserRigorOptions.php:31
MediaWiki\User\UserFactory\newFromAnyId
newFromAnyId(?int $userId, ?string $userName, ?int $actorId=null, $dbDomain=false)
Factory method for creation from an ID, name, and/or actor ID, replacing User::newFromAnyId.
Definition: UserFactory.php:207
MediaWiki\User\UserNameUtils
UserNameUtils service.
Definition: UserNameUtils.php:42
MediaWiki\User\UserFactory\newFromName
newFromName(string $name, string $validate=self::RIGOR_VALID)
Factory method for creating users by name, replacing static User::newFromName.
Definition: UserFactory.php:87
MediaWiki\User\UserFactory\$loadBalancer
ILoadBalancer $loadBalancer
RIGOR_* constants are inherited from UserRigorOptions READ_* constants are inherited from IDBAccessOb...
Definition: UserFactory.php:49
MediaWiki\User\UserFactory\newAnonymous
newAnonymous( $ip=null)
Returns a new anonymous User based on ip.
Definition: UserFactory.php:112
MediaWiki\User\UserFactory\$lastUserFromIdentity
User null $lastUserFromIdentity
Definition: UserFactory.php:55
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:66
MediaWiki\User\UserFactory
Creates User objects.
Definition: UserFactory.php:41
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81