7use InvalidArgumentException;
13use Wikimedia\Timestamp\ConvertibleTimestamp;
23 private const FIRST_EDIT = 1;
24 private const LATEST_EDIT = 2;
36 private $userEditCountCache = [];
48 $this->actorMigration = $actorMigration;
49 $this->dbProvider = $dbProvider;
50 $this->jobQueueGroup = $jobQueueGroup;
60 $userId = $user->getId();
65 $cacheKey =
'u' . $userId;
66 if ( isset( $this->userEditCountCache[ $cacheKey ] ) ) {
67 return $this->userEditCountCache[ $cacheKey ];
70 $count = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
71 ->select(
'user_editcount' )
73 ->where( [
'user_id' => $userId ] )
74 ->caller( __METHOD__ )->fetchField();
76 if ( $count ===
null ) {
81 $this->userEditCountCache[ $cacheKey ] = $count;
91 $dbr = $this->dbProvider->getReplicaDatabase();
92 $actorWhere = $this->actorMigration->getWhere( $dbr,
'rev_user', $user );
94 $count = (int)$dbr->selectField(
95 [
'revision' ] + $actorWhere[
'tables'],
97 [ $actorWhere[
'conds'] ],
105 'userId' => $user->
getId(),
106 'editCount' => $count,
119 if ( !$user->
getId() ) {
124 DeferredUpdates::addUpdate(
126 DeferredUpdates::POSTSEND
139 return $this->getUserEditTimestamp( $user, self::FIRST_EDIT, $flags );
151 return $this->getUserEditTimestamp( $user, self::LATEST_EDIT, $flags );
162 private function getUserEditTimestamp(
UserIdentity $user,
int $type,
int $flags = IDBAccessObject::READ_NORMAL ) {
163 if ( !$user->
getId() ) {
166 [ $index ] = DBAccessObjectUtils::getDBOptions( $flags );
167 $db = DBAccessObjectUtils::getDBFromIndex( $this->dbProvider, $index );
169 $actorWhere = $this->actorMigration->getWhere( $db,
'rev_user', $user );
171 $sortOrder = ( $type === self::FIRST_EDIT ) ?
'ASC' :
'DESC';
172 $time = $db->selectField(
173 [
'revision' ] + $actorWhere[
'tables'],
175 [ $actorWhere[
'conds'] ],
177 [
'ORDER BY' =>
"rev_timestamp $sortOrder" ],
185 return ConvertibleTimestamp::convert( TS_MW, $time );
193 $userId = $user->
getId();
198 $cacheKey =
'u' . $userId;
199 unset( $this->userEditCountCache[ $cacheKey ] );
209 $userId = $user->
getId();
211 throw new InvalidArgumentException( __METHOD__ .
' with an anonymous user' );
214 $cacheKey =
'u' . $userId;
215 $this->userEditCountCache[ $cacheKey ] = $editCount;
Helper class for DAO classes.
Handle enqueueing of background jobs.
Job that initializes an user's edit count.
Interface for database access objects.