MediaWiki REL1_39
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',
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 $performer = null;
122 if ( $performerName ) {
123 $performer = User::newFromName( $performerName );
124 } else {
125 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
126 }
127
128 if ( $performer === null ) {
129 $this->fatalError( "Unable to parse performer's username" );
130 }
131
132 if ( $this->hasArg( 0 ) ) {
133 $file = fopen( $this->getArg( 0 ), 'r' );
134 } else {
135 $file = $this->getStdin();
136 }
137
138 # Setup
139 if ( !$file ) {
140 $this->fatalError( "Unable to read file, exiting" );
141 }
142
143 # Handle each entry
144 $blockUserFactory = MediaWikiServices::getInstance()->getBlockUserFactory();
145 $unblockUserFactory = MediaWikiServices::getInstance()->getUnblockUserFactory();
146 $action = $unblocking ? "Unblocking" : "Blocking";
147 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
148 $line = trim( fgets( $file ) );
149 if ( $line == '' ) {
150 continue;
151 }
152
153 if ( $unblocking ) {
154 $res = $unblockUserFactory->newUnblockUser(
155 $line,
156 $performer,
157 $reason
158 )->unblockUnsafe();
159 } else {
160 $res = $blockUserFactory->newBlockUser(
161 $line,
162 $performer,
163 $expiry,
164 $reason,
165 [
166 'isCreateAccountBlocked' => !$this->hasOption( 'allow-createaccount' ),
167 'isEmailBlocked' => !$this->hasOption( 'allow-email' ),
168 'isUserTalkEditBlocked' => !$this->hasOption( 'allow-talkedit' ),
169 'isHardBlock' => !$this->hasOption( 'disable-hardblock' ),
170 'isAutoblocking' => !$this->hasOption( 'disable-autoblock' ),
171 ]
172 )->placeBlockUnsafe( $reblock );
173 }
174
175 if ( $res->isOK() ) {
176 $this->output( "{$action} '{$line}' succeeded.\n" );
177 } else {
178 $errorsText = $res->getMessage()->text();
179 $this->output( "{$action} '{$line}' failed ({$errorsText}).\n" );
180 }
181 }
182 }
183}
184
185$maintClass = BlockUsers::class;
186require_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)
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.
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.
Service locator for MediaWiki core services.
static newFromName( $name, $validate='valid')
Definition User.php:598
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:806
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition User.php:116
$line
Definition mcc.php:119
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42