Debian Codegraph @

maintenance/invalidateUserSessions.php

raw symbols SCIP
1 <?php
2 /**
3 * Invalidate the sessions of certain users on the wiki.
4 * If you want to invalidate all sessions, use $wgAuthenticationTokenVersion instead.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25 use MediaWiki\Session\SessionManager;
26 use MediaWiki\User\User;
27
28 // @codeCoverageIgnoreStart
29 require_once __DIR__ . '/Maintenance.php';
30 // @codeCoverageIgnoreEnd
31
32 /**
33 * Invalidate the sessions of certain users on the wiki.
34 * If you want to invalidate all sessions, use $wgAuthenticationTokenVersion instead.
35 *
36 * @ingroup Maintenance
37 */
38 class InvalidateUserSessions extends Maintenance {
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription(
42 'Invalidate the sessions of certain users on the wiki.'
43 );
44 $this->addOption( 'user', 'Username', false, true, 'u' );
45 $this->addOption( 'file', 'File with one username per line', false, true, 'f' );
46 $this->setBatchSize( 1000 );
47 }
48
49 public function execute() {
50 $username = $this->getOption( 'user' );
51 $file = $this->getOption( 'file' );
52
53 if ( $username === null && $file === null ) {
54 $this->fatalError( 'Either --user or --file is required' );
55 } elseif ( $username !== null && $file !== null ) {
56 $this->fatalError( 'Cannot use both --user and --file' );
57 }
58
59 if ( $username !== null ) {
60 $usernames = [ $username ];
61 } else {
62 $usernames = is_readable( $file ) ?
63 file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) : false;
64 if ( $usernames === false ) {
65 $this->fatalError( "Could not open $file", 2 );
66 }
67 }
68
69 $i = 0;
70 $sessionManager = SessionManager::singleton();
71 foreach ( $usernames as $username ) {
72 $i++;
73 $user = User::newFromName( $username );
74 try {
75 $sessionManager->invalidateSessionsForUser( $user );
76 if ( $user->isRegistered() ) {
77 $this->output( "Invalidated sessions for user $username\n" );
78 } else {
79 # session invalidation might still work if there is a central identity provider
80 $this->output( "Could not find user $username, tried to invalidate anyway\n" );
81 }
82 } catch ( Exception $e ) {
83 $this->output( "Failed to invalidate sessions for user $username | "
84 . str_replace( [ "\r", "\n" ], ' ', $e->getMessage() ) . "\n" );
85 }
86
87 if ( $i % $this->getBatchSize() ) {
88 $this->waitForReplication();
89 }
90 }
91 }
92 }
93
94 // @codeCoverageIgnoreStart
95 $maintClass = InvalidateUserSessions::class;
96 require_once RUN_MAINTENANCE_IF_MAIN;
97 // @codeCoverageIgnoreEnd