3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\User
;
23 use InvalidArgumentException
;
24 use MediaWiki\Block\HideUserUtils
;
25 use MediaWiki\Permissions\Authority
;
26 use Wikimedia\Rdbms\IConnectionProvider
;
27 use Wikimedia\Rdbms\IExpression
;
28 use Wikimedia\Rdbms\LikeValue
;
31 * Handles searching prefixes of user names
33 * @note There are two classes called UserNamePrefixSearch. You should use this class, in
34 * namespace MediaWiki\User, which is a service. \UserNamePrefixSearch is a deprecated static wrapper
35 * that forwards to the service class.
41 class UserNamePrefixSearch
{
43 public const AUDIENCE_PUBLIC
= 'public';
45 private IConnectionProvider
$dbProvider;
46 private UserNameUtils
$userNameUtils;
47 private HideUserUtils
$hideUserUtils;
50 * @param IConnectionProvider $dbProvider
51 * @param UserNameUtils $userNameUtils
52 * @param HideUserUtils $hideUserUtils
54 public function __construct(
55 IConnectionProvider
$dbProvider,
56 UserNameUtils
$userNameUtils,
57 HideUserUtils
$hideUserUtils
59 $this->dbProvider
= $dbProvider;
60 $this->userNameUtils
= $userNameUtils;
61 $this->hideUserUtils
= $hideUserUtils;
65 * Do a prefix search of user names and return a list of matching user names.
67 * @param string|Authority $audience Either AUDIENCE_PUBLIC or a user to
69 * @param string $search
71 * @param int $offset How many results to offset from the beginning
73 * @throws InvalidArgumentException if $audience is invalid
75 public function search( $audience, string $search, int $limit, int $offset = 0 ): array {
76 if ( $audience !== self
::AUDIENCE_PUBLIC
&&
77 !( $audience instanceof Authority
)
79 throw new InvalidArgumentException(
80 '$audience must be AUDIENCE_PUBLIC or an Authority object'
84 // Invalid user names are treated as empty strings
85 $prefix = $this->userNameUtils
->getCanonical( $search ) ?
: '';
87 $dbr = $this->dbProvider
->getReplicaDatabase();
88 $queryBuilder = $dbr->newSelectQueryBuilder()
89 ->select( 'user_name' )
91 ->where( $dbr->expr( 'user_name', IExpression
::LIKE
, new LikeValue( $prefix, $dbr->anyString() ) ) )
92 ->orderBy( 'user_name' )
96 // Filter out hidden user names
97 if ( $audience === self
::AUDIENCE_PUBLIC ||
!$audience->isAllowed( 'hideuser' ) ) {
98 $queryBuilder->andWhere( $this->hideUserUtils
->getExpression( $dbr ) );
101 return $queryBuilder->caller( __METHOD__
)->fetchFieldValues();