Special:Version now displays whether a SQLite database supports full-text search
[mediawiki.git] / maintenance / initEditCount.php
blob3ca067cb82f6c8a6889187dd27ecd2bc49307c0e
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @ingroup Maintenance
21 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
23 class InitEditCount extends Maintenance {
24 public function __construct() {
25 parent::__construct();
26 $this->addOption( 'quick', 'Force the update to be done in a single query' );
27 $this->addOption( 'background', 'Force replication-friendly mode; may be inefficient but
28 avoids locking tables or lagging slaves with large updates;
29 calculates counts on a slave if possible.
31 Background mode will be automatically used if the server is MySQL 4.0
32 (which does not support subqueries) or if multiple servers are listed
33 in $wgDBservers, usually indicating a replication environment.' );
34 $this->mDescription = "Batch-recalculate user_editcount fields from the revision table";
37 public function execute() {
38 global $wgDBservers;
39 $dbw = wfGetDB( DB_MASTER );
40 $user = $dbw->tableName( 'user' );
41 $revision = $dbw->tableName( 'revision' );
43 $dbver = $dbw->getServerVersion();
45 // Autodetect mode...
46 $backgroundMode = count( $wgDBservers ) > 1 ||
47 ( $dbw instanceof DatabaseMysql && version_compare( $dbver, '4.1' ) < 0 );
49 if ( $this->hasOption( 'background' ) ) {
50 $backgroundMode = true;
51 } elseif ( $this->hasOption( 'quick' ) ) {
52 $backgroundMode = false;
55 if ( $backgroundMode ) {
56 $this->output( "Using replication-friendly background mode...\n" );
58 $dbr = wfGetDB( DB_SLAVE );
59 $chunkSize = 100;
60 $lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __METHOD__ );
62 $start = microtime( true );
63 $migrated = 0;
64 for ( $min = 0; $min <= $lastUser; $min += $chunkSize ) {
65 $max = $min + $chunkSize;
66 $result = $dbr->query(
67 "SELECT
68 user_id,
69 COUNT(rev_user) AS user_editcount
70 FROM $user
71 LEFT OUTER JOIN $revision ON user_id=rev_user
72 WHERE user_id > $min AND user_id <= $max
73 GROUP BY user_id",
74 __METHOD__ );
76 foreach ( $result as $row ) {
77 $dbw->update( 'user',
78 array( 'user_editcount' => $row->user_editcount ),
79 array( 'user_id' => $row->user_id ),
80 __METHOD__ );
81 ++$migrated;
84 $delta = microtime( true ) - $start;
85 $rate = ( $delta == 0.0 ) ? 0.0 : $migrated / $delta;
86 $this->output( sprintf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n",
87 wfWikiID(),
88 $migrated,
89 min( $max, $lastUser ) / $lastUser * 100.0,
90 $delta,
91 $rate ) );
93 wfWaitForSlaves( 10 );
95 } else {
96 // Subselect should work on modern MySQLs etc
97 $this->output( "Using single-query mode...\n" );
98 $sql = "UPDATE $user SET user_editcount=(SELECT COUNT(*) FROM $revision WHERE rev_user=user_id)";
99 $dbw->query( $sql );
102 $this->output( "Done!\n" );
106 $maintClass = "InitEditCount";
107 require_once( DO_MAINTENANCE );