MediaWiki master
UserNamePrefixSearch.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\User;
8
9use InvalidArgumentException;
15
24
25 public const AUDIENCE_PUBLIC = 'public';
26
27 public function __construct(
28 private readonly IConnectionProvider $dbProvider,
29 private readonly UserNameUtils $userNameUtils,
30 private readonly HideUserUtils $hideUserUtils
31 ) {
32 }
33
45 public function search( $audience, string $search, int $limit, int $offset = 0 ): array {
46 if ( $audience !== self::AUDIENCE_PUBLIC &&
47 !( $audience instanceof Authority )
48 ) {
49 throw new InvalidArgumentException(
50 '$audience must be AUDIENCE_PUBLIC or an Authority object'
51 );
52 }
53
54 // Invalid usernames are treated as empty strings
55 $prefix = $this->userNameUtils->getCanonical( $search ) ?: '';
56
57 $dbr = $this->dbProvider->getReplicaDatabase();
58 $queryBuilder = $dbr->newSelectQueryBuilder()
59 ->select( 'user_name' )
60 ->from( 'user' )
61 ->where( $dbr->expr( 'user_name', IExpression::LIKE, new LikeValue( $prefix, $dbr->anyString() ) ) )
62 ->orderBy( 'user_name' )
63 ->limit( $limit )
64 ->offset( $offset );
65
66 // Filter out hidden usernames
67 if ( $audience === self::AUDIENCE_PUBLIC || !$audience->isAllowed( 'hideuser' ) ) {
68 $queryBuilder->andWhere( $this->hideUserUtils->getExpression( $dbr ) );
69 }
70
71 return $queryBuilder->caller( __METHOD__ )->fetchFieldValues();
72 }
73}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
Helpers for building queries that determine whether a user is hidden.
Handles searching prefixes of user names.
__construct(private readonly IConnectionProvider $dbProvider, private readonly UserNameUtils $userNameUtils, private readonly HideUserUtils $hideUserUtils)
search( $audience, string $search, int $limit, int $offset=0)
Do a prefix search of usernames and return a list of matching usernames.
UserNameUtils service.
Content of like value.
Definition LikeValue.php:14
This interface represents the authority associated with the current execution context,...
Definition Authority.php:23
Provide primary and replica IDatabase connections.