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 $loadBalancer;
46 
48  private $userNameUtils;
49 
54  public function __construct(
57  ) {
58  $this->loadBalancer = $loadBalancer;
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->loadBalancer->getConnectionRef( DB_REPLICA );
86 
87  $tables = [ 'user' ];
88  $conds = [ 'user_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ) ];
89  $joinConds = [];
90 
91  // Filter out hidden user names
92  if ( $audience === self::AUDIENCE_PUBLIC || !$audience->isAllowed( 'hideuser' ) ) {
93  $tables[] = 'ipblocks';
94  $conds['ipb_deleted'] = [ 0, null ];
95  $joinConds['ipblocks'] = [ 'LEFT JOIN', 'user_id=ipb_user' ];
96  }
97 
98  $res = $dbr->selectFieldValues(
99  $tables,
100  'user_name',
101  $conds,
102  __METHOD__,
103  [
104  'LIMIT' => $limit,
105  'ORDER BY' => 'user_name',
106  'OFFSET' => $offset
107  ],
108  $joinConds
109  );
110 
111  return $res;
112  }
113 }
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition: WebStart.php:82
Handles searching prefixes of user names.
__construct(ILoadBalancer $loadBalancer, UserNameUtils $userNameUtils)
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.
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
Database cluster connection, tracking, load balancing, and transaction manager interface.
const DB_REPLICA
Definition: defines.php:26