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;
|
|
|
|
|
|
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\Config\ServiceOptions;
|
|
|
|
|
use MediaWiki\DAO\WikiAwareEntity;
|
2022-04-26 15:48:03 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
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 Wikimedia\Rdbms\ILBFactory;
|
|
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ActorStore factory for various domains.
|
|
|
|
|
*
|
|
|
|
|
* @package MediaWiki\User
|
|
|
|
|
* @since 1.36
|
|
|
|
|
*/
|
|
|
|
|
class ActorStoreFactory {
|
|
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
2022-04-26 15:48:03 +00:00
|
|
|
MainConfigNames::SharedDB,
|
|
|
|
|
MainConfigNames::SharedTables,
|
2021-01-22 19:51:43 +00:00
|
|
|
];
|
|
|
|
|
|
2023-09-28 15:25:12 +00:00
|
|
|
private ILBFactory $loadBalancerFactory;
|
|
|
|
|
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 $sharedDB;
|
|
|
|
|
|
|
|
|
|
/** @var string[] */
|
|
|
|
|
private $sharedTables;
|
|
|
|
|
|
|
|
|
|
/** @var ActorStore[] */
|
|
|
|
|
private $storeCache = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ServiceOptions $options
|
|
|
|
|
* @param ILBFactory $loadBalancerFactory
|
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
ServiceOptions $options,
|
|
|
|
|
ILBFactory $loadBalancerFactory,
|
|
|
|
|
UserNameUtils $userNameUtils,
|
2023-08-02 13:27:41 +00:00
|
|
|
TempUserConfig $tempUserConfig,
|
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
|
|
|
LoggerInterface $logger,
|
|
|
|
|
HideUserUtils $hideUserUtils
|
2021-01-22 19:51:43 +00:00
|
|
|
) {
|
|
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
|
$this->loadBalancerFactory = $loadBalancerFactory;
|
2022-04-26 15:48:03 +00:00
|
|
|
$this->sharedDB = $options->get( MainConfigNames::SharedDB );
|
|
|
|
|
$this->sharedTables = $options->get( MainConfigNames::SharedTables );
|
2021-01-22 19:51:43 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-03-05 21:38:28 +00:00
|
|
|
* @param string|false $wikiId
|
2021-01-22 19:51:43 +00:00
|
|
|
* @return ActorNormalization
|
|
|
|
|
*/
|
|
|
|
|
public function getActorNormalization( $wikiId = WikiAwareEntity::LOCAL ): ActorNormalization {
|
|
|
|
|
return $this->getActorStore( $wikiId );
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 20:43:12 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.42
|
|
|
|
|
* @param string|false $wikiId
|
|
|
|
|
* @return ActorNormalization
|
|
|
|
|
*/
|
|
|
|
|
public function getActorNormalizationForImport(
|
|
|
|
|
$wikiId = WikiAwareEntity::LOCAL
|
|
|
|
|
): ActorNormalization {
|
|
|
|
|
return $this->getActorStoreForImport( $wikiId );
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 19:51:43 +00:00
|
|
|
/**
|
|
|
|
|
* @param string|false $wikiId
|
|
|
|
|
* @return ActorStore
|
|
|
|
|
*/
|
|
|
|
|
public function getActorStore( $wikiId = WikiAwareEntity::LOCAL ): ActorStore {
|
2023-11-29 20:43:12 +00:00
|
|
|
return $this->getStore( $wikiId, false );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 1.42
|
|
|
|
|
* @param string|false $wikiId
|
|
|
|
|
* @return ActorStore
|
|
|
|
|
*/
|
|
|
|
|
public function getActorStoreForImport( $wikiId = WikiAwareEntity::LOCAL ): ActorStore {
|
|
|
|
|
return $this->getStore( $wikiId, true );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-05-22 16:54:53 +00:00
|
|
|
* @since 1.43
|
2023-11-29 20:43:12 +00:00
|
|
|
* @param string|false $wikiId
|
|
|
|
|
* @return ActorStore
|
|
|
|
|
*/
|
2024-05-22 16:54:53 +00:00
|
|
|
public function getActorStoreForUndelete( $wikiId = WikiAwareEntity::LOCAL ): ActorStore {
|
|
|
|
|
return $this->getStore( $wikiId, true );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string|false $wikiId
|
|
|
|
|
* @param bool $allowingIpActorCreation
|
|
|
|
|
* @return ActorStore
|
|
|
|
|
*/
|
|
|
|
|
private function getStore( $wikiId, bool $allowingIpActorCreation ): ActorStore {
|
2021-01-22 19:51:43 +00:00
|
|
|
// During the transition from User, we still have old User objects
|
|
|
|
|
// representing users from a different wiki, so we still have IDatabase::getDomainId
|
|
|
|
|
// passed as $wikiId, so we need to remap it back to LOCAL.
|
2021-07-13 15:34:01 +00:00
|
|
|
if ( is_string( $wikiId ) && $this->loadBalancerFactory->getLocalDomainID() === $wikiId ) {
|
2021-01-22 19:51:43 +00:00
|
|
|
$wikiId = WikiAwareEntity::LOCAL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-22 16:54:53 +00:00
|
|
|
$storeCacheKey = ( $allowingIpActorCreation ? 'allowing-ip-actor-creation-' : '' ) .
|
2023-11-29 20:43:12 +00:00
|
|
|
( $wikiId === WikiAwareEntity::LOCAL ? 'LOCAL' : $wikiId );
|
|
|
|
|
|
2021-01-22 19:51:43 +00:00
|
|
|
if ( !isset( $this->storeCache[$storeCacheKey] ) ) {
|
2024-05-22 16:54:53 +00:00
|
|
|
$store = new ActorStore(
|
2021-01-22 19:51:43 +00:00
|
|
|
$this->getLoadBalancerForTable( 'actor', $wikiId ),
|
|
|
|
|
$this->userNameUtils,
|
2023-08-02 13:27:41 +00:00
|
|
|
$this->tempUserConfig,
|
2021-01-22 19:51:43 +00:00
|
|
|
$this->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,
|
2021-01-22 19:51:43 +00:00
|
|
|
$wikiId
|
|
|
|
|
);
|
2024-05-22 16:54:53 +00:00
|
|
|
if ( $allowingIpActorCreation ) {
|
2023-11-29 20:43:12 +00:00
|
|
|
$store->setAllowCreateIpActors( true );
|
|
|
|
|
}
|
|
|
|
|
$this->storeCache[$storeCacheKey] = $store;
|
2021-01-22 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
return $this->storeCache[$storeCacheKey];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-07-31 00:02:18 +00:00
|
|
|
* @param string|false $wikiId
|
2021-01-22 19:51:43 +00:00
|
|
|
* @return UserIdentityLookup
|
|
|
|
|
*/
|
|
|
|
|
public function getUserIdentityLookup(
|
|
|
|
|
$wikiId = WikiAwareEntity::LOCAL
|
|
|
|
|
): UserIdentityLookup {
|
|
|
|
|
return $this->getActorStore( $wikiId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a load balancer for the database that has the $table
|
|
|
|
|
* for the given $wikiId.
|
|
|
|
|
*
|
|
|
|
|
* @param string $table
|
2022-07-31 00:02:18 +00:00
|
|
|
* @param string|false $wikiId
|
2021-01-22 19:51:43 +00:00
|
|
|
* @return ILoadBalancer
|
|
|
|
|
*/
|
|
|
|
|
private function getLoadBalancerForTable(
|
|
|
|
|
string $table,
|
|
|
|
|
$wikiId = WikiAwareEntity::LOCAL
|
|
|
|
|
): ILoadBalancer {
|
|
|
|
|
if ( $this->sharedDB && in_array( $table, $this->sharedTables ) ) {
|
|
|
|
|
// The main LB is already properly set up for shared databases early in Setup.php
|
|
|
|
|
return $this->loadBalancerFactory->getMainLB();
|
|
|
|
|
}
|
|
|
|
|
return $this->loadBalancerFactory->getMainLB( $wikiId );
|
|
|
|
|
}
|
|
|
|
|
}
|