5use InvalidArgumentException;
14use Wikimedia\Timestamp\ConvertibleTimestamp;
25 private const FIRST_EDIT = 1;
26 private const LATEST_EDIT = 2;
38 private $userEditCountCache = [];
50 $this->actorNormalization = $actorNormalization;
51 $this->dbProvider = $dbProvider;
52 $this->jobQueueGroup = $jobQueueGroup;
62 $userId = $user->getId();
67 $cacheKey =
'u' . $userId;
68 if ( isset( $this->userEditCountCache[ $cacheKey ] ) ) {
69 return $this->userEditCountCache[ $cacheKey ];
72 $count = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
73 ->select(
'user_editcount' )
75 ->where( [
'user_id' => $userId ] )
76 ->caller( __METHOD__ )->fetchField();
78 if ( $count ===
null ) {
83 $this->userEditCountCache[ $cacheKey ] = $count;
93 $dbr = $this->dbProvider->getReplicaDatabase();
94 $count = (int)$dbr->newSelectQueryBuilder()
95 ->select(
'COUNT(*)' )
97 ->where( [
'rev_actor' => $this->actorNormalization->findActorId( $user, $dbr ) ] )
98 ->caller( __METHOD__ )
103 'userId' => $user->
getId(),
104 'editCount' => $count,
117 if ( !$user->
getId() ) {
122 DeferredUpdates::addUpdate(
124 DeferredUpdates::POSTSEND
137 return $this->getUserEditTimestamp( $user, self::FIRST_EDIT, $flags );
149 return $this->getUserEditTimestamp( $user, self::LATEST_EDIT, $flags );
160 private function getUserEditTimestamp(
UserIdentity $user,
int $type,
int $flags = IDBAccessObject::READ_NORMAL ) {
161 if ( !$user->
getId() ) {
164 $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags );
166 $sortOrder = ( $type === self::FIRST_EDIT ) ? SelectQueryBuilder::SORT_ASC : SelectQueryBuilder::SORT_DESC;
167 $time = $db->newSelectQueryBuilder()
168 ->select(
'rev_timestamp' )
170 ->where( [
'rev_actor' => $this->actorNormalization->findActorId( $user, $db ) ] )
171 ->orderBy(
'rev_timestamp', $sortOrder )
172 ->caller( __METHOD__ )
179 return ConvertibleTimestamp::convert( TS_MW, $time );
187 $userId = $user->
getId();
192 $cacheKey =
'u' . $userId;
193 unset( $this->userEditCountCache[ $cacheKey ] );
203 $userId = $user->
getId();
205 throw new InvalidArgumentException( __METHOD__ .
' with an anonymous user' );
208 $cacheKey =
'u' . $userId;
209 $this->userEditCountCache[ $cacheKey ] = $editCount;
Handle enqueueing of background jobs.
Job that initializes an user's edit count.