MediaWiki master
emptyUserGroup.php
Go to the documentation of this file.
1<?php
2
30
31// @codeCoverageIgnoreStart
32require_once __DIR__ . '/Maintenance.php';
33// @codeCoverageIgnoreEnd
34
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Remove all users from a given user group' );
39 $this->addArg( 'group', 'Group to be removed', true );
40 $this->addOption(
41 'create-log',
42 'If specified, then log entries are created for each user in the group when emptying the user group.',
43 );
44 $this->addOption(
45 'log-reason',
46 'If create-log is specified, then this is used as the reason for the log entries created for ' .
47 'emptying the user group. If not provided, then the log will have no reason.',
48 false,
49 true
50 );
51 $this->setBatchSize( 100 );
52 }
53
54 public function execute() {
55 $group = $this->getArg( 0 );
56 $userGroupManager = $this->getServiceContainer()->getUserGroupManager();
57
58 $totalCount = 0;
59 $this->output( "Removing users from $group...\n" );
60 while ( true ) {
61 $users = User::findUsersByGroup( $group, $this->getBatchSize() );
62 if ( iterator_count( $users ) === 0 ) {
63 break;
64 }
65
66 foreach ( $users as $user ) {
67 $oldGroups = $userGroupManager->getUserGroups( $user );
68 $groupRemoved = $userGroupManager->removeUserFromGroup( $user, $group );
69
70 if ( $groupRemoved ) {
71 $totalCount += 1;
72 if ( $this->hasOption( 'create-log' ) ) {
73 $this->createLogEntry( $user, $group, $oldGroups );
74 }
75 }
76 }
77
78 $this->waitForReplication();
79 }
80 if ( $totalCount ) {
81 $this->output( " ...done! Removed $totalCount users in total.\n" );
82 } else {
83 $this->output( " ...nothing to do, group was empty.\n" );
84 }
85 }
86
97 private function createLogEntry( UserIdentity $target, string $removedGroup, array $oldGroups ) {
98 $newGroups = $oldGroups;
99 $newGroups = array_diff( $newGroups, [ $removedGroup ] );
100
101 $logEntry = new ManualLogEntry( 'rights', 'rights' );
102 $logEntry->setPerformer( User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ) );
103 $logEntry->setTarget( Title::makeTitle( NS_USER, $target->getName() ) );
104 $logEntry->setComment( $this->getOption( 'log-reason', '' ) );
105 $logEntry->setParameters( [
106 '4::oldgroups' => $oldGroups,
107 '5::newgroups' => $newGroups,
108 ] );
109 $logEntry->insert();
110 }
111}
112
113// @codeCoverageIgnoreStart
114$maintClass = EmptyUserGroup::class;
115require_once RUN_MAINTENANCE_IF_MAIN;
116// @codeCoverageIgnoreEnd
const NS_USER
Definition Defines.php:67
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:78
User class for the MediaWiki software.
Definition User.php:123
$maintClass
Interface for objects representing user identity.