2009-09-07 13:18:54 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Re-assign users from an old group to a new one
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-07-25 19:31:06 +00:00
|
|
|
* @file
|
2009-09-07 13:18:54 +00:00
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2009-09-07 13:18:54 +00:00
|
|
|
|
2012-07-25 19:31:06 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script that re-assigns users from an old group to a new one.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-09-07 13:18:54 +00:00
|
|
|
class MigrateUserGroup extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Re-assign users from an old group to a new one' );
|
2009-09-07 13:18:54 +00:00
|
|
|
$this->addArg( 'oldgroup', 'Old user group key', true );
|
|
|
|
|
$this->addArg( 'newgroup', 'New user group key', true );
|
|
|
|
|
$this->setBatchSize( 200 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$count = 0;
|
|
|
|
|
$oldGroup = $this->getArg( 0 );
|
|
|
|
|
$newGroup = $this->getArg( 1 );
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbw = $this->getPrimaryDB();
|
2017-11-05 08:09:51 +00:00
|
|
|
$batchSize = $this->getBatchSize();
|
2024-07-27 08:47:24 +00:00
|
|
|
$userGroupManager = $this->getServiceContainer()->getUserGroupManager();
|
2023-09-21 11:54:38 +00:00
|
|
|
$start = $dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( 'MIN(ug_user)' )
|
|
|
|
|
->from( 'user_groups' )
|
|
|
|
|
->where( [ 'ug_group' => $oldGroup ] )
|
|
|
|
|
->caller( __FUNCTION__ )->fetchField();
|
|
|
|
|
$end = $dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( 'MAX(ug_user)' )
|
|
|
|
|
->from( 'user_groups' )
|
|
|
|
|
->where( [ 'ug_group' => $oldGroup ] )
|
|
|
|
|
->caller( __FUNCTION__ )->fetchField();
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $start === null ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Nothing to do - no users in the '$oldGroup' group" );
|
2009-09-07 13:18:54 +00:00
|
|
|
}
|
|
|
|
|
# Do remaining chunk
|
2017-11-05 08:09:51 +00:00
|
|
|
$end += $batchSize - 1;
|
2009-09-07 13:18:54 +00:00
|
|
|
$blockStart = $start;
|
2017-11-05 08:09:51 +00:00
|
|
|
$blockEnd = $start + $batchSize - 1;
|
2009-09-07 13:18:54 +00:00
|
|
|
// Migrate users over in batches...
|
2010-05-22 16:50:39 +00:00
|
|
|
while ( $blockEnd <= $end ) {
|
2012-09-19 00:24:35 +00:00
|
|
|
$affected = 0;
|
2009-09-07 13:18:54 +00:00
|
|
|
$this->output( "Doing users $blockStart to $blockEnd\n" );
|
2012-09-19 00:24:35 +00:00
|
|
|
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->beginTransaction( $dbw, __METHOD__ );
|
2024-07-27 08:47:24 +00:00
|
|
|
// Find the users already in the new group, so that we can exclude them from the UPDATE query
|
|
|
|
|
// and instead delete the rows.
|
|
|
|
|
$usersAlreadyInNewGroup = $dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( 'ug_user' )
|
|
|
|
|
->from( 'user_groups' )
|
2024-01-16 22:47:08 +00:00
|
|
|
->where( [
|
2024-07-27 08:47:24 +00:00
|
|
|
'ug_group' => $newGroup,
|
2024-04-21 12:24:21 +00:00
|
|
|
$dbw->expr( 'ug_user', '>=', (int)$blockStart ),
|
|
|
|
|
$dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
|
2024-01-16 22:47:08 +00:00
|
|
|
] )
|
2024-07-27 08:47:24 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->fetchFieldValues();
|
|
|
|
|
|
|
|
|
|
// Update the user group for the users which do not already have the new group.
|
|
|
|
|
$updateQueryBuilder = $dbw->newUpdateQueryBuilder()
|
|
|
|
|
->update( 'user_groups' )
|
|
|
|
|
->set( [ 'ug_group' => $newGroup ] )
|
2024-01-02 11:59:05 +00:00
|
|
|
->where( [
|
|
|
|
|
'ug_group' => $oldGroup,
|
2024-04-21 12:24:21 +00:00
|
|
|
$dbw->expr( 'ug_user', '>=', (int)$blockStart ),
|
|
|
|
|
$dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
|
2024-01-02 11:59:05 +00:00
|
|
|
] )
|
2024-07-27 08:47:24 +00:00
|
|
|
->caller( __METHOD__ );
|
|
|
|
|
if ( count( $usersAlreadyInNewGroup ) ) {
|
|
|
|
|
$updateQueryBuilder->where( $dbw->expr( 'ug_user', '!=', $usersAlreadyInNewGroup ) );
|
|
|
|
|
}
|
|
|
|
|
$updateQueryBuilder->execute();
|
2012-09-19 00:24:35 +00:00
|
|
|
$affected += $dbw->affectedRows();
|
2024-07-27 08:47:24 +00:00
|
|
|
|
|
|
|
|
// Delete rows that the UPDATE operation above had to ignore.
|
|
|
|
|
// This happens when a user is in both the old and new group, and as such the UPDATE would have failed.
|
|
|
|
|
if ( count( $usersAlreadyInNewGroup ) ) {
|
|
|
|
|
$dbw->newDeleteQueryBuilder()
|
|
|
|
|
->deleteFrom( 'user_groups' )
|
|
|
|
|
->where( [
|
|
|
|
|
'ug_group' => $oldGroup,
|
|
|
|
|
$dbw->expr( 'ug_user', '=', $usersAlreadyInNewGroup ),
|
|
|
|
|
] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
|
|
|
|
$affected += $dbw->affectedRows();
|
|
|
|
|
}
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->commitTransaction( $dbw, __METHOD__ );
|
2012-09-19 00:24:35 +00:00
|
|
|
|
2017-02-20 22:48:21 +00:00
|
|
|
// Clear cache for the affected users (T42340)
|
2012-09-19 00:24:35 +00:00
|
|
|
if ( $affected > 0 ) {
|
|
|
|
|
// XXX: This also invalidates cache of unaffected users that
|
|
|
|
|
// were in the new group and not in the group.
|
2023-09-21 11:54:38 +00:00
|
|
|
$res = $dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( 'ug_user' )
|
|
|
|
|
->from( 'user_groups' )
|
|
|
|
|
->where( [
|
|
|
|
|
'ug_group' => $newGroup,
|
2024-04-21 12:24:21 +00:00
|
|
|
$dbw->expr( 'ug_user', '>=', (int)$blockStart ),
|
|
|
|
|
$dbw->expr( 'ug_user', '<=', (int)$blockEnd ),
|
2023-09-21 11:54:38 +00:00
|
|
|
] )
|
|
|
|
|
->caller( __METHOD__ )->fetchResultSet();
|
2012-09-19 00:24:35 +00:00
|
|
|
if ( $res !== false ) {
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$user = User::newFromId( $row->ug_user );
|
|
|
|
|
$user->invalidateCache();
|
2024-07-27 08:47:24 +00:00
|
|
|
$userGroupManager->clearCache( $user );
|
2012-09-19 00:24:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$count += $affected;
|
2017-11-05 08:09:51 +00:00
|
|
|
$blockStart += $batchSize;
|
|
|
|
|
$blockEnd += $batchSize;
|
2009-09-07 13:18:54 +00:00
|
|
|
}
|
2012-09-19 00:24:35 +00:00
|
|
|
$this->output( "Done! $count users in group '$oldGroup' are now in '$newGroup' instead.\n" );
|
2009-09-07 13:18:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = MigrateUserGroup::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|