27use InvalidArgumentException;
58 private $loadBalancerFactory;
61 private $loadBalancer;
64 private $userNameUtils;
67 private $lastUserFromIdentity =
null;
80 $this->options = $options;
81 $this->loadBalancerFactory = $loadBalancerFactory;
82 $this->loadBalancer = $loadBalancerFactory->
getMainLB();
83 $this->userNameUtils = $userNameUtils;
106 string $validate = self::RIGOR_VALID
109 $canonicalName = $this->userNameUtils->getCanonical( $name, $validate );
110 if ( $canonicalName ===
false ) {
115 $user->mName = $canonicalName;
116 $user->mFrom =
'name';
117 $user->setItemLoaded(
'name' );
131 if ( !$this->userNameUtils->isIP( $ip ) ) {
132 throw new InvalidArgumentException(
'Invalid IP address' );
135 $user->setName( $ip );
168 $user->mActorId = $actorId;
169 $user->mFrom =
'actor';
183 if ( $userIdentity instanceof
User ) {
184 return $userIdentity;
187 $id = $userIdentity->
getId();
188 $name = $userIdentity->getName();
194 $this->lastUserFromIdentity
195 && $this->lastUserFromIdentity->getId() === $id
196 && $this->lastUserFromIdentity->getName() === $name
198 return $this->lastUserFromIdentity;
201 $this->lastUserFromIdentity = $this->newFromAnyId(
202 $id === 0 ?
null : $id,
203 $name ===
'' ? null : $name,
207 return $this->lastUserFromIdentity;
228 ?
int $actorId =
null,
234 if ( $dbDomain !== false ) {
240 $user->mFrom =
'defaults';
242 if ( $actorId !==
null ) {
243 $user->mActorId = $actorId;
244 if ( $actorId !== 0 ) {
245 $user->mFrom =
'actor';
250 if ( $userName !==
null && $userName !==
'' ) {
251 $user->mName = $userName;
252 $user->mFrom =
'name';
253 $user->setItemLoaded(
'name' );
256 if ( $userId !==
null ) {
257 $user->mId = $userId;
258 if ( $userId !== 0 ) {
261 $user->setItemLoaded(
'id' );
264 if ( $user->mFrom ===
'defaults' ) {
265 throw new InvalidArgumentException(
266 'Cannot create a user with no name, no ID, and no actor ID'
286 string $confirmationCode,
287 int $flags = self::READ_NORMAL
289 [ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
291 $db = $this->loadBalancer->getConnectionRef( $index );
293 $id = $db->newSelectQueryBuilder()
294 ->select(
'user_id' )
296 ->where( [
'user_email_token' => md5( $confirmationCode ) ] )
297 ->andWhere( $db->buildComparison(
'>', [
'user_email_token_expires' => $db->timestamp() ] ) )
298 ->options( $options )
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'))
Helper class for DAO classes.
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.