Updating composer/semver (1.5.1 => 1.5.2)
[mediawiki.git] / maintenance / emptyUserGroup.php
blob23bb5aac425c54af76922c08fb94da5bfdc93093
1 <?php
3 /**
4 * Removes all users from a given user group.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
22 * @ingroup Maintenance
25 require_once __DIR__ . '/Maintenance.php';
27 use MediaWiki\MediaWikiServices;
29 class EmptyUserGroup extends Maintenance {
30 public function __construct() {
31 parent::__construct();
32 $this->addDescription( 'Remove all users from a given user group' );
33 $this->addArg( 'group', 'Group to be removed', true );
34 $this->setBatchSize( 100 );
37 public function execute() {
38 $group = $this->getArg( 0 );
39 $lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
40 $userGroupManager = MediaWikiServices::getInstance()->getUserGroupManager();
42 $totalCount = 0;
43 $this->output( "Removing users from $group...\n" );
44 while ( true ) {
45 $users = User::findUsersByGroup( $group, $this->getBatchSize() );
46 if ( iterator_count( $users ) === 0 ) {
47 break;
50 foreach ( $users as $user ) {
51 $totalCount += (int)$userGroupManager->removeUserFromGroup( $user, $group );
53 $lb->waitForReplication();
55 if ( $totalCount ) {
56 $this->output( " ...done! Removed $totalCount users in total.\n" );
57 } else {
58 $this->output( " ...nothing to do, group was empty.\n" );
63 $maintClass = EmptyUserGroup::class;
64 require_once RUN_MAINTENANCE_IF_MAIN;