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