27 use InvalidArgumentException;
59 private $loadBalancerFactory;
62 private $loadBalancer;
65 private $userNameUtils;
68 private $lastUserFromIdentity =
null;
81 $this->options = $options;
82 $this->loadBalancerFactory = $loadBalancerFactory;
83 $this->loadBalancer = $loadBalancerFactory->
getMainLB();
84 $this->userNameUtils = $userNameUtils;
107 string $validate = self::RIGOR_VALID
110 $canonicalName = $this->userNameUtils->getCanonical( $name, $validate );
111 if ( $canonicalName ===
false ) {
116 $user->mName = $canonicalName;
117 $user->mFrom =
'name';
118 $user->setItemLoaded(
'name' );
132 if ( !$this->userNameUtils->isIP( $ip ) ) {
133 throw new InvalidArgumentException(
'Invalid IP address' );
136 $user->setName( $ip );
169 $user->mActorId = $actorId;
170 $user->mFrom =
'actor';
184 if ( $userIdentity instanceof
User ) {
185 return $userIdentity;
188 $id = $userIdentity->
getId();
189 $name = $userIdentity->getName();
195 $this->lastUserFromIdentity
196 && $this->lastUserFromIdentity->getId() === $id
197 && $this->lastUserFromIdentity->getName() === $name
199 return $this->lastUserFromIdentity;
202 $this->lastUserFromIdentity = $this->newFromAnyId(
203 $id === 0 ?
null : $id,
204 $name ===
'' ?
null : $name,
208 return $this->lastUserFromIdentity;
229 ?
int $actorId =
null,
235 if ( $dbDomain !== false ) {
241 $user->mFrom =
'defaults';
243 if ( $actorId !==
null ) {
244 $user->mActorId = $actorId;
245 if ( $actorId !== 0 ) {
246 $user->mFrom =
'actor';
251 if ( $userName !==
null && $userName !==
'' ) {
252 $user->mName = $userName;
253 $user->mFrom =
'name';
254 $user->setItemLoaded(
'name' );
257 if ( $userId !==
null ) {
258 $user->mId = $userId;
259 if ( $userId !== 0 ) {
262 $user->setItemLoaded(
'id' );
265 if ( $user->mFrom ===
'defaults' ) {
266 throw new InvalidArgumentException(
267 'Cannot create a user with no name, no ID, and no actor ID'
287 string $confirmationCode,
288 int $flags = self::READ_NORMAL
292 $db = $this->loadBalancer->getConnectionRef( $index );
294 $id = $db->selectField(
298 'user_email_token' => md5( $confirmationCode ),
299 'user_email_token_expires > ' . $db->addQuotes( $db->timestamp() ),
309 return $this->newFromId( (
int)$id );
331 if ( $authority instanceof
User ) {
334 return $this->newFromUserIdentity( $authority->getUser() );
347 $user->setName( $this->userNameUtils->getTempPlaceholder() );
360 $user->setName( $name );
375 if ( $wikiId === UserIdentity::LOCAL ) {
376 $legacyUser = $this->newFromUserIdentity( $userIdentity );
378 $legacyUser->invalidateCache();
381 $userId = $userIdentity->
getId( $wikiId );
384 $dbw->newUpdateQueryBuilder()
386 ->set( [
'user_touched' => $dbw->timestamp() ] )
387 ->where( [
'user_id' => $userId ] )
388 ->caller( __METHOD__ )->execute();
390 $dbw->onTransactionPreCommitOrIdle(
391 static function () use ( $wikiId, $userId ) {
404 private function getUserTableConnection( $mode, $wikiId ) {
405 if ( is_string( $wikiId ) && $this->loadBalancerFactory->getLocalDomainID() === $wikiId ) {
406 $wikiId = UserIdentity::LOCAL;
409 if ( $this->options->get( MainConfigNames::SharedDB ) &&
410 in_array(
'user', $this->options->get( MainConfigNames::SharedTables ) )
413 $lb = $this->loadBalancer;
415 $lb = $this->loadBalancerFactory->getMainLB( $wikiId );
418 return $lb->getConnection( $mode, [], $wikiId );
if(!defined('MW_SETUP_CALLBACK'))
Helper class for DAO classes.
static getDBOptions( $bitfield)
Get an appropriate DB index, options, and fallback DB index for a query.
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()
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
static purge( $dbDomain, $userId)
static newFromRow( $row, $data=null)
Create a new user object from a user row.
setItemLoaded( $item)
Set that an item has been loaded.
Interface for database access objects.