MediaWiki  REL1_22
resetUserTokens.php
Go to the documentation of this file.
00001 <?php
00027 require_once __DIR__ . '/Maintenance.php';
00028 
00034 class ResetUserTokens extends Maintenance {
00035     public function __construct() {
00036         parent::__construct();
00037         $this->mDescription = "Reset the user_token of all users on the wiki. Note that this may log some of them out.";
00038         $this->addOption( 'nowarn', "Hides the 5 seconds warning", false, false );
00039         $this->addOption( 'nulls', 'Only reset tokens that are currently null (string of \x00\'s)', false, false );
00040         $this->setBatchSize( 1000 );
00041     }
00042 
00043     public function execute() {
00044         $this->nullsOnly = $this->getOption( 'nulls' );
00045 
00046         if ( !$this->getOption( 'nowarn' ) ) {
00047             if ( $this->nullsOnly ) {
00048                 $this->output( "The script is about to reset the user_token for USERS WITH NULL TOKENS in the database.\n" );
00049             } else {
00050                 $this->output( "The script is about to reset the user_token for ALL USERS in the database.\n" );
00051                 $this->output( "This may log some of them out and is not necessary unless you believe your\n" );
00052                 $this->output( "user table has been compromised.\n" );
00053             }
00054             $this->output( "\n" );
00055             $this->output( "Abort with control-c in the next five seconds (skip this countdown with --nowarn) ... " );
00056             wfCountDown( 5 );
00057         }
00058 
00059         // We list user by user_id from one of the slave database
00060         $dbr = wfGetDB( DB_SLAVE );
00061 
00062         $where = array();
00063         if ( $this->nullsOnly ) {
00064             // Have to build this by hand, because \ is escaped in helper functions
00065             $where = array( 'user_token = \'' . str_repeat( '\0', 32) . '\'' );
00066         }
00067 
00068         $maxid = $dbr->selectField( 'user', 'MAX(user_id)', array(), __METHOD__ );
00069 
00070         $min = 0;
00071         $max = $this->mBatchSize;
00072 
00073         do {
00074             $result = $dbr->select( 'user',
00075                 array( 'user_id' ),
00076                 array_merge(
00077                     $where,
00078                     array( 'user_id > ' . $dbr->addQuotes( $min ),
00079                         'user_id <= ' . $dbr->addQuotes( $max )
00080                     )
00081                 ),
00082                 __METHOD__
00083             );
00084 
00085             foreach ( $result as $user ) {
00086                 $this->updateUser( $user->user_id );
00087             }
00088 
00089             $min = $max;
00090             $max = $min + $this->mBatchSize;
00091 
00092             wfWaitForSlaves();
00093 
00094         } while ( $max <= $maxid );
00095 
00096     }
00097 
00098     private function updateUser( $userid ) {
00099         $user = User::newFromId( $userid );
00100         $username = $user->getName();
00101         $this->output( 'Resetting user_token for "' . $username . '": ' );
00102         // Change value
00103         $user->setToken();
00104         $user->saveSettings();
00105         $this->output( " OK\n" );
00106     }
00107 }
00108 
00109 $maintClass = "ResetUserTokens";
00110 require_once RUN_MAINTENANCE_IF_MAIN;