MediaWiki  master
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 avoids'
34  . 'locking tables or lagging replica DBs with large updates; calculates counts on a replica DB'
35  . 'if possible. Background mode will be automatically used if multiple servers are listed in the'
36  . 'load balancer, usually indicating a replication environment.' );
37  $this->addDescription( 'Batch-recalculate user_editcount fields from the revision table' );
38  }
39 
40  public function execute() {
41  $dbw = $this->getDB( DB_MASTER );
42 
43  // Autodetect mode...
44  if ( $this->hasOption( 'background' ) ) {
45  $backgroundMode = true;
46  } elseif ( $this->hasOption( 'quick' ) ) {
47  $backgroundMode = false;
48  } else {
49  $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
50  $backgroundMode = $lb->getServerCount() > 1;
51  }
52 
53  $actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' );
54 
55  if ( $backgroundMode ) {
56  $this->output( "Using replication-friendly background mode...\n" );
57 
58  $dbr = $this->getDB( DB_REPLICA );
59  $chunkSize = 100;
60  $lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __METHOD__ );
61 
62  $start = microtime( true );
63  $migrated = 0;
64  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
65  for ( $min = 0; $min <= $lastUser; $min += $chunkSize ) {
66  $max = $min + $chunkSize;
67 
68  $revUser = $actorQuery['fields']['rev_user'];
69  $result = $dbr->select(
70  [ 'user', 'rev' => [ 'revision' ] + $actorQuery['tables'] ],
71  [ 'user_id', 'user_editcount' => "COUNT($revUser)" ],
72  "user_id > $min AND user_id <= $max",
73  __METHOD__,
74  [ 'GROUP BY' => 'user_id' ],
75  [ 'rev' => [ 'LEFT JOIN', "user_id = $revUser" ] ] + $actorQuery['joins']
76  );
77 
78  foreach ( $result as $row ) {
79  $dbw->update( 'user',
80  [ 'user_editcount' => $row->user_editcount ],
81  [ 'user_id' => $row->user_id ],
82  __METHOD__ );
83  ++$migrated;
84  }
85 
86  $delta = microtime( true ) - $start;
87  $rate = ( $delta == 0.0 ) ? 0.0 : $migrated / $delta;
88  $this->output( sprintf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n",
90  $migrated,
91  min( $max, $lastUser ) / $lastUser * 100.0,
92  $delta,
93  $rate ) );
94 
95  $lbFactory->waitForReplication();
96  }
97  } else {
98  $this->output( "Using single-query mode...\n" );
99 
100  $user = $dbw->tableName( 'user' );
101  $subquery = $dbw->selectSQLText(
102  [ 'revision' ] + $actorQuery['tables'],
103  [ 'COUNT(*)' ],
104  [ 'user_id = ' . $actorQuery['fields']['rev_user'] ],
105  __METHOD__,
106  [],
107  $actorQuery['joins']
108  );
109  $dbw->query( "UPDATE $user SET user_editcount=($subquery)", __METHOD__ );
110  }
111 
112  $this->output( "Done!\n" );
113  }
114 }
115 
116 $maintClass = InitEditCount::class;
117 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:323
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:53
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:139
$dbr
$dbr
Definition: testCompression.php:54
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:241
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:1350
InitEditCount\__construct
__construct()
Default constructor.
Definition: initEditCount.php:30
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:429
InitEditCount\execute
execute()
Do the actual work.
Definition: initEditCount.php:40
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:262
$maintClass
$maintClass
Definition: initEditCount.php:116