MediaWiki  master
reassignEdits.php
Go to the documentation of this file.
1 <?php
27 
28 require_once __DIR__ . '/Maintenance.php';
29 
36 class ReassignEdits extends Maintenance {
37  public function __construct() {
38  parent::__construct();
39  $this->addDescription( 'Reassign edits from one user to another' );
40  $this->addOption( "force", "Reassign even if the target user doesn't exist" );
41  $this->addOption( "norc", "Don't update the recent changes table" );
42  $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
43  $this->addArg( 'from', 'Old user to take edits from' );
44  $this->addArg( 'to', 'New user to give edits to' );
45  }
46 
47  public function execute() {
48  if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
49  # Set up the users involved
50  $from = $this->initialiseUser( $this->getArg( 0 ) );
51  $to = $this->initialiseUser( $this->getArg( 1 ) );
52 
53  # If the target doesn't exist, and --force is not set, stop here
54  if ( $to->getId() || $this->hasOption( 'force' ) ) {
55  # Reassign the edits
56  $report = $this->hasOption( 'report' );
57  $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
58  # If reporting, and there were items, advise the user to run without --report
59  if ( $report ) {
60  $this->output( "Run the script again without --report to update.\n" );
61  }
62  } else {
63  $ton = $to->getName();
64  $this->error( "User '{$ton}' not found." );
65  }
66  }
67  }
68 
78  private function doReassignEdits( &$from, &$to, $updateRC = false, $report = false ) {
79  $dbw = $this->getDB( DB_PRIMARY );
80  $this->beginTransaction( $dbw, __METHOD__ );
81  $actorNormalization = MediaWikiServices::getInstance()->getActorNormalization();
82  $fromActorId = $actorNormalization->findActorId( $from, $dbw );
83 
84  # Count things
85  $this->output( "Checking current edits..." );
86  $revQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rev_user', $from );
87  $revisionRows = $dbw->selectRowCount(
88  [ 'revision' ] + $revQueryInfo['tables'],
89  '*',
90  $revQueryInfo['conds'],
91  __METHOD__,
92  [],
93  $revQueryInfo['joins']
94  );
95  $this->output( "found {$revisionRows}.\n" );
96 
97  $this->output( "Checking deleted edits..." );
98  $archiveRows = $dbw->selectRowCount(
99  [ 'archive' ],
100  '*',
101  [ 'ar_actor' => $fromActorId ],
102  __METHOD__
103  );
104  $this->output( "found {$archiveRows}.\n" );
105 
106  # Don't count recent changes if we're not supposed to
107  if ( $updateRC ) {
108  $this->output( "Checking recent changes..." );
109  $recentChangesRows = $dbw->selectRowCount(
110  [ 'recentchanges' ],
111  '*',
112  [ 'rc_actor' => $fromActorId ],
113  __METHOD__
114  );
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  # Reassign edits
128  $this->output( "Reassigning current edits..." );
129  $dbw->update(
130  'revision',
131  [ 'rev_actor' => $toActorId ],
132  [ 'rev_actor' => $fromActorId ],
133  __METHOD__
134  );
135  $this->output( "done.\n" );
136  }
137 
138  if ( $archiveRows ) {
139  $this->output( "Reassigning deleted edits..." );
140  $dbw->update( 'archive',
141  [ 'ar_actor' => $toActorId ],
142  [ 'ar_actor' => $fromActorId ],
143  __METHOD__
144  );
145  $this->output( "done.\n" );
146  }
147  # Update recent changes if required
148  if ( $recentChangesRows ) {
149  $this->output( "Updating recent changes..." );
150  $dbw->update( 'recentchanges',
151  [ 'rc_actor' => $toActorId ],
152  [ 'rc_actor' => $fromActorId ],
153  __METHOD__
154  );
155  $this->output( "done.\n" );
156  }
157  }
158 
159  $this->commitTransaction( $dbw, __METHOD__ );
160 
161  return $total;
162  }
163 
170  private function initialiseUser( $username ) {
171  $services = MediaWikiServices::getInstance();
172  if ( $services->getUserNameUtils()->isIP( $username ) ) {
173  $user = User::newFromName( $username, false );
174  $user->getActorId();
175  } else {
176  $user = User::newFromName( $username );
177  if ( !$user ) {
178  $this->fatalError( "Invalid username" );
179  }
180  }
181  $user->load();
182 
183  return $user;
184  }
185 }
186 
187 $maintClass = ReassignEdits::class;
188 require_once RUN_MAINTENANCE_IF_MAIN;
static newMigration()
Static constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true)
Add some args that are needed.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasArg( $argId=0)
Does a given argument exist?
hasOption( $name)
Checks to see if a particular option was set.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
Maintenance script that reassigns edits from a user or IP address to another user.
execute()
Do the actual work.
__construct()
Default constructor.
static newFromName( $name, $validate='valid')
Definition: User.php:598
const DB_PRIMARY
Definition: defines.php:28
$maintClass