MediaWiki  1.34.0
initEditCount.php
Go to the documentation of this file.
1 <?php
25 require_once __DIR__ . '/Maintenance.php';
26 
28 
29 class InitEditCount extends Maintenance {
30  public function __construct() {
31  parent::__construct();
32  $this->addOption( 'quick', 'Force the update to be done in a single query' );
33  $this->addOption( 'background', 'Force replication-friendly mode; may be inefficient but
34  avoids locking tables or lagging replica DBs with large updates;
35  calculates counts on a replica DB if possible.
36 
37 Background mode will be automatically used if multiple servers are listed
38 in the load balancer, usually indicating a replication environment.' );
39  $this->addDescription( 'Batch-recalculate user_editcount fields from the revision table' );
40  }
41 
42  public function execute() {
43  $dbw = $this->getDB( DB_MASTER );
44 
45  // Autodetect mode...
46  if ( $this->hasOption( 'background' ) ) {
47  $backgroundMode = true;
48  } elseif ( $this->hasOption( 'quick' ) ) {
49  $backgroundMode = false;
50  } else {
51  $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
52  $backgroundMode = $lb->getServerCount() > 1;
53  }
54 
55  $actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' );
56 
57  if ( $backgroundMode ) {
58  $this->output( "Using replication-friendly background mode...\n" );
59 
60  $dbr = $this->getDB( DB_REPLICA );
61  $chunkSize = 100;
62  $lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __METHOD__ );
63 
64  $start = microtime( true );
65  $migrated = 0;
66  for ( $min = 0; $min <= $lastUser; $min += $chunkSize ) {
67  $max = $min + $chunkSize;
68 
69  $revUser = $actorQuery['fields']['rev_user'];
70  $result = $dbr->select(
71  [ 'user', 'rev' => [ 'revision' ] + $actorQuery['tables'] ],
72  [ 'user_id', 'user_editcount' => "COUNT($revUser)" ],
73  "user_id > $min AND user_id <= $max",
74  __METHOD__,
75  [ 'GROUP BY' => 'user_id' ],
76  [ 'rev' => [ 'LEFT JOIN', "user_id = $revUser" ] ] + $actorQuery['joins']
77  );
78 
79  foreach ( $result as $row ) {
80  $dbw->update( 'user',
81  [ 'user_editcount' => $row->user_editcount ],
82  [ 'user_id' => $row->user_id ],
83  __METHOD__ );
84  ++$migrated;
85  }
86 
87  $delta = microtime( true ) - $start;
88  $rate = ( $delta == 0.0 ) ? 0.0 : $migrated / $delta;
89  $this->output( sprintf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n",
91  $migrated,
92  min( $max, $lastUser ) / $lastUser * 100.0,
93  $delta,
94  $rate ) );
95 
97  }
98  } else {
99  $this->output( "Using single-query mode...\n" );
100 
101  $user = $dbw->tableName( 'user' );
102  $subquery = $dbw->selectSQLText(
103  [ 'revision' ] + $actorQuery['tables'],
104  [ 'COUNT(*)' ],
105  [ 'user_id = ' . $actorQuery['fields']['rev_user'] ],
106  __METHOD__,
107  [],
108  $actorQuery['joins']
109  );
110  $dbw->query( "UPDATE $user SET user_editcount=($subquery)", __METHOD__ );
111  }
112 
113  $this->output( "Done!\n" );
114  }
115 }
116 
117 $maintClass = InitEditCount::class;
118 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:292
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:2718
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:136
$dbr
$dbr
Definition: testCompression.php:50
InitEditCount
Definition: initEditCount.php:29
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
InitEditCount\__construct
__construct()
Default constructor.
Definition: initEditCount.php:30
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
InitEditCount\execute
execute()
Do the actual work.
Definition: initEditCount.php:42
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
$maintClass
$maintClass
Definition: initEditCount.php:117