Added more Setup.php profiling
[mediawiki.git] / maintenance / resetUserTokens.php
blobbfe04d793d188fefa74e41b4a089852e1aacaec4
1 <?php
2 /**
3 * Reset the user_token for all users on the wiki. Useful if you believe
4 * that your user table was acidentally leaked to an external source.
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.
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.
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
21 * @file
22 * @ingroup Maintenance
23 * @author Daniel Friesen <mediawiki@danielfriesen.name>
24 * @author Chris Steipp <csteipp@wikimedia.org>
27 require_once __DIR__ . '/Maintenance.php';
29 /**
30 * Maintenance script to reset the user_token for all users on the wiki.
32 * @ingroup Maintenance
34 class ResetUserTokens extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->mDescription = "Reset the user_token of all users on the wiki. Note that this may log some of them out.";
38 $this->addOption( 'nowarn', "Hides the 5 seconds warning", false, false );
39 $this->addOption( 'nulls', 'Only reset tokens that are currently null (string of \x00\'s)', false, false );
40 $this->setBatchSize( 1000 );
43 public function execute() {
44 $this->nullsOnly = $this->getOption( 'nulls' );
46 if ( !$this->getOption( 'nowarn' ) ) {
47 if ( $this->nullsOnly ) {
48 $this->output( "The script is about to reset the user_token for USERS WITH NULL TOKENS in the database.\n" );
49 } else {
50 $this->output( "The script is about to reset the user_token for ALL USERS in the database.\n" );
51 $this->output( "This may log some of them out and is not necessary unless you believe your\n" );
52 $this->output( "user table has been compromised.\n" );
54 $this->output( "\n" );
55 $this->output( "Abort with control-c in the next five seconds (skip this countdown with --nowarn) ... " );
56 wfCountDown( 5 );
59 // We list user by user_id from one of the slave database
60 $dbr = wfGetDB( DB_SLAVE );
62 $where = array();
63 if ( $this->nullsOnly ) {
64 // Have to build this by hand, because \ is escaped in helper functions
65 $where = array( 'user_token = \'' . str_repeat( '\0', 32) . '\'' );
68 $maxid = $dbr->selectField( 'user', 'MAX(user_id)', array(), __METHOD__ );
70 $min = 0;
71 $max = $this->mBatchSize;
73 do {
74 $result = $dbr->select( 'user',
75 array( 'user_id' ),
76 array_merge(
77 $where,
78 array( 'user_id > ' . $dbr->addQuotes( $min ),
79 'user_id <= ' . $dbr->addQuotes( $max )
82 __METHOD__
85 foreach ( $result as $user ) {
86 $this->updateUser( $user->user_id );
89 $min = $max;
90 $max = $min + $this->mBatchSize;
92 wfWaitForSlaves();
94 } while ( $max <= $maxid );
98 private function updateUser( $userid ) {
99 $user = User::newFromId( $userid );
100 $username = $user->getName();
101 $this->output( 'Resetting user_token for "' . $username . '": ' );
102 // Change value
103 $user->setToken();
104 $user->saveSettings();
105 $this->output( " OK\n" );
109 $maintClass = "ResetUserTokens";
110 require_once RUN_MAINTENANCE_IF_MAIN;