MediaWiki master
UserNamePrefixSearch.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\User;
22
23use InvalidArgumentException;
29
42
43 public const AUDIENCE_PUBLIC = 'public';
44
45 private IConnectionProvider $dbProvider;
46 private UserNameUtils $userNameUtils;
47 private HideUserUtils $hideUserUtils;
48
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
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}
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.