2021-01-22 19:51:43 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\User;
|
|
|
|
|
|
|
|
|
|
use CannotCreateActorException;
|
|
|
|
|
use DBAccessObjectUtils;
|
2024-01-23 14:01:06 +00:00
|
|
|
use IDBAccessObject;
|
2021-01-22 19:51:43 +00:00
|
|
|
use InvalidArgumentException;
|
Support new block schema
Support migration stages when reading and writing blocks.
I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.
However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.
HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.
When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.
Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-10-27 03:34:10 +00:00
|
|
|
use MediaWiki\Block\HideUserUtils;
|
2021-01-22 19:51:43 +00:00
|
|
|
use MediaWiki\DAO\WikiAwareEntity;
|
2023-08-02 13:27:41 +00:00
|
|
|
use MediaWiki\User\TempUser\TempUserConfig;
|
2021-01-22 19:51:43 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use stdClass;
|
|
|
|
|
use Wikimedia\Assert\Assert;
|
|
|
|
|
use Wikimedia\IPUtils;
|
2021-04-07 17:32:17 +00:00
|
|
|
use Wikimedia\Rdbms\DBQueryError;
|
2021-01-22 19:51:43 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
2023-02-26 13:45:43 +00:00
|
|
|
use Wikimedia\Rdbms\IReadableDatabase;
|
2021-01-22 19:51:43 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Service for interacting with the actor table.
|
|
|
|
|
*
|
|
|
|
|
* @package MediaWiki\User
|
|
|
|
|
* @since 1.36
|
|
|
|
|
*/
|
|
|
|
|
class ActorStore implements UserIdentityLookup, ActorNormalization {
|
|
|
|
|
|
2021-02-04 02:43:09 +00:00
|
|
|
public const UNKNOWN_USER_NAME = 'Unknown user';
|
|
|
|
|
|
2021-04-06 20:53:56 +00:00
|
|
|
private const LOCAL_CACHE_SIZE = 100;
|
2021-02-18 00:49:30 +00:00
|
|
|
|
2023-09-28 15:25:12 +00:00
|
|
|
private ILoadBalancer $loadBalancer;
|
|
|
|
|
private UserNameUtils $userNameUtils;
|
2023-08-02 13:27:41 +00:00
|
|
|
private TempUserConfig $tempUserConfig;
|
2023-09-28 15:25:12 +00:00
|
|
|
private LoggerInterface $logger;
|
Support new block schema
Support migration stages when reading and writing blocks.
I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.
However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.
HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.
When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.
Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-10-27 03:34:10 +00:00
|
|
|
private HideUserUtils $hideUserUtils;
|
2021-01-22 19:51:43 +00:00
|
|
|
|
|
|
|
|
/** @var string|false */
|
|
|
|
|
private $wikiId;
|
|
|
|
|
|
2023-09-28 15:25:12 +00:00
|
|
|
private ActorCache $cache;
|
2021-02-18 00:49:30 +00:00
|
|
|
|
2023-11-29 20:43:12 +00:00
|
|
|
private bool $allowCreateIpActors;
|
|
|
|
|
|
2021-01-22 19:51:43 +00:00
|
|
|
/**
|
|
|
|
|
* @param ILoadBalancer $loadBalancer
|
|
|
|
|
* @param UserNameUtils $userNameUtils
|
2023-08-02 13:27:41 +00:00
|
|
|
* @param TempUserConfig $tempUserConfig
|
2021-01-22 19:51:43 +00:00
|
|
|
* @param LoggerInterface $logger
|
Support new block schema
Support migration stages when reading and writing blocks.
I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.
However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.
HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.
When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.
Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-10-27 03:34:10 +00:00
|
|
|
* @param HideUserUtils $hideUserUtils
|
2021-01-22 19:51:43 +00:00
|
|
|
* @param string|false $wikiId
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
ILoadBalancer $loadBalancer,
|
|
|
|
|
UserNameUtils $userNameUtils,
|
2023-08-02 13:27:41 +00:00
|
|
|
TempUserConfig $tempUserConfig,
|
2021-01-22 19:51:43 +00:00
|
|
|
LoggerInterface $logger,
|
Support new block schema
Support migration stages when reading and writing blocks.
I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.
However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.
HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.
When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.
Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-10-27 03:34:10 +00:00
|
|
|
HideUserUtils $hideUserUtils,
|
2021-01-22 19:51:43 +00:00
|
|
|
$wikiId = WikiAwareEntity::LOCAL
|
|
|
|
|
) {
|
2022-06-17 21:46:29 +00:00
|
|
|
Assert::parameterType( [ 'string', 'false' ], $wikiId, '$wikiId' );
|
2021-01-22 19:51:43 +00:00
|
|
|
|
|
|
|
|
$this->loadBalancer = $loadBalancer;
|
|
|
|
|
$this->userNameUtils = $userNameUtils;
|
2023-08-02 13:27:41 +00:00
|
|
|
$this->tempUserConfig = $tempUserConfig;
|
2021-01-22 19:51:43 +00:00
|
|
|
$this->logger = $logger;
|
Support new block schema
Support migration stages when reading and writing blocks.
I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.
However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.
HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.
When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.
Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-10-27 03:34:10 +00:00
|
|
|
$this->hideUserUtils = $hideUserUtils;
|
2021-01-22 19:51:43 +00:00
|
|
|
$this->wikiId = $wikiId;
|
2021-02-18 00:49:30 +00:00
|
|
|
|
2021-04-16 04:16:55 +00:00
|
|
|
$this->cache = new ActorCache( self::LOCAL_CACHE_SIZE );
|
2023-11-29 20:43:12 +00:00
|
|
|
|
|
|
|
|
$this->allowCreateIpActors = !$this->tempUserConfig->isEnabled();
|
2021-01-22 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Instantiate a new UserIdentity object based on a $row from the actor table.
|
|
|
|
|
*
|
|
|
|
|
* Use this method when an actor row was already fetched from the DB via a join.
|
|
|
|
|
* This method just constructs a new instance and does not try fetching missing
|
|
|
|
|
* values from the DB again, use {@link UserIdentityLookup} for that.
|
|
|
|
|
*
|
|
|
|
|
* @param stdClass $row with the following fields:
|
|
|
|
|
* - int actor_id
|
|
|
|
|
* - string actor_name
|
|
|
|
|
* - int|null actor_user
|
|
|
|
|
* @return UserIdentity
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
|
|
|
|
public function newActorFromRow( stdClass $row ): UserIdentity {
|
|
|
|
|
$actorId = (int)$row->actor_id;
|
|
|
|
|
$userId = isset( $row->actor_user ) ? (int)$row->actor_user : 0;
|
|
|
|
|
if ( $actorId === 0 ) {
|
|
|
|
|
throw new InvalidArgumentException( "Actor ID is 0 for {$row->actor_name} and {$userId}" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$normalizedName = $this->normalizeUserName( $row->actor_name );
|
2021-04-07 00:50:30 +00:00
|
|
|
if ( $normalizedName === null ) {
|
2021-01-22 19:51:43 +00:00
|
|
|
$this->logger->warning( 'Encountered invalid actor name in database', [
|
|
|
|
|
'user_id' => $userId,
|
|
|
|
|
'actor_id' => $actorId,
|
|
|
|
|
'actor_name' => $row->actor_name,
|
|
|
|
|
'wiki_id' => $this->wikiId ?: 'local'
|
|
|
|
|
] );
|
|
|
|
|
// TODO: once we have guaranteed db only contains valid actor names,
|
|
|
|
|
// we can skip normalization here - T273933
|
|
|
|
|
if ( $row->actor_name === '' ) {
|
|
|
|
|
throw new InvalidArgumentException( "Actor name can not be empty for {$userId} and {$actorId}" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-18 00:49:30 +00:00
|
|
|
|
2021-02-15 18:58:09 +00:00
|
|
|
$actor = new UserIdentityValue( $userId, $row->actor_name, $this->wikiId );
|
2021-04-16 04:16:55 +00:00
|
|
|
$this->cache->add( $actorId, $actor );
|
2021-02-18 00:49:30 +00:00
|
|
|
return $actor;
|
2021-01-22 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Instantiate a new UserIdentity object based on field values from a DB row.
|
|
|
|
|
*
|
|
|
|
|
* Until {@link ActorMigration} is completed, the actor table joins alias actor field names
|
|
|
|
|
* to legacy field names. This method is convenience to construct the UserIdentity based on
|
|
|
|
|
* legacy field names. It's more relaxed with typing then ::newFromRow to better support legacy
|
|
|
|
|
* code, so always prefer ::newFromRow in new code. Eventually, once {@link ActorMigration}
|
|
|
|
|
* is completed and all queries use explicit join with actor table, this method will be
|
|
|
|
|
* deprecated and removed.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
* @param int|null $userId
|
2021-02-09 14:46:24 +00:00
|
|
|
* @param string|null $name
|
|
|
|
|
* @param int|null $actorId
|
2021-01-22 19:51:43 +00:00
|
|
|
* @return UserIdentity
|
|
|
|
|
*/
|
2021-11-26 17:06:36 +00:00
|
|
|
public function newActorFromRowFields( $userId, $name, $actorId ): UserIdentity {
|
2021-01-22 19:51:43 +00:00
|
|
|
// For backwards compatibility we are quite relaxed about what to accept,
|
2021-11-22 15:41:36 +00:00
|
|
|
// but try not to create entirely incorrect objects. As we move more code
|
2021-01-22 19:51:43 +00:00
|
|
|
// from ActorMigration aliases to proper join with the actor table,
|
|
|
|
|
// we should use ::newActorFromRow more, and eventually deprecate this method.
|
|
|
|
|
$userId = $userId === null ? 0 : (int)$userId;
|
2022-10-21 02:26:49 +00:00
|
|
|
$name ??= '';
|
2021-01-22 19:51:43 +00:00
|
|
|
if ( $actorId === null ) {
|
|
|
|
|
throw new InvalidArgumentException( "Actor ID is null for {$name} and {$userId}" );
|
|
|
|
|
}
|
|
|
|
|
if ( (int)$actorId === 0 ) {
|
|
|
|
|
throw new InvalidArgumentException( "Actor ID is 0 for {$name} and {$userId}" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$normalizedName = $this->normalizeUserName( $name );
|
2021-04-07 00:50:30 +00:00
|
|
|
if ( $normalizedName === null ) {
|
2021-01-22 19:51:43 +00:00
|
|
|
$this->logger->warning( 'Encountered invalid actor name in database', [
|
|
|
|
|
'user_id' => $userId,
|
|
|
|
|
'actor_id' => $actorId,
|
|
|
|
|
'actor_name' => $name,
|
|
|
|
|
'wiki_id' => $this->wikiId ?: 'local'
|
|
|
|
|
] );
|
|
|
|
|
// TODO: once we have guaranteed the DB entries only exist for normalized names,
|
|
|
|
|
// we can skip normalization here - T273933
|
|
|
|
|
if ( $name === '' ) {
|
|
|
|
|
throw new InvalidArgumentException( "Actor name can not be empty for {$userId} and {$actorId}" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-18 00:49:30 +00:00
|
|
|
|
|
|
|
|
$actorId = (int)$actorId;
|
|
|
|
|
$actor = new UserIdentityValue(
|
2021-01-22 19:51:43 +00:00
|
|
|
$userId,
|
|
|
|
|
$name,
|
|
|
|
|
$this->wikiId
|
|
|
|
|
);
|
2021-02-18 00:49:30 +00:00
|
|
|
|
2021-04-16 04:16:55 +00:00
|
|
|
$this->cache->add( $actorId, $actor );
|
2021-02-18 00:49:30 +00:00
|
|
|
return $actor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param UserIdentity $actor
|
2021-04-16 04:16:55 +00:00
|
|
|
* @internal for use in User object only
|
2021-02-18 00:49:30 +00:00
|
|
|
*/
|
2021-04-16 04:16:55 +00:00
|
|
|
public function deleteUserIdentityFromCache( UserIdentity $actor ) {
|
|
|
|
|
$this->cache->remove( $actor );
|
2021-03-22 19:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-22 19:51:43 +00:00
|
|
|
/**
|
|
|
|
|
* Find an actor by $id.
|
|
|
|
|
*
|
|
|
|
|
* @param int $actorId
|
2023-05-25 15:24:57 +00:00
|
|
|
* @param IReadableDatabase $db The database connection to operate on.
|
2021-03-04 21:46:45 +00:00
|
|
|
* The database must correspond to ActorStore's wiki ID.
|
2021-01-22 19:51:43 +00:00
|
|
|
* @return UserIdentity|null Returns null if no actor with this $actorId exists in the database.
|
|
|
|
|
*/
|
2023-05-25 15:24:57 +00:00
|
|
|
public function getActorById( int $actorId, IReadableDatabase $db ): ?UserIdentity {
|
2021-03-18 21:26:18 +00:00
|
|
|
$this->checkDatabaseDomain( $db );
|
|
|
|
|
|
2021-01-22 19:51:43 +00:00
|
|
|
if ( !$actorId ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-02-18 00:49:30 +00:00
|
|
|
|
2021-04-16 04:16:55 +00:00
|
|
|
return $this->cache->getActor( ActorCache::KEY_ACTOR_ID, $actorId ) ??
|
|
|
|
|
$this->newSelectQueryBuilder( $db )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->conds( [ 'actor_id' => $actorId ] )
|
|
|
|
|
->fetchUserIdentity() ??
|
|
|
|
|
// The actor ID mostly comes from DB, so if we can't find an actor by ID,
|
|
|
|
|
// it's most likely due to lagged replica and not cause it doesn't actually exist.
|
2021-09-01 21:04:40 +00:00
|
|
|
// Probably we just inserted it? Try primary database.
|
2024-01-23 14:01:06 +00:00
|
|
|
$this->newSelectQueryBuilder( IDBAccessObject::READ_LATEST )
|
2021-03-18 21:26:18 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->conds( [ 'actor_id' => $actorId ] )
|
|
|
|
|
->fetchUserIdentity();
|
2021-01-22 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find an actor by $name
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param int $queryFlags one of IDBAccessObject constants
|
|
|
|
|
* @return UserIdentity|null
|
|
|
|
|
*/
|
2024-01-23 14:01:06 +00:00
|
|
|
public function getUserIdentityByName(
|
|
|
|
|
string $name,
|
|
|
|
|
int $queryFlags = IDBAccessObject::READ_NORMAL
|
|
|
|
|
): ?UserIdentity {
|
2021-04-07 00:50:30 +00:00
|
|
|
$normalizedName = $this->normalizeUserName( $name );
|
|
|
|
|
if ( $normalizedName === null ) {
|
2021-05-19 18:09:04 +00:00
|
|
|
return null;
|
2021-01-22 19:51:43 +00:00
|
|
|
}
|
2021-02-18 00:49:30 +00:00
|
|
|
|
2021-04-16 04:16:55 +00:00
|
|
|
return $this->cache->getActor( ActorCache::KEY_USER_NAME, $normalizedName ) ??
|
2021-05-13 23:08:31 +00:00
|
|
|
$this->newSelectQueryBuilder( $queryFlags )
|
2021-04-16 04:16:55 +00:00
|
|
|
->caller( __METHOD__ )
|
2021-08-03 17:43:16 +00:00
|
|
|
->whereUserNames( $normalizedName )
|
2021-04-16 04:16:55 +00:00
|
|
|
->fetchUserIdentity();
|
2021-01-22 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find an actor by $userId
|
|
|
|
|
*
|
|
|
|
|
* @param int $userId
|
|
|
|
|
* @param int $queryFlags one of IDBAccessObject constants
|
|
|
|
|
* @return UserIdentity|null
|
|
|
|
|
*/
|
2024-01-23 14:01:06 +00:00
|
|
|
public function getUserIdentityByUserId(
|
|
|
|
|
int $userId,
|
|
|
|
|
int $queryFlags = IDBAccessObject::READ_NORMAL
|
|
|
|
|
): ?UserIdentity {
|
2021-01-22 19:51:43 +00:00
|
|
|
if ( !$userId ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-02-18 00:49:30 +00:00
|
|
|
|
2021-04-16 04:16:55 +00:00
|
|
|
return $this->cache->getActor( ActorCache::KEY_USER_ID, $userId ) ??
|
2021-05-13 23:08:31 +00:00
|
|
|
$this->newSelectQueryBuilder( $queryFlags )
|
2021-04-16 04:16:55 +00:00
|
|
|
->caller( __METHOD__ )
|
2021-08-03 17:43:16 +00:00
|
|
|
->whereUserIds( $userId )
|
2021-04-16 04:16:55 +00:00
|
|
|
->fetchUserIdentity();
|
2021-01-22 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-23 12:12:30 +00:00
|
|
|
/**
|
|
|
|
|
* Attach the actor ID to $user for backwards compatibility.
|
|
|
|
|
*
|
|
|
|
|
* @todo remove this method when no longer needed (T273974).
|
|
|
|
|
*
|
|
|
|
|
* @param UserIdentity $user
|
|
|
|
|
* @param int $id
|
2021-11-04 21:32:39 +00:00
|
|
|
* @param bool $assigned whether a new actor ID was just assigned.
|
2021-02-23 12:12:30 +00:00
|
|
|
*/
|
2021-04-20 11:49:35 +00:00
|
|
|
private function attachActorId( UserIdentity $user, int $id, bool $assigned ) {
|
2021-02-15 18:58:09 +00:00
|
|
|
if ( $user instanceof User ) {
|
2021-02-23 12:12:30 +00:00
|
|
|
$user->setActorId( $id );
|
2021-04-20 11:49:35 +00:00
|
|
|
if ( $assigned ) {
|
|
|
|
|
$user->invalidateCache();
|
|
|
|
|
}
|
2021-02-23 12:12:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 19:53:29 +00:00
|
|
|
/**
|
|
|
|
|
* Detach the actor ID from $user for backwards compatibility.
|
|
|
|
|
*
|
|
|
|
|
* @todo remove this method when no longer needed (T273974).
|
|
|
|
|
*
|
|
|
|
|
* @param UserIdentity $user
|
|
|
|
|
*/
|
|
|
|
|
private function detachActorId( UserIdentity $user ) {
|
2021-02-15 18:58:09 +00:00
|
|
|
if ( $user instanceof User ) {
|
2021-03-22 19:53:29 +00:00
|
|
|
$user->setActorId( 0 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 19:51:43 +00:00
|
|
|
/**
|
|
|
|
|
* Find the actor_id of the given $user.
|
|
|
|
|
*
|
|
|
|
|
* @param UserIdentity $user
|
2023-02-26 13:45:43 +00:00
|
|
|
* @param IReadableDatabase $db The database connection to operate on.
|
2021-03-04 21:46:45 +00:00
|
|
|
* The database must correspond to ActorStore's wiki ID.
|
2021-01-22 19:51:43 +00:00
|
|
|
* @return int|null
|
|
|
|
|
*/
|
2023-02-26 13:45:43 +00:00
|
|
|
public function findActorId( UserIdentity $user, IReadableDatabase $db ): ?int {
|
2021-01-22 19:51:43 +00:00
|
|
|
// TODO: we want to assert this user belongs to the correct wiki,
|
|
|
|
|
// but User objects are always local and we used to use them
|
|
|
|
|
// on a non-local DB connection. We need to first deprecate this
|
|
|
|
|
// possibility and then throw on mismatching User object - T273972
|
|
|
|
|
// $user->assertWiki( $this->wikiId );
|
2021-04-27 20:52:01 +00:00
|
|
|
$this->deprecateInvalidCrossWikiParam( $user );
|
2021-02-23 12:12:30 +00:00
|
|
|
|
|
|
|
|
// TODO: In the future we would be able to assume UserIdentity name is ok
|
|
|
|
|
// and will be able to skip normalization here - T273933
|
|
|
|
|
$name = $this->normalizeUserName( $user->getName() );
|
2021-04-07 00:50:30 +00:00
|
|
|
if ( $name === null ) {
|
2021-02-23 12:12:30 +00:00
|
|
|
$this->logger->warning( 'Encountered a UserIdentity with invalid name', [
|
|
|
|
|
'user_name' => $user->getName()
|
|
|
|
|
] );
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 21:46:45 +00:00
|
|
|
$id = $this->findActorIdInternal( $name, $db );
|
2021-02-23 12:12:30 +00:00
|
|
|
|
2021-02-15 18:58:09 +00:00
|
|
|
// Set the actor ID in the User object. To be removed, see T274148.
|
|
|
|
|
if ( $id && $user instanceof User ) {
|
|
|
|
|
$user->setActorId( $id );
|
2021-02-23 12:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find the actor_id of the given $name.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
2023-05-25 15:24:57 +00:00
|
|
|
* @param IReadableDatabase $db The database connection to operate on.
|
2021-03-04 21:46:45 +00:00
|
|
|
* The database must correspond to ActorStore's wiki ID.
|
2021-02-23 12:12:30 +00:00
|
|
|
* @return int|null
|
|
|
|
|
*/
|
2023-05-25 15:24:57 +00:00
|
|
|
public function findActorIdByName( $name, IReadableDatabase $db ): ?int {
|
2021-04-29 12:57:23 +00:00
|
|
|
$name = $this->normalizeUserName( $name );
|
2021-04-07 00:50:30 +00:00
|
|
|
if ( $name === null ) {
|
2021-02-23 12:12:30 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 04:16:55 +00:00
|
|
|
return $this->findActorIdInternal( $name, $db );
|
2021-01-22 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find actor_id of the given $user using the passed $db connection.
|
|
|
|
|
*
|
2021-02-23 12:12:30 +00:00
|
|
|
* @param string $name
|
2023-02-26 13:45:43 +00:00
|
|
|
* @param IReadableDatabase $db The database connection to operate on.
|
2021-03-04 21:46:45 +00:00
|
|
|
* The database must correspond to ActorStore's wiki ID.
|
2023-07-25 13:37:41 +00:00
|
|
|
* @param bool $lockInShareMode
|
2021-01-22 19:51:43 +00:00
|
|
|
* @return int|null
|
|
|
|
|
*/
|
2021-03-01 22:39:13 +00:00
|
|
|
private function findActorIdInternal(
|
|
|
|
|
string $name,
|
2023-02-26 13:45:43 +00:00
|
|
|
IReadableDatabase $db,
|
2023-07-25 13:37:41 +00:00
|
|
|
bool $lockInShareMode = false
|
2021-03-01 22:39:13 +00:00
|
|
|
): ?int {
|
2021-01-22 19:51:43 +00:00
|
|
|
// Note: UserIdentity::getActorId will be deprecated and removed,
|
|
|
|
|
// and this is the replacement for it. Can't call User::getActorId, cause
|
|
|
|
|
// User always thinks it's local, so we could end up fetching the ID
|
|
|
|
|
// from the wrong database.
|
|
|
|
|
|
2021-04-16 04:16:55 +00:00
|
|
|
$cachedValue = $this->cache->getActorId( ActorCache::KEY_USER_NAME, $name );
|
2021-02-18 00:49:30 +00:00
|
|
|
if ( $cachedValue ) {
|
2021-04-16 04:16:55 +00:00
|
|
|
return $cachedValue;
|
2021-02-18 00:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-25 13:37:41 +00:00
|
|
|
$queryBuilder = $db->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'actor_user', 'actor_name', 'actor_id' ] )
|
|
|
|
|
->from( 'actor' )
|
|
|
|
|
->where( [ 'actor_name' => $name ] );
|
|
|
|
|
if ( $lockInShareMode ) {
|
|
|
|
|
$queryBuilder->lockInShareMode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$row = $queryBuilder->caller( __METHOD__ )->fetchRow();
|
2021-01-22 19:51:43 +00:00
|
|
|
|
|
|
|
|
if ( !$row || !$row->actor_id ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-02-18 00:49:30 +00:00
|
|
|
// to cache row
|
|
|
|
|
$this->newActorFromRow( $row );
|
|
|
|
|
|
2021-04-07 17:32:17 +00:00
|
|
|
return (int)$row->actor_id;
|
2021-01-22 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-07 17:32:17 +00:00
|
|
|
* Attempt to assign an actor ID to the given $user. If it is already assigned,
|
|
|
|
|
* return the existing ID.
|
2021-01-22 19:51:43 +00:00
|
|
|
*
|
2021-03-22 19:53:29 +00:00
|
|
|
* @note If called within a transaction, the returned ID might become invalid
|
|
|
|
|
* if the transaction is rolled back, so it should not be passed outside of the
|
|
|
|
|
* transaction context.
|
|
|
|
|
*
|
2021-01-22 19:51:43 +00:00
|
|
|
* @param UserIdentity $user
|
2021-05-25 14:28:19 +00:00
|
|
|
* @param IDatabase $dbw The database connection to acquire the ID from.
|
2021-03-04 21:46:45 +00:00
|
|
|
* The database must correspond to ActorStore's wiki ID.
|
2021-04-07 17:32:17 +00:00
|
|
|
* @return int actor ID greater then 0
|
2021-01-22 19:51:43 +00:00
|
|
|
* @throws CannotCreateActorException if no actor ID has been assigned to this $user
|
|
|
|
|
*/
|
2021-05-25 14:28:19 +00:00
|
|
|
public function acquireActorId( UserIdentity $user, IDatabase $dbw ): int {
|
|
|
|
|
$this->checkDatabaseDomain( $dbw );
|
2021-04-07 17:32:17 +00:00
|
|
|
[ $userId, $userName ] = $this->validateActorForInsertion( $user );
|
2021-02-23 12:12:30 +00:00
|
|
|
|
2021-02-18 00:49:30 +00:00
|
|
|
// allow cache to be used, because if it is in the cache, it already has an actor ID
|
2021-02-23 12:12:30 +00:00
|
|
|
$existingActorId = $this->findActorIdInternal( $userName, $dbw );
|
2021-01-22 19:51:43 +00:00
|
|
|
if ( $existingActorId ) {
|
2021-04-20 11:49:35 +00:00
|
|
|
$this->attachActorId( $user, $existingActorId, false );
|
2021-01-22 19:51:43 +00:00
|
|
|
return $existingActorId;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 20:05:22 +00:00
|
|
|
$dbw->newInsertQueryBuilder()
|
In query builders, use insertInto() and deleteFrom() instead of insert() and delete()
The design principle for SelectQueryBuilder was to make the chained
builder calls look as much like SQL as possible, so that developers
could leverage their knowledge of SQL to understand what the query
builder is doing.
That's why SelectQueryBuilder::select() takes a list of fields, and by
the same principle, it makes sense for UpdateQueryBuilder::update() to
take a table. However with "insert" and "delete", the SQL designers
chose to add prepositions "into" and "from", and I think it makes sense
to follow that here.
In terms of natural language, we update a table, but we don't delete a
table, or insert a table. We delete rows from a table, or insert rows
into a table. The table is not the object of the verb.
So, add insertInto() as an alias for insert(), and add deleteFrom() as
an alias for delete(). Use the new methods in MW core callers where
PHPStorm knows the type.
Change-Id: Idb327a54a57a0fb2288ea067472c1e9727016000
2023-09-08 00:06:59 +00:00
|
|
|
->insertInto( 'actor' )
|
2023-08-03 20:05:22 +00:00
|
|
|
->ignore()
|
|
|
|
|
->row( [ 'actor_user' => $userId, 'actor_name' => $userName ] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
2021-04-07 17:32:17 +00:00
|
|
|
|
2021-01-22 19:51:43 +00:00
|
|
|
if ( $dbw->affectedRows() ) {
|
2021-04-07 17:32:17 +00:00
|
|
|
$actorId = $dbw->insertId();
|
2021-01-22 19:51:43 +00:00
|
|
|
} else {
|
|
|
|
|
// Outdated cache?
|
|
|
|
|
// Use LOCK IN SHARE MODE to bypass any MySQL REPEATABLE-READ snapshot.
|
2023-07-25 13:37:41 +00:00
|
|
|
$actorId = $this->findActorIdInternal( $userName, $dbw, true );
|
2021-01-22 19:51:43 +00:00
|
|
|
if ( !$actorId ) {
|
|
|
|
|
throw new CannotCreateActorException(
|
|
|
|
|
"Failed to create actor ID for " .
|
|
|
|
|
"user_id={$userId} user_name=\"{$userName}\""
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 11:49:35 +00:00
|
|
|
$this->attachActorId( $user, $actorId, true );
|
2021-04-07 17:32:17 +00:00
|
|
|
// Cache row we've just created
|
|
|
|
|
$cachedUserIdentity = $this->newActorFromRowFields( $userId, $userName, $actorId );
|
2021-04-16 04:16:55 +00:00
|
|
|
$this->setUpRollbackHandler( $dbw, $cachedUserIdentity, $user );
|
2021-04-07 17:32:17 +00:00
|
|
|
return $actorId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new actor for the given $user. If an actor with this name already exists,
|
|
|
|
|
* this method throws.
|
|
|
|
|
*
|
|
|
|
|
* @note If called within a transaction, the returned ID might become invalid
|
|
|
|
|
* if the transaction is rolled back, so it should not be passed outside of the
|
|
|
|
|
* transaction context.
|
|
|
|
|
*
|
|
|
|
|
* @param UserIdentity $user
|
|
|
|
|
* @param IDatabase $dbw
|
|
|
|
|
* @return int actor ID greater then 0
|
|
|
|
|
* @throws CannotCreateActorException if an actor with this name already exist.
|
|
|
|
|
* @internal for use in user account creation only.
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function createNewActor( UserIdentity $user, IDatabase $dbw ): int {
|
2021-04-07 17:32:17 +00:00
|
|
|
$this->checkDatabaseDomain( $dbw );
|
|
|
|
|
[ $userId, $userName ] = $this->validateActorForInsertion( $user );
|
|
|
|
|
|
|
|
|
|
try {
|
2023-08-03 20:05:22 +00:00
|
|
|
$dbw->newInsertQueryBuilder()
|
In query builders, use insertInto() and deleteFrom() instead of insert() and delete()
The design principle for SelectQueryBuilder was to make the chained
builder calls look as much like SQL as possible, so that developers
could leverage their knowledge of SQL to understand what the query
builder is doing.
That's why SelectQueryBuilder::select() takes a list of fields, and by
the same principle, it makes sense for UpdateQueryBuilder::update() to
take a table. However with "insert" and "delete", the SQL designers
chose to add prepositions "into" and "from", and I think it makes sense
to follow that here.
In terms of natural language, we update a table, but we don't delete a
table, or insert a table. We delete rows from a table, or insert rows
into a table. The table is not the object of the verb.
So, add insertInto() as an alias for insert(), and add deleteFrom() as
an alias for delete(). Use the new methods in MW core callers where
PHPStorm knows the type.
Change-Id: Idb327a54a57a0fb2288ea067472c1e9727016000
2023-09-08 00:06:59 +00:00
|
|
|
->insertInto( 'actor' )
|
2023-08-03 20:05:22 +00:00
|
|
|
->row( [ 'actor_user' => $userId, 'actor_name' => $userName ] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
2021-04-07 17:32:17 +00:00
|
|
|
} catch ( DBQueryError $e ) {
|
|
|
|
|
// We rely on the database to crash on unique actor_name constraint.
|
|
|
|
|
throw new CannotCreateActorException( $e->getMessage() );
|
2021-02-15 18:58:09 +00:00
|
|
|
}
|
2021-04-07 17:32:17 +00:00
|
|
|
$actorId = $dbw->insertId();
|
2021-01-22 19:51:43 +00:00
|
|
|
|
2021-04-20 11:49:35 +00:00
|
|
|
$this->attachActorId( $user, $actorId, true );
|
2021-03-22 19:53:29 +00:00
|
|
|
// Cache row we've just created
|
|
|
|
|
$cachedUserIdentity = $this->newActorFromRowFields( $userId, $userName, $actorId );
|
2021-04-16 04:16:55 +00:00
|
|
|
$this->setUpRollbackHandler( $dbw, $cachedUserIdentity, $user );
|
2021-04-07 17:32:17 +00:00
|
|
|
|
|
|
|
|
return $actorId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attempt to assign an ID to an actor for a system user. If an actor ID already
|
|
|
|
|
* exists, return it.
|
|
|
|
|
*
|
|
|
|
|
* @note For reserved user names this method will overwrite the user ID of the
|
|
|
|
|
* existing anon actor.
|
|
|
|
|
*
|
|
|
|
|
* @note If called within a transaction, the returned ID might become invalid
|
|
|
|
|
* if the transaction is rolled back, so it should not be passed outside of the
|
|
|
|
|
* transaction context.
|
|
|
|
|
*
|
|
|
|
|
* @param UserIdentity $user
|
|
|
|
|
* @param IDatabase $dbw
|
|
|
|
|
* @return int actor ID greater then zero
|
|
|
|
|
* @throws CannotCreateActorException if the existing actor is associated with registered user.
|
|
|
|
|
* @internal for use in user account creation only.
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function acquireSystemActorId( UserIdentity $user, IDatabase $dbw ): int {
|
2021-04-07 17:32:17 +00:00
|
|
|
$this->checkDatabaseDomain( $dbw );
|
|
|
|
|
[ $userId, $userName ] = $this->validateActorForInsertion( $user );
|
|
|
|
|
|
|
|
|
|
$existingActorId = $this->findActorIdInternal( $userName, $dbw );
|
|
|
|
|
if ( $existingActorId ) {
|
|
|
|
|
// It certainly will be cached if we just found it.
|
2021-04-16 04:16:55 +00:00
|
|
|
$existingActor = $this->cache->getActor( ActorCache::KEY_ACTOR_ID, $existingActorId );
|
2021-04-07 17:32:17 +00:00
|
|
|
|
|
|
|
|
// If we already have an existing actor with a matching user ID
|
|
|
|
|
// just return it, nothing to do here.
|
|
|
|
|
if ( $existingActor->getId( $this->wikiId ) === $user->getId( $this->wikiId ) ) {
|
|
|
|
|
return $existingActorId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allow overwriting user ID for already existing actor with reserved user name, see T236444
|
|
|
|
|
if ( $this->userNameUtils->isUsable( $userName ) || $existingActor->isRegistered() ) {
|
|
|
|
|
throw new CannotCreateActorException(
|
|
|
|
|
'Cannot replace user for existing actor: ' .
|
|
|
|
|
"actor_id=$existingActorId, new user_id=$userId"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-05 19:21:03 +00:00
|
|
|
$dbw->newInsertQueryBuilder()
|
In query builders, use insertInto() and deleteFrom() instead of insert() and delete()
The design principle for SelectQueryBuilder was to make the chained
builder calls look as much like SQL as possible, so that developers
could leverage their knowledge of SQL to understand what the query
builder is doing.
That's why SelectQueryBuilder::select() takes a list of fields, and by
the same principle, it makes sense for UpdateQueryBuilder::update() to
take a table. However with "insert" and "delete", the SQL designers
chose to add prepositions "into" and "from", and I think it makes sense
to follow that here.
In terms of natural language, we update a table, but we don't delete a
table, or insert a table. We delete rows from a table, or insert rows
into a table. The table is not the object of the verb.
So, add insertInto() as an alias for insert(), and add deleteFrom() as
an alias for delete(). Use the new methods in MW core callers where
PHPStorm knows the type.
Change-Id: Idb327a54a57a0fb2288ea067472c1e9727016000
2023-09-08 00:06:59 +00:00
|
|
|
->insertInto( 'actor' )
|
2023-09-05 19:21:03 +00:00
|
|
|
->row( [ 'actor_name' => $userName, 'actor_user' => $userId ] )
|
|
|
|
|
->onDuplicateKeyUpdate()
|
|
|
|
|
->uniqueIndexFields( [ 'actor_name' ] )
|
|
|
|
|
->set( [ 'actor_user' => $userId ] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
2021-04-07 17:32:17 +00:00
|
|
|
if ( !$dbw->affectedRows() ) {
|
|
|
|
|
throw new CannotCreateActorException(
|
|
|
|
|
'Failed to replace user for actor: ' .
|
|
|
|
|
"actor_id=$existingActorId, new user_id=$userId"
|
|
|
|
|
);
|
2021-03-22 19:53:29 +00:00
|
|
|
}
|
2021-04-07 17:32:17 +00:00
|
|
|
$actorId = $dbw->insertId() ?: $existingActorId;
|
2021-03-22 19:53:29 +00:00
|
|
|
|
2021-04-16 04:16:55 +00:00
|
|
|
$this->cache->remove( $user );
|
2021-04-20 11:49:35 +00:00
|
|
|
$this->attachActorId( $user, $actorId, true );
|
2021-04-07 17:32:17 +00:00
|
|
|
// Cache row we've just created
|
|
|
|
|
$cachedUserIdentity = $this->newActorFromRowFields( $userId, $userName, $actorId );
|
2021-04-16 04:16:55 +00:00
|
|
|
$this->setUpRollbackHandler( $dbw, $cachedUserIdentity, $user );
|
2021-01-22 19:51:43 +00:00
|
|
|
return $actorId;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 02:15:43 +00:00
|
|
|
/**
|
|
|
|
|
* Delete the actor from the actor table
|
|
|
|
|
*
|
|
|
|
|
* @warning this method does very limited validation and is extremely
|
|
|
|
|
* dangerous since it can break referential integrity of the database
|
|
|
|
|
* if used incorrectly. Use at your own risk!
|
|
|
|
|
*
|
|
|
|
|
* @since 1.37
|
|
|
|
|
* @param UserIdentity $actor
|
|
|
|
|
* @param IDatabase $dbw
|
|
|
|
|
* @return bool true on success, false if nothing was deleted.
|
|
|
|
|
*/
|
|
|
|
|
public function deleteActor( UserIdentity $actor, IDatabase $dbw ): bool {
|
|
|
|
|
$this->checkDatabaseDomain( $dbw );
|
|
|
|
|
$this->deprecateInvalidCrossWikiParam( $actor );
|
|
|
|
|
|
|
|
|
|
$normalizedName = $this->normalizeUserName( $actor->getName() );
|
|
|
|
|
if ( $normalizedName === null ) {
|
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
|
"Unable to normalize the provided actor name {$actor->getName()}"
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-04-29 22:03:51 +00:00
|
|
|
$dbw->newDeleteQueryBuilder()
|
In query builders, use insertInto() and deleteFrom() instead of insert() and delete()
The design principle for SelectQueryBuilder was to make the chained
builder calls look as much like SQL as possible, so that developers
could leverage their knowledge of SQL to understand what the query
builder is doing.
That's why SelectQueryBuilder::select() takes a list of fields, and by
the same principle, it makes sense for UpdateQueryBuilder::update() to
take a table. However with "insert" and "delete", the SQL designers
chose to add prepositions "into" and "from", and I think it makes sense
to follow that here.
In terms of natural language, we update a table, but we don't delete a
table, or insert a table. We delete rows from a table, or insert rows
into a table. The table is not the object of the verb.
So, add insertInto() as an alias for insert(), and add deleteFrom() as
an alias for delete(). Use the new methods in MW core callers where
PHPStorm knows the type.
Change-Id: Idb327a54a57a0fb2288ea067472c1e9727016000
2023-09-08 00:06:59 +00:00
|
|
|
->deleteFrom( 'actor' )
|
2023-04-29 22:03:51 +00:00
|
|
|
->where( [ 'actor_name' => $normalizedName ] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
2021-04-27 02:15:43 +00:00
|
|
|
if ( $dbw->affectedRows() !== 0 ) {
|
|
|
|
|
$this->cache->remove( $actor );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 19:51:43 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a canonical form of user name suitable for storage.
|
|
|
|
|
*
|
2021-02-10 21:56:38 +00:00
|
|
|
* @internal
|
2021-01-22 19:51:43 +00:00
|
|
|
* @param string $name
|
2021-02-23 12:12:30 +00:00
|
|
|
*
|
2021-01-22 19:51:43 +00:00
|
|
|
* @return string|null
|
|
|
|
|
*/
|
2021-04-29 12:57:23 +00:00
|
|
|
public function normalizeUserName( string $name ): ?string {
|
2021-01-22 19:51:43 +00:00
|
|
|
if ( $this->userNameUtils->isIP( $name ) ) {
|
|
|
|
|
return IPUtils::sanitizeIP( $name );
|
2021-02-23 12:12:30 +00:00
|
|
|
} elseif ( ExternalUserNames::isExternal( $name ) ) {
|
2021-01-22 19:51:43 +00:00
|
|
|
// TODO: ideally, we should probably canonicalize external usernames,
|
|
|
|
|
// but it was not done before, so we can not start doing it unless we
|
|
|
|
|
// fix existing DB rows - T273933
|
|
|
|
|
return $name;
|
2021-02-23 12:12:30 +00:00
|
|
|
} else {
|
2021-04-29 12:57:23 +00:00
|
|
|
$normalized = $this->userNameUtils->getCanonical( $name );
|
|
|
|
|
return $normalized === false ? null : $normalized;
|
2021-01-22 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-07 17:32:17 +00:00
|
|
|
/**
|
|
|
|
|
* Validates actor before insertion.
|
|
|
|
|
*
|
|
|
|
|
* @param UserIdentity $user
|
|
|
|
|
* @return array [ $normalizedUserId, $normalizedName ]
|
|
|
|
|
*/
|
|
|
|
|
private function validateActorForInsertion( UserIdentity $user ): array {
|
|
|
|
|
// TODO: we want to assert this user belongs to the correct wiki,
|
|
|
|
|
// but User objects are always local and we used to use them
|
|
|
|
|
// on a non-local DB connection. We need to first deprecate this
|
|
|
|
|
// possibility and then throw on mismatching User object - T273972
|
|
|
|
|
// $user->assertWiki( $this->wikiId );
|
2021-04-27 20:52:01 +00:00
|
|
|
$this->deprecateInvalidCrossWikiParam( $user );
|
2021-04-07 17:32:17 +00:00
|
|
|
|
|
|
|
|
$userName = $this->normalizeUserName( $user->getName() );
|
|
|
|
|
if ( $userName === null || $userName === '' ) {
|
|
|
|
|
$userIdForErrorMessage = $user->getId( $this->wikiId );
|
|
|
|
|
throw new CannotCreateActorException(
|
|
|
|
|
'Cannot create an actor for a user with no name: ' .
|
|
|
|
|
"user_id={$userIdForErrorMessage} user_name=\"{$user->getName()}\""
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$userId = $user->getId( $this->wikiId ) ?: null;
|
|
|
|
|
if ( $userId === null && $this->userNameUtils->isUsable( $user->getName() ) ) {
|
|
|
|
|
throw new CannotCreateActorException(
|
|
|
|
|
'Cannot create an actor for a usable name that is not an existing user: ' .
|
|
|
|
|
"user_name=\"{$user->getName()}\""
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-10-13 15:47:15 +00:00
|
|
|
|
2023-11-29 20:43:12 +00:00
|
|
|
if ( !$this->allowCreateIpActors && $this->userNameUtils->isIP( $userName ) ) {
|
2023-10-13 15:47:15 +00:00
|
|
|
throw new CannotCreateActorException(
|
|
|
|
|
'Cannot create an actor for an IP user when temporary accounts are enabled'
|
|
|
|
|
);
|
2023-10-13 15:47:15 +00:00
|
|
|
}
|
2021-04-07 17:32:17 +00:00
|
|
|
return [ $userId, $userName ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear in-process caches if transaction gets rolled back.
|
|
|
|
|
*
|
|
|
|
|
* @param IDatabase $dbw
|
|
|
|
|
* @param UserIdentity $cachedActor
|
|
|
|
|
* @param UserIdentity $originalActor
|
|
|
|
|
*/
|
|
|
|
|
private function setUpRollbackHandler(
|
|
|
|
|
IDatabase $dbw,
|
|
|
|
|
UserIdentity $cachedActor,
|
|
|
|
|
UserIdentity $originalActor
|
|
|
|
|
) {
|
|
|
|
|
if ( $dbw->trxLevel() ) {
|
|
|
|
|
// If called within a transaction and it was rolled back, the cached actor ID
|
|
|
|
|
// becomes invalid, so cache needs to be invalidated as well. See T277795.
|
|
|
|
|
$dbw->onTransactionResolution(
|
2021-04-16 04:16:55 +00:00
|
|
|
function ( int $trigger ) use ( $cachedActor, $originalActor ) {
|
2021-04-07 17:32:17 +00:00
|
|
|
if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
|
2021-04-16 04:16:55 +00:00
|
|
|
$this->cache->remove( $cachedActor );
|
2021-04-07 17:32:17 +00:00
|
|
|
$this->detachActorId( $originalActor );
|
|
|
|
|
}
|
2021-06-05 08:26:50 +00:00
|
|
|
},
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
2021-04-07 17:32:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 19:51:43 +00:00
|
|
|
/**
|
|
|
|
|
* @param int $queryFlags a bit field composed of READ_XXX flags
|
2021-03-01 22:39:13 +00:00
|
|
|
* @return array [ IDatabase $db, array $options ]
|
2021-01-22 19:51:43 +00:00
|
|
|
*/
|
2021-03-01 22:39:13 +00:00
|
|
|
private function getDBConnectionRefForQueryFlags( int $queryFlags ): array {
|
|
|
|
|
[ $mode, $options ] = DBAccessObjectUtils::getDBOptions( $queryFlags );
|
|
|
|
|
return [ $this->loadBalancer->getConnectionRef( $mode, [], $this->wikiId ), $options ];
|
2021-01-22 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Throws an exception if the given database connection does not belong to the wiki this
|
2023-02-08 21:58:41 +00:00
|
|
|
* ActorStore is bound to.
|
2021-01-22 19:51:43 +00:00
|
|
|
*
|
2023-05-25 15:24:57 +00:00
|
|
|
* @param IReadableDatabase $db
|
2021-01-22 19:51:43 +00:00
|
|
|
*/
|
2023-05-25 15:24:57 +00:00
|
|
|
private function checkDatabaseDomain( IReadableDatabase $db ) {
|
2021-01-22 19:51:43 +00:00
|
|
|
$dbDomain = $db->getDomainID();
|
|
|
|
|
$storeDomain = $this->loadBalancer->resolveDomainID( $this->wikiId );
|
|
|
|
|
if ( $dbDomain !== $storeDomain ) {
|
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
|
"DB connection domain '$dbDomain' does not match '$storeDomain'"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-04 02:43:09 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* In case all reasonable attempts of initializing a proper actor from the
|
|
|
|
|
* database have failed, entities can be attributed to special 'Unknown user' actor.
|
|
|
|
|
*
|
|
|
|
|
* @return UserIdentity
|
|
|
|
|
*/
|
|
|
|
|
public function getUnknownActor(): UserIdentity {
|
|
|
|
|
$actor = $this->getUserIdentityByName( self::UNKNOWN_USER_NAME );
|
|
|
|
|
if ( $actor ) {
|
|
|
|
|
return $actor;
|
|
|
|
|
}
|
2021-02-15 18:58:09 +00:00
|
|
|
$actor = new UserIdentityValue( 0, self::UNKNOWN_USER_NAME, $this->wikiId );
|
2021-03-04 21:46:45 +00:00
|
|
|
|
2024-01-23 14:01:06 +00:00
|
|
|
[ $db, ] = $this->getDBConnectionRefForQueryFlags( IDBAccessObject::READ_LATEST );
|
2021-03-04 21:46:45 +00:00
|
|
|
$this->acquireActorId( $actor, $db );
|
2021-02-04 02:43:09 +00:00
|
|
|
return $actor;
|
|
|
|
|
}
|
2021-02-10 21:56:38 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a specialized SelectQueryBuilder for querying the UserIdentity objects.
|
|
|
|
|
*
|
2023-05-25 15:24:57 +00:00
|
|
|
* @param IReadableDatabase|int $dbOrQueryFlags The database connection to perform the query on,
|
2024-01-23 14:01:06 +00:00
|
|
|
* or one of IDBAccessObject::READ_* constants.
|
2021-03-04 21:46:45 +00:00
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
2024-01-23 14:01:06 +00:00
|
|
|
public function newSelectQueryBuilder( $dbOrQueryFlags = IDBAccessObject::READ_NORMAL ): UserSelectQueryBuilder {
|
2023-05-25 15:24:57 +00:00
|
|
|
if ( $dbOrQueryFlags instanceof IReadableDatabase ) {
|
2021-05-13 23:08:31 +00:00
|
|
|
[ $db, $options ] = [ $dbOrQueryFlags, [] ];
|
2021-03-18 21:26:18 +00:00
|
|
|
$this->checkDatabaseDomain( $db );
|
|
|
|
|
} else {
|
2021-05-13 23:08:31 +00:00
|
|
|
[ $db, $options ] = $this->getDBConnectionRefForQueryFlags( $dbOrQueryFlags );
|
2021-03-04 21:46:45 +00:00
|
|
|
}
|
|
|
|
|
|
Support new block schema
Support migration stages when reading and writing blocks.
I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.
However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.
HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.
When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.
Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-10-27 03:34:10 +00:00
|
|
|
$builder = new UserSelectQueryBuilder(
|
|
|
|
|
$db,
|
|
|
|
|
$this,
|
|
|
|
|
$this->tempUserConfig,
|
|
|
|
|
$this->hideUserUtils
|
|
|
|
|
);
|
|
|
|
|
return $builder->options( $options );
|
2021-02-10 21:56:38 +00:00
|
|
|
}
|
2021-04-27 20:52:01 +00:00
|
|
|
|
2023-11-29 20:43:12 +00:00
|
|
|
/**
|
|
|
|
|
* @internal For use immediately after construction only
|
|
|
|
|
* @param bool $allow
|
|
|
|
|
*/
|
|
|
|
|
public function setAllowCreateIpActors( bool $allow ): void {
|
|
|
|
|
$this->allowCreateIpActors = $allow;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 20:52:01 +00:00
|
|
|
/**
|
|
|
|
|
* Emits a deprecation warning if $user does not belong to the
|
|
|
|
|
* same wiki this store belongs to.
|
|
|
|
|
*
|
|
|
|
|
* @param UserIdentity $user
|
|
|
|
|
*/
|
|
|
|
|
private function deprecateInvalidCrossWikiParam( UserIdentity $user ) {
|
|
|
|
|
if ( $user->getWikiId() !== $this->wikiId ) {
|
|
|
|
|
$expected = $this->wikiIdToString( $user->getWikiId() );
|
|
|
|
|
$actual = $this->wikiIdToString( $this->wikiId );
|
|
|
|
|
wfDeprecatedMsg(
|
|
|
|
|
'Deprecated passing invalid cross-wiki user. ' .
|
|
|
|
|
"Expected: {$expected}, Actual: {$actual}.",
|
|
|
|
|
'1.37'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert $wikiId to a string for logging.
|
|
|
|
|
*
|
|
|
|
|
* @param string|false $wikiId
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
private function wikiIdToString( $wikiId ): string {
|
2021-04-27 20:52:01 +00:00
|
|
|
return $wikiId === WikiAwareEntity::LOCAL ? 'the local wiki' : "'{$wikiId}'";
|
|
|
|
|
}
|
2021-01-22 19:51:43 +00:00
|
|
|
}
|