2021-02-10 21:56:38 +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 Iterator;
|
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;
|
2023-08-02 13:27:41 +00:00
|
|
|
use MediaWiki\User\TempUser\TempUserConfig;
|
2021-02-10 21:56:38 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
|
|
|
|
use Wikimedia\Assert\PreconditionException;
|
2023-11-07 17:30:50 +00:00
|
|
|
use Wikimedia\Rdbms\IExpression;
|
2023-05-25 15:24:57 +00:00
|
|
|
use Wikimedia\Rdbms\IReadableDatabase;
|
2023-11-07 17:30:50 +00:00
|
|
|
use Wikimedia\Rdbms\LikeValue;
|
2021-02-10 21:56:38 +00:00
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
|
|
|
|
|
|
|
|
|
class UserSelectQueryBuilder extends SelectQueryBuilder {
|
|
|
|
|
|
|
|
|
|
/** @var ActorStore */
|
|
|
|
|
private $actorStore;
|
2023-08-02 13:27:41 +00:00
|
|
|
private 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
|
|
|
private HideUserUtils $hideUserUtils;
|
2021-02-10 21:56:38 +00:00
|
|
|
|
2023-08-23 12:06:00 +00:00
|
|
|
private bool $userJoined = false;
|
|
|
|
|
|
2021-02-10 21:56:38 +00:00
|
|
|
/**
|
|
|
|
|
* @internal
|
2023-05-25 15:24:57 +00:00
|
|
|
* @param IReadableDatabase $db
|
2021-02-10 21:56:38 +00:00
|
|
|
* @param ActorStore $actorStore
|
2023-08-02 13:27:41 +00:00
|
|
|
* @param TempUserConfig $tempUserConfig
|
2021-02-10 21:56:38 +00:00
|
|
|
*/
|
2023-08-02 13:27:41 +00:00
|
|
|
public function __construct(
|
|
|
|
|
IReadableDatabase $db,
|
|
|
|
|
ActorStore $actorStore,
|
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
|
|
|
TempUserConfig $tempUserConfig,
|
|
|
|
|
HideUserUtils $hideUserUtils
|
2023-08-02 13:27:41 +00:00
|
|
|
) {
|
2021-02-10 21:56:38 +00:00
|
|
|
parent::__construct( $db );
|
2023-08-02 13:27:41 +00:00
|
|
|
|
2021-02-10 21:56:38 +00:00
|
|
|
$this->actorStore = $actorStore;
|
2023-08-02 13:27:41 +00:00
|
|
|
$this->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
|
|
|
$this->hideUserUtils = $hideUserUtils;
|
2021-02-10 21:56:38 +00:00
|
|
|
$this->table( 'actor' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find by provided user ids.
|
|
|
|
|
*
|
|
|
|
|
* @param int|int[] $userIds
|
|
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
2021-08-03 17:43:16 +00:00
|
|
|
public function whereUserIds( $userIds ): self {
|
2022-06-17 21:38:00 +00:00
|
|
|
Assert::parameterType( [ 'integer', 'array' ], $userIds, '$userIds' );
|
2021-02-10 21:56:38 +00:00
|
|
|
$this->conds( [ 'actor_user' => $userIds ] );
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 17:43:16 +00:00
|
|
|
/**
|
|
|
|
|
* Find by provided user ids.
|
|
|
|
|
* @deprecated since 1.37, use whereUserIds instead
|
|
|
|
|
* @param int|int[] $userIds
|
|
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
|
|
|
|
public function userIds( $userIds ): self {
|
|
|
|
|
return $this->whereUserIds( $userIds );
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 21:56:38 +00:00
|
|
|
/**
|
|
|
|
|
* Find by provided user names.
|
|
|
|
|
*
|
|
|
|
|
* @param string|string[] $userNames
|
|
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
2021-08-03 17:43:16 +00:00
|
|
|
public function whereUserNames( $userNames ): self {
|
2022-06-17 21:38:00 +00:00
|
|
|
Assert::parameterType( [ 'string', 'array' ], $userNames, '$userIds' );
|
2021-02-10 21:56:38 +00:00
|
|
|
$userNames = array_map( function ( $name ) {
|
|
|
|
|
return $this->actorStore->normalizeUserName( (string)$name );
|
|
|
|
|
}, (array)$userNames );
|
|
|
|
|
$this->conds( [ 'actor_name' => $userNames ] );
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 17:43:16 +00:00
|
|
|
/**
|
|
|
|
|
* Find by provided user names.
|
|
|
|
|
* @deprecated since 1.37, use whereUserNames instead
|
|
|
|
|
* @param string|string[] $userNames
|
|
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
|
|
|
|
public function userNames( $userNames ): self {
|
|
|
|
|
return $this->whereUserNames( $userNames );
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 21:56:38 +00:00
|
|
|
/**
|
|
|
|
|
* Find users with names starting from the provided prefix.
|
|
|
|
|
*
|
|
|
|
|
* @note this could produce a huge number of results, like User00000 ... User99999,
|
|
|
|
|
* so you must set a limit when using this condition.
|
|
|
|
|
*
|
|
|
|
|
* @param string $prefix
|
|
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
2021-08-03 17:43:16 +00:00
|
|
|
public function whereUserNamePrefix( string $prefix ): self {
|
2021-02-10 21:56:38 +00:00
|
|
|
if ( !isset( $this->options['LIMIT'] ) ) {
|
|
|
|
|
throw new PreconditionException( 'Must set a limit when using a user name prefix' );
|
|
|
|
|
}
|
2023-11-07 17:30:50 +00:00
|
|
|
$this->conds(
|
|
|
|
|
$this->db->expr( 'actor_name', IExpression::LIKE, new LikeValue( $prefix, $this->db->anyString() ) )
|
|
|
|
|
);
|
2021-02-10 21:56:38 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 17:43:16 +00:00
|
|
|
/**
|
|
|
|
|
* Find users with names starting from the provided prefix.
|
|
|
|
|
*
|
|
|
|
|
* @note this could produce a huge number of results, like User00000 ... User99999,
|
|
|
|
|
* so you must set a limit when using this condition.
|
|
|
|
|
* @deprecated since 1.37 use whereUserNamePrefix instead
|
|
|
|
|
* @param string $prefix
|
|
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
|
|
|
|
public function userNamePrefix( string $prefix ): self {
|
|
|
|
|
return $this->whereUserNamePrefix( $prefix );
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 12:06:00 +00:00
|
|
|
/**
|
|
|
|
|
* Find registered users who registered
|
|
|
|
|
*
|
|
|
|
|
* @param string $timestamp
|
|
|
|
|
* @param bool $direction Direction flag (if true, user_registration must be before $timestamp)
|
|
|
|
|
* @since 1.42
|
|
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
|
|
|
|
public function whereRegisteredTimestamp( string $timestamp, bool $direction ): self {
|
|
|
|
|
if ( !$this->userJoined ) {
|
|
|
|
|
$this->join( 'user', null, [ "actor_user=user_id" ] );
|
|
|
|
|
$this->userJoined = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 15:17:12 +00:00
|
|
|
$this->conds(
|
2024-05-21 23:27:22 +00:00
|
|
|
$this->db->expr( 'user_registration', ( $direction ? '<' : '>' ), $this->db->timestamp( $timestamp ) )
|
2024-03-26 15:17:12 +00:00
|
|
|
);
|
2023-08-23 12:06:00 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 21:56:38 +00:00
|
|
|
/**
|
|
|
|
|
* Order results by name in $direction
|
|
|
|
|
*
|
2024-08-08 10:15:58 +00:00
|
|
|
* @param string $dir one of self::SORT_ASC or self::SORT_DESC
|
2021-02-10 21:56:38 +00:00
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
|
|
|
|
public function orderByName( string $dir = self::SORT_ASC ): self {
|
|
|
|
|
$this->orderBy( 'actor_name', $dir );
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Order results by user id.
|
|
|
|
|
*
|
2024-08-08 10:15:58 +00:00
|
|
|
* @param string $dir one of self::SORT_ASC or self::SORT_DESC
|
2021-02-10 21:56:38 +00:00
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
|
|
|
|
public function orderByUserId( string $dir = self::SORT_ASC ): self {
|
|
|
|
|
$this->orderBy( 'actor_user', $dir );
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Only return registered users.
|
|
|
|
|
*
|
|
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
|
|
|
|
public function registered(): self {
|
2024-03-25 17:42:03 +00:00
|
|
|
$this->conds( $this->db->expr( 'actor_user', '!=', null ) );
|
2021-02-10 21:56:38 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Only return anonymous users.
|
|
|
|
|
*
|
|
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
|
|
|
|
public function anon(): self {
|
|
|
|
|
$this->conds( [ 'actor_user' => null ] );
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 13:27:41 +00:00
|
|
|
/**
|
|
|
|
|
* Only return named users.
|
|
|
|
|
*
|
|
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
|
|
|
|
public function named(): self {
|
2024-06-13 12:03:53 +00:00
|
|
|
if ( !$this->tempUserConfig->isKnown() ) {
|
|
|
|
|
// nothing to do: getMatchCondition throws if temp accounts aren't known
|
2023-08-02 13:27:41 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
2024-03-01 23:31:21 +00:00
|
|
|
$this->conds( $this->tempUserConfig->getMatchCondition( $this->db, 'actor_name', IExpression::NOT_LIKE ) );
|
2023-08-02 13:27:41 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Only return temp users
|
|
|
|
|
*
|
|
|
|
|
* @return UserSelectQueryBuilder
|
|
|
|
|
*/
|
|
|
|
|
public function temp(): self {
|
2024-06-13 12:03:53 +00:00
|
|
|
if ( !$this->tempUserConfig->isKnown() ) {
|
|
|
|
|
// nothing to do: getMatchCondition throws if temp accounts aren't known
|
2023-08-02 13:27:41 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
2024-03-01 23:31:21 +00:00
|
|
|
$this->conds( $this->tempUserConfig->getMatchCondition( $this->db, 'actor_name', IExpression::LIKE ) );
|
2023-08-02 13:27:41 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 11:12:40 +00:00
|
|
|
/**
|
|
|
|
|
* Filter based on user hidden status
|
|
|
|
|
*
|
2021-12-19 11:42:11 +00:00
|
|
|
* @since 1.38
|
2021-12-11 11:12:40 +00:00
|
|
|
* @param bool $hidden True - only hidden users, false - no hidden users
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function hidden( bool $hidden ): self {
|
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->conds( $this->hideUserUtils->getExpression(
|
|
|
|
|
$this->db,
|
|
|
|
|
'actor_user',
|
|
|
|
|
$hidden ? HideUserUtils::HIDDEN_USERS : HideUserUtils::SHOWN_USERS
|
|
|
|
|
) );
|
2021-12-11 11:12:40 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 21:56:38 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch a single UserIdentity that matches specified criteria.
|
|
|
|
|
*
|
|
|
|
|
* @return UserIdentity|null
|
|
|
|
|
*/
|
|
|
|
|
public function fetchUserIdentity(): ?UserIdentity {
|
|
|
|
|
$this->fields( [ 'actor_id', 'actor_name', 'actor_user' ] );
|
|
|
|
|
$row = $this->fetchRow();
|
|
|
|
|
if ( !$row ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return $this->actorStore->newActorFromRow( $row );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch UserIdentities for the specified query.
|
|
|
|
|
*
|
|
|
|
|
* @return Iterator<UserIdentity>
|
|
|
|
|
*/
|
|
|
|
|
public function fetchUserIdentities(): Iterator {
|
|
|
|
|
$this->fields( [ 'actor_id', 'actor_name', 'actor_user' ] );
|
2022-11-14 17:55:42 +00:00
|
|
|
|
|
|
|
|
$result = $this->fetchResultSet();
|
|
|
|
|
foreach ( $result as $row ) {
|
|
|
|
|
yield $this->actorStore->newActorFromRow( $row );
|
|
|
|
|
}
|
|
|
|
|
$result->free();
|
2021-02-10 21:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array of user names matching the query.
|
|
|
|
|
*
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
|
|
|
|
public function fetchUserNames(): array {
|
|
|
|
|
$this->field( 'actor_name' );
|
|
|
|
|
return $this->fetchFieldValues();
|
|
|
|
|
}
|
|
|
|
|
}
|