Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 71 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
MigrateUserGroup | |
0.00% |
0 / 68 |
|
0.00% |
0 / 2 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 63 |
|
0.00% |
0 / 1 |
42 |
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 | require_once __DIR__ . '/Maintenance.php'; |
27 | |
28 | /** |
29 | * Maintenance script that re-assigns users from an old group to a new one. |
30 | * |
31 | * @ingroup Maintenance |
32 | */ |
33 | class MigrateUserGroup extends Maintenance { |
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; |
127 | require_once RUN_MAINTENANCE_IF_MAIN; |