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