3 * Init the user_editcount database field based on the number of rows in the
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Maintenance
25 require_once( dirname( __FILE__
) . '/Maintenance.php' );
27 class InitEditCount
extends Maintenance
{
28 public function __construct() {
29 parent
::__construct();
30 $this->addOption( 'quick', 'Force the update to be done in a single query' );
31 $this->addOption( 'background', 'Force replication-friendly mode; may be inefficient but
32 avoids locking tables or lagging slaves with large updates;
33 calculates counts on a slave if possible.
35 Background mode will be automatically used if the server is MySQL 4.0
36 (which does not support subqueries) or if multiple servers are listed
37 in $wgDBservers, usually indicating a replication environment.' );
38 $this->mDescription
= "Batch-recalculate user_editcount fields from the revision table";
41 public function execute() {
43 $dbw = wfGetDB( DB_MASTER
);
44 $user = $dbw->tableName( 'user' );
45 $revision = $dbw->tableName( 'revision' );
47 $dbver = $dbw->getServerVersion();
50 $backgroundMode = count( $wgDBservers ) > 1 ||
51 ( $dbw instanceof DatabaseMysql
&& version_compare( $dbver, '4.1' ) < 0 );
53 if ( $this->hasOption( 'background' ) ) {
54 $backgroundMode = true;
55 } elseif ( $this->hasOption( 'quick' ) ) {
56 $backgroundMode = false;
59 if ( $backgroundMode ) {
60 $this->output( "Using replication-friendly background mode...\n" );
62 $dbr = wfGetDB( DB_SLAVE
);
64 $lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __METHOD__
);
66 $start = microtime( true );
68 for ( $min = 0; $min <= $lastUser; $min +
= $chunkSize ) {
69 $max = $min +
$chunkSize;
70 $result = $dbr->query(
73 COUNT(rev_user) AS user_editcount
75 LEFT OUTER JOIN $revision ON user_id=rev_user
76 WHERE user_id > $min AND user_id <= $max
80 foreach ( $result as $row ) {
82 array( 'user_editcount' => $row->user_editcount
),
83 array( 'user_id' => $row->user_id
),
88 $delta = microtime( true ) - $start;
89 $rate = ( $delta == 0.0 ) ?
0.0 : $migrated / $delta;
90 $this->output( sprintf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n",
93 min( $max, $lastUser ) / $lastUser * 100.0,
97 wfWaitForSlaves( 10 );
100 // Subselect should work on modern MySQLs etc
101 $this->output( "Using single-query mode...\n" );
102 $sql = "UPDATE $user SET user_editcount=(SELECT COUNT(*) FROM $revision WHERE rev_user=user_id)";
106 $this->output( "Done!\n" );
110 $maintClass = "InitEditCount";
111 require_once( DO_MAINTENANCE
);