Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
20 / 20 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
UserNamePrefixSearch | |
100.00% |
20 / 20 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
search | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
6 |
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 | namespace MediaWiki\User; |
24 | |
25 | use InvalidArgumentException; |
26 | use MediaWiki\Block\HideUserUtils; |
27 | use MediaWiki\Permissions\Authority; |
28 | use Wikimedia\Rdbms\IConnectionProvider; |
29 | use Wikimedia\Rdbms\IExpression; |
30 | use Wikimedia\Rdbms\LikeValue; |
31 | |
32 | /** |
33 | * Handles searching prefixes of user names |
34 | * |
35 | * @note There are two classes called UserNamePrefixSearch. You should use this first one, in |
36 | * namespace MediaWiki\User, which is a service. \UserNamePrefixSearch is a deprecated static wrapper |
37 | * that forwards to the global service. |
38 | * |
39 | * @since 1.36 as a service in the current namespace |
40 | * @author DannyS712 |
41 | */ |
42 | class UserNamePrefixSearch { |
43 | |
44 | public const AUDIENCE_PUBLIC = 'public'; |
45 | |
46 | private IConnectionProvider $dbProvider; |
47 | private UserNameUtils $userNameUtils; |
48 | private HideUserUtils $hideUserUtils; |
49 | |
50 | /** |
51 | * @param IConnectionProvider $dbProvider |
52 | * @param UserNameUtils $userNameUtils |
53 | * @param HideUserUtils $hideUserUtils |
54 | */ |
55 | public function __construct( |
56 | IConnectionProvider $dbProvider, |
57 | UserNameUtils $userNameUtils, |
58 | HideUserUtils $hideUserUtils |
59 | ) { |
60 | $this->dbProvider = $dbProvider; |
61 | $this->userNameUtils = $userNameUtils; |
62 | $this->hideUserUtils = $hideUserUtils; |
63 | } |
64 | |
65 | /** |
66 | * Do a prefix search of user names and return a list of matching user names. |
67 | * |
68 | * @param string|Authority $audience Either AUDIENCE_PUBLIC or a user to |
69 | * show the search for |
70 | * @param string $search |
71 | * @param int $limit |
72 | * @param int $offset How many results to offset from the beginning |
73 | * @return string[] |
74 | * @throws InvalidArgumentException if $audience is invalid |
75 | */ |
76 | public function search( $audience, string $search, int $limit, int $offset = 0 ): array { |
77 | if ( $audience !== self::AUDIENCE_PUBLIC && |
78 | !( $audience instanceof Authority ) |
79 | ) { |
80 | throw new InvalidArgumentException( |
81 | '$audience must be AUDIENCE_PUBLIC or an Authority object' |
82 | ); |
83 | } |
84 | |
85 | // Invalid user names are treated as empty strings |
86 | $prefix = $this->userNameUtils->getCanonical( $search ) ?: ''; |
87 | |
88 | $dbr = $this->dbProvider->getReplicaDatabase(); |
89 | $queryBuilder = $dbr->newSelectQueryBuilder() |
90 | ->select( 'user_name' ) |
91 | ->from( 'user' ) |
92 | ->where( $dbr->expr( 'user_name', IExpression::LIKE, new LikeValue( $prefix, $dbr->anyString() ) ) ) |
93 | ->orderBy( 'user_name' ) |
94 | ->limit( $limit ) |
95 | ->offset( $offset ); |
96 | |
97 | // Filter out hidden user names |
98 | if ( $audience === self::AUDIENCE_PUBLIC || !$audience->isAllowed( 'hideuser' ) ) { |
99 | $queryBuilder->andWhere( $this->hideUserUtils->getExpression( $dbr ) ); |
100 | } |
101 | |
102 | return $queryBuilder->caller( __METHOD__ )->fetchFieldValues(); |
103 | } |
104 | } |