MediaWiki  REL1_24
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(
00083             'revision',
00084             'COUNT(*) AS count',
00085             $this->userConditions( $from, 'rev_user', 'rev_user_text' ),
00086             __METHOD__
00087         );
00088         $row = $dbw->fetchObject( $res );
00089         $cur = $row->count;
00090         $this->output( "found {$cur}.\n" );
00091 
00092         $this->output( "Checking deleted edits..." );
00093         $res = $dbw->select(
00094             'archive',
00095             'COUNT(*) AS count',
00096             $this->userConditions( $from, 'ar_user', 'ar_user_text' ),
00097             __METHOD__
00098         );
00099         $row = $dbw->fetchObject( $res );
00100         $del = $row->count;
00101         $this->output( "found {$del}.\n" );
00102 
00103         # Don't count recent changes if we're not supposed to
00104         if ( $rc ) {
00105             $this->output( "Checking recent changes..." );
00106             $res = $dbw->select(
00107                 'recentchanges',
00108                 'COUNT(*) AS count',
00109                 $this->userConditions( $from, 'rc_user', 'rc_user_text' ),
00110                 __METHOD__
00111             );
00112             $row = $dbw->fetchObject( $res );
00113             $rec = $row->count;
00114             $this->output( "found {$rec}.\n" );
00115         } else {
00116             $rec = 0;
00117         }
00118 
00119         $total = $cur + $del + $rec;
00120         $this->output( "\nTotal entries to change: {$total}\n" );
00121 
00122         if ( !$report ) {
00123             if ( $total ) {
00124                 # Reassign edits
00125                 $this->output( "\nReassigning current edits..." );
00126                 $dbw->update( 'revision', $this->userSpecification( $to, 'rev_user', 'rev_user_text' ),
00127                     $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
00128                 $this->output( "done.\nReassigning deleted edits..." );
00129                 $dbw->update( 'archive', $this->userSpecification( $to, 'ar_user', 'ar_user_text' ),
00130                     $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
00131                 $this->output( "done.\n" );
00132                 # Update recent changes if required
00133                 if ( $rc ) {
00134                     $this->output( "Updating recent changes..." );
00135                     $dbw->update( 'recentchanges', $this->userSpecification( $to, 'rc_user', 'rc_user_text' ),
00136                         $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
00137                     $this->output( "done.\n" );
00138                 }
00139             }
00140         }
00141 
00142         $dbw->commit( __METHOD__ );
00143 
00144         return (int)$total;
00145     }
00146 
00156     private function userConditions( &$user, $idfield, $utfield ) {
00157         return $user->getId()
00158             ? array( $idfield => $user->getId() )
00159             : array( $utfield => $user->getName() );
00160     }
00161 
00171     private function userSpecification( &$user, $idfield, $utfield ) {
00172         return array( $idfield => $user->getId(), $utfield => $user->getName() );
00173     }
00174 
00181     private function initialiseUser( $username ) {
00182         if ( User::isIP( $username ) ) {
00183             $user = new User();
00184             $user->setId( 0 );
00185             $user->setName( $username );
00186         } else {
00187             $user = User::newFromName( $username );
00188             if ( !$user ) {
00189                 $this->error( "Invalid username", true );
00190             }
00191         }
00192         $user->load();
00193 
00194         return $user;
00195     }
00196 }
00197 
00198 $maintClass = "ReassignEdits";
00199 require_once RUN_MAINTENANCE_IF_MAIN;