MediaWiki REL1_37
emptyUserGroup.php
Go to the documentation of this file.
1<?php
2
25require_once __DIR__ . '/Maintenance.php';
26
28
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 );
35 }
36
37 public function execute() {
38 $group = $this->getArg( 0 );
39 $lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
40 $userGroupManager = MediaWikiServices::getInstance()->getUserGroupManager();
41
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;
48 }
49
50 foreach ( $users as $user ) {
51 $totalCount += (int)$userGroupManager->removeUserFromGroup( $user, $group );
52 }
53 $lb->waitForReplication();
54 }
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" );
59 }
60 }
61}
62
63$maintClass = EmptyUserGroup::class;
64require_once RUN_MAINTENANCE_IF_MAIN;
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
getBatchSize()
Returns batch size.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
MediaWikiServices is the service locator for the application scope of MediaWiki.
static findUsersByGroup( $groups, $limit=5000, $after=null)
Return the users who are members of the given group(s).
Definition User.php:1047
$maintClass