MediaWiki  1.34.0
reassignEdits.php
Go to the documentation of this file.
1 <?php
26 require_once __DIR__ . '/Maintenance.php';
27 
34 class ReassignEdits extends Maintenance {
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription( 'Reassign edits from one user to another' );
38  $this->addOption( "force", "Reassign even if the target user doesn't exist" );
39  $this->addOption( "norc", "Don't update the recent changes table" );
40  $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
41  $this->addArg( 'from', 'Old user to take edits from' );
42  $this->addArg( 'to', 'New user to give edits to' );
43  }
44 
45  public function execute() {
46  if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
47  # Set up the users involved
48  $from = $this->initialiseUser( $this->getArg( 0 ) );
49  $to = $this->initialiseUser( $this->getArg( 1 ) );
50 
51  # If the target doesn't exist, and --force is not set, stop here
52  if ( $to->getId() || $this->hasOption( 'force' ) ) {
53  # Reassign the edits
54  $report = $this->hasOption( 'report' );
55  $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
56  # If reporting, and there were items, advise the user to run without --report
57  if ( $report ) {
58  $this->output( "Run the script again without --report to update.\n" );
59  }
60  } else {
61  $ton = $to->getName();
62  $this->error( "User '{$ton}' not found." );
63  }
64  }
65  }
66 
76  private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
77  $dbw = $this->getDB( DB_MASTER );
78  $this->beginTransaction( $dbw, __METHOD__ );
79 
80  # Count things
81  $this->output( "Checking current edits..." );
82  $revQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rev_user', $from );
83  $res = $dbw->select(
84  [ 'revision' ] + $revQueryInfo['tables'],
85  'COUNT(*) AS count',
86  $revQueryInfo['conds'],
87  __METHOD__,
88  [],
89  $revQueryInfo['joins']
90  );
91  $row = $dbw->fetchObject( $res );
92  $cur = $row->count;
93  $this->output( "found {$cur}.\n" );
94 
95  $this->output( "Checking deleted edits..." );
96  $arQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'ar_user', $from, false );
97  $res = $dbw->select(
98  [ 'archive' ] + $arQueryInfo['tables'],
99  'COUNT(*) AS count',
100  $arQueryInfo['conds'],
101  __METHOD__,
102  [],
103  $arQueryInfo['joins']
104  );
105  $row = $dbw->fetchObject( $res );
106  $del = $row->count;
107  $this->output( "found {$del}.\n" );
108 
109  # Don't count recent changes if we're not supposed to
110  if ( $rc ) {
111  $this->output( "Checking recent changes..." );
112  $rcQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rc_user', $from, false );
113  $res = $dbw->select(
114  [ 'recentchanges' ] + $rcQueryInfo['tables'],
115  'COUNT(*) AS count',
116  $rcQueryInfo['conds'],
117  __METHOD__,
118  [],
119  $rcQueryInfo['joins']
120  );
121  $row = $dbw->fetchObject( $res );
122  $rec = $row->count;
123  $this->output( "found {$rec}.\n" );
124  } else {
125  $rec = 0;
126  }
127 
128  $total = $cur + $del + $rec;
129  $this->output( "\nTotal entries to change: {$total}\n" );
130 
131  if ( !$report ) {
132  if ( $total ) {
133  # Reassign edits
134  $this->output( "\nReassigning current edits..." );
135  $dbw->update(
136  'revision_actor_temp',
137  [ 'revactor_actor' => $to->getActorId( $dbw ) ],
138  [ 'revactor_actor' => $from->getActorId() ],
139  __METHOD__
140  );
141  $this->output( "done.\nReassigning deleted edits..." );
142  $dbw->update( 'archive',
143  [ 'ar_actor' => $to->getActorId( $dbw ) ],
144  [ $arQueryInfo['conds'] ], __METHOD__ );
145  $this->output( "done.\n" );
146  # Update recent changes if required
147  if ( $rc ) {
148  $this->output( "Updating recent changes..." );
149  $dbw->update( 'recentchanges',
150  [ 'rc_actor' => $to->getActorId( $dbw ) ],
151  [ $rcQueryInfo['conds'] ], __METHOD__ );
152  $this->output( "done.\n" );
153  }
154  }
155  }
156 
157  $this->commitTransaction( $dbw, __METHOD__ );
158 
159  return (int)$total;
160  }
161 
168  private function initialiseUser( $username ) {
169  if ( User::isIP( $username ) ) {
170  $user = new User();
171  $user->setId( 0 );
172  $user->setName( $username );
173  } else {
174  $user = User::newFromName( $username );
175  if ( !$user ) {
176  $this->fatalError( "Invalid username" );
177  }
178  }
179  $user->load();
180 
181  return $user;
182  }
183 }
184 
185 $maintClass = ReassignEdits::class;
186 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
ReassignEdits
Maintenance script that reassigns edits from a user or IP address to another user.
Definition: reassignEdits.php:34
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
Maintenance\hasArg
hasArg( $argId=0)
Does a given argument exist?
Definition: Maintenance.php:357
ReassignEdits\doReassignEdits
doReassignEdits(&$from, &$to, $rc=false, $report=false)
Reassign edits from one user to another.
Definition: reassignEdits.php:76
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
$res
$res
Definition: testCompression.php:52
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:136
ReassignEdits\initialiseUser
initialiseUser( $username)
Initialise the user object.
Definition: reassignEdits.php:168
ReassignEdits\__construct
__construct()
Default constructor.
Definition: reassignEdits.php:35
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1426
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
User\isIP
static isIP( $name)
Does the string match an anonymous IP address?
Definition: User.php:891
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$maintClass
$maintClass
Definition: reassignEdits.php:185
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1441
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:319
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:481
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:371
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
ReassignEdits\execute
execute()
Do the actual work.
Definition: reassignEdits.php:45