| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\User; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use JobQueueGroup; |
| 7 |
use MediaWiki\Deferred\DeferredUpdates; |
| 8 |
use MediaWiki\Deferred\UserEditCountUpdate; |
| 9 |
use UserEditCountInitJob; |
| 10 |
use Wikimedia\Rdbms\DBAccessObjectUtils; |
| 11 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 12 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 13 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 14 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class UserEditTracker { |
| 23 |
|
| 24 |
private const FIRST_EDIT = 1; |
| 25 |
private const LATEST_EDIT = 2; |
| 26 |
|
| 27 |
private ActorNormalization $actorNormalization; |
| 28 |
private IConnectionProvider $dbProvider; |
| 29 |
private JobQueueGroup $jobQueueGroup; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
⧽` |
| 36 |
|
| 37 |
private $userEditCountCache = []; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public function __construct( |
| 45 |
ActorNormalization $actorNormalization, |
| 46 |
IConnectionProvider $dbProvider, |
| 47 |
JobQueueGroup $jobQueueGroup |
| 48 |
) { |
| 49 |
$this->actorNormalization = $actorNormalization; |
| 50 |
$this->dbProvider = $dbProvider; |
| 51 |
$this->jobQueueGroup = $jobQueueGroup; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function getUserEditCount( UserIdentity $user ): ?int { |
| 61 |
$userId = $user->getId(); |
| 62 |
if ( !$userId ) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
$cacheKey = 'u' . $userId; |
| 67 |
if ( isset( $this->userEditCountCache[ $cacheKey ] ) ) { |
| 68 |
return $this->userEditCountCache[ $cacheKey ]; |
| 69 |
} |
| 70 |
|
| 71 |
$count = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder() |
| 72 |
->select( 'user_editcount' ) |
| 73 |
->from( 'user' ) |
| 74 |
->where( [ 'user_id' => $userId ] ) |
| 75 |
->caller( __METHOD__ )->fetchField(); |
| 76 |
|
| 77 |
if ( $count === null ) { |
| 78 |
|
| 79 |
$count = $this->initializeUserEditCount( $user ); |
| 80 |
} |
| 81 |
|
| 82 |
$this->userEditCountCache[ $cacheKey ] = $count; |
| 83 |
return $count; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function initializeUserEditCount( UserIdentity $user ): int { |
| 92 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 93 |
$count = (int)$dbr->newSelectQueryBuilder() |
| 94 |
->select( 'COUNT(*)' ) |
| 95 |
->from( 'revision' ) |
| 96 |
->where( [ 'rev_actor' => $this->actorNormalization->findActorId( $user, $dbr ) ] ) |
| 97 |
->caller( __METHOD__ ) |
| 98 |
->fetchField(); |
| 99 |
|
| 100 |
|
| 101 |
$this->jobQueueGroup->push( new UserEditCountInitJob( [ |
| 102 |
'userId' => $user->getId(), |
| 103 |
'editCount' => $count, |
| 104 |
] ) ); |
| 105 |
|
| 106 |
return $count; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function incrementUserEditCount( UserIdentity $user ) { |
| 116 |
if ( !$user->getId() ) { |
| 117 |
|
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
DeferredUpdates::addUpdate( |
| 122 |
new UserEditCountUpdate( $user, 1 ), |
| 123 |
DeferredUpdates::POSTSEND |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function getFirstEditTimestamp( UserIdentity $user, int $flags = IDBAccessObject::READ_NORMAL ) { |
| 136 |
return $this->getUserEditTimestamp( $user, self::FIRST_EDIT, $flags ); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function getLatestEditTimestamp( UserIdentity $user, int $flags = IDBAccessObject::READ_NORMAL ) { |
| 148 |
return $this->getUserEditTimestamp( $user, self::LATEST_EDIT, $flags ); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
private function getUserEditTimestamp( UserIdentity $user, int $type, int $flags = IDBAccessObject::READ_NORMAL ) { |
| 160 |
if ( !$user->getId() ) { |
| 161 |
return false; |
| 162 |
} |
| 163 |
$db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags ); |
| 164 |
|
| 165 |
$sortOrder = ( $type === self::FIRST_EDIT ) ? SelectQueryBuilder::SORT_ASC : SelectQueryBuilder::SORT_DESC; |
| 166 |
$time = $db->newSelectQueryBuilder() |
| 167 |
->select( 'rev_timestamp' ) |
| 168 |
->from( 'revision' ) |
| 169 |
->where( [ 'rev_actor' => $this->actorNormalization->findActorId( $user, $db ) ] ) |
| 170 |
->orderBy( 'rev_timestamp', $sortOrder ) |
| 171 |
->caller( __METHOD__ ) |
| 172 |
->fetchField(); |
| 173 |
|
| 174 |
if ( !$time ) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
return ConvertibleTimestamp::convert( TS_MW, $time ); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function clearUserEditCache( UserIdentity $user ) { |
| 186 |
$userId = $user->getId(); |
| 187 |
if ( !$userId ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
$cacheKey = 'u' . $userId; |
| 192 |
unset( $this->userEditCountCache[ $cacheKey ] ); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public function setCachedUserEditCount( UserIdentity $user, int $editCount ) { |
| 202 |
$userId = $user->getId(); |
| 203 |
if ( !$userId ) { |
| 204 |
throw new InvalidArgumentException( __METHOD__ . ' with an anonymous user' ); |
| 205 |
} |
| 206 |
|
| 207 |
$cacheKey = 'u' . $userId; |
| 208 |
$this->userEditCountCache[ $cacheKey ] = $editCount; |
| 209 |
} |
| 210 |
|
| 211 |
} |