26use InvalidArgumentException;
57 private $loadBalancerFactory;
60 private $loadBalancer;
63 private $userNameUtils;
66 private $lastUserFromIdentity =
null;
79 $this->options = $options;
80 $this->loadBalancerFactory = $loadBalancerFactory;
81 $this->loadBalancer = $loadBalancerFactory->
getMainLB();
82 $this->userNameUtils = $userNameUtils;
105 string $validate = self::RIGOR_VALID
108 $canonicalName = $this->userNameUtils->getCanonical( $name, $validate );
109 if ( $canonicalName ===
false ) {
114 $user->mName = $canonicalName;
115 $user->mFrom =
'name';
116 $user->setItemLoaded(
'name' );
130 if ( !$this->userNameUtils->isIP( $ip ) ) {
131 throw new InvalidArgumentException(
'Invalid IP address' );
134 $user->setName( $ip );
167 $user->mActorId = $actorId;
168 $user->mFrom =
'actor';
182 if ( $userIdentity instanceof
User ) {
183 return $userIdentity;
186 $id = $userIdentity->
getId();
187 $name = $userIdentity->getName();
193 $this->lastUserFromIdentity
194 && $this->lastUserFromIdentity->getId() === $id
195 && $this->lastUserFromIdentity->getName() === $name
197 return $this->lastUserFromIdentity;
200 $this->lastUserFromIdentity = $this->newFromAnyId(
201 $id === 0 ?
null : $id,
202 $name ===
'' ? null : $name,
206 return $this->lastUserFromIdentity;
227 ?
int $actorId =
null,
233 if ( $dbDomain !== false ) {
239 $user->mFrom =
'defaults';
241 if ( $actorId !==
null ) {
242 $user->mActorId = $actorId;
243 if ( $actorId !== 0 ) {
244 $user->mFrom =
'actor';
249 if ( $userName !==
null && $userName !==
'' ) {
250 $user->mName = $userName;
251 $user->mFrom =
'name';
252 $user->setItemLoaded(
'name' );
255 if ( $userId !==
null ) {
256 $user->mId = $userId;
257 if ( $userId !== 0 ) {
260 $user->setItemLoaded(
'id' );
263 if ( $user->mFrom ===
'defaults' ) {
264 throw new InvalidArgumentException(
265 'Cannot create a user with no name, no ID, and no actor ID'
285 string $confirmationCode,
286 int $flags = IDBAccessObject::READ_NORMAL
288 if ( ( $flags & IDBAccessObject::READ_LATEST ) == IDBAccessObject::READ_LATEST ) {
289 $db = $this->loadBalancer->getConnectionRef(
DB_PRIMARY );
291 $db = $this->loadBalancer->getConnectionRef(
DB_REPLICA );
294 $id = $db->newSelectQueryBuilder()
295 ->select(
'user_id' )
297 ->where( [
'user_email_token' => md5( $confirmationCode ) ] )
298 ->andWhere( $db->expr(
'user_email_token_expires',
'>', $db->timestamp() ) )
300 ->caller( __METHOD__ )->fetchField();
306 return $this->newFromId( (
int)$id );
319 return User::newFromRow( $row, $data );
328 if ( $authority instanceof
User ) {
331 return $this->newFromUserIdentity( $authority->getUser() );
344 $user->setName( $this->userNameUtils->getTempPlaceholder() );
357 $user->setName( $name ?? $this->userNameUtils->getTempPlaceholder() );
372 if ( $wikiId === UserIdentity::LOCAL ) {
373 $legacyUser = $this->newFromUserIdentity( $userIdentity );
375 $legacyUser->invalidateCache();
378 $userId = $userIdentity->
getId( $wikiId );
380 $dbw = $this->getUserTableConnection( ILoadBalancer::DB_PRIMARY, $wikiId );
381 $dbw->newUpdateQueryBuilder()
383 ->set( [
'user_touched' => $dbw->timestamp() ] )
384 ->where( [
'user_id' => $userId ] )
385 ->caller( __METHOD__ )->execute();
387 $dbw->onTransactionPreCommitOrIdle(
388 static function () use ( $wikiId, $userId ) {
389 User::purge( $wikiId, $userId );
401 private function getUserTableConnection( $mode, $wikiId ) {
402 if ( is_string( $wikiId ) && $this->loadBalancerFactory->getLocalDomainID() === $wikiId ) {
403 $wikiId = UserIdentity::LOCAL;
406 if ( $this->options->get( MainConfigNames::SharedDB ) &&
407 in_array(
'user', $this->options->get( MainConfigNames::SharedTables ) )
410 $lb = $this->loadBalancer;
412 $lb = $this->loadBalancerFactory->getMainLB( $wikiId );
415 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.