MediaWiki master
invalidateUserSessions.php
Go to the documentation of this file.
1<?php
27
28require_once __DIR__ . '/Maintenance.php';
29
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription(
40 'Invalidate the sessions of certain users on the wiki.'
41 );
42 $this->addOption( 'user', 'Username', false, true, 'u' );
43 $this->addOption( 'file', 'File with one username per line', false, true, 'f' );
44 $this->setBatchSize( 1000 );
45 }
46
47 public function execute() {
48 $username = $this->getOption( 'user' );
49 $file = $this->getOption( 'file' );
50
51 if ( $username === null && $file === null ) {
52 $this->fatalError( 'Either --user or --file is required' );
53 } elseif ( $username !== null && $file !== null ) {
54 $this->fatalError( 'Cannot use both --user and --file' );
55 }
56
57 if ( $username !== null ) {
58 $usernames = [ $username ];
59 } else {
60 $usernames = is_readable( $file ) ?
61 file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) : false;
62 if ( $usernames === false ) {
63 $this->fatalError( "Could not open $file", 2 );
64 }
65 }
66
67 $i = 0;
68 $sessionManager = SessionManager::singleton();
69 foreach ( $usernames as $username ) {
70 $i++;
71 $user = User::newFromName( $username );
72 try {
73 $sessionManager->invalidateSessionsForUser( $user );
74 if ( $user->isRegistered() ) {
75 $this->output( "Invalidated sessions for user $username\n" );
76 } else {
77 # session invalidation might still work if there is a central identity provider
78 $this->output( "Could not find user $username, tried to invalidate anyway\n" );
79 }
80 } catch ( Exception $e ) {
81 $this->output( "Failed to invalidate sessions for user $username | "
82 . str_replace( [ "\r", "\n" ], ' ', $e->getMessage() ) . "\n" );
83 }
84
85 if ( $i % $this->getBatchSize() ) {
86 $this->waitForReplication();
87 }
88 }
89 }
90}
91
92$maintClass = InvalidateUserSessions::class;
93require_once RUN_MAINTENANCE_IF_MAIN;
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...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getBatchSize()
Returns batch size.
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.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
This serves as the entry point to the MediaWiki session handling system.
internal since 1.36
Definition User.php:93