MediaWiki 1.40.4
invalidateUserSessions.php
Go to the documentation of this file.
1<?php
26
27require_once __DIR__ . '/Maintenance.php';
28
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription(
39 'Invalidate the sessions of certain users on the wiki.'
40 );
41 $this->addOption( 'user', 'Username', false, true, 'u' );
42 $this->addOption( 'file', 'File with one username per line', false, true, 'f' );
43 $this->setBatchSize( 1000 );
44 }
45
46 public function execute() {
47 $username = $this->getOption( 'user' );
48 $file = $this->getOption( 'file' );
49
50 if ( $username === null && $file === null ) {
51 $this->fatalError( 'Either --user or --file is required' );
52 } elseif ( $username !== null && $file !== null ) {
53 $this->fatalError( 'Cannot use both --user and --file' );
54 }
55
56 if ( $username !== null ) {
57 $usernames = [ $username ];
58 } else {
59 $usernames = is_readable( $file ) ?
60 file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) : false;
61 if ( $usernames === false ) {
62 $this->fatalError( "Could not open $file", 2 );
63 }
64 }
65
66 $i = 0;
67 $sessionManager = SessionManager::singleton();
68 foreach ( $usernames as $username ) {
69 $i++;
70 $user = User::newFromName( $username );
71 try {
72 $sessionManager->invalidateSessionsForUser( $user );
73 if ( $user->isRegistered() ) {
74 $this->output( "Invalidated sessions for user $username\n" );
75 } else {
76 # session invalidation might still work if there is a central identity provider
77 $this->output( "Could not find user $username, tried to invalidate anyway\n" );
78 }
79 } catch ( Exception $e ) {
80 $this->output( "Failed to invalidate sessions for user $username | "
81 . str_replace( [ "\r", "\n" ], ' ', $e->getMessage() ) . "\n" );
82 }
83
84 if ( $i % $this->getBatchSize() ) {
85 $this->waitForReplication();
86 }
87 }
88 }
89}
90
91$maintClass = InvalidateUserSessions::class;
92require_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.
static newFromName( $name, $validate='valid')
Definition User.php:592
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42