MediaWiki master
UserNamePrefixSearch.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\User;
24
25use InvalidArgumentException;
31
43
45 public const AUDIENCE_PUBLIC = 'public';
46
47 private IConnectionProvider $dbProvider;
48 private UserNameUtils $userNameUtils;
49 private HideUserUtils $hideUserUtils;
50
56 public function __construct(
57 IConnectionProvider $dbProvider,
58 UserNameUtils $userNameUtils,
59 HideUserUtils $hideUserUtils
60 ) {
61 $this->dbProvider = $dbProvider;
62 $this->userNameUtils = $userNameUtils;
63 $this->hideUserUtils = $hideUserUtils;
64 }
65
77 public function search( $audience, string $search, int $limit, int $offset = 0 ): array {
78 if ( $audience !== self::AUDIENCE_PUBLIC &&
79 !( $audience instanceof Authority )
80 ) {
81 throw new InvalidArgumentException(
82 '$audience must be AUDIENCE_PUBLIC or an Authority object'
83 );
84 }
85
86 // Invalid user names are treated as empty strings
87 $prefix = $this->userNameUtils->getCanonical( $search ) ?: '';
88
89 $dbr = $this->dbProvider->getReplicaDatabase();
90 $queryBuilder = $dbr->newSelectQueryBuilder()
91 ->select( 'user_name' )
92 ->from( 'user' )
93 ->where( $dbr->expr( 'user_name', IExpression::LIKE, new LikeValue( $prefix, $dbr->anyString() ) ) )
94 ->orderBy( 'user_name' )
95 ->limit( $limit )
96 ->offset( $offset );
97
98 // Filter out hidden user names
99 if ( $audience === self::AUDIENCE_PUBLIC || !$audience->isAllowed( 'hideuser' ) ) {
100 $queryBuilder->andWhere( $this->hideUserUtils->getExpression( $dbr ) );
101 }
102
103 return $queryBuilder->caller( __METHOD__ )->fetchFieldValues();
104 }
105}
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.