Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / user / UserNamePrefixSearch.php
blobb7d5058211100bd7bf7922c627200e24e157ea14
1 <?php
2 /**
3 * Prefix search of user names.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Handles searching prefixes of user names
26 * @since 1.27
28 class UserNamePrefixSearch {
30 /**
31 * Do a prefix search of user names and return a list of matching user names.
33 * @param string|User $audience The string 'public' or a user object to show the search for
34 * @param string $search
35 * @param int $limit
36 * @param int $offset How many results to offset from the beginning
37 * @return array Array of strings
39 public static function search( $audience, $search, $limit, $offset = 0 ) {
40 $user = User::newFromName( $search );
42 $dbr = wfGetDB( DB_REPLICA );
43 $prefix = $user ? $user->getName() : '';
44 $tables = [ 'user' ];
45 $cond = [ 'user_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ) ];
46 $joinConds = [];
48 // Filter out hidden user names
49 if ( $audience === 'public' || !$audience->isAllowed( 'hideuser' ) ) {
50 $tables[] = 'ipblocks';
51 $cond['ipb_deleted'] = [ 0, null ];
52 $joinConds['ipblocks'] = [ 'LEFT JOIN', 'user_id=ipb_user' ];
55 $res = $dbr->selectFieldValues(
56 $tables,
57 'user_name',
58 $cond,
59 __METHOD__,
61 'LIMIT' => $limit,
62 'ORDER BY' => 'user_name',
63 'OFFSET' => $offset
65 $joinConds
68 return $res === false ? [] : $res;