Debian Codegraph @

includes/deferred/UserEditCountUpdate.php

raw symbols SCIP
1 <?php
2 /**
3 * User edit count incrementing.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Deferred;
24
25 use MediaWiki\HookContainer\HookRunner;
26 use MediaWiki\MediaWikiServices;
27 use MediaWiki\User\UserIdentity;
28 use RuntimeException;
29 use Wikimedia\Assert\Assert;
30 use Wikimedia\Rdbms\RawSQLValue;
31
32 /**
33 * Handles increment the edit count for a given set of users
34 */
35 class UserEditCountUpdate implements DeferrableUpdate, MergeableUpdate {
36 /**
37 * We need to keep a single copy of the relevant UserIdentity to be able to pass to UserEditTracker
38 *
39 * @var UserEditCountInfo[] Map of (user ID => UserEditCountInfo)
40 */
41 private $infoByUser;
42
43 /**
44 * @param UserIdentity $user
45 * @param int $increment
46 */
47 public function __construct( UserIdentity $user, $increment ) {
48 if ( !$user->getId() ) {
49 throw new RuntimeException( "Got anonymous user" );
50 }
51 $this->infoByUser = [
52 $user->getId() => new UserEditCountInfo( $user, $increment ),
53 ];
54 }
55
56 public function merge( MergeableUpdate $update ) {
57 /** @var UserEditCountUpdate $update */
58 Assert::parameterType( __CLASS__, $update, '$update' );
59 '@phan-var UserEditCountUpdate $update';
60
61 foreach ( $update->infoByUser as $userId => $info ) {
62 if ( !isset( $this->infoByUser[$userId] ) ) {
63 $this->infoByUser[$userId] = $info;
64 } else {
65 $this->infoByUser[$userId]->merge( $info );
66 }
67 }
68 }
69
70 /**
71 * Commits the provided user edit count increments to the database
72 */
73 public function doUpdate() {
74 $mwServices = MediaWikiServices::getInstance();
75 $lb = $mwServices->getDBLoadBalancer();
76 $dbw = $lb->getConnection( DB_PRIMARY );
77 $editTracker = $mwServices->getUserEditTracker();
78 $fname = __METHOD__;
79
80 ( new AutoCommitUpdate( $dbw, __METHOD__, function () use ( $lb, $dbw, $fname, $editTracker ) {
81 foreach ( $this->infoByUser as $userId => $info ) {
82 $dbw->newUpdateQueryBuilder()
83 ->update( 'user' )
84 ->set( [
85 'user_editcount' => new RawSQLValue(
86 'user_editcount+' . (int)$info->getIncrement()
87 )
88 ] )
89 ->where( [ 'user_id' => $userId, $dbw->expr( 'user_editcount', '!=', null ) ] )
90 ->caller( $fname )->execute();
91 // Lazy initialization check...
92 if ( $dbw->affectedRows() == 0 ) {
93 // The user_editcount is probably NULL (e.g. not initialized).
94 // Since this update runs after the new revisions were committed,
95 // wait for the replica DB to catch up so they will be counted.
96 $dbr = $lb->getConnection( DB_REPLICA );
97 // If $dbr is actually the primary DB, then clearing the snapshot
98 // is harmless and waitForPrimaryPos() will just no-op.
99 $dbr->flushSnapshot( $fname );
100 $lb->waitForPrimaryPos( $dbr );
101 $editTracker->initializeUserEditCount( $info->getUser() );
102 }
103
104 // Clear the edit count in the UserEditTracker cache.
105 $editTracker->clearUserEditCache( $info->getUser() );
106 }
107 } ) )->doUpdate();
108
109 $hookRunner = new HookRunner( $mwServices->getHookContainer() );
110 $hookRunner->onUserEditCountUpdate( array_values( $this->infoByUser ) );
111 }
112 }
113
114 /** @deprecated class alias since 1.42 */
115 class_alias( UserEditCountUpdate::class, 'UserEditCountUpdate' );