[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/maintenance/ -> resetUserTokens.php (source)

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


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1