Merge "Use {{int:}} on MediaWiki:Blockedtext and MediaWiki:Autoblockedtext"
[lhc/web/wiklou.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 __DIR__ . '/Maintenance.php';
26
27 use MediaWiki\MediaWikiServices;
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
34 avoids locking tables or lagging replica DBs with large updates;
35 calculates counts on a replica DB if possible.
36
37 Background mode will be automatically used if multiple servers are listed
38 in the load balancer, usually indicating a replication environment.' );
39 $this->addDescription( 'Batch-recalculate user_editcount fields from the revision table' );
40 }
41
42 public function execute() {
43 global $wgActorTableSchemaMigrationStage;
44
45 $dbw = $this->getDB( DB_MASTER );
46
47 // Autodetect mode...
48 if ( $this->hasOption( 'background' ) ) {
49 $backgroundMode = true;
50 } elseif ( $this->hasOption( 'quick' ) ) {
51 $backgroundMode = false;
52 } else {
53 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
54 $backgroundMode = $lb->getServerCount() > 1;
55 }
56
57 $actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' );
58
59 $needSpecialQuery = ( $wgActorTableSchemaMigrationStage !== MIGRATION_OLD &&
60 $wgActorTableSchemaMigrationStage !== MIGRATION_NEW );
61 if ( $needSpecialQuery ) {
62 foreach ( $actorQuery['joins'] as &$j ) {
63 $j[0] = 'JOIN'; // replace LEFT JOIN
64 }
65 unset( $j );
66 }
67
68 if ( $backgroundMode ) {
69 $this->output( "Using replication-friendly background mode...\n" );
70
71 $dbr = $this->getDB( DB_REPLICA );
72 $chunkSize = 100;
73 $lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __METHOD__ );
74
75 $start = microtime( true );
76 $migrated = 0;
77 for ( $min = 0; $min <= $lastUser; $min += $chunkSize ) {
78 $max = $min + $chunkSize;
79
80 if ( $needSpecialQuery ) {
81 // Use separate subqueries to collect counts with the old
82 // and new schemas, to avoid having to do whole-table scans.
83 $result = $dbr->select(
84 [
85 'user',
86 'rev1' => '('
87 . $dbr->selectSQLText(
88 [ 'revision', 'revision_actor_temp' ],
89 [ 'rev_user', 'ct' => 'COUNT(*)' ],
90 [
91 "rev_user > $min AND rev_user <= $max",
92 'revactor_rev' => null,
93 ],
94 __METHOD__,
95 [ 'GROUP BY' => 'rev_user' ],
96 [ 'revision_actor_temp' => [ 'LEFT JOIN', 'revactor_rev = rev_id' ] ]
97 ) . ')',
98 'rev2' => '('
99 . $dbr->selectSQLText(
100 [ 'revision' ] + $actorQuery['tables'],
101 [ 'actor_user', 'ct' => 'COUNT(*)' ],
102 "actor_user > $min AND actor_user <= $max",
103 __METHOD__,
104 [ 'GROUP BY' => 'actor_user' ],
105 $actorQuery['joins']
106 ) . ')',
107 ],
108 [ 'user_id', 'user_editcount' => 'COALESCE(rev1.ct,0) + COALESCE(rev2.ct,0)' ],
109 "user_id > $min AND user_id <= $max",
110 __METHOD__,
111 [],
112 [
113 'rev1' => [ 'LEFT JOIN', 'user_id = rev_user' ],
114 'rev2' => [ 'LEFT JOIN', 'user_id = actor_user' ],
115 ]
116 );
117 } else {
118 $revUser = $actorQuery['fields']['rev_user'];
119 $result = $dbr->select(
120 [ 'user', 'rev' => [ 'revision' ] + $actorQuery['tables'] ],
121 [ 'user_id', 'user_editcount' => "COUNT($revUser)" ],
122 "user_id > $min AND user_id <= $max",
123 __METHOD__,
124 [ 'GROUP BY' => 'user_id' ],
125 [ 'rev' => [ 'LEFT JOIN', "user_id = $revUser" ] ] + $actorQuery['joins']
126 );
127 }
128
129 foreach ( $result as $row ) {
130 $dbw->update( 'user',
131 [ 'user_editcount' => $row->user_editcount ],
132 [ 'user_id' => $row->user_id ],
133 __METHOD__ );
134 ++$migrated;
135 }
136
137 $delta = microtime( true ) - $start;
138 $rate = ( $delta == 0.0 ) ? 0.0 : $migrated / $delta;
139 $this->output( sprintf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n",
140 wfWikiID(),
141 $migrated,
142 min( $max, $lastUser ) / $lastUser * 100.0,
143 $delta,
144 $rate ) );
145
146 wfWaitForSlaves();
147 }
148 } else {
149 $this->output( "Using single-query mode...\n" );
150
151 $user = $dbw->tableName( 'user' );
152 if ( $needSpecialQuery ) {
153 $subquery1 = $dbw->selectSQLText(
154 [ 'revision', 'revision_actor_temp' ],
155 [ 'COUNT(*)' ],
156 [
157 'user_id = rev_user',
158 'revactor_rev' => null,
159 ],
160 __METHOD__,
161 [],
162 [ 'revision_actor_temp' => [ 'LEFT JOIN', 'revactor_rev = rev_id' ] ]
163 );
164 $subquery2 = $dbw->selectSQLText(
165 [ 'revision' ] + $actorQuery['tables'],
166 [ 'COUNT(*)' ],
167 'user_id = actor_user',
168 __METHOD__,
169 [],
170 $actorQuery['joins']
171 );
172 $dbw->query(
173 "UPDATE $user SET user_editcount=($subquery1) + ($subquery2)",
174 __METHOD__
175 );
176 } else {
177 $subquery = $dbw->selectSQLText(
178 [ 'revision' ] + $actorQuery['tables'],
179 [ 'COUNT(*)' ],
180 [ 'user_id = ' . $actorQuery['fields']['rev_user'] ],
181 __METHOD__,
182 [],
183 $actorQuery['joins']
184 );
185 $dbw->query( "UPDATE $user SET user_editcount=($subquery)", __METHOD__ );
186 }
187 }
188
189 $this->output( "Done!\n" );
190 }
191 }
192
193 $maintClass = InitEditCount::class;
194 require_once RUN_MAINTENANCE_IF_MAIN;