MediaWiki REL1_37
UserFactory.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\User;
24
27use InvalidArgumentException;
29use stdClass;
30use User;
32
42
50
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( ?string $ip = null ): User {
113 if ( $ip ) {
114 if ( !$this->userNameUtils->isIP( $ip ) ) {
115 throw new InvalidArgumentException( 'Invalid IP address' );
116 }
117 $user = $this->newFromName( $ip, self::RIGOR_NONE );
118 } else {
119 $user = new User();
120 }
121 return $user;
122 }
123
132 public function newFromId( int $id ): User {
133 $user = new User();
134 $user->mId = $id;
135 $user->mFrom = 'id';
136 $user->setItemLoaded( 'id' );
137 return $user;
138 }
139
148 public function newFromActorId( int $actorId ): User {
149 $user = new User();
150 $user->mActorId = $actorId;
151 $user->mFrom = 'actor';
152 $user->setItemLoaded( 'actor' );
153 return $user;
154 }
155
164 public function newFromUserIdentity( UserIdentity $userIdentity ): User {
165 if ( $userIdentity instanceof User ) {
166 return $userIdentity;
167 }
168
169 $id = $userIdentity->getId();
170 $name = $userIdentity->getName();
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() === $id
178 && $this->lastUserFromIdentity->getName() === $name
179 ) {
180 return $this->lastUserFromIdentity;
181 }
182
183 $this->lastUserFromIdentity = $this->newFromAnyId(
184 $id === 0 ? null : $id,
185 $name === '' ? null : $name,
186 null
187 );
188
189 return $this->lastUserFromIdentity;
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}
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:88
Helper class for DAO classes.
Creates User objects.
newFromId(int $id)
Factory method for creation from a given user ID, replacing User::newFromId.
newFromAuthority(Authority $authority)
newFromName(string $name, string $validate=self::RIGOR_VALID)
Factory method for creating users by name, replacing static User::newFromName.
newAnonymous(?string $ip=null)
Returns a new anonymous User based on ip.
newFromRow( $row, $data=null)
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.
newFromUserIdentity(UserIdentity $userIdentity)
Factory method for creation fom a given UserIdentity, replacing User::newFromIdentity.
ILoadBalancer $loadBalancer
RIGOR_* constants are inherited from UserRigorOptions READ_* constants are inherited from IDBAccessOb...
__construct(ILoadBalancer $loadBalancer, UserNameUtils $userNameUtils)
newFromActorId(int $actorId)
Factory method for creation from a given actor ID, replacing User::newFromActorId.
newFromConfirmationCode(string $confirmationCode, int $flags=self::READ_NORMAL)
Factory method to fetch the user for a given email confirmation code, replacing User::newFromConfirma...
UserNameUtils service.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:69
static newFromRow( $row, $data=null)
Create a new user object from a user row.
Definition User.php:769
setItemLoaded( $item)
Set that an item has been loaded.
Definition User.php:1254
Interface for database access objects.
This interface represents the authority associated the current execution context, such as a web reque...
Definition Authority.php:37
Interface for objects representing user identity.
getId( $wikiId=self::LOCAL)
Shared interface for rigor levels when dealing with User methods.
Database cluster connection, tracking, load balancing, and transaction manager interface.