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