Add a maintenance script to remove all users from a User Group
[lhc/web/wiklou.git] / maintenance / emptyUserGroup.php
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
25 require_once __DIR__ . '/Maintenance.php';
26
27 use MediaWiki\MediaWikiServices;
28
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 }
35
36 public function execute() {
37 $group = $this->getArg( 0 );
38
39 $lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
40
41 $users = User::findUsersByGroup( $group );
42
43 $count = iterator_count( $users );
44
45 $this->output( "Removing $count users from $group..." );
46
47 /**
48 * @var User $user
49 */
50 foreach ( $users as $user ) {
51 $user->removeGroup( $group );
52
53 $lb->waitForReplication();
54 }
55
56 $this->output( " Done!\n" );
57 }
58 }
59
60 $maintClass = EmptyUserGroup::class;
61 require_once RUN_MAINTENANCE_IF_MAIN;