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, $rc = false, $report = false ) {
79  $actorTableSchemaMigrationStage = $this->getConfig()->get( 'ActorTableSchemaMigrationStage' );
80  $dbw = $this->getDB( DB_PRIMARY );
81  $this->beginTransaction( $dbw, __METHOD__ );
82  $actorNormalization = MediaWikiServices::getInstance()->getActorNormalization();
83  $fromActorId = $actorNormalization->findActorId( $from, $dbw );
84 
85  # Count things
86  $this->output( "Checking current edits..." );
87  $revQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rev_user', $from );
88  $res = $dbw->select(
89  [ 'revision' ] + $revQueryInfo['tables'],
90  'COUNT(*) AS count',
91  $revQueryInfo['conds'],
92  __METHOD__,
93  [],
94  $revQueryInfo['joins']
95  );
96  $row = $dbw->fetchObject( $res );
97  $cur = $row->count;
98  $this->output( "found {$cur}.\n" );
99 
100  $this->output( "Checking deleted edits..." );
101  $res = $dbw->select(
102  [ 'archive' ],
103  'COUNT(*) AS count',
104  [ 'ar_actor' => $fromActorId ],
105  __METHOD__
106  );
107  $row = $dbw->fetchObject( $res );
108  $del = $row->count;
109  $this->output( "found {$del}.\n" );
110 
111  # Don't count recent changes if we're not supposed to
112  if ( $rc ) {
113  $this->output( "Checking recent changes..." );
114  $res = $dbw->select(
115  [ 'recentchanges' ],
116  'COUNT(*) AS count',
117  [ 'rc_actor' => $fromActorId ],
118  __METHOD__
119  );
120  $row = $dbw->fetchObject( $res );
121  $rec = $row->count;
122  $this->output( "found {$rec}.\n" );
123  } else {
124  $rec = 0;
125  }
126 
127  $total = $cur + $del + $rec;
128  $this->output( "\nTotal entries to change: {$total}\n" );
129 
130  $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
131  if ( !$report ) {
132  if ( $total ) {
133  # Reassign edits
134  $this->output( "\nReassigning current edits..." );
135  if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_TEMP ) {
136  $dbw->update(
137  'revision_actor_temp',
138  [ 'revactor_actor' => $toActorId ],
139  [ 'revactor_actor' => $fromActorId ],
140  __METHOD__
141  );
142  }
143  if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
144  $dbw->update(
145  'revision',
146  [ 'rev_actor' => $toActorId ],
147  [ 'rev_actor' => $fromActorId ],
148  __METHOD__
149  );
150  }
151  $this->output( "done.\nReassigning deleted edits..." );
152  $dbw->update( 'archive',
153  [ 'ar_actor' => $toActorId ],
154  [ 'ar_actor' => $fromActorId ],
155  __METHOD__
156  );
157  $this->output( "done.\n" );
158  # Update recent changes if required
159  if ( $rc ) {
160  $this->output( "Updating recent changes..." );
161  $dbw->update( 'recentchanges',
162  [ 'rc_actor' => $toActorId ],
163  [ 'rc_actor' => $fromActorId ],
164  __METHOD__
165  );
166  $this->output( "done.\n" );
167  }
168  }
169  }
170 
171  $this->commitTransaction( $dbw, __METHOD__ );
172 
173  return (int)$total;
174  }
175 
182  private function initialiseUser( $username ) {
183  $services = MediaWikiServices::getInstance();
184  if ( $services->getUserNameUtils()->isIP( $username ) ) {
185  $user = User::newFromName( $username, false );
186  $user->getActorId();
187  } else {
188  $user = User::newFromName( $username );
189  if ( !$user ) {
190  $this->fatalError( "Invalid username" );
191  }
192  }
193  $user->load();
194 
195  return $user;
196  }
197 }
198 
199 $maintClass = ReassignEdits::class;
200 require_once RUN_MAINTENANCE_IF_MAIN;
ReassignEdits
Maintenance script that reassigns edits from a user or IP address to another user.
Definition: reassignEdits.php:36
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:191
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:606
Maintenance\hasArg
hasArg( $argId=0)
Does a given argument exist?
Definition: Maintenance.php:338
ReassignEdits\doReassignEdits
doReassignEdits(&$from, &$to, $rc=false, $report=false)
Reassign edits from one user to another.
Definition: reassignEdits.php:78
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
$res
$res
Definition: testCompression.php:57
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:76
ReassignEdits\initialiseUser
initialiseUser( $username)
Initialise the user object.
Definition: reassignEdits.php:182
ReassignEdits\__construct
__construct()
Default constructor.
Definition: reassignEdits.php:37
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
Definition: Maintenance.php:1393
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:598
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
SCHEMA_COMPAT_WRITE_NEW
const SCHEMA_COMPAT_WRITE_NEW
Definition: Defines.php:266
$maintClass
$maintClass
Definition: reassignEdits.php:199
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
SCHEMA_COMPAT_WRITE_TEMP
const SCHEMA_COMPAT_WRITE_TEMP
Definition: Defines.php:264
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1408
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1362
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:300
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:464
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:353
ReassignEdits\execute
execute()
Do the actual work.
Definition: reassignEdits.php:47