]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/initEditCount.php
MediaWiki 1.16.0
[autoinstallsdev/mediawiki.git] / maintenance / initEditCount.php
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.
7  *
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.
12  *
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
17  *
18  * @ingroup Maintenance
19  */
20
21 require_once( dirname(__FILE__) . '/Maintenance.php' );
22
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.
30
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";
35         }
36
37         public function execute() {
38                 global $wgDBservers;
39                 $dbw = wfGetDB( DB_MASTER );
40                 $user = $dbw->tableName( 'user' );
41                 $revision = $dbw->tableName( 'revision' );
42
43                 $dbver = $dbw->getServerVersion();
44
45                 // Autodetect mode...
46                 $backgroundMode = count( $wgDBservers ) > 1 ||
47                         ($dbw instanceof DatabaseMySql && version_compare( $dbver, '4.1' ) < 0);
48         
49                 if( $this->hasOption('background') ) {
50                         $backgroundMode = true;
51                 } elseif( $this->hasOption('quick') ) {
52                         $backgroundMode = false;
53                 }
54
55                 if( $backgroundMode ) {
56                         $this->output( "Using replication-friendly background mode...\n" );
57
58                         $dbr = wfGetDB( DB_SLAVE );
59                         $chunkSize = 100;
60                         $lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __METHOD__ );
61
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__ );
75
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;
82                                 }
83                                 $dbr->freeResult( $result );
84
85                                 $delta = microtime( true ) - $start;
86                                 $rate = ($delta == 0.0) ? 0.0 : $migrated / $delta;
87                                 $this->output( sprintf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n",
88                                         wfWikiID(),
89                                         $migrated,
90                                         min( $max, $lastUser ) / $lastUser * 100.0,
91                                         $delta,
92                                         $rate ) );
93
94                                 wfWaitForSlaves( 10 );
95                         }
96                 } else {
97                         // Subselect should work on modern MySQLs etc
98                         $this->output( "Using single-query mode...\n" );
99                         $sql = "UPDATE $user SET user_editcount=(SELECT COUNT(*) FROM $revision WHERE rev_user=user_id)";
100                         $dbw->query( $sql );
101                 }
102
103                 $this->output( "Done!\n" );
104         }
105 }
106
107 $maintClass = "InitEditCount";
108 require_once( DO_MAINTENANCE );