MediaWiki master
migrateUserGroup.php
Go to the documentation of this file.
1<?php
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
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
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.
getArg( $argId=0, $default=null)
Get an argument.
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DB servers to catch up.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB handle.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
User class for the MediaWiki software.
Definition User.php:119
Maintenance script that re-assigns users from an old group to a new one.
execute()
Do the actual work.
__construct()
Default constructor.