Debian Codegraph @

maintenance/findMissingActors.php

raw symbols SCIP
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 * @file
19 * @ingroup Maintenance
20 */
21
22 use MediaWiki\User\ActorNormalization;
23 use MediaWiki\User\UserFactory;
24 use MediaWiki\User\UserNameUtils;
25 use MediaWiki\User\UserRigorOptions;
26
27 // @codeCoverageIgnoreStart
28 require_once __DIR__ . '/Maintenance.php';
29 // @codeCoverageIgnoreEnd
30
31 /**
32 * Maintenance script for finding and replacing invalid actor IDs, see T261325 and T307738.
33 *
34 * @ingroup Maintenance
35 */
36 class FindMissingActors extends Maintenance {
37
38 private UserFactory $userFactory;
39 private UserNameUtils $userNameUtils;
40 private ActorNormalization $actorNormalization;
41
42 public function __construct() {
43 parent::__construct();
44
45 $this->addDescription( 'Find and fix invalid actor IDs.' );
46 $this->addOption( 'field', 'The name of a database field to process',
47 true, true );
48 $this->addOption( 'type', 'Which type of invalid actors to find or fix, '
49 . 'missing or broken (with empty actor_name which can\'t be associated '
50 . 'with an existing user).',
51 false, true );
52 $this->addOption( 'skip', 'A comma-separated list of actor IDs to skip.',
53 false, true );
54 $this->addOption( 'overwrite-with', 'Replace invalid actors with this user. '
55 . 'Typically, this would be "Unknown user", but it could be any reserved '
56 . 'system user (per $wgReservedUsernames) or locally registered user. '
57 . 'If not given, invalid actors will only be listed, not fixed. '
58 . 'You will be prompted for confirmation before data is written. ',
59 false, true );
60
61 $this->setBatchSize( 1000 );
62 }
63
64 /**
65 * @return array
66 */
67 private function getTables() {
68 return [
69 'ar_actor' => [ 'archive', 'ar_actor', 'ar_id' ],
70 'img_actor' => [ 'image', 'img_actor', 'img_name' ],
71 'oi_actor' => [ 'oldimage', 'oi_actor', 'oi_archive_name' ], // no index on oi_archive_name!
72 'fa_actor' => [ 'filearchive', 'fa_actor', 'fa_id' ],
73 'rc_actor' => [ 'recentchanges', 'rc_actor', 'rc_id' ],
74 'log_actor' => [ 'logging', 'log_actor', 'log_id' ],
75 'rev_actor' => [ 'revision', 'rev_actor', 'rev_id' ],
76 'bl_by_actor' => [ 'block', 'bl_by_actor', 'bl_id' ], // no index on bl_by_actor!
77 ];
78 }
79
80 /**
81 * @param string $field
82 * @return array|null
83 */
84 private function getTableInfo( $field ) {
85 $tables = $this->getTables();
86 return $tables[$field] ?? null;
87 }
88
89 /**
90 * Returns the actor ID of the user specified with the --overwrite-with option,
91 * or null if --overwrite-with is not set.
92 *
93 * Existing users and reserved system users are supported.
94 * If the user does not have an actor ID yet, one will be assigned.
95 *
96 * @return int|null
97 */
98 private function getNewActorId() {
99 $name = $this->getOption( 'overwrite-with' );
100
101 if ( $name === null ) {
102 return null;
103 }
104
105 $user = $this->userFactory->newFromName( $name );
106
107 if ( !$user ) {
108 $this->fatalError( "Not a valid user name: '$name'" );
109 }
110
111 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
112
113 if ( $user->isRegistered() ) {
114 $this->output( "Using existing user: '$user'\n" );
115 } elseif ( !$this->userNameUtils->isValid( $name ) ) {
116 $this->fatalError( "Not a valid user name: '$name'" );
117 } elseif ( !$this->userNameUtils->isUsable( $name ) ) {
118 $this->output( "Using system user: '$name'\n" );
119 } else {
120 $this->fatalError( "Unknown user: '$name'" );
121 }
122
123 $dbw = $this->getPrimaryDB();
124 $actorId = $this->actorNormalization->acquireActorId( $user, $dbw );
125
126 if ( !$actorId ) {
127 $this->fatalError( "Failed to acquire an actor ID for user '$user'" );
128 }
129
130 $this->output( "Replacement actor ID is $actorId.\n" );
131 return $actorId;
132 }
133
134 public function execute() {
135 $services = $this->getServiceContainer();
136 $this->userFactory = $services->getUserFactory();
137 $this->userNameUtils = $services->getUserNameUtils();
138 $this->actorNormalization = $services->getActorNormalization();
139 $this->setDBProvider( $services->getConnectionProvider() );
140
141 $field = $this->getOption( 'field' );
142 if ( !$this->getTableInfo( $field ) ) {
143 $this->fatalError( "Unknown field: $field.\n" );
144 }
145
146 $type = $this->getOption( 'type', 'missing' );
147 if ( $type !== 'missing' && $type !== 'broken' ) {
148 $this->fatalError( "Unknown type: $type.\n" );
149 }
150
151 $skip = $this->parseIntList( $this->getOption( 'skip', '' ) );
152 $overwrite = $this->getNewActorId();
153
154 $bad = $this->findBadActors( $field, $type, $skip );
155
156 if ( $bad && $overwrite ) {
157 $this->output( "\n" );
158 $this->output( "Do you want to OVERWRITE the listed actor IDs?\n" );
159 $this->output( "Information about the invalid IDs will be lost!\n" );
160 $this->output( "\n" );
161 $confirm = self::readconsole( 'Type "yes" to continue: ' );
162
163 if ( $confirm === 'yes' ) {
164 $this->overwriteActorIDs( $field, array_keys( $bad ), $overwrite );
165 } else {
166 $this->fatalError( 'Aborted.' );
167 }
168 }
169
170 $this->output( "Done.\n" );
171 }
172
173 /**
174 * Find rows that have bad actor IDs.
175 *
176 * @param string $field the database field in which to detect bad actor IDs.
177 * @param string $type type of bad actors, missing or broken.
178 * @param int[] $skip bad actor IDs not to replace.
179 *
180 * @return array a list of row IDs, identifying rows in which the actor ID needs to be replaced.
181 */
182 private function findBadActors( $field, $type, $skip ) {
183 [ $table, $actorField, $idField ] = $this->getTableInfo( $field );
184 $this->output( "Finding invalid actor IDs in $table.$actorField...\n" );
185
186 $dbr = $this->getServiceContainer()->getDBLoadBalancer()->getConnection( DB_REPLICA, 'vslow' );
187
188 /*
189 We are building an SQL query like this one here, performing a left join
190 to detect rows in $table that lack a matching row in the actor table.
191
192 In this example, $field is 'log_actor', so $table is 'logging',
193 $actorField is 'log_actor', and $idField is 'log_id'.
194 Further, $skip is [ 1, 2, 3, 4 ] and the batch size is 1000.
195
196 SELECT log_id
197 FROM logging
198 LEFT JOIN actor ON log_actor = actor_id
199 WHERE actor_id IS NULL
200 AND log_actor NOT IN (1, 2, 3, 4)
201 LIMIT 1000;
202 */
203
204 $queryBuilder = $dbr->newSelectQueryBuilder()
205 ->select( [ $actorField, $idField ] )
206 ->from( $table )
207 ->leftJoin( 'actor', null, [ "$actorField = actor_id" ] )
208 ->where( $type == 'missing' ? [ 'actor_id' => null ] : [ 'actor_name' => '' ] )
209 ->limit( $this->getBatchSize() );
210
211 if ( $skip ) {
212 $queryBuilder->andWhere( $dbr->expr( $actorField, '!=', $skip ) );
213 }
214
215 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
216 $count = $res->numRows();
217
218 $bad = [];
219
220 if ( $count ) {
221 $this->output( "\t\tID\tACTOR\n" );
222 }
223
224 foreach ( $res as $row ) {
225 $id = $row->$idField;
226 $actor = (int)( $row->$actorField );
227
228 $bad[$id] = $actor;
229 $this->output( "\t\t$id\t$actor\n" );
230 }
231
232 $this->output( "\tFound $count invalid actor IDs.\n" );
233
234 if ( $count >= $this->getBatchSize() ) {
235 $this->output( "\tBatch size reached, run again after fixing the current batch.\n" );
236 }
237
238 return $bad;
239 }
240
241 /**
242 * Overwrite the actor ID in a given set of rows.
243 *
244 * @param string $field the database field in which to replace IDs.
245 * @param array $ids The row IDs of the rows in which the actor ID should be replaced
246 * @param int $overwrite The actor ID to write to the rows identified by $ids.
247 *
248 * @return int
249 */
250 private function overwriteActorIDs( $field, array $ids, int $overwrite ) {
251 [ $table, $actorField, $idField ] = $this->getTableInfo( $field );
252
253 $count = count( $ids );
254 $this->output( "OVERWRITING $count actor IDs in $table.$actorField with $overwrite...\n" );
255
256 $dbw = $this->getPrimaryDB();
257
258 $dbw->newUpdateQueryBuilder()
259 ->update( $table )
260 ->set( [ $actorField => $overwrite ] )
261 ->where( [ $idField => $ids ] )
262 ->caller( __METHOD__ )->execute();
263
264 $count = $dbw->affectedRows();
265
266 $this->waitForReplication();
267 $this->output( "\tUpdated $count rows.\n" );
268
269 return $count;
270 }
271
272 }
273
274 // @codeCoverageIgnoreStart
275 $maintClass = FindMissingActors::class;
276 require_once RUN_MAINTENANCE_IF_MAIN;
277 // @codeCoverageIgnoreEnd