1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
|
<?php
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\IDatabase;
/**
* Contains the actual database backend logic for merging users
*/
class MergeUser {
/**
* @var User
*/
private $oldUser, $newUser;
/**
* @var IUserMergeLogger
*/
private $logger;
/** @var integer */
private $flags;
const USE_MULTI_COMMIT = 1; // allow begin/commit; useful for jobs or CLI mode
/**
* @param User $oldUser
* @param User $newUser
* @param IUserMergeLogger $logger
* @param int $flags Bitfield (Supports MergeUser::USE_*)
*/
public function __construct(
User $oldUser,
User $newUser,
IUserMergeLogger $logger,
$flags = 0
) {
$this->newUser = $newUser;
$this->oldUser = $oldUser;
$this->logger = $logger;
$this->flags = $flags;
}
/**
* @param User $performer
* @param string $fnameTrxOwner
*/
public function merge( User $performer, $fnameTrxOwner = __METHOD__ ) {
$this->mergeEditcount();
$this->mergeDatabaseTables( $fnameTrxOwner );
$this->logger->addMergeEntry( $performer, $this->oldUser, $this->newUser );
}
/**
* @param User $performer
* @param callable $msg something that returns a Message object
*
* @return array Array of failed page moves, see MergeUser::movePages
*/
public function delete( User $performer, /* callable */ $msg ) {
$failed = $this->movePages( $performer, $msg );
$this->deleteUser();
$this->logger->addDeleteEntry( $performer, $this->oldUser );
return $failed;
}
/**
* Adds edit count of both users
*/
private function mergeEditcount() {
$dbw = wfGetDB( DB_MASTER );
$dbw->startAtomic( __METHOD__ );
$totalEdits = $dbw->selectField(
'user',
'SUM(user_editcount)',
[ 'user_id' => [ $this->newUser->getId(), $this->oldUser->getId() ] ],
__METHOD__
);
$totalEdits = (int)$totalEdits;
# don't run queries if neither user has any edits
if ( $totalEdits > 0 ) {
# update new user with total edits
$dbw->update( 'user',
[ 'user_editcount' => $totalEdits ],
[ 'user_id' => $this->newUser->getId() ],
__METHOD__
);
# clear old user's edits
$dbw->update( 'user',
[ 'user_editcount' => 0 ],
[ 'user_id' => $this->oldUser->getId() ],
__METHOD__
);
}
$dbw->endAtomic( __METHOD__ );
}
/**
* @param IDatabase $dbw
* @return void
*/
private function mergeBlocks( IDatabase $dbw ) {
$dbw->startAtomic( __METHOD__ );
// Pull blocks directly from master
$qi = DatabaseBlock::getQueryInfo();
$rows = $dbw->select(
$qi['tables'],
array_merge( $qi['fields'], [ 'ipb_user' ] ),
[
'ipb_user' => [ $this->oldUser->getId(), $this->newUser->getId() ],
],
__METHOD__,
[],
$qi['joins']
);
$newBlock = null;
$oldBlock = null;
foreach ( $rows as $row ) {
if ( (int)$row->ipb_user === $this->oldUser->getId() ) {
$oldBlock = $row;
} elseif ( (int)$row->ipb_user === $this->newUser->getId() ) {
$newBlock = $row;
}
}
if ( !$oldBlock ) {
// No one is blocked or
// Only the new user is blocked, so nothing to do.
$dbw->endAtomic( __METHOD__ );
return;
}
if ( !$newBlock ) {
// Just move the old block to the new username
$dbw->update(
'ipblocks',
[ 'ipb_user' => $this->newUser->getId() ],
[ 'ipb_id' => $oldBlock->ipb_id ],
__METHOD__
);
$dbw->endAtomic( __METHOD__ );
return;
}
// Okay, let's pick the "strongest" block, and re-apply it to
// the new user.
$oldBlockObj = DatabaseBlock::newFromRow( $oldBlock );
$newBlockObj = DatabaseBlock::newFromRow( $newBlock );
$winner = $this->chooseBlock( $oldBlockObj, $newBlockObj );
if ( $winner->getId() === $newBlockObj->getId() ) {
$oldBlockObj->delete();
} else { // Old user block won
$newBlockObj->delete(); // Delete current new block
$dbw->update(
'ipblocks',
[ 'ipb_user' => $this->newUser->getId() ],
[ 'ipb_id' => $winner->getId() ],
__METHOD__
);
}
$dbw->endAtomic( __METHOD__ );
}
/**
* @param DatabaseBlock $b1
* @param DatabaseBlock $b2
* @return DatabaseBlock
*/
private function chooseBlock( DatabaseBlock $b1, DatabaseBlock $b2 ) {
// First, see if one is longer than the other.
if ( $b1->getExpiry() !== $b2->getExpiry() ) {
// This works for infinite blocks because:
// "infinity" > "20141024234513"
if ( $b1->getExpiry() > $b2->getExpiry() ) {
return $b1;
} else {
return $b2;
}
}
// Next check what they block, in order
$blockProps = [];
foreach ( [ $b1, $b2 ] as $block ) {
$blockProps[] = [
'block' => $block,
'createaccount' => $block->isCreateAccountBlocked(),
'sendemail' => $block->isEmailBlocked(),
'editownusertalk' => !$block->isUsertalkEditAllowed(),
];
}
foreach ( [ 'createaccount', 'sendemail', 'editownusertalk' ] as $action ) {
if ( $blockProps[0][$action] xor $blockProps[1][$action] ) {
if ( $blockProps[0][$action] ) {
return $blockProps[0]['block'];
} else {
return $blockProps[1]['block'];
}
}
}
// Give up, return the second one.
return $b2;
}
private function stageNeedsUser( $stage ) {
if ( !defined( 'MIGRATION_NEW' ) ) {
return true;
}
if ( !class_exists( ActorMigration::class ) ) {
return false;
}
if ( defined( 'ActorMigration::MIGRATION_STAGE_SCHEMA_COMPAT' ) ) {
return (bool)( (int)$stage & SCHEMA_COMPAT_WRITE_OLD );
} else {
return $stage < MIGRATION_NEW;
}
}
private function stageNeedsActor( $stage ) {
if ( !defined( 'MIGRATION_NEW' ) ) {
return false;
}
if ( !class_exists( ActorMigration::class ) ) {
return true;
}
if ( defined( 'ActorMigration::MIGRATION_STAGE_SCHEMA_COMPAT' ) ) {
return (bool)( $stage & SCHEMA_COMPAT_WRITE_NEW );
} else {
return $stage > MIGRATION_OLD;
}
}
/**
* Function to merge database references from one user to another user
*
* Merges database references from one user ID or username to another user ID or username
* to preserve referential integrity.
*
* @param string $fnameTrxOwner
*/
private function mergeDatabaseTables( $fnameTrxOwner ) {
// Fields to update with the format:
// [
// tableName, idField, textField,
// 'batchKey' => unique field, 'options' => array(), 'db' => IDatabase
// 'actorId' => actor ID field,
// 'actorStage' => actor schema migration stage
// ];
// textField, batchKey, db, and options are optional
$updateFields = [
[ 'archive', 'ar_user', 'ar_user_text', 'batchKey' => 'ar_id', 'actorId' => 'ar_actor',
'actorStage' => SCHEMA_COMPAT_NEW ],
[ 'revision', 'rev_user', 'rev_user_text', 'batchKey' => 'rev_id', 'actorId' => '',
'actorStage' => SCHEMA_COMPAT_NEW ],
[ 'filearchive', 'fa_user', 'fa_user_text', 'batchKey' => 'fa_id', 'actorId' => 'fa_actor',
'actorStage' => SCHEMA_COMPAT_NEW ],
[ 'image', 'img_user', 'img_user_text', 'batchKey' => 'img_name', 'actorId' => 'img_actor',
'actorStage' => SCHEMA_COMPAT_NEW ],
[ 'oldimage', 'oi_user', 'oi_user_text', 'batchKey' => 'oi_archive_name',
'actorId' => 'oi_actor', 'actorStage' => SCHEMA_COMPAT_NEW ],
[ 'recentchanges', 'rc_user', 'rc_user_text', 'batchKey' => 'rc_id', 'actorId' => 'rc_actor',
'actorStage' => SCHEMA_COMPAT_NEW ],
[ 'logging', 'log_user', 'log_user_text', 'batchKey' => 'log_id', 'actorId' => 'log_actor',
'actorStage' => SCHEMA_COMPAT_NEW ],
[ 'ipblocks', 'ipb_by', 'ipb_by_text', 'batchKey' => 'ipb_id', 'actorId' => 'ipb_by_actor',
'actorStage' => SCHEMA_COMPAT_NEW ],
[ 'watchlist', 'wl_user', 'batchKey' => 'wl_title' ],
[ 'user_groups', 'ug_user', 'options' => [ 'IGNORE' ] ],
[ 'user_properties', 'up_user', 'options' => [ 'IGNORE' ] ],
[ 'user_former_groups', 'ufg_user', 'options' => [ 'IGNORE' ] ],
[ 'revision_actor_temp', 'batchKey' => 'revactor_rev', 'actorId' => 'revactor_actor',
'actorStage' => SCHEMA_COMPAT_NEW ],
];
Hooks::run( 'UserMergeAccountFields', [ &$updateFields ] );
$dbw = wfGetDB( DB_MASTER );
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
$ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
$this->deduplicateWatchlistEntries( $dbw );
$this->mergeBlocks( $dbw );
if ( $this->flags & self::USE_MULTI_COMMIT ) {
// Flush prior writes; this actives the non-transaction path in the loop below.
$lbFactory->commitMasterChanges( $fnameTrxOwner );
}
foreach ( $updateFields as $fieldInfo ) {
if ( !isset( $fieldInfo[1] ) ) {
// Actors only
continue;
}
$options = $fieldInfo['options'] ?? [];
unset( $fieldInfo['options'] );
$db = $fieldInfo['db'] ?? $dbw;
unset( $fieldInfo['db'] );
$tableName = array_shift( $fieldInfo );
$idField = array_shift( $fieldInfo );
$keyField = $fieldInfo['batchKey'] ?? null;
unset( $fieldInfo['batchKey'] );
if ( isset( $fieldInfo['actorId'] ) && isset( $fieldInfo['actorStage'] ) &&
!$this->stageNeedsUser( $fieldInfo['actorStage'] )
) {
continue;
}
unset( $fieldInfo['actorId'], $fieldInfo['actorStage'] );
if ( $db->trxLevel() || $keyField === null ) {
// Can't batch/wait when in a transaction or when no batch key is given
$db->update(
$tableName,
[ $idField => $this->newUser->getId() ]
+ array_fill_keys( $fieldInfo, $this->newUser->getName() ),
[ $idField => $this->oldUser->getId() ],
__METHOD__,
$options
);
} else {
$limit = 200;
do {
$checkSince = microtime( true );
// Note that UPDATE with ORDER BY + LIMIT is not well supported.
// Grab a batch of values on a mostly unique column for this user ID.
$res = $db->select(
$tableName,
[ $keyField ],
[ $idField => $this->oldUser->getId() ],
__METHOD__,
[ 'LIMIT' => $limit ]
);
$keyValues = [];
foreach ( $res as $row ) {
$keyValues[] = $row->$keyField;
}
// Update only those rows with the given column values
if ( count( $keyValues ) ) {
$db->update(
$tableName,
[ $idField => $this->newUser->getId() ]
+ array_fill_keys( $fieldInfo, $this->newUser->getName() ),
[ $idField => $this->oldUser->getId(), $keyField => $keyValues ],
__METHOD__,
$options
);
}
// Wait for replication to catch up
$opts = [ 'ifWritesSince' => $checkSince ];
$lbFactory->commitAndWaitForReplication( __METHOD__, $ticket, $opts );
} while ( count( $keyValues ) >= $limit );
}
}
if ( $this->oldUser->getActorId() ) {
$oldActorId = $this->oldUser->getActorId();
$newActorId = $this->newUser->getActorId( $dbw );
foreach ( $updateFields as $fieldInfo ) {
if ( empty( $fieldInfo['actorId'] ) || empty( $fieldInfo['actorStage'] ) ||
!$this->stageNeedsActor( $fieldInfo['actorStage'] )
) {
continue;
}
$options = $fieldInfo['options'] ?? [];
$db = $fieldInfo['db'] ?? $dbw;
$tableName = array_shift( $fieldInfo );
$idField = $fieldInfo['actorId'];
$keyField = $fieldInfo['batchKey'] ?? null;
if ( $db->trxLevel() || $keyField === null ) {
// Can't batch/wait when in a transaction or when no batch key is given
$db->update(
$tableName,
[ $idField => $newActorId ],
[ $idField => $oldActorId ],
__METHOD__,
$options
);
} else {
$limit = 200;
do {
$checkSince = microtime( true );
// Note that UPDATE with ORDER BY + LIMIT is not well supported.
// Grab a batch of values on a mostly unique column for this user ID.
$res = $db->select(
$tableName,
[ $keyField ],
[ $idField => $oldActorId ],
__METHOD__,
[ 'LIMIT' => $limit ]
);
$keyValues = [];
foreach ( $res as $row ) {
$keyValues[] = $row->$keyField;
}
// Update only those rows with the given column values
if ( count( $keyValues ) ) {
$db->update(
$tableName,
[ $idField => $newActorId ],
[ $idField => $oldActorId, $keyField => $keyValues ],
__METHOD__,
$options
);
}
// Wait for replication to catch up
$opts = [ 'ifWritesSince' => $checkSince ];
$lbFactory->commitAndWaitForReplication( __METHOD__, $ticket, $opts );
} while ( count( $keyValues ) >= $limit );
}
}
}
$dbw->delete( 'user_newtalk', [ 'user_id' => $this->oldUser->getId() ], __METHOD__ );
$this->oldUser->clearInstanceCache();
$this->newUser->clearInstanceCache();
Hooks::run( 'MergeAccountFromTo', [ &$this->oldUser, &$this->newUser ] );
}
/**
* Deduplicate watchlist entries
* which old (merge-from) and new (merge-to) users are watching
*
* @param IDatabase $dbw
*/
private function deduplicateWatchlistEntries( $dbw ) {
$dbw->startAtomic( __METHOD__ );
// Get all titles both watched by the old and new user accounts.
// Avoid using self-joins as this fails on temporary tables (e.g. unit tests).
// See https://bugs.mysql.com/bug.php?id=10327.
$titlesToDelete = [];
$res = $dbw->select(
'watchlist',
[ 'wl_namespace', 'wl_title' ],
[ 'wl_user' => $this->oldUser->getId() ],
__METHOD__,
[ 'FOR UPDATE' ]
);
foreach ( $res as $row ) {
$titlesToDelete[$row->wl_namespace . "|" . $row->wl_title] = false;
}
$res = $dbw->select(
'watchlist',
[ 'wl_namespace', 'wl_title' ],
[ 'wl_user' => $this->newUser->getId() ],
__METHOD__,
[ 'FOR UPDATE' ]
);
foreach ( $res as $row ) {
$key = $row->wl_namespace . "|" . $row->wl_title;
if ( isset( $titlesToDelete[$key] ) ) {
$titlesToDelete[$key] = true;
}
}
$dbw->freeResult( $res );
$titlesToDelete = array_filter( $titlesToDelete );
$conds = [];
foreach ( array_keys( $titlesToDelete ) as $tuple ) {
list( $ns, $dbKey ) = explode( "|", $tuple, 2 );
$conds[] = $dbw->makeList(
[
'wl_user' => $this->oldUser->getId(),
'wl_namespace' => $ns,
'wl_title' => $dbKey
],
LIST_AND
);
}
if ( count( $conds ) ) {
# Perform a multi-row delete
$dbw->delete(
'watchlist',
$dbw->makeList( $conds, LIST_OR ),
__METHOD__
);
}
$dbw->endAtomic( __METHOD__ );
}
/**
* Function to merge user pages
*
* Deletes all pages when merging to Anon
* Moves user page when the target user page does not exist or is empty
* Deletes redirect if nothing links to old page
* Deletes the old user page when the target user page exists
*
* @todo This code is a duplicate of Renameuser and GlobalRename
*
* @param User $performer
* @param callable $msg Function that returns a Message object
* @return array Array of old name (string) => new name (Title) where the move failed
*/
private function movePages( User $performer, /* callable */ $msg ) {
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
$oldusername = trim( str_replace( '_', ' ', $this->oldUser->getName() ) );
$oldusername = Title::makeTitle( NS_USER, $oldusername );
$newusername = Title::makeTitleSafe( NS_USER, $contLang->ucfirst( $this->newUser->getName() ) );
# select all user pages and sub-pages
$dbr = wfGetDB( DB_REPLICA );
$pages = $dbr->select(
'page',
[ 'page_namespace', 'page_title' ],
[
'page_namespace' => [ NS_USER, NS_USER_TALK ],
'page_title' . $dbr->buildLike( $oldusername->getDBkey() . '/', $dbr->anyString() )
. ' OR page_title = ' . $dbr->addQuotes( $oldusername->getDBkey() ),
],
__METHOD__
);
$message = function ( /* ... */ ) use ( $msg ) {
return call_user_func_array( $msg, func_get_args() );
};
$failedMoves = [];
foreach ( $pages as $row ) {
$oldPage = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
$newPage = Title::makeTitleSafe( $row->page_namespace,
preg_replace( '!^[^/]+!', $newusername->getDBkey(), $row->page_title ) );
if ( $this->newUser->getName() === 'Anonymous' ) { # delete ALL old pages
if ( $oldPage->exists() ) {
$this->deletePage( $message, $performer, $oldPage );
}
} elseif ( $newPage->exists()
&& !MediaWikiServices::getInstance()
->getMovePageFactory()
->newMovePage( $oldPage, $newPage )
->isValidMove()
->isOk()
&& $newPage->getLength() > 0
) {
# delete old pages that can't be moved
$this->deletePage( $message, $performer, $oldPage );
} else { # move content to new page
# delete target page if it exists and is blank
if ( $newPage->exists() ) {
$this->deletePage( $message, $performer, $newPage );
}
# move to target location
$status = MediaWikiServices::getInstance()
->getMovePageFactory()
->newMovePage( $oldPage, $newPage )
->move(
$performer,
$message(
'usermerge-move-log',
$oldusername->getText(),
$newusername->getText() )->inContentLanguage()->text()
);
if ( !$status->isOk() ) {
$failedMoves[$oldPage->getPrefixedText()] = $newPage;
}
# check if any pages link here
$res = $dbr->selectField( 'pagelinks',
'pl_title',
[ 'pl_title' => $this->oldUser->getName() ],
__METHOD__
);
if ( !$dbr->numRows( $res ) ) {
# nothing links here, so delete unmoved page/redirect
$this->deletePage( $message, $performer, $oldPage );
}
}
}
return $failedMoves;
}
/**
* Helper to delete pages
*
* @param callable $msg
* @param User $user
* @param Title $title
*/
private function deletePage( $msg, User $user, Title $title ) {
$wikipage = WikiPage::factory( $title );
$reason = $msg( 'usermerge-autopagedelete' )->inContentLanguage()->text();
$error = '';
if ( version_compare( MW_VERSION, '1.35', '<' ) ) {
$wikipage->doDeleteArticle( $reason, false, null, null, $error, $user, true );
} else {
$wikipage->doDeleteArticleReal(
$reason,
$user,
false,
null, // Unused
$error,
null, // Unused
[],
'delete',
true
);
}
}
/**
* Function to delete users following a successful mergeUser call.
*
* Removes rows from the user, user_groups, user_properties
* and user_former_groups tables.
*/
private function deleteUser() {
$dbw = wfGetDB( DB_MASTER );
/**
* Format is: table => user_id column
*
* If you want it to use a different db object:
* table => array( user_id colum, 'db' => IDatabase );
*/
$tablesToDelete = [
'user_groups' => 'ug_user',
'user_properties' => 'up_user',
'user_former_groups' => 'ufg_user',
];
Hooks::run( 'UserMergeAccountDeleteTables', [ &$tablesToDelete ] );
// Make sure these are always set and last
if ( $dbw->tableExists( 'actor', __METHOD__ ) ) {
$tablesToDelete['actor'] = 'actor_user';
}
$tablesToDelete['user'] = 'user_id';
foreach ( $tablesToDelete as $table => $field ) {
// Check if a different database object was passed (Echo or Flow)
if ( is_array( $field ) ) {
$db = $field['db'] ?? $dbw;
$field = $field[0];
} else {
$db = $dbw;
}
$db->delete(
$table,
[ $field => $this->oldUser->getId() ],
__METHOD__
);
}
Hooks::run( 'DeleteAccount', [ &$this->oldUser ] );
DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'users' => -1 ] ) );
}
}
|