3 * Re-assign users from an old group to a new one
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 use MediaWiki\Maintenance\Maintenance
;
25 use MediaWiki\User\User
;
27 // @codeCoverageIgnoreStart
28 require_once __DIR__
. '/Maintenance.php';
29 // @codeCoverageIgnoreEnd
32 * Maintenance script that re-assigns users from an old group to a new one.
34 * @ingroup Maintenance
36 class MigrateUserGroup
extends Maintenance
{
37 public function __construct() {
38 parent
::__construct();
39 $this->addDescription( 'Re-assign users from an old group to a new one' );
40 $this->addArg( 'oldgroup', 'Old user group key', true );
41 $this->addArg( 'newgroup', 'New user group key', true );
42 $this->setBatchSize( 200 );
45 public function execute() {
47 $oldGroup = $this->getArg( 0 );
48 $newGroup = $this->getArg( 1 );
49 $dbw = $this->getPrimaryDB();
50 $batchSize = $this->getBatchSize();
51 $userGroupManager = $this->getServiceContainer()->getUserGroupManager();
52 $start = $dbw->newSelectQueryBuilder()
53 ->select( 'MIN(ug_user)' )
54 ->from( 'user_groups' )
55 ->where( [ 'ug_group' => $oldGroup ] )
56 ->caller( __FUNCTION__
)->fetchField();
57 $end = $dbw->newSelectQueryBuilder()
58 ->select( 'MAX(ug_user)' )
59 ->from( 'user_groups' )
60 ->where( [ 'ug_group' => $oldGroup ] )
61 ->caller( __FUNCTION__
)->fetchField();
62 if ( $start === null ) {
63 $this->fatalError( "Nothing to do - no users in the '$oldGroup' group" );
66 $end +
= $batchSize - 1;
68 $blockEnd = $start +
$batchSize - 1;
69 // Migrate users over in batches...
70 while ( $blockEnd <= $end ) {
72 $this->output( "Doing users $blockStart to $blockEnd\n" );
74 $this->beginTransaction( $dbw, __METHOD__
);
75 // Find the users already in the new group, so that we can exclude them from the UPDATE query
76 // and instead delete the rows.
77 $usersAlreadyInNewGroup = $dbw->newSelectQueryBuilder()
79 ->from( 'user_groups' )
81 'ug_group' => $newGroup,
82 $dbw->expr( 'ug_user', '>=', (int)$blockStart ),
83 $dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
85 ->caller( __METHOD__
)
88 // Update the user group for the users which do not already have the new group.
89 $updateQueryBuilder = $dbw->newUpdateQueryBuilder()
90 ->update( 'user_groups' )
91 ->set( [ 'ug_group' => $newGroup ] )
93 'ug_group' => $oldGroup,
94 $dbw->expr( 'ug_user', '>=', (int)$blockStart ),
95 $dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
97 ->caller( __METHOD__
);
98 if ( count( $usersAlreadyInNewGroup ) ) {
99 $updateQueryBuilder->where( $dbw->expr( 'ug_user', '!=', $usersAlreadyInNewGroup ) );
101 $updateQueryBuilder->execute();
102 $affected +
= $dbw->affectedRows();
104 // Delete rows that the UPDATE operation above had to ignore.
105 // This happens when a user is in both the old and new group, and as such the UPDATE would have failed.
106 if ( count( $usersAlreadyInNewGroup ) ) {
107 $dbw->newDeleteQueryBuilder()
108 ->deleteFrom( 'user_groups' )
110 'ug_group' => $oldGroup,
111 $dbw->expr( 'ug_user', '=', $usersAlreadyInNewGroup ),
113 ->caller( __METHOD__
)->execute();
114 $affected +
= $dbw->affectedRows();
116 $this->commitTransaction( $dbw, __METHOD__
);
118 // Clear cache for the affected users (T42340)
119 if ( $affected > 0 ) {
120 // XXX: This also invalidates cache of unaffected users that
121 // were in the new group and not in the group.
122 $res = $dbw->newSelectQueryBuilder()
123 ->select( 'ug_user' )
124 ->from( 'user_groups' )
126 'ug_group' => $newGroup,
127 $dbw->expr( 'ug_user', '>=', (int)$blockStart ),
128 $dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
130 ->caller( __METHOD__
)->fetchResultSet();
131 if ( $res !== false ) {
132 foreach ( $res as $row ) {
133 $user = User
::newFromId( $row->ug_user
);
134 $user->invalidateCache();
135 $userGroupManager->clearCache( $user );
141 $blockStart +
= $batchSize;
142 $blockEnd +
= $batchSize;
144 $this->output( "Done! $count users in group '$oldGroup' are now in '$newGroup' instead.\n" );
148 // @codeCoverageIgnoreStart
149 $maintClass = MigrateUserGroup
::class;
150 require_once RUN_MAINTENANCE_IF_MAIN
;
151 // @codeCoverageIgnoreEnd