MediaWiki  master
UserEditCountUpdate.php
Go to the documentation of this file.
1 <?php
25 use Wikimedia\Assert\Assert;
26 
36  private $infoByUser;
37 
42  public function __construct( UserIdentity $user, $increment ) {
43  if ( !$user->getId() ) {
44  throw new RuntimeException( "Got user ID of zero" );
45  }
46  $this->infoByUser = [
47  $user->getId() => [ 'increment' => $increment, 'object' => $user ]
48  ];
49  }
50 
51  public function merge( MergeableUpdate $update ) {
53  Assert::parameterType( __CLASS__, $update, '$update' );
54  '@phan-var UserEditCountUpdate $update';
55 
56  foreach ( $update->infoByUser as $userId => $info ) {
57  if ( !isset( $this->infoByUser[$userId] ) ) {
58  // Object will be filled in below
59  $this->infoByUser[$userId] = [ 'increment' => 0 ];
60  }
61  // Merge the increment amount
62  $this->infoByUser[$userId]['increment'] += $info['increment'];
63  // Always use the UserIdentity from the other update in case we don't
64  // already have info for the user
65  $this->infoByUser[$userId]['object'] = $info['object'];
66  }
67  }
68 
72  public function doUpdate() {
73  $mwServices = MediaWikiServices::getInstance();
74  $lb = $mwServices->getDBLoadBalancer();
75  $dbw = $lb->getConnectionRef( DB_PRIMARY );
76  $editTracker = $mwServices->getUserEditTracker();
77  $fname = __METHOD__;
78 
79  ( new AutoCommitUpdate( $dbw, __METHOD__, function () use ( $lb, $dbw, $fname, $editTracker ) {
80  foreach ( $this->infoByUser as $userId => $info ) {
81  $dbw->update(
82  'user',
83  [ 'user_editcount=user_editcount+' . (int)$info['increment'] ],
84  [ 'user_id' => $userId, 'user_editcount IS NOT NULL' ],
85  $fname
86  );
88  $targetUserIdentity = $info['object'];
89  // Lazy initialization check...
90  if ( $dbw->affectedRows() == 0 ) {
91  // The user_editcount is probably NULL (e.g. not initialized).
92  // Since this update runs after the new revisions were committed,
93  // wait for the replica DB to catch up so they will be counted.
94  $dbr = $lb->getConnectionRef( DB_REPLICA );
95  // If $dbr is actually the primary DB, then clearing the snapshot
96  // is harmless and waitForPrimaryPos() will just no-op.
97  $dbr->flushSnapshot( $fname );
98  $lb->waitForPrimaryPos( $dbr );
99  $editTracker->initializeUserEditCount( $targetUserIdentity );
100  }
101 
102  // Clear the edit count in the UserEditTracker cache.
103  $editTracker->clearUserEditCache( $targetUserIdentity );
104  }
105  } ) )->doUpdate();
106  }
107 }
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:204
AutoCommitUpdate
Deferrable Update for closure/callback updates that should use auto-commit mode.
Definition: AutoCommitUpdate.php:9
MergeableUpdate
Interface that deferrable updates can implement to signal that updates can be combined.
Definition: MergeableUpdate.php:21
MediaWiki\User\UserIdentity\getId
getId( $wikiId=self::LOCAL)
UserEditCountUpdate
Handles increment the edit count for a given set of users.
Definition: UserEditCountUpdate.php:30
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
UserEditCountUpdate\merge
merge(MergeableUpdate $update)
Merge this enqueued update with a new MergeableUpdate of the same qualified class name.
Definition: UserEditCountUpdate.php:51
UserEditCountUpdate\__construct
__construct(UserIdentity $user, $increment)
Definition: UserEditCountUpdate.php:42
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
UserEditCountUpdate\doUpdate
doUpdate()
Commits the provided user edit count increments to the database.
Definition: UserEditCountUpdate.php:72
UserEditCountUpdate\$infoByUser
array[] $infoByUser
We need to keep a single copy of the relevant UserIdentity to be able to pass to UserEditTracker.
Definition: UserEditCountUpdate.php:36
DeferrableUpdate
Interface that deferrable updates should implement.
Definition: DeferrableUpdate.php:11