Debian Codegraph @

maintenance/migrateUserGroup.php

raw symbols SCIP
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
24 use MediaWiki\User\User;
25
26 // @codeCoverageIgnoreStart
27 require_once __DIR__ . '/Maintenance.php';
28 // @codeCoverageIgnoreEnd
29
30 /**
31 * Maintenance script that re-assigns users from an old group to a new one.
32 *
33 * @ingroup Maintenance
34 */
35 class MigrateUserGroup extends Maintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Re-assign users from an old group to a new one' );
39 $this->addArg( 'oldgroup', 'Old user group key', true );
40 $this->addArg( 'newgroup', 'New user group key', true );
41 $this->setBatchSize( 200 );
42 }
43
44 public function execute() {
45 $count = 0;
46 $oldGroup = $this->getArg( 0 );
47 $newGroup = $this->getArg( 1 );
48 $dbw = $this->getPrimaryDB();
49 $batchSize = $this->getBatchSize();
50 $userGroupManager = $this->getServiceContainer()->getUserGroupManager();
51 $start = $dbw->newSelectQueryBuilder()
52 ->select( 'MIN(ug_user)' )
53 ->from( 'user_groups' )
54 ->where( [ 'ug_group' => $oldGroup ] )
55 ->caller( __FUNCTION__ )->fetchField();
56 $end = $dbw->newSelectQueryBuilder()
57 ->select( 'MAX(ug_user)' )
58 ->from( 'user_groups' )
59 ->where( [ 'ug_group' => $oldGroup ] )
60 ->caller( __FUNCTION__ )->fetchField();
61 if ( $start === null ) {
62 $this->fatalError( "Nothing to do - no users in the '$oldGroup' group" );
63 }
64 # Do remaining chunk
65 $end += $batchSize - 1;
66 $blockStart = $start;
67 $blockEnd = $start + $batchSize - 1;
68 // Migrate users over in batches...
69 while ( $blockEnd <= $end ) {
70 $affected = 0;
71 $this->output( "Doing users $blockStart to $blockEnd\n" );
72
73 $this->beginTransaction( $dbw, __METHOD__ );
74 // Find the users already in the new group, so that we can exclude them from the UPDATE query
75 // and instead delete the rows.
76 $usersAlreadyInNewGroup = $dbw->newSelectQueryBuilder()
77 ->select( 'ug_user' )
78 ->from( 'user_groups' )
79 ->where( [
80 'ug_group' => $newGroup,
81 $dbw->expr( 'ug_user', '>=', (int)$blockStart ),
82 $dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
83 ] )
84 ->caller( __METHOD__ )
85 ->fetchFieldValues();
86
87 // Update the user group for the users which do not already have the new group.
88 $updateQueryBuilder = $dbw->newUpdateQueryBuilder()
89 ->update( 'user_groups' )
90 ->set( [ 'ug_group' => $newGroup ] )
91 ->where( [
92 'ug_group' => $oldGroup,
93 $dbw->expr( 'ug_user', '>=', (int)$blockStart ),
94 $dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
95 ] )
96 ->caller( __METHOD__ );
97 if ( count( $usersAlreadyInNewGroup ) ) {
98 $updateQueryBuilder->where( $dbw->expr( 'ug_user', '!=', $usersAlreadyInNewGroup ) );
99 }
100 $updateQueryBuilder->execute();
101 $affected += $dbw->affectedRows();
102
103 // Delete rows that the UPDATE operation above had to ignore.
104 // This happens when a user is in both the old and new group, and as such the UPDATE would have failed.
105 if ( count( $usersAlreadyInNewGroup ) ) {
106 $dbw->newDeleteQueryBuilder()
107 ->deleteFrom( 'user_groups' )
108 ->where( [
109 'ug_group' => $oldGroup,
110 $dbw->expr( 'ug_user', '=', $usersAlreadyInNewGroup ),
111 ] )
112 ->caller( __METHOD__ )->execute();
113 $affected += $dbw->affectedRows();
114 }
115 $this->commitTransaction( $dbw, __METHOD__ );
116
117 // Clear cache for the affected users (T42340)
118 if ( $affected > 0 ) {
119 // XXX: This also invalidates cache of unaffected users that
120 // were in the new group and not in the group.
121 $res = $dbw->newSelectQueryBuilder()
122 ->select( 'ug_user' )
123 ->from( 'user_groups' )
124 ->where( [
125 'ug_group' => $newGroup,
126 $dbw->expr( 'ug_user', '>=', (int)$blockStart ),
127 $dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
128 ] )
129 ->caller( __METHOD__ )->fetchResultSet();
130 if ( $res !== false ) {
131 foreach ( $res as $row ) {
132 $user = User::newFromId( $row->ug_user );
133 $user->invalidateCache();
134 $userGroupManager->clearCache( $user );
135 }
136 }
137 }
138
139 $count += $affected;
140 $blockStart += $batchSize;
141 $blockEnd += $batchSize;
142 }
143 $this->output( "Done! $count users in group '$oldGroup' are now in '$newGroup' instead.\n" );
144 }
145 }
146
147 // @codeCoverageIgnoreStart
148 $maintClass = MigrateUserGroup::class;
149 require_once RUN_MAINTENANCE_IF_MAIN;
150 // @codeCoverageIgnoreEnd