Merge "Follow-up 9c9cfa2ec3d7: fix non-session entry point error"
[lhc/web/wiklou.git] / includes / deferred / UserEditCountUpdate.php
index 5194e4f..ed7e00c 100644 (file)
@@ -68,35 +68,35 @@ class UserEditCountUpdate implements DeferrableUpdate, MergeableUpdate {
        public function doUpdate() {
                $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
                $dbw = $lb->getConnection( DB_MASTER );
+               $fname = __METHOD__;
 
-               ( new AutoCommitUpdate( $dbw, __METHOD__, function () use ( $lb, $dbw ) {
+               ( new AutoCommitUpdate( $dbw, __METHOD__, function () use ( $lb, $dbw, $fname ) {
                        foreach ( $this->infoByUser as $userId => $info ) {
                                $dbw->update(
                                        'user',
                                        [ 'user_editcount=user_editcount+' . (int)$info['increment'] ],
                                        [ 'user_id' => $userId, 'user_editcount IS NOT NULL' ],
-                                       __METHOD__
+                                       $fname
                                );
                                /** @var User[] $affectedInstances */
                                $affectedInstances = $info['instances'];
                                // Lazy initialization check...
                                if ( $dbw->affectedRows() == 0 ) {
-                                       // No rows will be "affected" if user_editcount is NULL.
-                                       // Check if the generic "replica" connection is not the master.
+                                       // The user_editcount is probably NULL (e.g. not initialized).
+                                       // Since this update runs after the new revisions were committed,
+                                       // wait for the replica DB to catch up so they will be counted.
                                        $dbr = $lb->getConnection( DB_REPLICA );
-                                       if ( $dbr !== $dbw ) {
-                                               // This method runs after the new revisions were committed.
-                                               // Wait for the replica to catch up so they will all be counted.
-                                               $dbr->flushSnapshot( __METHOD__ );
-                                               $lb->safeWaitForMasterPos( $dbr );
-                                       }
-                                       $affectedInstances[0]->initEditCountInternal();
+                                       // If $dbr is actually the master DB, then clearing the snapshot is
+                                       // is harmless and waitForMasterPos() will just no-op.
+                                       $dbr->flushSnapshot( $fname );
+                                       $lb->waitForMasterPos( $dbr );
+                                       $affectedInstances[0]->initEditCountInternal( $dbr );
                                }
                                $newCount = (int)$dbw->selectField(
                                        'user',
-                                       [ 'user_editcount' ],
+                                       'user_editcount',
                                        [ 'user_id' => $userId ],
-                                       __METHOD__
+                                       $fname
                                );
 
                                // Update the edit count in the instance caches. This is mostly useful