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 
54  public function __construct(
57  ) {
58  $this->loadBalancer = $loadBalancer;
59  $this->userFactory = $userFactory;
60  }
61 
73  public function search( $audience, string $search, int $limit, int $offset = 0 ) : array {
74  if ( $audience instanceof UserIdentity && !( $audience instanceof Authority ) ) {
75  wfDeprecated( __METHOD__ . ' with a UserIdentity', '1.37' );
76  $audience = $this->userFactory->newFromUserIdentity( $audience );
77  }
78 
79  if ( $audience !== self::AUDIENCE_PUBLIC &&
80  !( $audience instanceof Authority )
81  ) {
82  throw new InvalidArgumentException(
83  '$audience must be AUDIENCE_PUBLIC or an Authority object'
84  );
85  }
86 
87  // TODO this was kept when switching to a service, but it should probably
88  // use UserNameUtils::getCanonical( $search, UserNameUtils::RIGOR_VALID ) and
89  // use an empty string if that is false, or the returned string, instead of
90  // taking the time to construct a user object.
91  $user = $this->userFactory->newFromName( $search );
92  $prefix = $user ? $user->getName() : '';
93 
94  $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
95 
96  $tables = [ 'user' ];
97  $conds = [ 'user_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ) ];
98  $joinConds = [];
99 
100  // Filter out hidden user names
101  if ( $audience === self::AUDIENCE_PUBLIC || !$audience->isAllowed( 'hideuser' ) ) {
102  $tables[] = 'ipblocks';
103  $conds['ipb_deleted'] = [ 0, null ];
104  $joinConds['ipblocks'] = [ 'LEFT JOIN', 'user_id=ipb_user' ];
105  }
106 
107  $res = $dbr->selectFieldValues(
108  $tables,
109  'user_name',
110  $conds,
111  __METHOD__,
112  [
113  'LIMIT' => $limit,
114  'ORDER BY' => 'user_name',
115  'OFFSET' => $offset
116  ],
117  $joinConds
118  );
119 
120  return $res;
121  }
122 }
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:73
MediaWiki\User\UserNamePrefixSearch\$loadBalancer
ILoadBalancer $loadBalancer
Definition: UserNamePrefixSearch.php:41
$res
$res
Definition: testCompression.php:57
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\User\UserNamePrefixSearch\__construct
__construct(ILoadBalancer $loadBalancer, UserFactory $userFactory)
Definition: UserNamePrefixSearch.php:54
$dbr
$dbr
Definition: testCompression.php:54
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:996
MediaWiki\User\UserNamePrefixSearch
Handles searching prefixes of user names.
Definition: UserNamePrefixSearch.php:39
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:35
MediaWiki\User
Definition: ActorCache.php:21
MediaWiki\User\UserNamePrefixSearch\$userFactory
UserFactory $userFactory
Definition: UserNamePrefixSearch.php:48
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