]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/initEditCount.php
MediaWiki 1.17.1-scripts
[autoinstalls/mediawiki.git] / maintenance / initEditCount.php
1 <?php
2 /**
3  * Init the user_editcount database field based on the number of rows in the
4  * revision table.
5  *
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.
10  *
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.
15  *
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
20  *
21  * @file
22  * @ingroup Maintenance
23  */
24
25 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
26
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.
34
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 the load balancer, usually indicating a replication environment.' );
38                 $this->mDescription = "Batch-recalculate user_editcount fields from the revision table";
39         }
40
41         public function execute() {
42                 $dbw = wfGetDB( DB_MASTER );
43                 $user = $dbw->tableName( 'user' );
44                 $revision = $dbw->tableName( 'revision' );
45
46                 $dbver = $dbw->getServerVersion();
47
48                 // Autodetect mode...
49                 $backgroundMode = wfGetLB()->getServerCount() > 1 ||
50                         ( $dbw instanceof DatabaseMysql && version_compare( $dbver, '4.1' ) < 0 );
51
52                 if ( $this->hasOption( 'background' ) ) {
53                         $backgroundMode = true;
54                 } elseif ( $this->hasOption( 'quick' ) ) {
55                         $backgroundMode = false;
56                 }
57
58                 if ( $backgroundMode ) {
59                         $this->output( "Using replication-friendly background mode...\n" );
60
61                         $dbr = wfGetDB( DB_SLAVE );
62                         $chunkSize = 100;
63                         $lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __METHOD__ );
64
65                         $start = microtime( true );
66                         $migrated = 0;
67                         for ( $min = 0; $min <= $lastUser; $min += $chunkSize ) {
68                                 $max = $min + $chunkSize;
69                                 $result = $dbr->query(
70                                         "SELECT
71                                                 user_id,
72                                                 COUNT(rev_user) AS user_editcount
73                                         FROM $user
74                                         LEFT OUTER JOIN $revision ON user_id=rev_user
75                                         WHERE user_id > $min AND user_id <= $max
76                                         GROUP BY user_id",
77                                         __METHOD__ );
78
79                                 foreach ( $result as $row ) {
80                                         $dbw->update( 'user',
81                                                 array( 'user_editcount' => $row->user_editcount ),
82                                                 array( 'user_id' => $row->user_id ),
83                                                 __METHOD__ );
84                                         ++$migrated;
85                                 }
86
87                                 $delta = microtime( true ) - $start;
88                                 $rate = ( $delta == 0.0 ) ? 0.0 : $migrated / $delta;
89                                 $this->output( sprintf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n",
90                                         wfWikiID(),
91                                         $migrated,
92                                         min( $max, $lastUser ) / $lastUser * 100.0,
93                                         $delta,
94                                         $rate ) );
95
96                                 wfWaitForSlaves( 10 );
97                         }
98                 } else {
99                         // Subselect should work on modern MySQLs etc
100                         $this->output( "Using single-query mode...\n" );
101                         $sql = "UPDATE $user SET user_editcount=(SELECT COUNT(*) FROM $revision WHERE rev_user=user_id)";
102                         $dbw->query( $sql );
103                 }
104
105                 $this->output( "Done!\n" );
106         }
107 }
108
109 $maintClass = "InitEditCount";
110 require_once( RUN_MAINTENANCE_IF_MAIN );