3 require_once "commandLine.inc";
5 if( isset( $options['help'] ) ) {
6 die( "Batch-recalculate user_editcount fields from the revision table.
8 --quick Force the update to be done in a single query.
9 --background Force replication-friendly mode; may be inefficient but
10 avoids locking tables or lagging slaves with large updates;
11 calculates counts on a slave if possible.
13 Background mode will be automatically used if the server is MySQL 4.0
14 (which does not support subqueries) or if multiple servers are listed
15 in \$wgDBservers, usually indicating a replication environment.
19 $dbw = wfGetDB( DB_MASTER
);
20 $user = $dbw->tableName( 'user' );
21 $revision = $dbw->tableName( 'revision' );
23 $dbver = $dbw->getServerVersion();
26 $backgroundMode = count( $wgDBservers ) > 1 ||
27 ($dbw instanceof DatabaseMySql
&& version_compare( $dbver, '4.1' ) < 0);
29 if( isset( $options['background'] ) ) {
30 $backgroundMode = true;
31 } elseif( isset( $options['quick'] ) ) {
32 $backgroundMode = false;
35 if( $backgroundMode ) {
36 echo "Using replication-friendly background mode...\n";
38 $dbr = wfGetDB( DB_SLAVE
);
40 $lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __FUNCTION__
);
42 $start = microtime( true );
44 for( $min = 0; $min <= $lastUser; $min +
= $chunkSize ) {
45 $max = $min +
$chunkSize;
46 $result = $dbr->query(
49 COUNT(rev_user) AS user_editcount
51 LEFT OUTER JOIN $revision ON user_id=rev_user
52 WHERE user_id > $min AND user_id <= $max
56 while( $row = $dbr->fetchObject( $result ) ) {
58 array( 'user_editcount' => $row->user_editcount
),
59 array( 'user_id' => $row->user_id
),
63 $dbr->freeResult( $result );
65 $delta = microtime( true ) - $start;
66 $rate = ($delta == 0.0) ?
0.0 : $migrated / $delta;
67 printf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n",
70 min( $max, $lastUser ) / $lastUser * 100.0,
74 wfWaitForSlaves( 10 );
77 // Subselect should work on modern MySQLs etc
78 echo "Using single-query mode...\n";
79 $sql = "UPDATE $user SET user_editcount=(SELECT COUNT(*) FROM $revision WHERE rev_user=user_id)";