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