MediaWiki  1.23.2
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->mDescription = "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 = wfGetDB( DB_MASTER );
78  $dbw->begin( __METHOD__ );
79 
80  # Count things
81  $this->output( "Checking current edits..." );
82  $res = $dbw->select( 'revision', 'COUNT(*) AS count', $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
83  $row = $dbw->fetchObject( $res );
84  $cur = $row->count;
85  $this->output( "found {$cur}.\n" );
86 
87  $this->output( "Checking deleted edits..." );
88  $res = $dbw->select( 'archive', 'COUNT(*) AS count', $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
89  $row = $dbw->fetchObject( $res );
90  $del = $row->count;
91  $this->output( "found {$del}.\n" );
92 
93  # Don't count recent changes if we're not supposed to
94  if ( $rc ) {
95  $this->output( "Checking recent changes..." );
96  $res = $dbw->select( 'recentchanges', 'COUNT(*) AS count', $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
97  $row = $dbw->fetchObject( $res );
98  $rec = $row->count;
99  $this->output( "found {$rec}.\n" );
100  } else {
101  $rec = 0;
102  }
103 
104  $total = $cur + $del + $rec;
105  $this->output( "\nTotal entries to change: {$total}\n" );
106 
107  if ( !$report ) {
108  if ( $total ) {
109  # Reassign edits
110  $this->output( "\nReassigning current edits..." );
111  $dbw->update( 'revision', $this->userSpecification( $to, 'rev_user', 'rev_user_text' ),
112  $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
113  $this->output( "done.\nReassigning deleted edits..." );
114  $dbw->update( 'archive', $this->userSpecification( $to, 'ar_user', 'ar_user_text' ),
115  $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
116  $this->output( "done.\n" );
117  # Update recent changes if required
118  if ( $rc ) {
119  $this->output( "Updating recent changes..." );
120  $dbw->update( 'recentchanges', $this->userSpecification( $to, 'rc_user', 'rc_user_text' ),
121  $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
122  $this->output( "done.\n" );
123  }
124  }
125  }
126 
127  $dbw->commit( __METHOD__ );
128  return (int)$total;
129  }
130 
140  private function userConditions( &$user, $idfield, $utfield ) {
141  return $user->getId() ? array( $idfield => $user->getId() ) : array( $utfield => $user->getName() );
142  }
143 
153  private function userSpecification( &$user, $idfield, $utfield ) {
154  return array( $idfield => $user->getId(), $utfield => $user->getName() );
155  }
156 
163  private function initialiseUser( $username ) {
164  if ( User::isIP( $username ) ) {
165  $user = new User();
166  $user->setId( 0 );
167  $user->setName( $username );
168  } else {
169  $user = User::newFromName( $username );
170  if ( !$user ) {
171  $this->error( "Invalid username", true );
172  }
173  }
174  $user->load();
175  return $user;
176  }
177 }
178 
179 $maintClass = "ReassignEdits";
180 require_once RUN_MAINTENANCE_IF_MAIN;
ReassignEdits
Maintenance script that reassigns edits from a user or IP address to another user.
Definition: reassignEdits.php:34
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
$from
$from
Definition: importImages.php:90
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:388
Maintenance\hasArg
hasArg( $argId=0)
Does a given argument exist?
Definition: Maintenance.php:236
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.txt:39
ReassignEdits\initialiseUser
initialiseUser( $username)
Initialise the user object.
Definition: reassignEdits.php:163
ReassignEdits\__construct
__construct()
Default constructor.
Definition: reassignEdits.php:35
$total
$total
Definition: Utf8Test.php:92
User\isIP
static isIP( $name)
Does the string match an anonymous IPv4 address?
Definition: User.php:554
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
$maintClass
$maintClass
Definition: reassignEdits.php:179
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:207
ReassignEdits\userSpecification
userSpecification(&$user, $idfield, $utfield)
Return user specifications i.e.
Definition: reassignEdits.php:153
User
User
Definition: All_system_messages.txt:425
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
ReassignEdits\userConditions
userConditions(&$user, $idfield, $utfield)
Return the most efficient set of user conditions i.e.
Definition: reassignEdits.php:140
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:246
$res
$res
Definition: database.txt:21
ReassignEdits\execute
execute()
Do the actual work.
Definition: reassignEdits.php:45