Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
EmptyUserGroup
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3/**
4 * Removes all users from a given user group.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25use MediaWiki\Maintenance\Maintenance;
26use MediaWiki\User\User;
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
32class EmptyUserGroup extends Maintenance {
33    public function __construct() {
34        parent::__construct();
35        $this->addDescription( 'Remove all users from a given user group' );
36        $this->addArg( 'group', 'Group to be removed', true );
37        $this->setBatchSize( 100 );
38    }
39
40    public function execute() {
41        $group = $this->getArg( 0 );
42        $userGroupManager = $this->getServiceContainer()->getUserGroupManager();
43
44        $totalCount = 0;
45        $this->output( "Removing users from $group...\n" );
46        while ( true ) {
47            $users = User::findUsersByGroup( $group, $this->getBatchSize() );
48            if ( iterator_count( $users ) === 0 ) {
49                break;
50            }
51
52            foreach ( $users as $user ) {
53                $totalCount += (int)$userGroupManager->removeUserFromGroup( $user, $group );
54            }
55            $this->waitForReplication();
56        }
57        if ( $totalCount ) {
58            $this->output( "  ...done! Removed $totalCount users in total.\n" );
59        } else {
60            $this->output( "  ...nothing to do, group was empty.\n" );
61        }
62    }
63}
64
65// @codeCoverageIgnoreStart
66$maintClass = EmptyUserGroup::class;
67require_once RUN_MAINTENANCE_IF_MAIN;
68// @codeCoverageIgnoreEnd