00001 <?php
00025 require_once( dirname(__FILE__) . '/Maintenance.php' );
00026
00027 class ReassignEdits extends Maintenance {
00028 public function __construct() {
00029 parent::__construct();
00030 $this->mDescription = "Reassign edits from one user to another";
00031 $this->addOption( "force", "Reassign even if the target user doesn't exist" );
00032 $this->addOption( "norc", "Don't update the recent changes table" );
00033 $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
00034 $this->addArg( 'from', 'Old user to take edits from' );
00035 $this->addArg( 'to', 'New user to give edits to' );
00036 }
00037
00038 public function execute() {
00039 if( $this->hasArg(0) && $this->hasArg(1) ) {
00040 # Set up the users involved
00041 $from = $this->initialiseUser( $this->getArg(0) );
00042 $to = $this->initialiseUser( $this->getArg(1) );
00043
00044 # If the target doesn't exist, and --force is not set, stop here
00045 if( $to->getId() || $this->hasOption('force') ) {
00046 # Reassign the edits
00047 $report = $this->hasOption('report');
00048 $count = $this->doReassignEdits( $from, $to, !$this->hasOption('norc'), $report );
00049 # If reporting, and there were items, advise the user to run without --report
00050 if( $report )
00051 $this->output( "Run the script again without --report to update.\n" );
00052 } else {
00053 $ton = $to->getName();
00054 $this->error( "User '{$ton}' not found." );
00055 }
00056 }
00057 }
00058
00068 private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
00069 $dbw = wfGetDB( DB_MASTER );
00070 $dbw->begin();
00071
00072 # Count things
00073 $this->output( "Checking current edits..." );
00074 $res = $dbw->select( 'revision', 'COUNT(*) AS count', $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
00075 $row = $dbw->fetchObject( $res );
00076 $cur = $row->count;
00077 $this->output( "found {$cur}.\n" );
00078
00079 $this->output( "Checking deleted edits..." );
00080 $res = $dbw->select( 'archive', 'COUNT(*) AS count', $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
00081 $row = $dbw->fetchObject( $res );
00082 $del = $row->count;
00083 $this->output( "found {$del}.\n" );
00084
00085 # Don't count recent changes if we're not supposed to
00086 if( $rc ) {
00087 $this->output( "Checking recent changes..." );
00088 $res = $dbw->select( 'recentchanges', 'COUNT(*) AS count', $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
00089 $row = $dbw->fetchObject( $res );
00090 $rec = $row->count;
00091 $this->output( "found {$rec}.\n" );
00092 } else {
00093 $rec = 0;
00094 }
00095
00096 $total = $cur + $del + $rec;
00097 $this->output( "\nTotal entries to change: {$total}\n" );
00098
00099 if( !$report ) {
00100 if( $total ) {
00101 # Reassign edits
00102 $this->output( "\nReassigning current edits..." );
00103 $res = $dbw->update( 'revision', $this->userSpecification( $to, 'rev_user', 'rev_user_text' ), $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
00104 $this->output( "done.\nReassigning deleted edits..." );
00105 $res = $dbw->update( 'archive', $this->userSpecification( $to, 'ar_user', 'ar_user_text' ), $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
00106 $this->output( "done.\n" );
00107 # Update recent changes if required
00108 if( $rc ) {
00109 $this->output( "Updating recent changes..." );
00110 $res = $dbw->update( 'recentchanges', $this->userSpecification( $to, 'rc_user', 'rc_user_text' ), $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
00111 $this->output( "done.\n" );
00112 }
00113 }
00114 }
00115
00116 $dbw->commit();
00117 return (int)$total;
00118 }
00119
00129 private function userConditions( &$user, $idfield, $utfield ) {
00130 return $user->getId() ? array( $idfield => $user->getId() ) : array( $utfield => $user->getName() );
00131 }
00132
00142 private function userSpecification( &$user, $idfield, $utfield ) {
00143 return array( $idfield => $user->getId(), $utfield => $user->getName() );
00144 }
00145
00152 private function initialiseUser( $username ) {
00153 if( User::isIP( $username ) ) {
00154 $user = new User();
00155 $user->setId( 0 );
00156 $user->setName( $username );
00157 } else {
00158 $user = User::newFromName( $username );
00159 }
00160 $user->load();
00161 return $user;
00162 }
00163
00164
00165 }
00166
00167 $maintClass = "ReassignEdits";
00168 require_once( DO_MAINTENANCE );
00169