25 require_once __DIR__ .
'/Maintenance.php';
33 parent::__construct();
34 $this->
addOption(
'quick',
'Force the update to be done in a single query' );
35 $this->
addOption(
'background',
'Force replication-friendly mode; may be inefficient but avoids'
36 .
'locking tables or lagging replica DBs with large updates; calculates counts on a replica DB'
37 .
'if possible. Background mode will be automatically used if multiple servers are listed in the'
38 .
'load balancer, usually indicating a replication environment.' );
39 $this->
addDescription(
'Batch-recalculate user_editcount fields from the revision table' );
47 $backgroundMode =
true;
48 } elseif ( $this->
hasOption(
'quick' ) ) {
49 $backgroundMode =
false;
51 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
52 $backgroundMode = $lb->hasReplicaServers();
55 $actorQuery = ActorMigration::newMigration()->getJoin(
'rev_user' );
57 if ( $backgroundMode ) {
58 $this->
output(
"Using replication-friendly background mode...\n" );
62 $lastUser =
$dbr->selectField(
'user',
'MAX(user_id)',
'', __METHOD__ );
64 $start = microtime(
true );
66 for ( $min = 0; $min <= $lastUser; $min += $chunkSize ) {
67 $max = $min + $chunkSize;
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",
75 [
'GROUP BY' =>
'user_id' ],
76 [
'rev' => [
'LEFT JOIN',
"user_id = $revUser" ] ] + $actorQuery[
'joins']
79 foreach ( $result as $row ) {
81 [
'user_editcount' => $row->user_editcount ],
82 [
'user_id' => $row->user_id ],
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",
90 WikiMap::getCurrentWikiDbDomain()->getId(),
92 min( $max, $lastUser ) / $lastUser * 100.0,
99 $this->
output(
"Using single-query mode...\n" );
101 $user = $dbw->tableName(
'user' );
102 $subquery = $dbw->selectSQLText(
103 [
'revision' ] + $actorQuery[
'tables'],
105 [
'user_id = ' . $actorQuery[
'fields'][
'rev_user'] ],
110 $dbw->query(
"UPDATE $user SET user_editcount=($subquery)", __METHOD__ );
113 $this->
output(
"Done!\n" );
118 require_once RUN_MAINTENANCE_IF_MAIN;
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.