MediaWiki master
emptyUserGroup.php
Go to the documentation of this file.
1<?php
2
16
17// @codeCoverageIgnoreStart
18require_once __DIR__ . '/Maintenance.php';
19// @codeCoverageIgnoreEnd
20
22 public function __construct() {
23 parent::__construct();
24 $this->addDescription( 'Remove all users from a given user group' );
25 $this->addArg( 'group', 'Group to be removed', true );
26 $this->addOption(
27 'create-log',
28 'If specified, then log entries are created for each user in the group when emptying the user group.',
29 );
30 $this->addOption(
31 'log-reason',
32 'If create-log is specified, then this is used as the reason for the log entries created for ' .
33 'emptying the user group. If not provided, then the log will have no reason.',
34 false,
35 true
36 );
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 $oldGroups = $userGroupManager->getUserGroups( $user );
54 $groupRemoved = $userGroupManager->removeUserFromGroup( $user, $group );
55
56 if ( $groupRemoved ) {
57 $totalCount += 1;
58 if ( $this->hasOption( 'create-log' ) ) {
59 $this->createLogEntry( $user, $group, $oldGroups );
60 }
61 }
62 }
63
64 $this->waitForReplication();
65 }
66 if ( $totalCount ) {
67 $this->output( " ...done! Removed $totalCount users in total.\n" );
68 } else {
69 $this->output( " ...nothing to do, group was empty.\n" );
70 }
71 }
72
83 private function createLogEntry( UserIdentity $target, string $removedGroup, array $oldGroups ) {
84 $newGroups = $oldGroups;
85 $newGroups = array_diff( $newGroups, [ $removedGroup ] );
86
87 $logEntry = new ManualLogEntry( 'rights', 'rights' );
88 $logEntry->setPerformer( User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ) );
89 $logEntry->setTarget( Title::makeTitle( NS_USER, $target->getName() ) );
90 $logEntry->setComment( $this->getOption( 'log-reason', '' ) );
91 $logEntry->setParameters( [
92 '4::oldgroups' => $oldGroups,
93 '5::newgroups' => $newGroups,
94 ] );
95 $logEntry->insert();
96 }
97}
98
99// @codeCoverageIgnoreStart
100$maintClass = EmptyUserGroup::class;
101require_once RUN_MAINTENANCE_IF_MAIN;
102// @codeCoverageIgnoreEnd
const NS_USER
Definition Defines.php:53
execute()
Do the actual work.
__construct()
Default constructor.
Class for creating new log entries and inserting them into the database.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:69
User class for the MediaWiki software.
Definition User.php:130
$maintClass
Interface for objects representing user identity.