MediaWiki  master
UserEditCountUpdate.php
Go to the documentation of this file.
1 <?php
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->waitForMasterPos( $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 }
doUpdate()
Purges the list of URLs passed to the constructor.
array [] $infoByUser
Map of (user ID => (&#39;increment&#39;: int, &#39;instances&#39;: User[]))
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Handles increment the edit count for a given set of users.
Interface that deferrable updates can implement to signal that updates can be combined.
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
merge(MergeableUpdate $update)
Merge this update with $update.
const DB_MASTER
Definition: defines.php:26
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
__construct(User $user, $increment)
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
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings, LocalSettings).
Definition: Setup.php:123
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
getId()
Get the user&#39;s ID.
Definition: User.php:2277
const DB_REPLICA
Definition: defines.php:25
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1473