Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
82 / 82
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
MigrateUserGroup
100.00% covered (success)
100.00%
82 / 82
100.00% covered (success)
100.00%
2 / 2
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
77 / 77
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2/**
3 * Re-assign users from an old group to a new one
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24use MediaWiki\Maintenance\Maintenance;
25use MediaWiki\User\User;
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
31/**
32 * Maintenance script that re-assigns users from an old group to a new one.
33 *
34 * @ingroup Maintenance
35 */
36class 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 );
43    }
44
45    public function execute() {
46        $count = 0;
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" );
64        }
65        # Do remaining chunk
66        $end += $batchSize - 1;
67        $blockStart = $start;
68        $blockEnd = $start + $batchSize - 1;
69        // Migrate users over in batches...
70        while ( $blockEnd <= $end ) {
71            $affected = 0;
72            $this->output( "Doing users $blockStart to $blockEnd\n" );
73
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()
78                ->select( 'ug_user' )
79                ->from( 'user_groups' )
80                ->where( [
81                    'ug_group' => $newGroup,
82                    $dbw->expr( 'ug_user', '>=', (int)$blockStart ),
83                    $dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
84                ] )
85                ->caller( __METHOD__ )
86                ->fetchFieldValues();
87
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 ] )
92                ->where( [
93                    'ug_group' => $oldGroup,
94                    $dbw->expr( 'ug_user', '>=', (int)$blockStart ),
95                    $dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
96                ] )
97                ->caller( __METHOD__ );
98            if ( count( $usersAlreadyInNewGroup ) ) {
99                $updateQueryBuilder->where( $dbw->expr( 'ug_user', '!=', $usersAlreadyInNewGroup ) );
100            }
101            $updateQueryBuilder->execute();
102            $affected += $dbw->affectedRows();
103
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' )
109                    ->where( [
110                        'ug_group' => $oldGroup,
111                        $dbw->expr( 'ug_user', '=', $usersAlreadyInNewGroup ),
112                    ] )
113                    ->caller( __METHOD__ )->execute();
114                $affected += $dbw->affectedRows();
115            }
116            $this->commitTransaction( $dbw, __METHOD__ );
117
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' )
125                    ->where( [
126                        'ug_group' => $newGroup,
127                        $dbw->expr( 'ug_user', '>=', (int)$blockStart ),
128                        $dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
129                    ] )
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 );
136                    }
137                }
138            }
139
140            $count += $affected;
141            $blockStart += $batchSize;
142            $blockEnd += $batchSize;
143        }
144        $this->output( "Done! $count users in group '$oldGroup' are now in '$newGroup' instead.\n" );
145    }
146}
147
148// @codeCoverageIgnoreStart
149$maintClass = MigrateUserGroup::class;
150require_once RUN_MAINTENANCE_IF_MAIN;
151// @codeCoverageIgnoreEnd