MediaWiki  REL1_22
reassignEdits.php
Go to the documentation of this file.
00001 <?php
00026 require_once __DIR__ . '/Maintenance.php';
00027 
00034 class ReassignEdits extends Maintenance {
00035     public function __construct() {
00036         parent::__construct();
00037         $this->mDescription = "Reassign edits from one user to another";
00038         $this->addOption( "force", "Reassign even if the target user doesn't exist" );
00039         $this->addOption( "norc", "Don't update the recent changes table" );
00040         $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
00041         $this->addArg( 'from', 'Old user to take edits from' );
00042         $this->addArg( 'to', 'New user to give edits to' );
00043     }
00044 
00045     public function execute() {
00046         if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
00047             # Set up the users involved
00048             $from = $this->initialiseUser( $this->getArg( 0 ) );
00049             $to = $this->initialiseUser( $this->getArg( 1 ) );
00050 
00051             # If the target doesn't exist, and --force is not set, stop here
00052             if ( $to->getId() || $this->hasOption( 'force' ) ) {
00053                 # Reassign the edits
00054                 $report = $this->hasOption( 'report' );
00055                 $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
00056                 # If reporting, and there were items, advise the user to run without --report
00057                 if ( $report ) {
00058                     $this->output( "Run the script again without --report to update.\n" );
00059                 }
00060             } else {
00061                 $ton = $to->getName();
00062                 $this->error( "User '{$ton}' not found." );
00063             }
00064         }
00065     }
00066 
00076     private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
00077         $dbw = wfGetDB( DB_MASTER );
00078         $dbw->begin( __METHOD__ );
00079 
00080         # Count things
00081         $this->output( "Checking current edits..." );
00082         $res = $dbw->select( 'revision', 'COUNT(*) AS count', $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
00083         $row = $dbw->fetchObject( $res );
00084         $cur = $row->count;
00085         $this->output( "found {$cur}.\n" );
00086 
00087         $this->output( "Checking deleted edits..." );
00088         $res = $dbw->select( 'archive', 'COUNT(*) AS count', $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
00089         $row = $dbw->fetchObject( $res );
00090         $del = $row->count;
00091         $this->output( "found {$del}.\n" );
00092 
00093         # Don't count recent changes if we're not supposed to
00094         if ( $rc ) {
00095             $this->output( "Checking recent changes..." );
00096             $res = $dbw->select( 'recentchanges', 'COUNT(*) AS count', $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
00097             $row = $dbw->fetchObject( $res );
00098             $rec = $row->count;
00099             $this->output( "found {$rec}.\n" );
00100         } else {
00101             $rec = 0;
00102         }
00103 
00104         $total = $cur + $del + $rec;
00105         $this->output( "\nTotal entries to change: {$total}\n" );
00106 
00107         if ( !$report ) {
00108             if ( $total ) {
00109                 # Reassign edits
00110                 $this->output( "\nReassigning current edits..." );
00111                 $dbw->update( 'revision', $this->userSpecification( $to, 'rev_user', 'rev_user_text' ),
00112                     $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
00113                 $this->output( "done.\nReassigning deleted edits..." );
00114                 $dbw->update( 'archive', $this->userSpecification( $to, 'ar_user', 'ar_user_text' ),
00115                     $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
00116                 $this->output( "done.\n" );
00117                 # Update recent changes if required
00118                 if ( $rc ) {
00119                     $this->output( "Updating recent changes..." );
00120                     $dbw->update( 'recentchanges', $this->userSpecification( $to, 'rc_user', 'rc_user_text' ),
00121                         $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
00122                     $this->output( "done.\n" );
00123                 }
00124             }
00125         }
00126 
00127         $dbw->commit( __METHOD__ );
00128         return (int)$total;
00129     }
00130 
00140     private function userConditions( &$user, $idfield, $utfield ) {
00141         return $user->getId() ? array( $idfield => $user->getId() ) : array( $utfield => $user->getName() );
00142     }
00143 
00153     private function userSpecification( &$user, $idfield, $utfield ) {
00154         return array( $idfield => $user->getId(), $utfield => $user->getName() );
00155     }
00156 
00163     private function initialiseUser( $username ) {
00164         if ( User::isIP( $username ) ) {
00165             $user = new User();
00166             $user->setId( 0 );
00167             $user->setName( $username );
00168         } else {
00169             $user = User::newFromName( $username );
00170             if ( !$user ) {
00171                 $this->error( "Invalid username", true );
00172             }
00173         }
00174         $user->load();
00175         return $user;
00176     }
00177 
00178 
00179 }
00180 
00181 $maintClass = "ReassignEdits";
00182 require_once RUN_MAINTENANCE_IF_MAIN;