Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 118
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
BlockUsers
0.00% covered (danger)
0.00%
0 / 115
0.00% covered (danger)
0.00%
0 / 2
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2
3/**
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.
8 *
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.
13 *
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
18 *
19 * @file
20 * @ingroup Maintenance
21 */
22
23require_once __DIR__ . '/Maintenance.php';
24
25use MediaWiki\User\User;
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;