MediaWiki  master
emptyUserGroup.php
Go to the documentation of this file.
1 <?php
2 
25 require_once __DIR__ . '/Maintenance.php';
26 
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  $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;
64 require_once RUN_MAINTENANCE_IF_MAIN;
EmptyUserGroup\__construct
__construct()
Default constructor.
Definition: emptyUserGroup.php:30
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:204
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:330
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:60
EmptyUserGroup\execute
execute()
Do the actual work.
Definition: emptyUserGroup.php:37
$maintClass
$maintClass
Definition: emptyUserGroup.php:63
User\findUsersByGroup
static findUsersByGroup( $groups, $limit=5000, $after=null)
Return the users who are members of the given group(s).
Definition: User.php:951
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:301
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:369
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:436
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:354
Maintenance\setBatchSize
setBatchSize( $s=0)
Definition: Maintenance.php:376
EmptyUserGroup
Definition: emptyUserGroup.php:29