Debian Codegraph @

maintenance/emptyUserGroup.php

raw symbols SCIP
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 use MediaWiki\User\User;
26
27 // @codeCoverageIgnoreStart
28 require_once __DIR__ . '/Maintenance.php';
29 // @codeCoverageIgnoreEnd
30
31 class EmptyUserGroup extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->addDescription( 'Remove all users from a given user group' );
35 $this->addArg( 'group', 'Group to be removed', true );
36 $this->setBatchSize( 100 );
37 }
38
39 public function execute() {
40 $group = $this->getArg( 0 );
41 $userGroupManager = $this->getServiceContainer()->getUserGroupManager();
42
43 $totalCount = 0;
44 $this->output( "Removing users from $group...\n" );
45 while ( true ) {
46 $users = User::findUsersByGroup( $group, $this->getBatchSize() );
47 if ( iterator_count( $users ) === 0 ) {
48 break;
49 }
50
51 foreach ( $users as $user ) {
52 $totalCount += (int)$userGroupManager->removeUserFromGroup( $user, $group );
53 }
54 $this->waitForReplication();
55 }
56 if ( $totalCount ) {
57 $this->output( " ...done! Removed $totalCount users in total.\n" );
58 } else {
59 $this->output( " ...nothing to do, group was empty.\n" );
60 }
61 }
62 }
63
64 // @codeCoverageIgnoreStart
65 $maintClass = EmptyUserGroup::class;
66 require_once RUN_MAINTENANCE_IF_MAIN;
67 // @codeCoverageIgnoreEnd