MediaWiki  master
UserNamePrefixSearch.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\User;
24 
25 use InvalidArgumentException;
28 
40 
42  public const AUDIENCE_PUBLIC = 'public';
43 
45  private $dbProvider;
46 
48  private $userNameUtils;
49 
54  public function __construct(
55  IConnectionProvider $dbProvider,
56  UserNameUtils $userNameUtils
57  ) {
58  $this->dbProvider = $dbProvider;
59  $this->userNameUtils = $userNameUtils;
60  }
61 
73  public function search( $audience, string $search, int $limit, int $offset = 0 ): array {
74  if ( $audience !== self::AUDIENCE_PUBLIC &&
75  !( $audience instanceof Authority )
76  ) {
77  throw new InvalidArgumentException(
78  '$audience must be AUDIENCE_PUBLIC or an Authority object'
79  );
80  }
81 
82  // Invalid user names are treated as empty strings
83  $prefix = $this->userNameUtils->getCanonical( $search ) ?: '';
84 
85  $dbr = $this->dbProvider->getReplicaDatabase();
86  $queryBuilder = $dbr->newSelectQueryBuilder()
87  ->select( 'user_name' )
88  ->from( 'user' )
89  ->where( [ 'user_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ) ] )
90  ->orderBy( 'user_name' )
91  ->limit( $limit )
92  ->offset( $offset );
93 
94  // Filter out hidden user names
95  if ( $audience === self::AUDIENCE_PUBLIC || !$audience->isAllowed( 'hideuser' ) ) {
96  $queryBuilder->leftJoin( 'ipblocks', null, 'user_id=ipb_user' );
97  $queryBuilder->andWhere( [ 'ipb_deleted' => [ 0, null ] ] );
98  }
99 
100  return $queryBuilder->caller( __METHOD__ )->fetchFieldValues();
101  }
102 }
if(!defined('MW_SETUP_CALLBACK'))
Definition: WebStart.php:88
Handles searching prefixes of user names.
search( $audience, string $search, int $limit, int $offset=0)
Do a prefix search of user names and return a list of matching user names.
__construct(IConnectionProvider $dbProvider, UserNameUtils $userNameUtils)
UserNameUtils service.
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
Provide primary and replica IDatabase connections.