MediaWiki master
migrateUserGroup.php
Go to the documentation of this file.
1<?php
25
26require_once __DIR__ . '/Maintenance.php';
27
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 "ug_user BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd
78 ] )
79 ->caller( __METHOD__ )->execute();
80 $affected += $dbw->affectedRows();
81 // Delete rows that the UPDATE operation above had to ignore.
82 // This happens when a user is in both the old and new group.
83 // Updating the row for the old group membership failed since
84 // user/group is UNIQUE.
85 $dbw->newDeleteQueryBuilder()
86 ->deleteFrom( 'user_groups' )
87 ->where( [
88 'ug_group' => $oldGroup,
89 "ug_user BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd
90 ] )
91 ->caller( __METHOD__ )->execute();
92 $affected += $dbw->affectedRows();
93 $this->commitTransaction( $dbw, __METHOD__ );
94
95 // Clear cache for the affected users (T42340)
96 if ( $affected > 0 ) {
97 // XXX: This also invalidates cache of unaffected users that
98 // were in the new group and not in the group.
99 $res = $dbw->newSelectQueryBuilder()
100 ->select( 'ug_user' )
101 ->from( 'user_groups' )
102 ->where( [
103 'ug_group' => $newGroup,
104 "ug_user BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd
105 ] )
106 ->caller( __METHOD__ )->fetchResultSet();
107 if ( $res !== false ) {
108 foreach ( $res as $row ) {
109 $user = User::newFromId( $row->ug_user );
110 $user->invalidateCache();
111 }
112 }
113 }
114
115 $count += $affected;
116 $blockStart += $batchSize;
117 $blockEnd += $batchSize;
118 }
119 $this->output( "Done! $count users in group '$oldGroup' are now in '$newGroup' instead.\n" );
120 }
121}
122
123$maintClass = MigrateUserGroup::class;
124require_once RUN_MAINTENANCE_IF_MAIN;
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.
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.