MediaWiki  1.33.0
UserEditCountUpdate.php
Go to the documentation of this file.
1 <?php
23 use Wikimedia\Assert\Assert;
25 
31  private $infoByUser;
32 
37  public function __construct( User $user, $increment ) {
38  if ( !$user->getId() ) {
39  throw new RuntimeException( "Got user ID of zero" );
40  }
41  $this->infoByUser = [
42  $user->getId() => [ 'increment' => $increment, 'instances' => [ $user ] ]
43  ];
44  }
45 
46  public function merge( MergeableUpdate $update ) {
48  Assert::parameterType( __CLASS__, $update, '$update' );
49 
50  foreach ( $update->infoByUser as $userId => $info ) {
51  if ( !isset( $this->infoByUser[$userId] ) ) {
52  $this->infoByUser[$userId] = [ 'increment' => 0, 'instances' => [] ];
53  }
54  // Merge the increment amount
55  $this->infoByUser[$userId]['increment'] += $info['increment'];
56  // Merge the list of User instances to update in doUpdate()
57  foreach ( $info['instances'] as $user ) {
58  if ( !in_array( $user, $this->infoByUser[$userId]['instances'], true ) ) {
59  $this->infoByUser[$userId]['instances'][] = $user;
60  }
61  }
62  }
63  }
64 
68  public function doUpdate() {
69  $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
70  $dbw = $lb->getConnection( DB_MASTER );
71  $fname = __METHOD__;
72 
73  ( new AutoCommitUpdate( $dbw, __METHOD__, function () use ( $lb, $dbw, $fname ) {
74  foreach ( $this->infoByUser as $userId => $info ) {
75  $dbw->update(
76  'user',
77  [ 'user_editcount=user_editcount+' . (int)$info['increment'] ],
78  [ 'user_id' => $userId, 'user_editcount IS NOT NULL' ],
79  $fname
80  );
82  $affectedInstances = $info['instances'];
83  // Lazy initialization check...
84  if ( $dbw->affectedRows() == 0 ) {
85  // No rows will be "affected" if user_editcount is NULL.
86  // Check if the generic "replica" connection is not the master.
87  $dbr = $lb->getConnection( DB_REPLICA );
88  if ( $dbr !== $dbw ) {
89  // This method runs after the new revisions were committed.
90  // Wait for the replica to catch up so they will all be counted.
91  $dbr->flushSnapshot( $fname );
92  $lb->safeWaitForMasterPos( $dbr );
93  }
94  $affectedInstances[0]->initEditCountInternal();
95  }
96  $newCount = (int)$dbw->selectField(
97  'user',
98  [ 'user_editcount' ],
99  [ 'user_id' => $userId ],
100  $fname
101  );
102 
103  // Update the edit count in the instance caches. This is mostly useful
104  // for maintenance scripts, where deferred updates might run immediately
105  // and user instances might be reused for a long time.
106  foreach ( $affectedInstances as $affectedInstance ) {
107  $affectedInstance->setEditCountInternal( $newCount );
108  }
109  // Clear the edit count in user cache too
110  $affectedInstances[0]->invalidateCache();
111  }
112  } ) )->doUpdate();
113  }
114 }
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
AutoCommitUpdate
Deferrable Update for closure/callback updates that should use auto-commit mode.
Definition: AutoCommitUpdate.php:9
UserEditCountUpdate\__construct
__construct(User $user, $increment)
Definition: UserEditCountUpdate.php:37
MergeableUpdate
Interface that deferrable updates can implement to signal that updates can be combined.
Definition: MergeableUpdate.php:18
UserEditCountUpdate
Handles increment the edit count for a given set of users.
Definition: UserEditCountUpdate.php:29
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$dbr
$dbr
Definition: testCompression.php:50
UserEditCountUpdate\merge
merge(MergeableUpdate $update)
Merge this update with $update.
Definition: UserEditCountUpdate.php:46
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:123
UserEditCountUpdate\doUpdate
doUpdate()
Purges the list of URLs passed to the constructor.
Definition: UserEditCountUpdate.php:68
UserEditCountUpdate\$infoByUser
array[] $infoByUser
Map of (user ID => ('increment': int, 'instances': User[]))
Definition: UserEditCountUpdate.php:31
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
DeferrableUpdate
Interface that deferrable updates should implement.
Definition: DeferrableUpdate.php:9
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48