26use InvalidArgumentException;
56 private $loadBalancerFactory;
59 private $loadBalancer;
62 private $userNameUtils;
65 private $lastUserFromIdentity =
null;
78 $this->options = $options;
79 $this->loadBalancerFactory = $loadBalancerFactory;
80 $this->loadBalancer = $loadBalancerFactory->
getMainLB();
81 $this->userNameUtils = $userNameUtils;
104 string $validate = self::RIGOR_VALID
107 $canonicalName = $this->userNameUtils->getCanonical( $name, $validate );
108 if ( $canonicalName ===
false ) {
113 $user->mName = $canonicalName;
114 $user->mFrom =
'name';
115 $user->setItemLoaded(
'name' );
129 if ( !$this->userNameUtils->isIP( $ip ) ) {
130 throw new InvalidArgumentException(
'Invalid IP address' );
133 $user->setName( $ip );
166 $user->mActorId = $actorId;
167 $user->mFrom =
'actor';
181 if ( $userIdentity instanceof
User ) {
182 return $userIdentity;
185 $id = $userIdentity->
getId();
186 $name = $userIdentity->getName();
192 $this->lastUserFromIdentity
193 && $this->lastUserFromIdentity->getId() === $id
194 && $this->lastUserFromIdentity->getName() === $name
196 return $this->lastUserFromIdentity;
199 $this->lastUserFromIdentity = $this->newFromAnyId(
200 $id === 0 ?
null : $id,
201 $name ===
'' ? null : $name,
205 return $this->lastUserFromIdentity;
226 ?
int $actorId =
null,
232 if ( $dbDomain !== false ) {
238 $user->mFrom =
'defaults';
240 if ( $actorId !==
null ) {
241 $user->mActorId = $actorId;
242 if ( $actorId !== 0 ) {
243 $user->mFrom =
'actor';
248 if ( $userName !==
null && $userName !==
'' ) {
249 $user->mName = $userName;
250 $user->mFrom =
'name';
251 $user->setItemLoaded(
'name' );
254 if ( $userId !==
null ) {
255 $user->mId = $userId;
256 if ( $userId !== 0 ) {
259 $user->setItemLoaded(
'id' );
262 if ( $user->mFrom ===
'defaults' ) {
263 throw new InvalidArgumentException(
264 'Cannot create a user with no name, no ID, and no actor ID'
284 string $confirmationCode,
285 int $flags = IDBAccessObject::READ_NORMAL
287 if ( ( $flags & IDBAccessObject::READ_LATEST ) == IDBAccessObject::READ_LATEST ) {
288 $db = $this->loadBalancer->getConnectionRef(
DB_PRIMARY );
290 $db = $this->loadBalancer->getConnectionRef(
DB_REPLICA );
293 $id = $db->newSelectQueryBuilder()
294 ->select(
'user_id' )
296 ->where( [
'user_email_token' => md5( $confirmationCode ) ] )
297 ->andWhere( $db->expr(
'user_email_token_expires',
'>', $db->timestamp() ) )
299 ->caller( __METHOD__ )->fetchField();
305 return $this->newFromId( (
int)$id );
318 return User::newFromRow( $row, $data );
327 if ( $authority instanceof
User ) {
330 return $this->newFromUserIdentity( $authority->getUser() );
343 $user->setName( $this->userNameUtils->getTempPlaceholder() );
356 $user->setName( $name ?? $this->userNameUtils->getTempPlaceholder() );
371 if ( $wikiId === UserIdentity::LOCAL ) {
372 $legacyUser = $this->newFromUserIdentity( $userIdentity );
374 $legacyUser->invalidateCache();
377 $userId = $userIdentity->
getId( $wikiId );
379 $dbw = $this->getUserTableConnection( ILoadBalancer::DB_PRIMARY, $wikiId );
380 $dbw->newUpdateQueryBuilder()
382 ->set( [
'user_touched' => $dbw->timestamp() ] )
383 ->where( [
'user_id' => $userId ] )
384 ->caller( __METHOD__ )->execute();
386 $dbw->onTransactionPreCommitOrIdle(
387 static function () use ( $wikiId, $userId ) {
388 User::purge( $wikiId, $userId );
400 private function getUserTableConnection( $mode, $wikiId ) {
401 if ( is_string( $wikiId ) && $this->loadBalancerFactory->getLocalDomainID() === $wikiId ) {
402 $wikiId = UserIdentity::LOCAL;
405 if ( $this->options->get( MainConfigNames::SharedDB ) &&
406 in_array(
'user', $this->options->get( MainConfigNames::SharedTables ) )
409 $lb = $this->loadBalancer;
411 $lb = $this->loadBalancerFactory->getMainLB( $wikiId );
414 return $lb->getConnection( $mode, [], $wikiId );
if(!defined('MW_SETUP_CALLBACK'))
A class containing constants representing the names of configuration variables.
const SharedDB
Name constant for the SharedDB setting, for use with Config::get()
const SharedTables
Name constant for the SharedTables setting, for use with Config::get()
Interface for database access objects.