| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\User; |
| 24 |
|
| 25 |
use InvalidArgumentException; |
| 26 |
use MediaWiki\Block\HideUserUtils; |
| 27 |
use MediaWiki\Permissions\Authority; |
| 28 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 29 |
use Wikimedia\Rdbms\IExpression; |
| 30 |
use Wikimedia\Rdbms\LikeValue; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class UserNamePrefixSearch { |
| 43 |
|
| 44 |
public const AUDIENCE_PUBLIC = 'public'; |
| 45 |
|
| 46 |
private IConnectionProvider $dbProvider; |
| 47 |
private UserNameUtils $userNameUtils; |
| 48 |
private HideUserUtils $hideUserUtils; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct( |
| 56 |
IConnectionProvider $dbProvider, |
| 57 |
UserNameUtils $userNameUtils, |
| 58 |
HideUserUtils $hideUserUtils |
| 59 |
) { |
| 60 |
$this->dbProvider = $dbProvider; |
| 61 |
$this->userNameUtils = $userNameUtils; |
| 62 |
$this->hideUserUtils = $hideUserUtils; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function search( $audience, string $search, int $limit, int $offset = 0 ): array { |
| 77 |
if ( $audience !== self::AUDIENCE_PUBLIC && |
| 78 |
!( $audience instanceof Authority ) |
| 79 |
) { |
| 80 |
throw new InvalidArgumentException( |
| 81 |
'$audience must be AUDIENCE_PUBLIC or an Authority object' |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
$prefix = $this->userNameUtils->getCanonical( $search ) ?: ''; |
| 87 |
|
| 88 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 89 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 90 |
->select( 'user_name' ) |
| 91 |
->from( 'user' ) |
| 92 |
->where( $dbr->expr( 'user_name', IExpression::LIKE, new LikeValue( $prefix, $dbr->anyString() ) ) ) |
| 93 |
->orderBy( 'user_name' ) |
| 94 |
->limit( $limit ) |
| 95 |
->offset( $offset ); |
| 96 |
|
| 97 |
|
| 98 |
if ( $audience === self::AUDIENCE_PUBLIC || !$audience->isAllowed( 'hideuser' ) ) { |
| 99 |
$queryBuilder->andWhere( $this->hideUserUtils->getExpression( $dbr ) ); |
| 100 |
} |
| 101 |
|
| 102 |
return $queryBuilder->caller( __METHOD__ )->fetchFieldValues(); |
| 103 |
} |
| 104 |
} |