26 require_once __DIR__ .
'/Maintenance.php';
35 parent::__construct();
36 $this->
addDescription(
'Re-assign users from an old group to a new one' );
37 $this->
addArg(
'oldgroup',
'Old user group key',
true );
38 $this->
addArg(
'newgroup',
'New user group key',
true );
44 $oldGroup = $this->
getArg( 0 );
45 $newGroup = $this->
getArg( 1 );
48 $start = $dbw->newSelectQueryBuilder()
49 ->select(
'MIN(ug_user)' )
50 ->from(
'user_groups' )
51 ->where( [
'ug_group' => $oldGroup ] )
52 ->caller( __FUNCTION__ )->fetchField();
53 $end = $dbw->newSelectQueryBuilder()
54 ->select(
'MAX(ug_user)' )
55 ->from(
'user_groups' )
56 ->where( [
'ug_group' => $oldGroup ] )
57 ->caller( __FUNCTION__ )->fetchField();
58 if ( $start ===
null ) {
59 $this->
fatalError(
"Nothing to do - no users in the '$oldGroup' group" );
62 $end += $batchSize - 1;
64 $blockEnd = $start + $batchSize - 1;
66 while ( $blockEnd <= $end ) {
68 $this->
output(
"Doing users $blockStart to $blockEnd\n" );
71 $dbw->update(
'user_groups',
72 [
'ug_group' => $newGroup ],
73 [
'ug_group' => $oldGroup,
74 "ug_user BETWEEN " . (
int)$blockStart .
" AND " . (
int)$blockEnd ],
78 $affected += $dbw->affectedRows();
83 $dbw->delete(
'user_groups',
84 [
'ug_group' => $oldGroup,
85 "ug_user BETWEEN " . (
int)$blockStart .
" AND " . (
int)$blockEnd ],
88 $affected += $dbw->affectedRows();
92 if ( $affected > 0 ) {
95 $res = $dbw->newSelectQueryBuilder()
97 ->from(
'user_groups' )
99 'ug_group' => $newGroup,
100 "ug_user BETWEEN " . (
int)$blockStart .
" AND " . (
int)$blockEnd
102 ->caller( __METHOD__ )->fetchResultSet();
103 if ( $res !==
false ) {
104 foreach ( $res as $row ) {
105 $user = User::newFromId( $row->ug_user );
106 $user->invalidateCache();
112 $blockStart += $batchSize;
113 $blockEnd += $batchSize;
115 $this->
output(
"Done! $count users in group '$oldGroup' are now in '$newGroup' instead.\n" );
120 require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getBatchSize()
Returns batch size.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Maintenance script that re-assigns users from an old group to a new one.
execute()
Do the actual work.
__construct()
Default constructor.