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