4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
20 * @ingroup Maintenance
23 // @codeCoverageIgnoreStart
24 require_once __DIR__
. '/Maintenance.php';
25 // @codeCoverageIgnoreEnd
27 use MediaWiki\Maintenance\Maintenance
;
28 use MediaWiki\User\User
;
30 class BlockUsers
extends Maintenance
{
32 public function __construct() {
33 parent
::__construct();
35 $this->addDescription(
36 "Blocks a list of usernames. Can use STDIN or the file argument.\n\n" .
37 'Note, this is probably most useful when dealing with spammers, with a ' .
38 "list you've generated with an SQL query or similar.\n\n" .
39 'By default, all users are hard blocked, auto blocked from any current and subsequent ' .
40 'IP addresses, email disabled, unable to write to their user page and unable to ' .
41 'create further accounts with no expiry to this block. You can change the expiry ' .
42 'with --expiry parameter.'
47 'File with list of users to block',
53 'User to make the blocks',
60 'Reason for the blocks',
67 'Reblock users who are already blocked',
74 'Expiry of your block',
81 'Should this unblock?',
87 'allow-createaccount',
88 'Allow account creation for blocked IPs',
94 'Allow blocked accounts to send emails',
100 'Allow blocked accounts to edit their own talk page',
106 'Don\'t block logged in accounts from a blocked IP address (will still block temporary accounts)',
112 'Don\'t autoblock IP addresses used by the accounts',
117 public function execute() {
118 $performerName = $this->getOption( 'performer', false );
119 $reason = $this->getOption( 'reason', '' );
120 $unblocking = $this->getOption( 'unblock', false );
121 $reblock = $this->hasOption( 'reblock' );
122 $expiry = $this->getOption( 'expiry', 'indefinite' );
124 if ( $performerName ) {
125 $performer = $this->getServiceContainer()->getUserFactory()->newFromName( $performerName );
127 $performer = User
::newSystemUser( User
::MAINTENANCE_SCRIPT_USER
, [ 'steal' => true ] );
130 if ( $performer === null ) {
131 $this->fatalError( "Unable to parse performer's username" );
134 if ( $this->hasArg( 0 ) ) {
135 $file = fopen( $this->getArg( 0 ), 'r' );
137 $file = $this->getStdin();
142 $this->fatalError( "Unable to read file, exiting" );
146 $blockUserFactory = $this->getServiceContainer()->getBlockUserFactory();
147 $unblockUserFactory = $this->getServiceContainer()->getUnblockUserFactory();
148 $action = $unblocking ?
"Unblocking" : "Blocking";
149 for ( $linenum = 1; !feof( $file ); $linenum++
) {
150 $line = trim( fgets( $file ) );
156 $res = $unblockUserFactory->newUnblockUser(
162 $res = $blockUserFactory->newBlockUser(
168 'isCreateAccountBlocked' => !$this->hasOption( 'allow-createaccount' ),
169 'isEmailBlocked' => !$this->hasOption( 'allow-email' ),
170 'isUserTalkEditBlocked' => !$this->hasOption( 'allow-talkedit' ),
171 'isHardBlock' => !$this->hasOption( 'disable-hardblock' ),
172 'isAutoblocking' => !$this->hasOption( 'disable-autoblock' ),
174 )->placeBlockUnsafe( $reblock );
177 if ( $res->isOK() ) {
178 $this->output( "{$action} '{$line}' succeeded.\n" );
180 $errorsText = $res->getMessage()->text();
181 $this->output( "{$action} '{$line}' failed ({$errorsText}).\n" );
187 // @codeCoverageIgnoreStart
188 $maintClass = BlockUsers
::class;
189 require_once RUN_MAINTENANCE_IF_MAIN
;
190 // @codeCoverageIgnoreEnd