Per r40414: Add new CSS files to message files.
[lhc/web/wiklou.git] / maintenance / initEditCount.php
1 <?php
2 /**
3 * @file
4 * @ingroup Maintenance
5 */
6
7 require_once "commandLine.inc";
8
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.
16
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.
20
21 ");
22 }
23 $dbw = wfGetDB( DB_MASTER );
24 $user = $dbw->tableName( 'user' );
25 $revision = $dbw->tableName( 'revision' );
26
27 $dbver = $dbw->getServerVersion();
28
29 // Autodetect mode...
30 $backgroundMode = count( $wgDBservers ) > 1 ||
31 ($dbw instanceof DatabaseMySql && version_compare( $dbver, '4.1' ) < 0);
32
33 if( isset( $options['background'] ) ) {
34 $backgroundMode = true;
35 } elseif( isset( $options['quick'] ) ) {
36 $backgroundMode = false;
37 }
38
39 if( $backgroundMode ) {
40 echo "Using replication-friendly background mode...\n";
41
42 $dbr = wfGetDB( DB_SLAVE );
43 $chunkSize = 100;
44 $lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __FUNCTION__ );
45
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__ );
59
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;
66 }
67 $dbr->freeResult( $result );
68
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 );
77
78 wfWaitForSlaves( 10 );
79 }
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 );
85 }
86
87 echo "Done!\n";
88
89