MediaWiki  master
blockUsers.php
Go to the documentation of this file.
1 <?php
2 
23 require_once __DIR__ . '/Maintenance.php';
24 
27 
28 class 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 = $user->getBlock();
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;
189 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
BlockUsers
Definition: blockUsers.php:28
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:402
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:149
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:480
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:323
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:534
Maintenance\hasArg
hasArg( $argId=0)
Does a given argument exist?
Definition: Maintenance.php:332
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:53
$success
$success
Definition: NoLocalSettings.php:42
MediaWiki\Block\DatabaseBlock
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Definition: DatabaseBlock.php:54
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:241
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:592
BlockUsers\blockUsers
blockUsers( $users, $performer, $reason, $reblock)
Definition: blockUsers.php:121
Maintenance\getHookRunner
getHookRunner()
Get a HookRunner for running core hooks.
Definition: Maintenance.php:1688
BlockUsers\__construct
__construct()
Default constructor.
Definition: blockUsers.php:30
$maintClass
$maintClass
Definition: blockUsers.php:188
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:277
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:294
SpecialBlock\parseExpiryInput
static parseExpiryInput( $expiry)
Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute ("24 May 2034",...
Definition: SpecialBlock.php:1122
NS_USER
const NS_USER
Definition: Defines.php:71
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:38
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:429
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:262
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:347
BlockUsers\execute
execute()
Do the actual work.
Definition: blockUsers.php:70