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 userIds( $userIds ): self {
52  Assert::parameterType( 'integer|array', $userIds, '$userIds' );
53  $this->conds( [ 'actor_user' => $userIds ] );
54  return $this;
55  }
56 
63  public function userNames( $userNames ): self {
64  Assert::parameterType( 'string|array', $userNames, '$userIds' );
65  $userNames = array_map( function ( $name ) {
66  return $this->actorStore->normalizeUserName( (string)$name );
67  }, (array)$userNames );
68  $this->conds( [ 'actor_name' => $userNames ] );
69  return $this;
70  }
71 
81  public function userNamePrefix( string $prefix ): self {
82  if ( !isset( $this->options['LIMIT'] ) ) {
83  throw new PreconditionException( 'Must set a limit when using a user name prefix' );
84  }
85  $like = $this->db->buildLike( $prefix, $this->db->anyString() );
86  $this->conds( "actor_name{$like}" );
87  return $this;
88  }
89 
96  public function orderByName( string $dir = self::SORT_ASC ): self {
97  $this->orderBy( 'actor_name', $dir );
98  return $this;
99  }
100 
107  public function orderByUserId( string $dir = self::SORT_ASC ): self {
108  $this->orderBy( 'actor_user', $dir );
109  return $this;
110  }
111 
117  public function registered(): self {
118  $this->conds( [ 'actor_user != 0' ] );
119  return $this;
120  }
121 
127  public function anon(): self {
128  $this->conds( [ 'actor_user' => null ] );
129  return $this;
130  }
131 
137  public function fetchUserIdentity(): ?UserIdentity {
138  $this->fields( [ 'actor_id', 'actor_name', 'actor_user' ] );
139  $row = $this->fetchRow();
140  if ( !$row ) {
141  return null;
142  }
143  return $this->actorStore->newActorFromRow( $row );
144  }
145 
151  public function fetchUserIdentities(): Iterator {
152  $this->fields( [ 'actor_id', 'actor_name', 'actor_user' ] );
153  return call_user_func( function () {
154  $result = $this->fetchResultSet();
155  foreach ( $result as $row ) {
156  yield $this->actorStore->newActorFromRow( $row );
157  }
158  $result->free();
159  } );
160  }
161 
167  public function fetchUserNames(): array {
168  $this->field( 'actor_name' );
169  return $this->fetchFieldValues();
170  }
171 }
MediaWiki\User\UserSelectQueryBuilder\userNamePrefix
userNamePrefix(string $prefix)
Find users with names starting from the provided prefix.
Definition: UserSelectQueryBuilder.php:81
Wikimedia\Rdbms\SelectQueryBuilder\options
options(array $options)
Manually set multiple options in the $options array to be passed to IDatabase::select().
Definition: SelectQueryBuilder.php:591
MediaWiki\User\UserSelectQueryBuilder\userIds
userIds( $userIds)
Find by provided user ids.
Definition: UserSelectQueryBuilder.php:51
MediaWiki\User\UserSelectQueryBuilder\registered
registered()
Only return registered users.
Definition: UserSelectQueryBuilder.php:117
Wikimedia\Rdbms\SelectQueryBuilder\fetchResultSet
fetchResultSet()
Run the constructed SELECT query and return all results.
Definition: SelectQueryBuilder.php:612
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:137
MediaWiki\User\UserSelectQueryBuilder\anon
anon()
Only return anonymous users.
Definition: UserSelectQueryBuilder.php:127
Wikimedia\Rdbms\SelectQueryBuilder
Definition: SelectQueryBuilder.php:5
Wikimedia\Rdbms\SelectQueryBuilder\fields
fields( $fields)
Add a field or an array of fields to the query.
Definition: SelectQueryBuilder.php:178
Wikimedia\Rdbms\SelectQueryBuilder\fetchRow
fetchRow()
Run the constructed SELECT query, and return the first result row.
Definition: SelectQueryBuilder.php:657
MediaWiki\User\UserSelectQueryBuilder\orderByUserId
orderByUserId(string $dir=self::SORT_ASC)
Order results by user id.
Definition: UserSelectQueryBuilder.php:107
Wikimedia\Rdbms\SelectQueryBuilder\fetchFieldValues
fetchFieldValues()
Run the constructed SELECT query, and extract a single field from each result row,...
Definition: SelectQueryBuilder.php:641
MediaWiki\User\UserSelectQueryBuilder\orderByName
orderByName(string $dir=self::SORT_ASC)
Order results by name in $direction.
Definition: UserSelectQueryBuilder.php:96
MediaWiki\User
Definition: ActorCache.php:21
MediaWiki\User\UserSelectQueryBuilder\userNames
userNames( $userNames)
Find by provided user names.
Definition: UserSelectQueryBuilder.php:63
Wikimedia\Rdbms\SelectQueryBuilder\conds
conds( $conds)
Add conditions to the query.
Definition: SelectQueryBuilder.php:275
MediaWiki\User\UserSelectQueryBuilder
Definition: UserSelectQueryBuilder.php:29
Wikimedia\Rdbms\SelectQueryBuilder\field
field( $field, $alias=null)
Add a single field to the query, optionally with an alias.
Definition: SelectQueryBuilder.php:205
Wikimedia\Rdbms\SelectQueryBuilder\orderBy
orderBy( $fields, $direction=null)
Set the ORDER BY clause.
Definition: SelectQueryBuilder.php:415
Wikimedia\Rdbms\SelectQueryBuilder\$db
IDatabase $db
Definition: SelectQueryBuilder.php:39
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:167
MediaWiki\User\ActorStore
Definition: ActorStore.php:43
MediaWiki\User\UserSelectQueryBuilder\fetchUserIdentities
fetchUserIdentities()
Fetch UserIdentities for the specified query.
Definition: UserSelectQueryBuilder.php:151
Wikimedia\Rdbms\JoinGroupBase\table
table( $table, $alias=null)
Add a single table or a single parenthesized group.
Definition: JoinGroupBase.php:28