| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\User; |
| 22 |
|
| 23 |
use Iterator; |
| 24 |
use MediaWiki\Block\HideUserUtils; |
| 25 |
use MediaWiki\User\TempUser\TempUserConfig; |
| 26 |
use Wikimedia\Assert\Assert; |
| 27 |
use Wikimedia\Assert\PreconditionException; |
| 28 |
use Wikimedia\Rdbms\IExpression; |
| 29 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 30 |
use Wikimedia\Rdbms\LikeValue; |
| 31 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 32 |
|
| 33 |
class UserSelectQueryBuilder extends SelectQueryBuilder { |
| 34 |
|
| 35 |
|
| 36 |
private $actorStore; |
| 37 |
private TempUserConfig $tempUserConfig; |
| 38 |
private HideUserUtils $hideUserUtils; |
| 39 |
|
| 40 |
private bool $userJoined = false; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function __construct( |
| 49 |
IReadableDatabase $db, |
| 50 |
ActorStore $actorStore, |
| 51 |
TempUserConfig $tempUserConfig, |
| 52 |
HideUserUtils $hideUserUtils |
| 53 |
) { |
| 54 |
parent::__construct( $db ); |
| 55 |
|
| 56 |
$this->actorStore = $actorStore; |
| 57 |
$this->tempUserConfig = $tempUserConfig; |
| 58 |
$this->hideUserUtils = $hideUserUtils; |
| 59 |
$this->table( 'actor' ); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function whereUserIds( $userIds ): self { |
| 69 |
Assert::parameterType( [ 'integer', 'array' ], $userIds, '$userIds' ); |
| 70 |
$this->conds( [ 'actor_user' => $userIds ] ); |
| 71 |
return $this; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function userIds( $userIds ): self { |
| 81 |
return $this->whereUserIds( $userIds ); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function whereUserNames( $userNames ): self { |
| 91 |
Assert::parameterType( [ 'string', 'array' ], $userNames, '$userIds' ); |
| 92 |
$userNames = array_map( function ( $name ) { |
| 93 |
return $this->actorStore->normalizeUserName( (string)$name ); |
| 94 |
}, (array)$userNames ); |
| 95 |
$this->conds( [ 'actor_name' => $userNames ] ); |
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function userNames( $userNames ): self { |
| 106 |
return $this->whereUserNames( $userNames ); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public function whereUserNamePrefix( string $prefix ): self { |
| 119 |
if ( !isset( $this->options['LIMIT'] ) ) { |
| 120 |
throw new PreconditionException( 'Must set a limit when using a user name prefix' ); |
| 121 |
} |
| 122 |
$this->conds( |
| 123 |
$this->db->expr( 'actor_name', IExpression::LIKE, new LikeValue( $prefix, $this->db->anyString() ) ) |
| 124 |
); |
| 125 |
return $this; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function userNamePrefix( string $prefix ): self { |
| 138 |
return $this->whereUserNamePrefix( $prefix ); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function whereRegisteredTimestamp( string $timestamp, bool $direction ): self { |
| 150 |
if ( !$this->userJoined ) { |
| 151 |
$this->join( 'user', null, [ "actor_user=user_id" ] ); |
| 152 |
$this->userJoined = true; |
| 153 |
} |
| 154 |
|
| 155 |
$this->conds( |
| 156 |
$this->db->expr( 'user_registration', ( $direction ? '<' : '>' ), $this->db->timestamp( $timestamp ) ) |
| 157 |
); |
| 158 |
return $this; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public function orderByName( string $dir = self::SORT_ASC ): self { |
| 168 |
$this->orderBy( 'actor_name', $dir ); |
| 169 |
return $this; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public function orderByUserId( string $dir = self::SORT_ASC ): self { |
| 179 |
$this->orderBy( 'actor_user', $dir ); |
| 180 |
return $this; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public function registered(): self { |
| 189 |
$this->conds( $this->db->expr( 'actor_user', '!=', null ) ); |
| 190 |
return $this; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public function anon(): self { |
| 199 |
$this->conds( [ 'actor_user' => null ] ); |
| 200 |
return $this; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public function named(): self { |
| 209 |
if ( !$this->tempUserConfig->isKnown() ) { |
| 210 |
|
| 211 |
return $this; |
| 212 |
} |
| 213 |
$this->conds( $this->tempUserConfig->getMatchCondition( $this->db, 'actor_name', IExpression::NOT_LIKE ) ); |
| 214 |
return $this; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public function temp(): self { |
| 223 |
if ( !$this->tempUserConfig->isKnown() ) { |
| 224 |
$this->conds( '1=0' ); |
| 225 |
return $this; |
| 226 |
} |
| 227 |
$this->conds( $this->tempUserConfig->getMatchCondition( $this->db, 'actor_name', IExpression::LIKE ) ); |
| 228 |
return $this; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public function hidden( bool $hidden ): self { |
| 239 |
$this->conds( $this->hideUserUtils->getExpression( |
| 240 |
$this->db, |
| 241 |
'actor_user', |
| 242 |
$hidden ? HideUserUtils::HIDDEN_USERS : HideUserUtils::SHOWN_USERS |
| 243 |
) ); |
| 244 |
return $this; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public function fetchUserIdentity(): ?UserIdentity { |
| 253 |
$this->fields( [ 'actor_id', 'actor_name', 'actor_user' ] ); |
| 254 |
$row = $this->fetchRow(); |
| 255 |
if ( !$row ) { |
| 256 |
return null; |
| 257 |
} |
| 258 |
return $this->actorStore->newActorFromRow( $row ); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public function fetchUserIdentities(): Iterator { |
| 267 |
$this->fields( [ 'actor_id', 'actor_name', 'actor_user' ] ); |
| 268 |
|
| 269 |
$result = $this->fetchResultSet(); |
| 270 |
foreach ( $result as $row ) { |
| 271 |
yield $this->actorStore->newActorFromRow( $row ); |
| 272 |
} |
| 273 |
$result->free(); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public function fetchUserNames(): array { |
| 282 |
$this->field( 'actor_name' ); |
| 283 |
return $this->fetchFieldValues(); |
| 284 |
} |
| 285 |
} |