MediaWiki REL1_39
UserEditCountUpdate.php
Go to the documentation of this file.
1<?php
26use Wikimedia\Assert\Assert;
27
37 private $infoByUser;
38
43 public function __construct( UserIdentity $user, $increment ) {
44 if ( !$user->isRegistered() ) {
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->update(
79 'user',
80 [ 'user_editcount=user_editcount+' . (int)$info->getIncrement() ],
81 [ 'user_id' => $userId, 'user_editcount IS NOT NULL' ],
82 $fname
83 );
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 primary DB, then clearing the snapshot
91 // is harmless and waitForPrimaryPos() will just no-op.
92 $dbr->flushSnapshot( $fname );
93 $lb->waitForPrimaryPos( $dbr );
94 $editTracker->initializeUserEditCount( $info->getUser() );
95 }
96
97 // Clear the edit count in the UserEditTracker cache.
98 $editTracker->clearUserEditCache( $info->getUser() );
99 }
100 } ) )->doUpdate();
101
102 $hookRunner = new HookRunner( $mwServices->getHookContainer() );
103 $hookRunner->onUserEditCountUpdate( array_values( $this->infoByUser ) );
104 }
105}
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...
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_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28