MediaWiki master
migrateUserGroup.php
Go to the documentation of this file.
1<?php
25
26require_once __DIR__ . '/Maintenance.php';
27
34 public function __construct() {
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 );
39 $this->setBatchSize( 200 );
40 }
41
42 public function execute() {
43 $count = 0;
44 $oldGroup = $this->getArg( 0 );
45 $newGroup = $this->getArg( 1 );
46 $dbw = $this->getPrimaryDB();
47 $batchSize = $this->getBatchSize();
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" );
60 }
61 # Do remaining chunk
62 $end += $batchSize - 1;
63 $blockStart = $start;
64 $blockEnd = $start + $batchSize - 1;
65 // Migrate users over in batches...
66 while ( $blockEnd <= $end ) {
67 $affected = 0;
68 $this->output( "Doing users $blockStart to $blockEnd\n" );
69
70 $this->beginTransaction( $dbw, __METHOD__ );
71 $dbw->newUpdateQueryBuilder()
72 ->update( 'user_groups' )
73 ->ignore()
74 ->set( [ 'ug_group' => $newGroup ] )
75 ->where( [
76 'ug_group' => $oldGroup,
77 $dbw->expr( 'ug_user', '>=', (int)$blockStart ),
78 $dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
79 ] )
80 ->caller( __METHOD__ )->execute();
81 $affected += $dbw->affectedRows();
82 // Delete rows that the UPDATE operation above had to ignore.
83 // This happens when a user is in both the old and new group.
84 // Updating the row for the old group membership failed since
85 // user/group is UNIQUE.
86 $dbw->newDeleteQueryBuilder()
87 ->deleteFrom( 'user_groups' )
88 ->where( [
89 'ug_group' => $oldGroup,
90 $dbw->expr( 'ug_user', '>=', (int)$blockStart ),
91 $dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
92 ] )
93 ->caller( __METHOD__ )->execute();
94 $affected += $dbw->affectedRows();
95 $this->commitTransaction( $dbw, __METHOD__ );
96
97 // Clear cache for the affected users (T42340)
98 if ( $affected > 0 ) {
99 // XXX: This also invalidates cache of unaffected users that
100 // were in the new group and not in the group.
101 $res = $dbw->newSelectQueryBuilder()
102 ->select( 'ug_user' )
103 ->from( 'user_groups' )
104 ->where( [
105 'ug_group' => $newGroup,
106 $dbw->expr( 'ug_user', '>=', (int)$blockStart ),
107 $dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
108 ] )
109 ->caller( __METHOD__ )->fetchResultSet();
110 if ( $res !== false ) {
111 foreach ( $res as $row ) {
112 $user = User::newFromId( $row->ug_user );
113 $user->invalidateCache();
114 }
115 }
116 }
117
118 $count += $affected;
119 $blockStart += $batchSize;
120 $blockEnd += $batchSize;
121 }
122 $this->output( "Done! $count users in group '$oldGroup' are now in '$newGroup' instead.\n" );
123 }
124}
125
126$maintClass = MigrateUserGroup::class;
127require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
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.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
internal since 1.36
Definition User.php:94
Maintenance script that re-assigns users from an old group to a new one.
execute()
Do the actual work.
__construct()
Default constructor.