Merge "New hook for filters on Special:Contributions form"
[lhc/web/wiklou.git] / maintenance / resetUserTokens.php
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.
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 * @author Daniel Friesen <mediawiki@danielfriesen.name>
24 * @author Chris Steipp <csteipp@wikimedia.org>
25 */
26
27 require_once __DIR__ . '/Maintenance.php';
28
29 /**
30 * Maintenance script to reset the user_token for all users on the wiki.
31 *
32 * @ingroup Maintenance
33 */
34 class ResetUserTokens extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription(
38 'Reset the user_token of all users on the wiki. Note that this may log some of them out.'
39 );
40 $this->addOption( 'nowarn', "Hides the 5 seconds warning", false, false );
41 $this->addOption(
42 'nulls',
43 'Only reset tokens that are currently null (string of \x00\'s)',
44 false,
45 false
46 );
47 $this->setBatchSize( 1000 );
48 }
49
50 public function execute() {
51 $this->nullsOnly = $this->getOption( 'nulls' );
52
53 if ( !$this->getOption( 'nowarn' ) ) {
54 if ( $this->nullsOnly ) {
55 $this->output( "The script is about to reset the user_token "
56 . "for USERS WITH NULL TOKENS in the database.\n" );
57 } else {
58 $this->output( "The script is about to reset the user_token for ALL USERS in the database.\n" );
59 $this->output( "This may log some of them out and is not necessary unless you believe your\n" );
60 $this->output( "user table has been compromised.\n" );
61 }
62 $this->output( "\n" );
63 $this->output( "Abort with control-c in the next five seconds "
64 . "(skip this countdown with --nowarn) ... " );
65 wfCountDown( 5 );
66 }
67
68 // We list user by user_id from one of the slave database
69 $dbr = $this->getDB( DB_SLAVE );
70
71 $where = array();
72 if ( $this->nullsOnly ) {
73 // Have to build this by hand, because \ is escaped in helper functions
74 $where = array( 'user_token = \'' . str_repeat( '\0', 32 ) . '\'' );
75 }
76
77 $maxid = $dbr->selectField( 'user', 'MAX(user_id)', array(), __METHOD__ );
78
79 $min = 0;
80 $max = $this->mBatchSize;
81
82 do {
83 $result = $dbr->select( 'user',
84 array( 'user_id' ),
85 array_merge(
86 $where,
87 array( 'user_id > ' . $dbr->addQuotes( $min ),
88 'user_id <= ' . $dbr->addQuotes( $max )
89 )
90 ),
91 __METHOD__
92 );
93
94 foreach ( $result as $user ) {
95 $this->updateUser( $user->user_id );
96 }
97
98 $min = $max;
99 $max = $min + $this->mBatchSize;
100
101 wfWaitForSlaves();
102 } while ( $min <= $maxid );
103 }
104
105 private function updateUser( $userid ) {
106 $user = User::newFromId( $userid );
107 $username = $user->getName();
108 $this->output( 'Resetting user_token for "' . $username . '": ' );
109 // Change value
110 $user->setToken();
111 $user->saveSettings();
112 $this->output( " OK\n" );
113 }
114 }
115
116 $maintClass = "ResetUserTokens";
117 require_once RUN_MAINTENANCE_IF_MAIN;