3 * User edit count incrementing.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use Wikimedia\Assert\Assert
;
24 use MediaWiki\MediaWikiServices
;
27 * Handles increment the edit count for a given set of users
29 class UserEditCountUpdate
implements DeferrableUpdate
, MergeableUpdate
{
30 /** @var array[] Map of (user ID => ('increment': int, 'instances': User[])) */
35 * @param int $increment
37 public function __construct( User
$user, $increment ) {
38 if ( !$user->getId() ) {
39 throw new RuntimeException( "Got user ID of zero" );
42 $user->getId() => [ 'increment' => $increment, 'instances' => [ $user ] ]
46 public function merge( MergeableUpdate
$update ) {
47 /** @var UserEditCountUpdate $update */
48 Assert
::parameterType( __CLASS__
, $update, '$update' );
49 '@phan-var UserEditCountUpdate $update';
51 foreach ( $update->infoByUser
as $userId => $info ) {
52 if ( !isset( $this->infoByUser
[$userId] ) ) {
53 $this->infoByUser
[$userId] = [ 'increment' => 0, 'instances' => [] ];
55 // Merge the increment amount
56 $this->infoByUser
[$userId]['increment'] +
= $info['increment'];
57 // Merge the list of User instances to update in doUpdate()
58 foreach ( $info['instances'] as $user ) {
59 if ( !in_array( $user, $this->infoByUser
[$userId]['instances'], true ) ) {
60 $this->infoByUser
[$userId]['instances'][] = $user;
67 * Purges the list of URLs passed to the constructor.
69 public function doUpdate() {
70 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
71 $dbw = $lb->getConnectionRef( DB_MASTER
);
74 ( new AutoCommitUpdate( $dbw, __METHOD__
, function () use ( $lb, $dbw, $fname ) {
75 foreach ( $this->infoByUser
as $userId => $info ) {
78 [ 'user_editcount=user_editcount+' . (int)$info['increment'] ],
79 [ 'user_id' => $userId, 'user_editcount IS NOT NULL' ],
82 /** @var User[] $affectedInstances */
83 $affectedInstances = $info['instances'];
84 // Lazy initialization check...
85 if ( $dbw->affectedRows() == 0 ) {
86 // The user_editcount is probably NULL (e.g. not initialized).
87 // Since this update runs after the new revisions were committed,
88 // wait for the replica DB to catch up so they will be counted.
89 $dbr = $lb->getConnectionRef( DB_REPLICA
);
90 // If $dbr is actually the master DB, then clearing the snapshot
91 // is harmless and waitForMasterPos() will just no-op.
92 $dbr->flushSnapshot( $fname );
93 $lb->waitForMasterPos( $dbr );
94 $affectedInstances[0]->initEditCountInternal( $dbr );
96 $newCount = (int)$dbw->selectField(
99 [ 'user_id' => $userId ],
103 // Update the edit count in the instance caches. This is mostly useful
104 // for maintenance scripts, where deferred updates might run immediately
105 // and user instances might be reused for a long time.
106 foreach ( $affectedInstances as $affectedInstance ) {
107 $affectedInstance->setEditCountInternal( $newCount );
109 // Clear the edit count in user cache too
110 $affectedInstances[0]->invalidateCache();