MediaWiki master
migrateUserGroup.php
Go to the documentation of this file.
1<?php
25
26// @codeCoverageIgnoreStart
27require_once __DIR__ . '/Maintenance.php';
28// @codeCoverageIgnoreEnd
29
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;
149require_once RUN_MAINTENANCE_IF_MAIN;
150// @codeCoverageIgnoreEnd
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.
getServiceContainer()
Returns the main service container.
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:93
Maintenance script that re-assigns users from an old group to a new one.
execute()
Do the actual work.
__construct()
Default constructor.