MediaWiki  master
UserSelectQueryBuilder.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\User;
22 
23 use Iterator;
24 use Wikimedia\Assert\Assert;
25 use Wikimedia\Assert\PreconditionException;
28 
30 
32  private $actorStore;
33 
40  parent::__construct( $db );
41  $this->actorStore = $actorStore;
42  $this->table( 'actor' );
43  }
44 
51  public function whereUserIds( $userIds ): self {
52  Assert::parameterType( 'integer|array', $userIds, '$userIds' );
53  $this->conds( [ 'actor_user' => $userIds ] );
54  return $this;
55  }
56 
63  public function userIds( $userIds ): self {
64  return $this->whereUserIds( $userIds );
65  }
66 
73  public function whereUserNames( $userNames ): self {
74  Assert::parameterType( 'string|array', $userNames, '$userIds' );
75  $userNames = array_map( function ( $name ) {
76  return $this->actorStore->normalizeUserName( (string)$name );
77  }, (array)$userNames );
78  $this->conds( [ 'actor_name' => $userNames ] );
79  return $this;
80  }
81 
88  public function userNames( $userNames ): self {
89  return $this->whereUserNames( $userNames );
90  }
91 
101  public function whereUserNamePrefix( string $prefix ): self {
102  if ( !isset( $this->options['LIMIT'] ) ) {
103  throw new PreconditionException( 'Must set a limit when using a user name prefix' );
104  }
105  $like = $this->db->buildLike( $prefix, $this->db->anyString() );
106  $this->conds( "actor_name{$like}" );
107  return $this;
108  }
109 
119  public function userNamePrefix( string $prefix ): self {
120  return $this->whereUserNamePrefix( $prefix );
121  }
122 
129  public function orderByName( string $dir = self::SORT_ASC ): self {
130  $this->orderBy( 'actor_name', $dir );
131  return $this;
132  }
133 
140  public function orderByUserId( string $dir = self::SORT_ASC ): self {
141  $this->orderBy( 'actor_user', $dir );
142  return $this;
143  }
144 
150  public function registered(): self {
151  $this->conds( [ 'actor_user != 0' ] );
152  return $this;
153  }
154 
160  public function anon(): self {
161  $this->conds( [ 'actor_user' => null ] );
162  return $this;
163  }
164 
170  public function fetchUserIdentity(): ?UserIdentity {
171  $this->fields( [ 'actor_id', 'actor_name', 'actor_user' ] );
172  $row = $this->fetchRow();
173  if ( !$row ) {
174  return null;
175  }
176  return $this->actorStore->newActorFromRow( $row );
177  }
178 
184  public function fetchUserIdentities(): Iterator {
185  $this->fields( [ 'actor_id', 'actor_name', 'actor_user' ] );
186  return call_user_func( function () {
187  $result = $this->fetchResultSet();
188  foreach ( $result as $row ) {
189  yield $this->actorStore->newActorFromRow( $row );
190  }
191  $result->free();
192  } );
193  }
194 
200  public function fetchUserNames(): array {
201  $this->field( 'actor_name' );
202  return $this->fetchFieldValues();
203  }
204 }
MediaWiki\User\UserSelectQueryBuilder\whereUserNamePrefix
whereUserNamePrefix(string $prefix)
Find users with names starting from the provided prefix.
Definition: UserSelectQueryBuilder.php:101
MediaWiki\User\UserSelectQueryBuilder\userNamePrefix
userNamePrefix(string $prefix)
Find users with names starting from the provided prefix.
Definition: UserSelectQueryBuilder.php:119
Wikimedia\Rdbms\SelectQueryBuilder\options
options(array $options)
Manually set multiple options in the $options array to be passed to IDatabase::select().
Definition: SelectQueryBuilder.php:607
MediaWiki\User\UserSelectQueryBuilder\userIds
userIds( $userIds)
Find by provided user ids.
Definition: UserSelectQueryBuilder.php:63
MediaWiki\User\UserSelectQueryBuilder\registered
registered()
Only return registered users.
Definition: UserSelectQueryBuilder.php:150
Wikimedia\Rdbms\SelectQueryBuilder\fetchResultSet
fetchResultSet()
Run the constructed SELECT query and return all results.
Definition: SelectQueryBuilder.php:628
MediaWiki\User\UserSelectQueryBuilder\$actorStore
ActorStore $actorStore
Definition: UserSelectQueryBuilder.php:32
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
MediaWiki\User\UserSelectQueryBuilder\fetchUserIdentity
fetchUserIdentity()
Fetch a single UserIdentity that matches specified criteria.
Definition: UserSelectQueryBuilder.php:170
MediaWiki\User\UserSelectQueryBuilder\anon
anon()
Only return anonymous users.
Definition: UserSelectQueryBuilder.php:160
Wikimedia\Rdbms\SelectQueryBuilder
Definition: SelectQueryBuilder.php:11
Wikimedia\Rdbms\SelectQueryBuilder\fields
fields( $fields)
Add a field or an array of fields to the query.
Definition: SelectQueryBuilder.php:184
Wikimedia\Rdbms\SelectQueryBuilder\fetchRow
fetchRow()
Run the constructed SELECT query, and return the first result row.
Definition: SelectQueryBuilder.php:673
MediaWiki\User\UserSelectQueryBuilder\orderByUserId
orderByUserId(string $dir=self::SORT_ASC)
Order results by user id.
Definition: UserSelectQueryBuilder.php:140
Wikimedia\Rdbms\SelectQueryBuilder\fetchFieldValues
fetchFieldValues()
Run the constructed SELECT query, and extract a single field from each result row,...
Definition: SelectQueryBuilder.php:657
MediaWiki\User\UserSelectQueryBuilder\orderByName
orderByName(string $dir=self::SORT_ASC)
Order results by name in $direction.
Definition: UserSelectQueryBuilder.php:129
MediaWiki\User
Definition: ActorCache.php:21
MediaWiki\User\UserSelectQueryBuilder\userNames
userNames( $userNames)
Find by provided user names.
Definition: UserSelectQueryBuilder.php:88
Wikimedia\Rdbms\SelectQueryBuilder\conds
conds( $conds)
Add conditions to the query.
Definition: SelectQueryBuilder.php:291
MediaWiki\User\UserSelectQueryBuilder
Definition: UserSelectQueryBuilder.php:29
MediaWiki\User\UserSelectQueryBuilder\whereUserNames
whereUserNames( $userNames)
Find by provided user names.
Definition: UserSelectQueryBuilder.php:73
Wikimedia\Rdbms\SelectQueryBuilder\field
field( $field, $alias=null)
Add a single field to the query, optionally with an alias.
Definition: SelectQueryBuilder.php:211
Wikimedia\Rdbms\SelectQueryBuilder\orderBy
orderBy( $fields, $direction=null)
Set the ORDER BY clause.
Definition: SelectQueryBuilder.php:431
MediaWiki\User\UserSelectQueryBuilder\whereUserIds
whereUserIds( $userIds)
Find by provided user ids.
Definition: UserSelectQueryBuilder.php:51
Wikimedia\Rdbms\SelectQueryBuilder\$db
IDatabase $db
Definition: SelectQueryBuilder.php:45
MediaWiki\User\UserSelectQueryBuilder\__construct
__construct(IDatabase $db, ActorStore $actorStore)
Definition: UserSelectQueryBuilder.php:39
MediaWiki\User\UserSelectQueryBuilder\fetchUserNames
fetchUserNames()
Returns an array of user names matching the query.
Definition: UserSelectQueryBuilder.php:200
MediaWiki\User\ActorStore
Definition: ActorStore.php:43
MediaWiki\User\UserSelectQueryBuilder\fetchUserIdentities
fetchUserIdentities()
Fetch UserIdentities for the specified query.
Definition: UserSelectQueryBuilder.php:184
Wikimedia\Rdbms\JoinGroupBase\table
table( $table, $alias=null)
Add a single table or a single parenthesized group.
Definition: JoinGroupBase.php:28