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