MediaWiki REL1_37
UserNamePrefixSearch.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\User;
24
25use InvalidArgumentException;
28
40
42 public const AUDIENCE_PUBLIC = 'public';
43
46
48 private $userFactory;
49
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}
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:88
Creates User objects.
Handles searching prefixes of user names.
__construct(ILoadBalancer $loadBalancer, UserFactory $userFactory, 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
Interface for objects representing user identity.
Database cluster connection, tracking, load balancing, and transaction manager interface.
const DB_REPLICA
Definition defines.php:25