MediaWiki master
UserEditCountUpdate.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Deferred;
10
14use RuntimeException;
15use Wikimedia\Assert\Assert;
17
27 private $infoByUser;
28
33 public function __construct( UserIdentity $user, $increment ) {
34 if ( !$user->getId() ) {
35 throw new RuntimeException( "Got anonymous user" );
36 }
37 $this->infoByUser = [
38 $user->getId() => new UserEditCountInfo( $user, $increment ),
39 ];
40 }
41
42 public function merge( MergeableUpdate $update ) {
44 Assert::parameterType( __CLASS__, $update, '$update' );
45 '@phan-var UserEditCountUpdate $update';
46
47 foreach ( $update->infoByUser as $userId => $info ) {
48 if ( !isset( $this->infoByUser[$userId] ) ) {
49 $this->infoByUser[$userId] = $info;
50 } else {
51 $this->infoByUser[$userId]->merge( $info );
52 }
53 }
54 }
55
59 public function doUpdate() {
60 $mwServices = MediaWikiServices::getInstance();
61 $lb = $mwServices->getDBLoadBalancer();
62 $dbw = $lb->getConnection( DB_PRIMARY );
63 $editTracker = $mwServices->getUserEditTracker();
64 $fname = __METHOD__;
65
66 ( new AutoCommitUpdate( $dbw, __METHOD__, function () use ( $lb, $dbw, $fname, $editTracker ) {
67 foreach ( $this->infoByUser as $userId => $info ) {
68 $dbw->newUpdateQueryBuilder()
69 ->update( 'user' )
70 ->set( [
71 'user_editcount' => new RawSQLValue(
72 'user_editcount+' . (int)$info->getIncrement()
73 )
74 ] )
75 ->where( [ 'user_id' => $userId, $dbw->expr( 'user_editcount', '!=', null ) ] )
76 ->caller( $fname )->execute();
77 // Lazy initialization check...
78 if ( $dbw->affectedRows() == 0 ) {
79 // The user_editcount is probably NULL (e.g. not initialized).
80 // Since this update runs after the new revisions were committed,
81 // wait for the replica DB to catch up so they will be counted.
82 $dbr = $lb->getConnection( DB_REPLICA );
83 // If $dbr is actually the primary DB, then clearing the snapshot
84 // is harmless and waitForPrimaryPos() will just no-op.
85 $dbr->flushSnapshot( $fname );
86 $lb->waitForPrimaryPos( $dbr );
87 $editTracker->initializeUserEditCount( $info->getUser() );
88 }
89
90 // Clear the edit count in the UserEditTracker cache.
91 $editTracker->clearUserEditCache( $info->getUser() );
92 }
93 } ) )->doUpdate();
94
95 $hookRunner = new HookRunner( $mwServices->getHookContainer() );
96 $hookRunner->onUserEditCountUpdate( array_values( $this->infoByUser ) );
97 }
98}
99
101class_alias( UserEditCountUpdate::class, 'UserEditCountUpdate' );
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28
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.
Raw SQL value to be used in query builders.
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)