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 $userFactory;
49 
51  private $userNameUtils;
52 
58  public function __construct(
62  ) {
63  $this->loadBalancer = $loadBalancer;
64  $this->userFactory = $userFactory;
65  $this->userNameUtils = $userNameUtils;
66  }
67 
79  public function search( $audience, string $search, int $limit, int $offset = 0 ): array {
80  if ( $audience instanceof UserIdentity && !( $audience instanceof Authority ) ) {
81  wfDeprecated( __METHOD__ . ' with a UserIdentity', '1.37' );
82  $audience = $this->userFactory->newFromUserIdentity( $audience );
83  }
84 
85  if ( $audience !== self::AUDIENCE_PUBLIC &&
86  !( $audience instanceof Authority )
87  ) {
88  throw new InvalidArgumentException(
89  '$audience must be AUDIENCE_PUBLIC or an Authority object'
90  );
91  }
92 
93  // Invalid user names are treated as empty strings
94  $prefix = $this->userNameUtils->getCanonical( $search ) ?: '';
95 
96  $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
97 
98  $tables = [ 'user' ];
99  $conds = [ 'user_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ) ];
100  $joinConds = [];
101 
102  // Filter out hidden user names
103  if ( $audience === self::AUDIENCE_PUBLIC || !$audience->isAllowed( 'hideuser' ) ) {
104  $tables[] = 'ipblocks';
105  $conds['ipb_deleted'] = [ 0, null ];
106  $joinConds['ipblocks'] = [ 'LEFT JOIN', 'user_id=ipb_user' ];
107  }
108 
109  $res = $dbr->selectFieldValues(
110  $tables,
111  'user_name',
112  $conds,
113  __METHOD__,
114  [
115  'LIMIT' => $limit,
116  'ORDER BY' => 'user_name',
117  'OFFSET' => $offset
118  ],
119  $joinConds
120  );
121 
122  return $res;
123  }
124 }
MediaWiki\User\UserNamePrefixSearch\search
search( $audience, string $search, int $limit, int $offset=0)
Do a prefix search of user names and return a list of matching user names.
Definition: UserNamePrefixSearch.php:79
MediaWiki\User\UserNamePrefixSearch\$loadBalancer
ILoadBalancer $loadBalancer
Definition: UserNamePrefixSearch.php:41
$res
$res
Definition: testCompression.php:57
MediaWiki\User\UserNamePrefixSearch\__construct
__construct(ILoadBalancer $loadBalancer, UserFactory $userFactory, UserNameUtils $userNameUtils)
Definition: UserNamePrefixSearch.php:58
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
$dbr
$dbr
Definition: testCompression.php:54
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
MediaWiki\User\UserNamePrefixSearch
Handles searching prefixes of user names.
Definition: UserNamePrefixSearch.php:39
MediaWiki\User\UserNamePrefixSearch\$userNameUtils
UserNameUtils $userNameUtils
Definition: UserNamePrefixSearch.php:51
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
MediaWiki\User
Definition: ActorCache.php:21
MediaWiki\User\UserNamePrefixSearch\$userFactory
UserFactory $userFactory
Definition: UserNamePrefixSearch.php:48
MediaWiki\User\UserNameUtils
UserNameUtils service.
Definition: UserNameUtils.php:42
MediaWiki\User\UserFactory
Creates User objects.
Definition: UserFactory.php:41
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81