MediaWiki master
invalidateUserSessions.php
Go to the documentation of this file.
1<?php
14
15// @codeCoverageIgnoreStart
16require_once __DIR__ . '/Maintenance.php';
17// @codeCoverageIgnoreEnd
18
26 public function __construct() {
27 parent::__construct();
28 $this->addDescription(
29 'Invalidate the sessions of certain users on the wiki.'
30 );
31 $this->addOption( 'user', 'Username', false, true, 'u' );
32 $this->addOption( 'file', 'File with one username per line', false, true, 'f' );
33 $this->setBatchSize( 1000 );
34 }
35
36 public function execute() {
37 $username = $this->getOption( 'user' );
38 $file = $this->getOption( 'file' );
39
40 if ( $username === null && $file === null ) {
41 $this->fatalError( 'Either --user or --file is required' );
42 } elseif ( $username !== null && $file !== null ) {
43 $this->fatalError( 'Cannot use both --user and --file' );
44 }
45
46 if ( $username !== null ) {
47 $usernames = [ $username ];
48 } else {
49 $usernames = is_readable( $file ) ?
50 file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) : false;
51 if ( $usernames === false ) {
52 $this->fatalError( "Could not open $file", 2 );
53 }
54 }
55
56 $i = 0;
57 $sessionManager = SessionManager::singleton();
58 foreach ( $usernames as $username ) {
59 $i++;
60 $user = User::newFromName( $username );
61 try {
62 $sessionManager->invalidateSessionsForUser( $user );
63 if ( $user->isRegistered() ) {
64 $this->output( "Invalidated sessions for user $username\n" );
65 } else {
66 # session invalidation might still work if there is a central identity provider
67 $this->output( "Could not find user $username, tried to invalidate anyway\n" );
68 }
69 } catch ( Exception $e ) {
70 $this->output( "Failed to invalidate sessions for user $username | "
71 . str_replace( [ "\r", "\n" ], ' ', $e->getMessage() ) . "\n" );
72 }
73
74 if ( $i % $this->getBatchSize() ) {
75 $this->waitForReplication();
76 }
77 }
78 }
79}
80
81// @codeCoverageIgnoreStart
82$maintClass = InvalidateUserSessions::class;
83require_once RUN_MAINTENANCE_IF_MAIN;
84// @codeCoverageIgnoreEnd
Invalidate the sessions of certain users on the wiki.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
getOption( $name, $default=null)
Get an option, or return the default.
addDescription( $text)
Set the description text.
This serves as the entry point to the MediaWiki session handling system.
User class for the MediaWiki software.
Definition User.php:130