| 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\Config\ServiceOptions; |
| 27 |
use MediaWiki\Logger\LoggerFactory; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\Permissions\Authority; |
| 30 |
use RuntimeException; |
| 31 |
use stdClass; |
| 32 |
use Wikimedia\Rdbms\IDatabase; |
| 33 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 34 |
use Wikimedia\Rdbms\ILBFactory; |
| 35 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class UserFactory implements UserRigorOptions { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public const CONSTRUCTOR_OPTIONS = [ |
| 50 |
MainConfigNames::SharedDB, |
| 51 |
MainConfigNames::SharedTables, |
| 52 |
]; |
| 53 |
|
| 54 |
private ServiceOptions $options; |
| 55 |
private ILBFactory $loadBalancerFactory; |
| 56 |
private ILoadBalancer $loadBalancer; |
| 57 |
private UserNameUtils $userNameUtils; |
| 58 |
|
| 59 |
private ?User $lastUserFromIdentity = null; |
| 60 |
|
| 61 |
public function __construct( |
| 62 |
ServiceOptions $options, |
| 63 |
ILBFactory $loadBalancerFactory, |
| 64 |
UserNameUtils $userNameUtils |
| 65 |
) { |
| 66 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 67 |
$this->options = $options; |
| 68 |
$this->loadBalancerFactory = $loadBalancerFactory; |
| 69 |
$this->loadBalancer = $loadBalancerFactory->getMainLB(); |
| 70 |
$this->userNameUtils = $userNameUtils; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function newFromName( |
| 92 |
string $name, |
| 93 |
string $validate = self::RIGOR_VALID |
| 94 |
): ?User { |
| 95 |
|
| 96 |
$canonicalName = $this->userNameUtils->getCanonical( $name, $validate ); |
| 97 |
if ( $canonicalName === false ) { |
| 98 |
return null; |
| 99 |
} |
| 100 |
|
| 101 |
$user = new User(); |
| 102 |
$user->mName = $canonicalName; |
| 103 |
$user->mFrom = 'name'; |
| 104 |
$user->setItemLoaded( 'name' ); |
| 105 |
return $user; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function newAnonymous( ?string $ip = null ): User { |
| 117 |
if ( $ip ) { |
| 118 |
if ( !$this->userNameUtils->isIP( $ip ) ) { |
| 119 |
throw new InvalidArgumentException( 'Invalid IP address' ); |
| 120 |
} |
| 121 |
$user = new User(); |
| 122 |
$user->setName( $ip ); |
| 123 |
} else { |
| 124 |
$user = new User(); |
| 125 |
} |
| 126 |
return $user; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function newFromId( int $id ): User { |
| 138 |
$user = new User(); |
| 139 |
$user->mId = $id; |
| 140 |
$user->mFrom = 'id'; |
| 141 |
$user->setItemLoaded( 'id' ); |
| 142 |
return $user; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function newFromActorId( int $actorId ): User { |
| 154 |
$user = new User(); |
| 155 |
$user->mActorId = $actorId; |
| 156 |
$user->mFrom = 'actor'; |
| 157 |
$user->setItemLoaded( 'actor' ); |
| 158 |
return $user; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function newFromUserIdentity( UserIdentity $userIdentity ): User { |
| 170 |
if ( $userIdentity instanceof User ) { |
| 171 |
return $userIdentity; |
| 172 |
} |
| 173 |
|
| 174 |
$id = $userIdentity->getId(); |
| 175 |
$name = $userIdentity->getName(); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
if ( |
| 181 |
$this->lastUserFromIdentity |
| 182 |
&& $this->lastUserFromIdentity->getId() === $id |
| 183 |
&& $this->lastUserFromIdentity->getName() === $name |
| 184 |
&& $this->lastUserFromIdentity->getWikiId() === $userIdentity->getWikiId() |
| 185 |
) { |
| 186 |
return $this->lastUserFromIdentity; |
| 187 |
} |
| 188 |
|
| 189 |
$this->lastUserFromIdentity = $this->newFromAnyId( |
| 190 |
$id === 0 ? null : $id, |
| 191 |
$name === '' ? null : $name, |
| 192 |
null, |
| 193 |
$userIdentity->getWikiId() |
| 194 |
); |
| 195 |
|
| 196 |
return $this->lastUserFromIdentity; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public function newFromAnyId( |
| 215 |
?int $userId, |
| 216 |
?string $userName, |
| 217 |
?int $actorId = null, |
| 218 |
$dbDomain = false |
| 219 |
): User { |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
if ( $dbDomain !== false ) { |
| 225 |
LoggerFactory::getInstance( 'user' )->warning( |
| 226 |
'UserFactory::newFromAnyId called with cross-wiki user data', |
| 227 |
[ 'userId' => $userId, 'userName' => $userName, 'actorId' => $actorId, |
| 228 |
'dbDomain' => $dbDomain, 'exception' => new RuntimeException() ] |
| 229 |
); |
| 230 |
$userId = 0; |
| 231 |
$actorId = 0; |
| 232 |
} |
| 233 |
|
| 234 |
$user = new User; |
| 235 |
$user->mFrom = 'defaults'; |
| 236 |
|
| 237 |
if ( $actorId !== null ) { |
| 238 |
$user->mActorId = $actorId; |
| 239 |
if ( $actorId !== 0 ) { |
| 240 |
$user->mFrom = 'actor'; |
| 241 |
} |
| 242 |
$user->setItemLoaded( 'actor' ); |
| 243 |
} |
| 244 |
|
| 245 |
if ( $userName !== null && $userName !== '' ) { |
| 246 |
$user->mName = $userName; |
| 247 |
$user->mFrom = 'name'; |
| 248 |
$user->setItemLoaded( 'name' ); |
| 249 |
} |
| 250 |
|
| 251 |
if ( $userId !== null ) { |
| 252 |
$user->mId = $userId; |
| 253 |
if ( $userId !== 0 ) { |
| 254 |
$user->mFrom = 'id'; |
| 255 |
} |
| 256 |
$user->setItemLoaded( 'id' ); |
| 257 |
} |
| 258 |
|
| 259 |
if ( $user->mFrom === 'defaults' ) { |
| 260 |
throw new InvalidArgumentException( |
| 261 |
'Cannot create a user with no name, no ID, and no actor ID' |
| 262 |
); |
| 263 |
} |
| 264 |
|
| 265 |
return $user; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public function newFromConfirmationCode( |
| 281 |
string $confirmationCode, |
| 282 |
int $flags = IDBAccessObject::READ_NORMAL |
| 283 |
): ?User { |
| 284 |
if ( ( $flags & IDBAccessObject::READ_LATEST ) == IDBAccessObject::READ_LATEST ) { |
| 285 |
$db = $this->loadBalancer->getConnection( DB_PRIMARY ); |
| 286 |
} else { |
| 287 |
$db = $this->loadBalancer->getConnection( DB_REPLICA ); |
| 288 |
} |
| 289 |
|
| 290 |
$id = $db->newSelectQueryBuilder() |
| 291 |
->select( 'user_id' ) |
| 292 |
->from( 'user' ) |
| 293 |
->where( [ 'user_email_token' => md5( $confirmationCode ) ] ) |
| 294 |
->andWhere( $db->expr( 'user_email_token_expires', '>', $db->timestamp() ) ) |
| 295 |
->recency( $flags ) |
| 296 |
->caller( __METHOD__ )->fetchField(); |
| 297 |
|
| 298 |
if ( !$id ) { |
| 299 |
return null; |
| 300 |
} |
| 301 |
|
| 302 |
return $this->newFromId( (int)$id ); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public function newFromRow( $row, $data = null ): User { |
| 315 |
return User::newFromRow( $row, $data ); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public function newFromAuthority( Authority $authority ): User { |
| 324 |
if ( $authority instanceof User ) { |
| 325 |
return $authority; |
| 326 |
} |
| 327 |
return $this->newFromUserIdentity( $authority->getUser() ); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public function newTempPlaceholder(): User { |
| 339 |
$user = new User(); |
| 340 |
$user->setName( $this->userNameUtils->getTempPlaceholder() ); |
| 341 |
return $user; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public function newUnsavedTempUser( ?string $name ): User { |
| 352 |
$user = new User(); |
| 353 |
$user->setName( $name ?? $this->userNameUtils->getTempPlaceholder() ); |
| 354 |
return $user; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public function invalidateCache( UserIdentity $userIdentity ): void { |
| 363 |
if ( !$userIdentity->isRegistered() ) { |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
$wikiId = $userIdentity->getWikiId(); |
| 368 |
if ( $wikiId === UserIdentity::LOCAL ) { |
| 369 |
$legacyUser = $this->newFromUserIdentity( $userIdentity ); |
| 370 |
|
| 371 |
$legacyUser->invalidateCache(); |
| 372 |
} else { |
| 373 |
|
| 374 |
$userId = $userIdentity->getId( $wikiId ); |
| 375 |
|
| 376 |
$dbw = $this->getUserTableConnection( ILoadBalancer::DB_PRIMARY, $wikiId ); |
| 377 |
$dbw->newUpdateQueryBuilder() |
| 378 |
->update( 'user' ) |
| 379 |
->set( [ 'user_touched' => $dbw->timestamp() ] ) |
| 380 |
->where( [ 'user_id' => $userId ] ) |
| 381 |
->caller( __METHOD__ )->execute(); |
| 382 |
|
| 383 |
$dbw->onTransactionPreCommitOrIdle( |
| 384 |
static function () use ( $wikiId, $userId ) { |
| 385 |
User::purge( $wikiId, $userId ); |
| 386 |
}, |
| 387 |
__METHOD__ |
| 388 |
); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
private function getUserTableConnection( $mode, $wikiId ): IDatabase { |
| 398 |
if ( is_string( $wikiId ) && $this->loadBalancerFactory->getLocalDomainID() === $wikiId ) { |
| 399 |
$wikiId = UserIdentity::LOCAL; |
| 400 |
} |
| 401 |
|
| 402 |
if ( $this->options->get( MainConfigNames::SharedDB ) && |
| 403 |
in_array( 'user', $this->options->get( MainConfigNames::SharedTables ) ) |
| 404 |
) { |
| 405 |
|
| 406 |
$lb = $this->loadBalancer; |
| 407 |
} else { |
| 408 |
$lb = $this->loadBalancerFactory->getMainLB( $wikiId ); |
| 409 |
} |
| 410 |
|
| 411 |
return $lb->getConnection( $mode, [], $wikiId ); |
| 412 |
} |
| 413 |
} |