MediaWiki  master
UserEditTracker.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\User;
4 
6 use InvalidArgumentException;
7 use JobQueueGroup;
9 use MWTimestamp;
11 
20 
21  private const FIRST_EDIT = 1;
22  private const LATEST_EDIT = 2;
23 
25  private $actorMigration;
26 
28  private $loadBalancer;
29 
31  private $jobQueueGroup;
32 
39  private $userEditCountCache = [];
40 
46  public function __construct(
50  ) {
51  $this->actorMigration = $actorMigration;
52  $this->loadBalancer = $loadBalancer;
53  $this->jobQueueGroup = $jobQueueGroup;
54  }
55 
63  public function getUserEditCount( UserIdentity $user ) : int {
64  if ( !$user->getId() ) {
65  throw new InvalidArgumentException(
66  __METHOD__ . ' requires Users with ids set'
67  );
68  }
69 
70  $userId = $user->getId();
71  $cacheKey = 'u' . (string)$userId;
72 
73  if ( isset( $this->userEditCountCache[ $cacheKey ] ) ) {
74  return $this->userEditCountCache[ $cacheKey ];
75  }
76 
77  $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
78  $count = $dbr->selectField(
79  'user',
80  'user_editcount',
81  [ 'user_id' => $userId ],
82  __METHOD__
83  );
84 
85  if ( $count === null ) {
86  // it has not been initialized. do so.
87  $count = $this->initializeUserEditCount( $user );
88  }
89 
90  $this->userEditCountCache[ $cacheKey ] = $count;
91  return $count;
92  }
93 
100  public function initializeUserEditCount( UserIdentity $user ) : int {
101  $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
102  $actorWhere = $this->actorMigration->getWhere( $dbr, 'rev_user', $user );
103 
104  $count = (int)$dbr->selectField(
105  [ 'revision' ] + $actorWhere['tables'],
106  'COUNT(*)',
107  [ $actorWhere['conds'] ],
108  __METHOD__,
109  [],
110  $actorWhere['joins']
111  );
112 
113  // (T259719) Defer updating the edit count via a job
114  $this->jobQueueGroup->push(
115  new JobSpecification(
116  'userEditCountInit',
117  [
118  'userId' => $user->getId(),
119  'editCount' => $count,
120  ],
121  [
122  'removeDuplicates' => true,
123  ]
124  )
125  );
126 
127  return $count;
128  }
129 
137  public function getFirstEditTimestamp( UserIdentity $user ) {
138  return $this->getUserEditTimestamp( $user, self::FIRST_EDIT );
139  }
140 
148  public function getLatestEditTimestamp( UserIdentity $user ) {
149  return $this->getUserEditTimestamp( $user, self::LATEST_EDIT );
150  }
151 
160  private function getUserEditTimestamp( UserIdentity $user, int $type ) {
161  if ( $user->getId() === 0 ) {
162  return false; // anonymous user
163  }
164 
165  $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
166  $actorWhere = $this->actorMigration->getWhere( $dbr, 'rev_user', $user );
167 
168  $tsField = isset( $actorWhere['tables']['temp_rev_user'] )
169  ? 'revactor_timestamp' : 'rev_timestamp';
170 
171  $sortOrder = ( $type === self::FIRST_EDIT ) ? 'ASC' : 'DESC';
172  $time = $dbr->selectField(
173  [ 'revision' ] + $actorWhere['tables'],
174  $tsField,
175  [ $actorWhere['conds'] ],
176  __METHOD__,
177  [ 'ORDER BY' => "$tsField $sortOrder" ],
178  $actorWhere['joins']
179  );
180 
181  if ( !$time ) {
182  return false; // no edits
183  }
184 
185  return MWTimestamp::convert( TS_MW, $time );
186  }
187 
193  public function clearUserEditCache( UserIdentity $user ) {
194  if ( !$user->isRegistered() ) {
195  return;
196  }
197 
198  $userId = $user->getId();
199  $cacheKey = 'u' . (string)$userId;
200 
201  $this->userEditCountCache[ $cacheKey ] = null;
202  }
203 
204 }
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:34
MediaWiki\User\UserEditTracker\getUserEditCount
getUserEditCount(UserIdentity $user)
Get a user's edit count from the user_editcount field, falling back to initialize.
Definition: UserEditTracker.php:63
MediaWiki\User\UserEditTracker\LATEST_EDIT
const LATEST_EDIT
Definition: UserEditTracker.php:22
MediaWiki\User\UserEditTracker\__construct
__construct(ActorMigration $actorMigration, ILoadBalancer $loadBalancer, JobQueueGroup $jobQueueGroup)
Definition: UserEditTracker.php:46
MediaWiki\User\UserEditTracker\getUserEditTimestamp
getUserEditTimestamp(UserIdentity $user, int $type)
Get the timestamp of a user's edit, either their first or latest.
Definition: UserEditTracker.php:160
ActorMigration
This class handles the logic for the actor table migration and should always be used in lieu of direc...
Definition: ActorMigration.php:39
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
$dbr
$dbr
Definition: testCompression.php:54
MediaWiki\User\UserEditTracker\getLatestEditTimestamp
getLatestEditTimestamp(UserIdentity $user)
Get the user's latest edit timestamp.
Definition: UserEditTracker.php:148
MediaWiki\User\UserIdentity\isRegistered
isRegistered()
MediaWiki\User\UserEditTracker\getFirstEditTimestamp
getFirstEditTimestamp(UserIdentity $user)
Get the user's first edit timestamp.
Definition: UserEditTracker.php:137
MediaWiki\User\UserEditTracker\$loadBalancer
ILoadBalancer $loadBalancer
Definition: UserEditTracker.php:28
MediaWiki\User\UserEditTracker\FIRST_EDIT
const FIRST_EDIT
Definition: UserEditTracker.php:21
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
MediaWiki\User\UserEditTracker\$actorMigration
ActorMigration $actorMigration
Definition: UserEditTracker.php:25
MediaWiki\User\UserEditTracker\$userEditCountCache
array $userEditCountCache
Mapping of user id to edit count for caching To avoid using non-sequential numerical keys,...
Definition: UserEditTracker.php:39
MediaWiki\User
Definition: DefaultOptionsLookup.php:21
MediaWiki\User\UserEditTracker\initializeUserEditCount
initializeUserEditCount(UserIdentity $user)
Definition: UserEditTracker.php:100
MediaWiki\User\UserIdentity\getId
getId()
MediaWiki\User\UserEditTracker
Track info about user edit counts and timings.
Definition: UserEditTracker.php:19
JobSpecification
Job queue task description base code.
Definition: JobSpecification.php:39
MediaWiki\User\UserEditTracker\clearUserEditCache
clearUserEditCache(UserIdentity $user)
Definition: UserEditTracker.php:193
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
JobQueueGroup
Class to handle enqueueing of background jobs.
Definition: JobQueueGroup.php:30
MediaWiki\User\UserEditTracker\$jobQueueGroup
JobQueueGroup $jobQueueGroup
Definition: UserEditTracker.php:31
$type
$type
Definition: testCompression.php:52