Replace usages of deprecated User::isAllowed. Step 2.
[lhc/web/wiklou.git] / includes / user / UserNamePrefixSearch.php
1 <?php
2 /**
3 * Prefix search of user names.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * Handles searching prefixes of user names
27 *
28 * @since 1.27
29 */
30 class UserNamePrefixSearch {
31
32 /**
33 * Do a prefix search of user names and return a list of matching user names.
34 *
35 * @param string|User $audience The string 'public' or a user object to show the search for
36 * @param string $search
37 * @param int $limit
38 * @param int $offset How many results to offset from the beginning
39 * @return array Array of strings
40 */
41 public static function search( $audience, $search, $limit, $offset = 0 ) {
42 $user = User::newFromName( $search );
43
44 $dbr = wfGetDB( DB_REPLICA );
45 $prefix = $user ? $user->getName() : '';
46 $tables = [ 'user' ];
47 $cond = [ 'user_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ) ];
48 $joinConds = [];
49
50 // Filter out hidden user names
51 if ( $audience === 'public' || !MediaWikiServices::getInstance()
52 ->getPermissionManager()
53 ->userHasRight( $audience, 'hideuser' )
54 ) {
55 $tables[] = 'ipblocks';
56 $cond['ipb_deleted'] = [ 0, null ];
57 $joinConds['ipblocks'] = [ 'LEFT JOIN', 'user_id=ipb_user' ];
58 }
59
60 $res = $dbr->selectFieldValues(
61 $tables,
62 'user_name',
63 $cond,
64 __METHOD__,
65 [
66 'LIMIT' => $limit,
67 'ORDER BY' => 'user_name',
68 'OFFSET' => $offset
69 ],
70 $joinConds
71 );
72
73 return $res;
74 }
75 }