MediaWiki  master
UserEditCountUpdate.php
Go to the documentation of this file.
1 <?php
26 use Wikimedia\Assert\Assert;
27 
37  private $infoByUser;
38 
43  public function __construct( UserIdentity $user, $increment ) {
44  if ( !$user->getId() ) {
45  throw new RuntimeException( "Got anonymous user" );
46  }
47  $this->infoByUser = [
48  $user->getId() => new UserEditCountInfo( $user, $increment ),
49  ];
50  }
51 
52  public function merge( MergeableUpdate $update ) {
54  Assert::parameterType( __CLASS__, $update, '$update' );
55  '@phan-var UserEditCountUpdate $update';
56 
57  foreach ( $update->infoByUser as $userId => $info ) {
58  if ( !isset( $this->infoByUser[$userId] ) ) {
59  $this->infoByUser[$userId] = $info;
60  } else {
61  $this->infoByUser[$userId]->merge( $info );
62  }
63  }
64  }
65 
69  public function doUpdate() {
70  $mwServices = MediaWikiServices::getInstance();
71  $lb = $mwServices->getDBLoadBalancer();
72  $dbw = $lb->getConnectionRef( DB_PRIMARY );
73  $editTracker = $mwServices->getUserEditTracker();
74  $fname = __METHOD__;
75 
76  ( new AutoCommitUpdate( $dbw, __METHOD__, function () use ( $lb, $dbw, $fname, $editTracker ) {
77  foreach ( $this->infoByUser as $userId => $info ) {
78  $dbw->newUpdateQueryBuilder()
79  ->update( 'user' )
80  ->set( [ 'user_editcount=user_editcount+' . (int)$info->getIncrement() ] )
81  ->where( [ 'user_id' => $userId, 'user_editcount IS NOT NULL' ] )
82  ->caller( $fname )->execute();
83  // Lazy initialization check...
84  if ( $dbw->affectedRows() == 0 ) {
85  // The user_editcount is probably NULL (e.g. not initialized).
86  // Since this update runs after the new revisions were committed,
87  // wait for the replica DB to catch up so they will be counted.
88  $dbr = $lb->getConnectionRef( DB_REPLICA );
89  // If $dbr is actually the primary DB, then clearing the snapshot
90  // is harmless and waitForPrimaryPos() will just no-op.
91  $dbr->flushSnapshot( $fname );
92  $lb->waitForPrimaryPos( $dbr );
93  $editTracker->initializeUserEditCount( $info->getUser() );
94  }
95 
96  // Clear the edit count in the UserEditTracker cache.
97  $editTracker->clearUserEditCache( $info->getUser() );
98  }
99  } ) )->doUpdate();
100 
101  $hookRunner = new HookRunner( $mwServices->getHookContainer() );
102  $hookRunner->onUserEditCountUpdate( array_values( $this->infoByUser ) );
103  }
104 }
if(!defined('MW_SETUP_CALLBACK'))
Definition: WebStart.php:88
Deferrable Update for closure/callback updates that should use auto-commit mode.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:569
Service locator for MediaWiki core services.
Helper class for UserEditCountUpdate.
Handles increment the edit count for a given set of users.
__construct(UserIdentity $user, $increment)
doUpdate()
Commits the provided user edit count increments to the database.
merge(MergeableUpdate $update)
Merge this enqueued update with a new MergeableUpdate of the same qualified class name.
Interface that deferrable updates should implement.
Interface for objects representing user identity.
getId( $wikiId=self::LOCAL)
Interface that deferrable updates can implement to signal that updates can be combined.
const DB_PRIMARY
Definition: defines.php:28