Debian Codegraph @

maintenance/initEditCount.php

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