MediaWiki  REL1_20
migrateUserGroup.php
Go to the documentation of this file.
00001 <?php
00024 require_once( __DIR__ . '/Maintenance.php' );
00025 
00031 class MigrateUserGroup extends Maintenance {
00032         public function __construct() {
00033                 parent::__construct();
00034                 $this->mDescription = "Re-assign users from an old group to a new one";
00035                 $this->addArg( 'oldgroup', 'Old user group key', true );
00036                 $this->addArg( 'newgroup', 'New user group key', true );
00037                 $this->setBatchSize( 200 );
00038         }
00039 
00040         public function execute() {
00041                 $count = 0;
00042                 $oldGroup = $this->getArg( 0 );
00043                 $newGroup = $this->getArg( 1 );
00044                 $dbw = wfGetDB( DB_MASTER );
00045                 $start = $dbw->selectField( 'user_groups', 'MIN(ug_user)',
00046                         array( 'ug_group' => $oldGroup ), __FUNCTION__ );
00047                 $end = $dbw->selectField( 'user_groups', 'MAX(ug_user)',
00048                         array( 'ug_group' => $oldGroup ), __FUNCTION__ );
00049                 if ( $start === null ) {
00050                         $this->error( "Nothing to do - no users in the '$oldGroup' group", true );
00051                 }
00052                 # Do remaining chunk
00053                 $end += $this->mBatchSize - 1;
00054                 $blockStart = $start;
00055                 $blockEnd = $start + $this->mBatchSize - 1;
00056                 // Migrate users over in batches...
00057                 while ( $blockEnd <= $end ) {
00058                         $this->output( "Doing users $blockStart to $blockEnd\n" );
00059                         $dbw->begin( __METHOD__ );
00060                         $dbw->update( 'user_groups',
00061                                 array( 'ug_group' => $newGroup ),
00062                                 array( 'ug_group' => $oldGroup,
00063                                         "ug_user BETWEEN $blockStart AND $blockEnd" ),
00064                                 __METHOD__,
00065                                 array( 'IGNORE' )
00066                         );
00067                         $count += $dbw->affectedRows();
00068                         $dbw->delete( 'user_groups',
00069                                 array( 'ug_group' => $oldGroup,
00070                                         "ug_user BETWEEN $blockStart AND $blockEnd" ),
00071                                 __METHOD__
00072                         );
00073                         $count += $dbw->affectedRows();
00074                         $dbw->commit( __METHOD__ );
00075                         $blockStart += $this->mBatchSize;
00076                         $blockEnd += $this->mBatchSize;
00077                         wfWaitForSlaves();
00078                 }
00079                 $this->output( "Done! $count user(s) in group '$oldGroup' are now in '$newGroup' instead.\n" );
00080         }
00081 }
00082 
00083 $maintClass = "MigrateUserGroup";
00084 require_once( RUN_MAINTENANCE_IF_MAIN );