MediaWiki REL1_35
UserEditTracker.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\User;
4
6use InvalidArgumentException;
9
18
19 private const FIRST_EDIT = 1;
20 private const LATEST_EDIT = 2;
21
24
27
34 private $userEditCountCache = [];
35
40 public function __construct(
43 ) {
44 $this->actorMigration = $actorMigration;
45 $this->loadBalancer = $loadBalancer;
46 }
47
55 public function getUserEditCount( UserIdentity $user ) : int {
56 if ( !$user->getId() ) {
57 throw new InvalidArgumentException(
58 __METHOD__ . ' requires Users with ids set'
59 );
60 }
61
62 $userId = $user->getId();
63 $cacheKey = 'u' . (string)$userId;
64
65 if ( isset( $this->userEditCountCache[ $cacheKey ] ) ) {
66 return $this->userEditCountCache[ $cacheKey ];
67 }
68
69 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
70 $count = $dbr->selectField(
71 'user',
72 'user_editcount',
73 [ 'user_id' => $userId ],
74 __METHOD__
75 );
76
77 if ( $count === null ) {
78 // it has not been initialized. do so.
79 $count = $this->initializeUserEditCount( $user );
80 }
81
82 $this->userEditCountCache[ $cacheKey ] = $count;
83 return $count;
84 }
85
92 public function initializeUserEditCount( UserIdentity $user ) : int {
93 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
94 $actorWhere = $this->actorMigration->getWhere( $dbr, 'rev_user', $user );
95
96 $count = (int)$dbr->selectField(
97 [ 'revision' ] + $actorWhere['tables'],
98 'COUNT(*)',
99 [ $actorWhere['conds'] ],
100 __METHOD__,
101 [],
102 $actorWhere['joins']
103 );
104
105 $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
106 $dbw->update(
107 'user',
108 [ 'user_editcount' => $count ],
109 [
110 'user_id' => $user->getId(),
111 'user_editcount IS NULL OR user_editcount < ' . $count
112 ],
113 __METHOD__
114 );
115
116 return $count;
117 }
118
126 public function getFirstEditTimestamp( UserIdentity $user ) {
127 return $this->getUserEditTimestamp( $user, self::FIRST_EDIT );
128 }
129
137 public function getLatestEditTimestamp( UserIdentity $user ) {
138 return $this->getUserEditTimestamp( $user, self::LATEST_EDIT );
139 }
140
149 private function getUserEditTimestamp( UserIdentity $user, int $type ) {
150 if ( $user->getId() === 0 ) {
151 return false; // anonymous user
152 }
153
154 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
155 $actorWhere = $this->actorMigration->getWhere( $dbr, 'rev_user', $user );
156
157 $tsField = isset( $actorWhere['tables']['temp_rev_user'] )
158 ? 'revactor_timestamp' : 'rev_timestamp';
159
160 $sortOrder = ( $type === self::FIRST_EDIT ) ? 'ASC' : 'DESC';
161 $time = $dbr->selectField(
162 [ 'revision' ] + $actorWhere['tables'],
163 $tsField,
164 [ $actorWhere['conds'] ],
165 __METHOD__,
166 [ 'ORDER BY' => "$tsField $sortOrder" ],
167 $actorWhere['joins']
168 );
169
170 if ( !$time ) {
171 return false; // no edits
172 }
173
174 return MWTimestamp::convert( TS_MW, $time );
175 }
176
182 public function clearUserEditCache( UserIdentity $user ) {
183 if ( !$user->isRegistered() ) {
184 return;
185 }
186
187 $userId = $user->getId();
188 $cacheKey = 'u' . (string)$userId;
189
190 $this->userEditCountCache[ $cacheKey ] = null;
191 }
192
193}
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:85
This class handles the logic for the actor table migration and should always be used in lieu of direc...
Library for creating and parsing MW-style timestamps.
Track info about user edit counts and timings.
getLatestEditTimestamp(UserIdentity $user)
Get the user's latest edit timestamp.
getUserEditTimestamp(UserIdentity $user, int $type)
Get the timestamp of a user's edit, either their first or latest.
getUserEditCount(UserIdentity $user)
Get a user's edit count from the user_editcount field, falling back to initialize.
array $userEditCountCache
Mapping of user id to edit count for caching To avoid using non-sequential numerical keys,...
__construct(ActorMigration $actorMigration, ILoadBalancer $loadBalancer)
getFirstEditTimestamp(UserIdentity $user)
Get the user's first edit timestamp.
initializeUserEditCount(UserIdentity $user)
clearUserEditCache(UserIdentity $user)
Interface for objects representing user identity.
Database cluster connection, tracking, load balancing, and transaction manager interface.
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29