MediaWiki REL1_35
blockUsers.php
Go to the documentation of this file.
1<?php
2
23require_once __DIR__ . '/Maintenance.php';
24
27
28class BlockUsers extends Maintenance {
29
30 public function __construct() {
31 parent::__construct();
32
33 $this->addDescription(
34 "Blocks a list of usernames. Can use STDIN or the file argument.\n\n" .
35 'Note, this is probably most useful when dealing with spammers, with a ' .
36 "list you've generated with an SQL query or similar.\n\n" .
37 'All users are hard blocked, auto blocked from any current and subsequent IP ' .
38 'addresses, email disabled, unable to write to their user page and unable to ' .
39 'create further accounts with no expiry to this block.'
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 true,
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
70 public function execute() {
71 $performerName = $this->getOption( 'performer' );
72 $performer = User::newFromName( $performerName );
73
74 if ( !$performer ) {
75 $this->fatalError( "Username '{$performerName}' isn't valid.\n" );
76 }
77
78 if ( !$performer->getId() ) {
79 $this->fatalError( "User '{$performerName}' doesn't exist.\n" );
80 }
81
82 $permManager = MediaWikiServices::getInstance()->getPermissionManager();
83 if ( !$permManager->userHasRight( $performer, 'block' ) ) {
84 $this->fatalError( "User '{$performerName}' doesn't have blocking rights.\n" );
85 }
86
87 $reblock = $this->hasOption( 'reblock' );
88
89 $users = null;
90 if ( $this->hasArg( 0 ) ) {
91 $users = explode(
92 "\n",
93 trim( file_get_contents( $this->getArg( 0 ) ) )
94 );
95 } else {
96 $stdin = $this->getStdin();
97 $users = [];
98 while ( !feof( $stdin ) ) {
99 $name = trim( fgets( $stdin ) );
100 if ( $name ) {
101 $users[] = $name;
102 }
103 }
104 }
105
106 $this->blockUsers(
107 $users,
108 $performer,
109 $this->getOption( 'reason', '' ),
110 $reblock
111 );
112 }
113
121 private function blockUsers( $users, $performer, $reason, $reblock ) {
122 foreach ( $users as $name ) {
123 $user = User::newFromName( $name );
124
125 // User is invalid, skip
126 if ( !$user || !$user->getId() ) {
127 $this->output( "Blocking '{$name}' skipped (user doesn't exist or is invalid).\n" );
128 continue;
129 }
130
131 $priorBlock = DatabaseBlock::newFromTarget( $user );
132 if ( $priorBlock === null ) {
133 $block = new DatabaseBlock();
134 } elseif ( $reblock ) {
135 $block = clone $priorBlock;
136 } else {
137 $this->output( "Blocking '{$name}' skipped (user already blocked).\n" );
138 continue;
139 }
140
141 $block->setTarget( $name );
142 $block->setBlocker( $performer );
143 $block->setReason( $reason );
144 $block->isHardblock( true );
145 $block->isAutoblocking( true );
146 $block->isCreateAccountBlocked( true );
147 $block->isEmailBlocked( true );
148 $block->isUsertalkEditAllowed( false );
149 $block->setExpiry( SpecialBlock::parseExpiryInput( 'infinity' ) );
150
151 if ( $priorBlock === null ) {
152 $success = $block->insert();
153 } else {
154 $success = $block->update();
155 }
156
157 if ( $success ) {
158 // Fire any post block hooks
159 $this->getHookRunner()->onBlockIpComplete( $block, $performer, $priorBlock );
160 // Log it only if the block was successful
161 $flags = [
162 'nocreate',
163 'noemail',
164 'nousertalk',
165 ];
166 $logParams = [
167 '5::duration' => 'indefinite',
168 '6::flags' => implode( ',', $flags ),
169 ];
170
171 $logEntry = new ManualLogEntry( 'block', 'block' );
172 $logEntry->setTarget( Title::makeTitle( NS_USER, $name ) );
173 $logEntry->setComment( $reason );
174 $logEntry->setPerformer( $performer );
175 $logEntry->setParameters( $logParams );
176 $blockIds = array_merge( [ $success['id'] ], $success['autoIds'] );
177 $logEntry->setRelations( [ 'ipb_id' => $blockIds ] );
178 $logEntry->publish( $logEntry->insert() );
179
180 $this->output( "Blocking '{$name}' succeeded.\n" );
181 } else {
182 $this->output( "Blocking '{$name}' failed.\n" );
183 }
184 }
185 }
186}
187
188$maintClass = BlockUsers::class;
189require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
$maintClass
__construct()
Default constructor.
blockUsers( $users, $performer, $reason, $reblock)
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.
getHookRunner()
Get a HookRunner for running core hooks.
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.
Class for creating new log entries and inserting them into the database.
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
MediaWikiServices is the service locator for the application scope of MediaWiki.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:541
const NS_USER
Definition Defines.php:72