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 
49 
51  private $userFactory;
52 
58  public function __construct(
62  ) {
63  $this->loadBalancer = $loadBalancer;
64  $this->permissionManager = $permissionManager;
65  $this->userFactory = $userFactory;
66  }
67 
78  public function search( $audience, string $search, int $limit, int $offset = 0 ) : array {
79  if ( $audience !== self::AUDIENCE_PUBLIC &&
80  !( $audience instanceof UserIdentity )
81  ) {
82  throw new InvalidArgumentException(
83  '$audience must be AUDIENCE_PUBLIC or a UserIdentity'
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 ||
102  !$this->permissionManager->userHasRight( $audience, 'hideuser' )
103  ) {
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:78
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:32
$dbr
$dbr
Definition: testCompression.php:54
MediaWiki\User\UserNamePrefixSearch
Handles searching prefixes of user names.
Definition: UserNamePrefixSearch.php:39
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
MediaWiki\User\UserNamePrefixSearch\$permissionManager
PermissionManager $permissionManager
Definition: UserNamePrefixSearch.php:48
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:51
MediaWiki\User\UserNamePrefixSearch\__construct
__construct(ILoadBalancer $loadBalancer, PermissionManager $permissionManager, UserFactory $userFactory)
Definition: UserNamePrefixSearch.php:58
MediaWiki\User
Definition: DefaultOptionsLookup.php:21
MediaWiki\User\UserNamePrefixSearch\$userFactory
UserFactory $userFactory
Definition: UserNamePrefixSearch.php:51
MediaWiki\User\UserFactory
Creates User objects.
Definition: UserFactory.php:40
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81