MediaWiki 1.42.0
blockUsers.php
Go to the documentation of this file.
1<?php
2
23require_once __DIR__ . '/Maintenance.php';
24
26
27class BlockUsers extends Maintenance {
28
29 public function __construct() {
30 parent::__construct();
31
32 $this->addDescription(
33 "Blocks a list of usernames. Can use STDIN or the file argument.\n\n" .
34 'Note, this is probably most useful when dealing with spammers, with a ' .
35 "list you've generated with an SQL query or similar.\n\n" .
36 'By default, all users are hard blocked, auto blocked from any current and subsequent ' .
37 'IP addresses, email disabled, unable to write to their user page and unable to ' .
38 'create further accounts with no expiry to this block. You can change the expiry ' .
39 'with --expiry parameter.'
40 );
41
42 $this->addArg(
43 'file',
44 'File with list of users to block',
45 false
46 );
47
48 $this->addOption(
49 'performer',
50 'User to make the blocks',
51 false,
52 true
53 );
54
55 $this->addOption(
56 'reason',
57 'Reason for the blocks',
58 false,
59 true
60 );
61
62 $this->addOption(
63 'reblock',
64 'Reblock users who are already blocked',
65 false,
66 false
67 );
68
69 $this->addOption(
70 'expiry',
71 'Expiry of your block',
72 false,
73 true
74 );
75
76 $this->addOption(
77 'unblock',
78 'Should this unblock?',
79 false,
80 false
81 );
82
83 $this->addOption(
84 'allow-createaccount',
85 'Allow account creation for blocked IPs',
86 false
87 );
88
89 $this->addOption(
90 'allow-email',
91 'Allow blocked accounts to send emails',
92 false
93 );
94
95 $this->addOption(
96 'allow-talkedit',
97 'Allow blocked accounts to edit their own talk page',
98 false
99 );
100
101 $this->addOption(
102 'disable-hardblock',
103 'Don\'t block logged in accounts from a blocked IP address (will still block temporary accounts)',
104 false
105 );
106
107 $this->addOption(
108 'disable-autoblock',
109 'Don\'t autoblock IP addresses used by the accounts',
110 false
111 );
112 }
113
114 public function execute() {
115 $performerName = $this->getOption( 'performer', false );
116 $reason = $this->getOption( 'reason', '' );
117 $unblocking = $this->getOption( 'unblock', false );
118 $reblock = $this->hasOption( 'reblock' );
119 $expiry = $this->getOption( 'expiry', 'indefinite' );
120
121 if ( $performerName ) {
122 $performer = User::newFromName( $performerName );
123 } else {
124 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
125 }
126
127 if ( $performer === null ) {
128 $this->fatalError( "Unable to parse performer's username" );
129 }
130
131 if ( $this->hasArg( 0 ) ) {
132 $file = fopen( $this->getArg( 0 ), 'r' );
133 } else {
134 $file = $this->getStdin();
135 }
136
137 # Setup
138 if ( !$file ) {
139 $this->fatalError( "Unable to read file, exiting" );
140 }
141
142 # Handle each entry
143 $blockUserFactory = $this->getServiceContainer()->getBlockUserFactory();
144 $unblockUserFactory = $this->getServiceContainer()->getUnblockUserFactory();
145 $action = $unblocking ? "Unblocking" : "Blocking";
146 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
147 $line = trim( fgets( $file ) );
148 if ( $line == '' ) {
149 continue;
150 }
151
152 if ( $unblocking ) {
153 $res = $unblockUserFactory->newUnblockUser(
154 $line,
155 $performer,
156 $reason
157 )->unblockUnsafe();
158 } else {
159 $res = $blockUserFactory->newBlockUser(
160 $line,
161 $performer,
162 $expiry,
163 $reason,
164 [
165 'isCreateAccountBlocked' => !$this->hasOption( 'allow-createaccount' ),
166 'isEmailBlocked' => !$this->hasOption( 'allow-email' ),
167 'isUserTalkEditBlocked' => !$this->hasOption( 'allow-talkedit' ),
168 'isHardBlock' => !$this->hasOption( 'disable-hardblock' ),
169 'isAutoblocking' => !$this->hasOption( 'disable-autoblock' ),
170 ]
171 )->placeBlockUnsafe( $reblock );
172 }
173
174 if ( $res->isOK() ) {
175 $this->output( "{$action} '{$line}' succeeded.\n" );
176 } else {
177 $errorsText = $res->getMessage()->text();
178 $this->output( "{$action} '{$line}' failed ({$errorsText}).\n" );
179 }
180 }
181 }
182}
183
184$maintClass = BlockUsers::class;
185require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
__construct()
Default constructor.
execute()
Do the actual work.
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.
output( $out, $channel=null)
Throw some output to the user.
getStdin( $len=null)
Return input from stdin.
hasArg( $argId=0)
Does a given argument exist?
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
internal since 1.36
Definition User.php:93