MediaWiki  REL1_19
reassignEdits.php
Go to the documentation of this file.
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                                 $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                                 }
00053                         } else {
00054                                 $ton = $to->getName();
00055                                 $this->error( "User '{$ton}' not found." );
00056                         }
00057                 }
00058         }
00059 
00069         private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
00070                 $dbw = wfGetDB( DB_MASTER );
00071                 $dbw->begin();
00072 
00073                 # Count things
00074                 $this->output( "Checking current edits..." );
00075                 $res = $dbw->select( 'revision', 'COUNT(*) AS count', $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
00076                 $row = $dbw->fetchObject( $res );
00077                 $cur = $row->count;
00078                 $this->output( "found {$cur}.\n" );
00079 
00080                 $this->output( "Checking deleted edits..." );
00081                 $res = $dbw->select( 'archive', 'COUNT(*) AS count', $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
00082                 $row = $dbw->fetchObject( $res );
00083                 $del = $row->count;
00084                 $this->output( "found {$del}.\n" );
00085 
00086                 # Don't count recent changes if we're not supposed to
00087                 if ( $rc ) {
00088                         $this->output( "Checking recent changes..." );
00089                         $res = $dbw->select( 'recentchanges', 'COUNT(*) AS count', $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
00090                         $row = $dbw->fetchObject( $res );
00091                         $rec = $row->count;
00092                         $this->output( "found {$rec}.\n" );
00093                 } else {
00094                         $rec = 0;
00095                 }
00096 
00097                 $total = $cur + $del + $rec;
00098                 $this->output( "\nTotal entries to change: {$total}\n" );
00099 
00100                 if ( !$report ) {
00101                         if ( $total ) {
00102                                 # Reassign edits
00103                                 $this->output( "\nReassigning current edits..." );
00104                                 $dbw->update( 'revision', $this->userSpecification( $to, 'rev_user', 'rev_user_text' ),
00105                                         $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
00106                                 $this->output( "done.\nReassigning deleted edits..." );
00107                                 $dbw->update( 'archive', $this->userSpecification( $to, 'ar_user', 'ar_user_text' ),
00108                                         $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
00109                                 $this->output( "done.\n" );
00110                                 # Update recent changes if required
00111                                 if ( $rc ) {
00112                                         $this->output( "Updating recent changes..." );
00113                                         $dbw->update( 'recentchanges', $this->userSpecification( $to, 'rc_user', 'rc_user_text' ),
00114                                                 $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
00115                                         $this->output( "done.\n" );
00116                                 }
00117                         }
00118                 }
00119 
00120                 $dbw->commit();
00121                 return (int)$total;
00122         }
00123 
00133         private function userConditions( &$user, $idfield, $utfield ) {
00134                 return $user->getId() ? array( $idfield => $user->getId() ) : array( $utfield => $user->getName() );
00135         }
00136 
00146         private function userSpecification( &$user, $idfield, $utfield ) {
00147                 return array( $idfield => $user->getId(), $utfield => $user->getName() );
00148         }
00149 
00156         private function initialiseUser( $username ) {
00157                 if ( User::isIP( $username ) ) {
00158                         $user = new User();
00159                         $user->setId( 0 );
00160                         $user->setName( $username );
00161                 } else {
00162                         $user = User::newFromName( $username );
00163                         if ( !$user ) {
00164                                 $this->error( "Invalid username", true );
00165                         }
00166                 }
00167                 $user->load();
00168                 return $user;
00169         }
00170 
00171 
00172 }
00173 
00174 $maintClass = "ReassignEdits";
00175 require_once( RUN_MAINTENANCE_IF_MAIN );
00176