Merge "docs: Fix typo"
[mediawiki.git] / maintenance / initEditCount.php
blob06a793cbdaac5773abaa1d04fd336876343e212c
1 <?php
2 /**
3 * Init the user_editcount database field based on the number of rows in the
4 * revision table.
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
21 * @file
22 * @ingroup Maintenance
25 // @codeCoverageIgnoreStart
26 require_once __DIR__ . '/Maintenance.php';
27 // @codeCoverageIgnoreEnd
29 use MediaWiki\Maintenance\Maintenance;
30 use MediaWiki\WikiMap\WikiMap;
31 use Wikimedia\Rdbms\IDatabase;
32 use Wikimedia\Rdbms\RawSQLValue;
34 class InitEditCount extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addOption( 'quick', 'Force the update to be done in a single query' );
38 $this->addOption( 'background', 'Force replication-friendly mode; may be inefficient but avoids'
39 . 'locking tables or lagging replica DBs with large updates; calculates counts on a replica DB'
40 . 'if possible. Background mode will be automatically used if multiple servers are listed in the'
41 . 'load balancer, usually indicating a replication environment.' );
42 $this->addDescription( 'Batch-recalculate user_editcount fields from the revision table' );
45 public function execute() {
46 $dbw = $this->getPrimaryDB();
48 // Autodetect mode...
49 if ( $this->hasOption( 'background' ) ) {
50 $backgroundMode = true;
51 } elseif ( $this->hasOption( 'quick' ) ) {
52 $backgroundMode = false;
53 } else {
54 $lb = $this->getServiceContainer()->getDBLoadBalancer();
55 $backgroundMode = $lb->hasReplicaServers();
58 if ( $backgroundMode ) {
59 $this->output( "Using replication-friendly background mode...\n" );
61 $dbr = $this->getReplicaDB();
62 $chunkSize = 100;
63 $lastUser = $dbr->newSelectQueryBuilder()
64 ->select( 'MAX(user_id)' )
65 ->from( 'user' )
66 ->caller( __METHOD__ )->fetchField();
68 $start = microtime( true );
69 $migrated = 0;
70 for ( $min = 0; $min <= $lastUser; $min += $chunkSize ) {
71 $max = $min + $chunkSize;
73 $result = $dbr->newSelectQueryBuilder()
74 ->select( [ 'user_id', 'user_editcount' => "COUNT(actor_rev_user.actor_user)" ] )
75 ->from( 'user' )
76 ->leftJoin( 'revision', 'rev', "user_id = actor_rev_user.actor_user" )
77 ->join( 'actor', 'actor_rev_user', 'actor_rev_user.actor_id = rev_actor' )
78 ->where( $dbr->expr( 'user_id', '>', $min )->and( 'user_id', '<=', $max ) )
79 ->groupBy( 'user_id' )
80 ->caller( __METHOD__ )->fetchResultSet();
82 foreach ( $result as $row ) {
83 $dbw->newUpdateQueryBuilder()
84 ->update( 'user' )
85 ->set( [ 'user_editcount' => $row->user_editcount ] )
86 ->where( [ 'user_id' => $row->user_id ] )
87 ->caller( __METHOD__ )->execute();
88 ++$migrated;
91 $delta = microtime( true ) - $start;
92 $rate = ( $delta == 0.0 ) ? 0.0 : $migrated / $delta;
93 $this->output( sprintf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n",
94 WikiMap::getCurrentWikiDbDomain()->getId(),
95 $migrated,
96 min( $max, $lastUser ) / $lastUser * 100.0,
97 $delta,
98 $rate ) );
100 $this->waitForReplication();
102 } else {
103 $this->output( "Using single-query mode...\n" );
105 $subquery = $dbw->newSelectQueryBuilder()
106 ->select( 'COUNT(*)' )
107 ->from( 'revision' )
108 ->join( 'actor', 'actor_rev_user', 'actor_rev_user.actor_id = rev_actor' )
109 ->where( 'user_id = actor_rev_user.actor_user' )
110 ->caller( __METHOD__ )->getSQL();
112 $dbw->newUpdateQueryBuilder()
113 ->table( 'user' )
114 ->set( [ 'user_editcount' => new RawSQLValue( "($subquery)" ) ] )
115 ->where( IDatabase::ALL_ROWS )
116 ->caller( __METHOD__ )
117 ->execute();
120 $this->output( "Done!\n" );
124 // @codeCoverageIgnoreStart
125 $maintClass = InitEditCount::class;
126 require_once RUN_MAINTENANCE_IF_MAIN;
127 // @codeCoverageIgnoreEnd