| 1 |
<?php |
| 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 |
use MediaWiki\User\User; |
| 27 |
use Wikimedia\IPUtils; |
| 28 |
|
| 29 |
|
| 30 |
require_once __DIR__ . '/Maintenance.php'; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class ReassignEdits extends Maintenance { |
| 40 |
public function __construct() { |
| 41 |
parent::__construct(); |
| 42 |
$this->addDescription( 'Reassign edits from one user to another' ); |
| 43 |
$this->addOption( "force", "Reassign even if the target user doesn't exist" ); |
| 44 |
$this->addOption( "norc", "Don't update the recent changes table" ); |
| 45 |
$this->addOption( "report", "Print out details of what would be changed, but don't update it" ); |
| 46 |
$this->addArg( 'from', 'Old user to take edits from' ); |
| 47 |
$this->addArg( 'to', 'New user to give edits to' ); |
| 48 |
} |
| 49 |
|
| 50 |
public function execute() { |
| 51 |
if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) { |
| 52 |
|
| 53 |
$from = $this->initialiseUser( $this->getArg( 0 ) ); |
| 54 |
$to = $this->initialiseUser( $this->getArg( 1 ) ); |
| 55 |
|
| 56 |
|
| 57 |
if ( $to->getId() || $this->hasOption( 'force' ) ) { |
| 58 |
|
| 59 |
$report = $this->hasOption( 'report' ); |
| 60 |
$this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report ); |
| 61 |
|
| 62 |
if ( $report ) { |
| 63 |
$this->output( "Run the script again without --report to update.\n" ); |
| 64 |
} |
| 65 |
} else { |
| 66 |
$ton = $to->getName(); |
| 67 |
$this->error( "User '{$ton}' not found." ); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private function doReassignEdits( &$from, &$to, $updateRC = false, $report = false ) { |
| 82 |
$dbw = $this->getPrimaryDB(); |
| 83 |
$this->beginTransaction( $dbw, __METHOD__ ); |
| 84 |
$actorNormalization = $this->getServiceContainer()->getActorNormalization(); |
| 85 |
$fromActorId = $actorNormalization->findActorId( $from, $dbw ); |
| 86 |
|
| 87 |
|
| 88 |
$this->output( "Checking current edits..." ); |
| 89 |
|
| 90 |
$revisionRows = $dbw->newSelectQueryBuilder() |
| 91 |
->select( '*' ) |
| 92 |
->from( 'revision' ) |
| 93 |
->where( [ 'rev_actor' => $fromActorId ] ) |
| 94 |
->caller( __METHOD__ ) |
| 95 |
->fetchRowCount(); |
| 96 |
|
| 97 |
$this->output( "found {$revisionRows}.\n" ); |
| 98 |
|
| 99 |
$this->output( "Checking deleted edits..." ); |
| 100 |
$archiveRows = $dbw->newSelectQueryBuilder() |
| 101 |
->select( '*' ) |
| 102 |
->from( 'archive' ) |
| 103 |
->where( [ 'ar_actor' => $fromActorId ] ) |
| 104 |
->caller( __METHOD__ )->fetchRowCount(); |
| 105 |
$this->output( "found {$archiveRows}.\n" ); |
| 106 |
|
| 107 |
|
| 108 |
if ( $updateRC ) { |
| 109 |
$this->output( "Checking recent changes..." ); |
| 110 |
$recentChangesRows = $dbw->newSelectQueryBuilder() |
| 111 |
->select( '*' ) |
| 112 |
->from( 'recentchanges' ) |
| 113 |
->where( [ 'rc_actor' => $fromActorId ] ) |
| 114 |
->caller( __METHOD__ )->fetchRowCount(); |
| 115 |
$this->output( "found {$recentChangesRows}.\n" ); |
| 116 |
} else { |
| 117 |
$recentChangesRows = 0; |
| 118 |
} |
| 119 |
|
| 120 |
$total = $revisionRows + $archiveRows + $recentChangesRows; |
| 121 |
$this->output( "\nTotal entries to change: {$total}\n" ); |
| 122 |
|
| 123 |
$toActorId = $actorNormalization->acquireActorId( $to, $dbw ); |
| 124 |
if ( !$report && $total ) { |
| 125 |
$this->output( "\n" ); |
| 126 |
if ( $revisionRows ) { |
| 127 |
|
| 128 |
$this->output( "Reassigning current edits..." ); |
| 129 |
|
| 130 |
$dbw->newUpdateQueryBuilder() |
| 131 |
->update( 'revision' ) |
| 132 |
->set( [ 'rev_actor' => $toActorId ] ) |
| 133 |
->where( [ 'rev_actor' => $fromActorId ] ) |
| 134 |
->caller( __METHOD__ )->execute(); |
| 135 |
|
| 136 |
$this->output( "done.\n" ); |
| 137 |
} |
| 138 |
|
| 139 |
if ( $archiveRows ) { |
| 140 |
$this->output( "Reassigning deleted edits..." ); |
| 141 |
|
| 142 |
$dbw->newUpdateQueryBuilder() |
| 143 |
->update( 'archive' ) |
| 144 |
->set( [ 'ar_actor' => $toActorId ] ) |
| 145 |
->where( [ 'ar_actor' => $fromActorId ] ) |
| 146 |
->caller( __METHOD__ )->execute(); |
| 147 |
|
| 148 |
$this->output( "done.\n" ); |
| 149 |
} |
| 150 |
|
| 151 |
if ( $recentChangesRows ) { |
| 152 |
$this->output( "Updating recent changes..." ); |
| 153 |
|
| 154 |
$dbw->newUpdateQueryBuilder() |
| 155 |
->update( 'recentchanges' ) |
| 156 |
->set( [ 'rc_actor' => $toActorId ] ) |
| 157 |
->where( [ 'rc_actor' => $fromActorId ] ) |
| 158 |
->caller( __METHOD__ )->execute(); |
| 159 |
|
| 160 |
$this->output( "done.\n" ); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
if ( !$from->isRegistered() ) { |
| 166 |
$this->output( "Deleting ip_changes..." ); |
| 167 |
|
| 168 |
$dbw->newDeleteQueryBuilder() |
| 169 |
->deleteFrom( 'ip_changes' ) |
| 170 |
->where( [ 'ipc_hex' => IPUtils::toHex( $from->getName() ) ] ) |
| 171 |
->caller( __METHOD__ )->execute(); |
| 172 |
|
| 173 |
$this->output( "done.\n" ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
$this->commitTransaction( $dbw, __METHOD__ ); |
| 178 |
|
| 179 |
return $total; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private function initialiseUser( $username ) { |
| 189 |
$services = $this->getServiceContainer(); |
| 190 |
if ( $services->getUserNameUtils()->isIP( $username ) ) { |
| 191 |
$user = User::newFromName( $username, false ); |
| 192 |
$user->getActorId(); |
| 193 |
} else { |
| 194 |
$user = User::newFromName( $username ); |
| 195 |
if ( !$user ) { |
| 196 |
$this->fatalError( "Invalid username" ); |
| 197 |
} |
| 198 |
} |
| 199 |
$user->load(); |
| 200 |
|
| 201 |
return $user; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
$maintClass = ReassignEdits::class; |
| 207 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 208 |
|