Go to the documentation of this file.
26 use InvalidArgumentException;
29 use Psr\Log\LoggerInterface;
32 use Wikimedia\Assert\Assert;
33 use Wikimedia\IPUtils;
80 $wikiId = WikiAwareEntity::LOCAL
82 Assert::parameterType(
'string|boolean',
$wikiId,
'$wikiId' );
83 Assert::parameter(
$wikiId !==
true,
'$wikiId',
'must be false or a string' );
90 $this->actorsByActorId =
new MapCacheLRU( self::LOCAL_CACHE_SIZE );
91 $this->actorsByUserId =
new MapCacheLRU( self::LOCAL_CACHE_SIZE );
92 $this->actorsByName =
new MapCacheLRU( self::LOCAL_CACHE_SIZE );
110 $actorId = (int)$row->actor_id;
111 $userId = isset( $row->actor_user ) ? (int)$row->actor_user : 0;
112 if ( $actorId === 0 ) {
113 throw new InvalidArgumentException(
"Actor ID is 0 for {$row->actor_name} and {$userId}" );
117 if ( !$normalizedName ) {
118 $this->logger->warning(
'Encountered invalid actor name in database', [
119 'user_id' => $userId,
120 'actor_id' => $actorId,
121 'actor_name' => $row->actor_name,
122 'wiki_id' => $this->wikiId ?:
'local'
126 if ( $row->actor_name ===
'' ) {
127 throw new InvalidArgumentException(
"Actor name can not be empty for {$userId} and {$actorId}" );
131 $actor =
new UserIdentityValue( $userId, $row->actor_name, $actorId, $this->wikiId );
157 $userId = $userId ===
null ? 0 : (int)$userId;
159 if ( $actorId ===
null ) {
160 throw new InvalidArgumentException(
"Actor ID is null for {$name} and {$userId}" );
162 if ( (
int)$actorId === 0 ) {
163 throw new InvalidArgumentException(
"Actor ID is 0 for {$name} and {$userId}" );
167 if ( !$normalizedName ) {
168 $this->logger->warning(
'Encountered invalid actor name in database', [
169 'user_id' => $userId,
170 'actor_id' => $actorId,
171 'actor_name' => $name,
172 'wiki_id' => $this->wikiId ?:
'local'
176 if ( $name ===
'' ) {
177 throw new InvalidArgumentException(
"Actor name can not be empty for {$userId} and {$actorId}" );
181 $actorId = (int)$actorId;
198 $this->actorsByActorId->set( $actorId, [ $actor, $actorId ] );
199 $userId = $actor->
getId( $this->wikiId );
201 $this->actorsByUserId->set( $userId, [ $actor, $actorId ] );
203 $this->actorsByName->set( $actor->
getName(), [ $actor, $actorId ] );
221 $cachedValue = $this->actorsByActorId->get( $actorId );
222 if ( $cachedValue ) {
223 return $cachedValue[0];
227 ->caller( __METHOD__ )
228 ->conds( [
'actor_id' => $actorId ] )
229 ->fetchUserIdentity();
236 ->caller( __METHOD__ )
237 ->conds( [
'actor_id' => $actorId ] )
238 ->fetchUserIdentity();
252 if ( $name ===
'' ) {
253 throw new InvalidArgumentException(
'Empty string passed as actor name' );
258 throw new InvalidArgumentException(
"Unable to normalize the provided actor name {$name}" );
261 $cachedValue = $this->actorsByName->get( $name );
262 if ( $cachedValue ) {
263 return $cachedValue[0];
267 ->caller( __METHOD__ )
269 ->fetchUserIdentity();
284 $cachedValue = $this->actorsByUserId->get( $userId );
285 if ( $cachedValue ) {
286 return $cachedValue[0];
290 ->caller( __METHOD__ )
292 ->fetchUserIdentity();
305 $user->setActorId( $id );
306 } elseif ( $user instanceof
User ) {
307 $user->setActorId( $id );
330 $this->logger->warning(
'Encountered a UserIdentity with invalid name', [
331 'user_name' => $user->
getName()
377 array $queryOptions = []
384 $cachedValue = $this->actorsByName->get( $name );
385 if ( $cachedValue ) {
386 return $cachedValue[1];
391 [
'actor_user',
'actor_name',
'actor_id' ],
392 [
'actor_name' => $name ],
397 if ( !$row || !$row->actor_id ) {
401 $id = (int)$row->actor_id;
404 $this->newActorFromRow( $row );
430 'Calling acquireActorId() without the $dbw parameter is deprecated',
442 if ( $userName ===
null || $userName ===
'' ) {
443 $userIdForErrorMessage = $user->
getId( $this->wikiId );
445 'Cannot create an actor for a user with no name: ' .
446 "user_id={$userIdForErrorMessage} user_name=\"{$user->getName()}\""
452 if ( $existingActorId ) {
454 return $existingActorId;
457 $userId = $user->
getId( $this->wikiId ) ?:
null;
458 if ( $userId ===
null && $this->userNameUtils->isUsable( $user->
getName() ) ) {
460 'Cannot create an actor for a usable name that is not an existing user: ' .
461 "user_name=\"{$user->getName()}\""
468 'actor_user' => $userId,
469 'actor_name' => $userName,
473 if ( $dbw->affectedRows() ) {
474 $actorId = (int)$dbw->insertId();
481 [
'LOCK IN SHARE MODE' ]
485 "Failed to create actor ID for " .
486 "user_id={$userId} user_name=\"{$userName}\""
507 public function normalizeUserName(
string $name, $rigor = UserNameUtils::RIGOR_NONE ): ?string {
508 if ( $this->userNameUtils->isIP( $name ) ) {
509 return IPUtils::sanitizeIP( $name );
515 } elseif ( $rigor !== UserNameUtils::RIGOR_NONE ) {
516 return $this->userNameUtils->getCanonical( $name, $rigor ) ?:
null;
528 return [ $this->loadBalancer->getConnectionRef( $mode, [], $this->wikiId ), $options ];
539 $storeDomain = $this->loadBalancer->resolveDomainID( $this->wikiId );
540 if ( $dbDomain !== $storeDomain ) {
541 throw new InvalidArgumentException(
542 "DB connection domain '$dbDomain' does not match '$storeDomain'"
574 $queryBuilder->options( $options );
575 return $queryBuilder;
if(ini_get( 'mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Class to parse and build external user names.
static getDBOptions( $bitfield)
Get an appropriate DB index, options, and fallback DB index for a query.
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
Handles a simple LRU key/value map with a maximum number of entries.
Helper class for DAO classes.
Exception thrown when an actor can't be created.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
static isExternal( $username)
Tells whether the username is external or not.