MediaWiki 1.43.1
UserNamePrefixSearch.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\User;
24
25use InvalidArgumentException;
31
43
44 public const AUDIENCE_PUBLIC = 'public';
45
46 private IConnectionProvider $dbProvider;
47 private UserNameUtils $userNameUtils;
48 private HideUserUtils $hideUserUtils;
49
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
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}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Helpers for building queries that determine whether a user is hidden.
Handles searching prefixes of user names.
__construct(IConnectionProvider $dbProvider, UserNameUtils $userNameUtils, HideUserUtils $hideUserUtils)
search( $audience, string $search, int $limit, int $offset=0)
Do a prefix search of user names and return a list of matching user names.
UserNameUtils service.
Content of like value.
Definition LikeValue.php:14
This interface represents the authority associated with the current execution context,...
Definition Authority.php:37
Provide primary and replica IDatabase connections.
Utility class for bot passwords.