5use InvalidArgumentException;
14use Wikimedia\Timestamp\ConvertibleTimestamp;
24 private const FIRST_EDIT = 1;
25 private const LATEST_EDIT = 2;
37 private $userEditCountCache = [];
49 $this->actorNormalization = $actorNormalization;
50 $this->dbProvider = $dbProvider;
51 $this->jobQueueGroup = $jobQueueGroup;
61 $userId = $user->getId();
66 $cacheKey =
'u' . $userId;
67 if ( isset( $this->userEditCountCache[ $cacheKey ] ) ) {
68 return $this->userEditCountCache[ $cacheKey ];
71 $count = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
72 ->select(
'user_editcount' )
74 ->where( [
'user_id' => $userId ] )
75 ->caller( __METHOD__ )->fetchField();
77 if ( $count ===
null ) {
82 $this->userEditCountCache[ $cacheKey ] = $count;
92 $dbr = $this->dbProvider->getReplicaDatabase();
93 $count = (int)$dbr->newSelectQueryBuilder()
94 ->select(
'COUNT(*)' )
96 ->where( [
'rev_actor' => $this->actorNormalization->findActorId( $user, $dbr ) ] )
97 ->caller( __METHOD__ )
102 'userId' => $user->
getId(),
103 'editCount' => $count,
116 if ( !$user->
getId() ) {
121 DeferredUpdates::addUpdate(
123 DeferredUpdates::POSTSEND
136 return $this->getUserEditTimestamp( $user, self::FIRST_EDIT, $flags );
148 return $this->getUserEditTimestamp( $user, self::LATEST_EDIT, $flags );
159 private function getUserEditTimestamp(
UserIdentity $user,
int $type,
int $flags = IDBAccessObject::READ_NORMAL ) {
160 if ( !$user->
getId() ) {
163 $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags );
165 $sortOrder = ( $type === self::FIRST_EDIT ) ? SelectQueryBuilder::SORT_ASC : SelectQueryBuilder::SORT_DESC;
166 $time = $db->newSelectQueryBuilder()
167 ->select(
'rev_timestamp' )
169 ->where( [
'rev_actor' => $this->actorNormalization->findActorId( $user, $db ) ] )
170 ->orderBy(
'rev_timestamp', $sortOrder )
171 ->caller( __METHOD__ )
178 return ConvertibleTimestamp::convert( TS_MW, $time );
186 $userId = $user->
getId();
191 $cacheKey =
'u' . $userId;
192 unset( $this->userEditCountCache[ $cacheKey ] );
202 $userId = $user->
getId();
204 throw new InvalidArgumentException( __METHOD__ .
' with an anonymous user' );
207 $cacheKey =
'u' . $userId;
208 $this->userEditCountCache[ $cacheKey ] = $editCount;
Handle enqueueing of background jobs.
Job that initializes an user's edit count.