Language object cache, for faster wfMsg() performance with unusual languages, and...
[mediawiki.git] / maintenance / initEditCount.php
blobd26349bbe40c85bfbe059f9c5738503332f9492c
1 <?php
2 /**
3 * @file
4 * @ingroup Maintenance
5 */
7 require_once "commandLine.inc";
9 if( isset( $options['help'] ) ) {
10 die( "Batch-recalculate user_editcount fields from the revision table.
11 Options:
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.
21 ");
23 $dbw = wfGetDB( DB_MASTER );
24 $user = $dbw->tableName( 'user' );
25 $revision = $dbw->tableName( 'revision' );
27 $dbver = $dbw->getServerVersion();
29 // Autodetect mode...
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 );
43 $chunkSize = 100;
44 $lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __FUNCTION__ );
46 $start = microtime( true );
47 $migrated = 0;
48 for( $min = 0; $min <= $lastUser; $min += $chunkSize ) {
49 $max = $min + $chunkSize;
50 $result = $dbr->query(
51 "SELECT
52 user_id,
53 COUNT(rev_user) AS user_editcount
54 FROM $user
55 LEFT OUTER JOIN $revision ON user_id=rev_user
56 WHERE user_id > $min AND user_id <= $max
57 GROUP BY user_id",
58 __FUNCTION__ );
60 while( $row = $dbr->fetchObject( $result ) ) {
61 $dbw->update( 'user',
62 array( 'user_editcount' => $row->user_editcount ),
63 array( 'user_id' => $row->user_id ),
64 __FUNCTION__ );
65 ++$migrated;
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",
72 $wgDBname,
73 $migrated,
74 min( $max, $lastUser ) / $lastUser * 100.0,
75 $delta,
76 $rate );
78 wfWaitForSlaves( 10 );
80 } else {
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)";
84 $dbw->query( $sql );
87 echo "Done!\n";