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