MediaWiki  1.34.0
invalidateUserSessions.php
Go to the documentation of this file.
1 <?php
27 
28 require_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  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
69  $sessionManager = SessionManager::singleton();
70  foreach ( $usernames as $username ) {
71  $i++;
72  $user = User::newFromName( $username );
73  try {
74  $sessionManager->invalidateSessionsForUser( $user );
75  if ( $user->getId() ) {
76  $this->output( "Invalidated sessions for user $username\n" );
77  } else {
78  # session invalidation might still work if there is a central identity provider
79  $this->output( "Could not find user $username, tried to invalidate anyway\n" );
80  }
81  } catch ( Exception $e ) {
82  $this->output( "Failed to invalidate sessions for user $username | "
83  . str_replace( [ "\r", "\n" ], ' ', $e->getMessage() ) . "\n" );
84  }
85 
86  if ( $i % $this->getBatchSize() ) {
87  $lbFactory->waitForReplication();
88  }
89  }
90  }
91 }
92 
93 $maintClass = InvalidateUserSesssions::class;
94 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
InvalidateUserSesssions
Invalidate the sessions of certain users on the wiki.
Definition: invalidateUserSessions.php:36
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
InvalidateUserSesssions\__construct
__construct()
Default constructor.
Definition: invalidateUserSessions.php:37
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
MediaWiki\Session\SessionManager
This serves as the entry point to the MediaWiki session handling system.
Definition: SessionManager.php:50
$maintClass
$maintClass
Definition: invalidateUserSessions.php:93
InvalidateUserSesssions\execute
execute()
Do the actual work.
Definition: invalidateUserSessions.php:47
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:394