Declare dynamic properties
[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 /**
24 * Handles searching prefixes of user names
25 *
26 * @since 1.27
27 */
28 class UserNamePrefixSearch {
29
30 /**
31 * Do a prefix search of user names and return a list of matching user names.
32 *
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
38 */
39 public static function search( $audience, $search, $limit, $offset = 0 ) {
40 $user = User::newFromName( $search );
41
42 $dbr = wfGetDB( DB_REPLICA );
43 $prefix = $user ? $user->getName() : '';
44 $tables = [ 'user' ];
45 $cond = [ 'user_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ) ];
46 $joinConds = [];
47
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' ];
53 }
54
55 $res = $dbr->selectFieldValues(
56 $tables,
57 'user_name',
58 $cond,
59 __METHOD__,
60 [
61 'LIMIT' => $limit,
62 'ORDER BY' => 'user_name',
63 'OFFSET' => $offset
64 ],
65 $joinConds
66 );
67
68 return $res;
69 }
70 }