[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/extensions/Renameuser/ -> renameUserCleanup.php (source)

   1  <?php
   2  /**
   3   * Maintenance script to clean up after incomplete user renames
   4   * Sometimes user edits are left lying around under the old name,
   5   * check for that and assign them to the new username
   6   *
   7   * This program is free software; you can redistribute it and/or modify
   8   * it under the terms of the GNU General Public License as published by
   9   * the Free Software Foundation; either version 2 of the License, or
  10   * (at your option) any later version.
  11   *
  12   * This program is distributed in the hope that it will be useful,
  13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15   * GNU General Public License for more details.
  16   *
  17   * You should have received a copy of the GNU General Public License along
  18   * with this program; if not, write to the Free Software Foundation, Inc.,
  19   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20   * http://www.gnu.org/copyleft/gpl.html
  21   *
  22   * @ingroup Maintenance
  23   * @author Ariel Glenn <[email protected]>
  24   */
  25  
  26  $IP = getenv( 'MW_INSTALL_PATH' );
  27  if ( $IP === false ) {
  28      $IP = __DIR__ . '/../..';
  29  }
  30  require_once( "$IP/maintenance/Maintenance.php" );
  31  
  32  class RenameUserCleanup extends Maintenance {
  33  	public function __construct() {
  34          parent::__construct();
  35          $this->mDescription = "Maintenance script to finish incomplete rename user, in particular to reassign edits that were missed";
  36          $this->addOption( 'olduser', 'Old user name', true, true );
  37          $this->addOption( 'newuser', 'New user name', true, true );
  38          $this->addOption( 'olduid', 'Old user id in revision records (DANGEROUS)', false, true );
  39          $this->mBatchSize = 1000;
  40      }
  41  
  42  	public function execute() {
  43          $this->output( "Rename User Cleanup starting...\n\n" );
  44          $olduser = User::newFromName( $this->getOption( 'olduser' ) );
  45          $newuser = User::newFromName( $this->getOption( 'newuser' ) );
  46          $olduid = $this->getOption( 'olduid' );
  47  
  48          $this->checkUserExistence( $olduser, $newuser );
  49          $this->checkRenameLog( $olduser, $newuser );
  50  
  51          if ( $olduid ) {
  52              $this->doUpdates( $olduser, $newuser, $olduid );
  53          }
  54          $this->doUpdates( $olduser, $newuser, $newuser->getId() );
  55          $this->doUpdates( $olduser, $newuser, 0 );
  56          
  57          print "Done!\n";
  58          exit(0);
  59      }
  60  
  61      /**
  62       * @param $olduser User
  63       * @param $newuser User
  64       */
  65  	public function checkUserExistence( $olduser, $newuser ) {
  66          if ( !$newuser->getId() ) {
  67              $this->error( "No such user: " . $this->getOption( 'newuser' ), true );
  68              exit(1);
  69          }
  70          if ($olduser->getId() ) {
  71              print "WARNING!!: Old user still exists: " . $this->getOption( 'olduser' ) . "\n";
  72              print "proceed anyways? We'll only re-attribute edits that have the new user uid (or 0)";
  73              print " or the uid specified by the caller, and the old user name.  [N/y]   ";
  74              $stdin = fopen ("php://stdin","rt");
  75              $line = fgets($stdin);
  76              fclose($stdin);
  77              if ( $line[0] != "Y" && $line[0] != "y" ) {
  78                  print "Exiting at user's request\n";
  79                  exit(0);
  80              }
  81          }
  82      }
  83  
  84      /**
  85       * @param $olduser User
  86       * @param $newuser User
  87       */
  88  	public function checkRenameLog( $olduser, $newuser ) {
  89          $dbr = wfGetDB( DB_SLAVE );
  90  
  91          $oldTitle = Title::makeTitle( NS_USER, $olduser->getName() );
  92  
  93          $result = $dbr->select( 'logging', '*',
  94              array( 'log_type' => 'renameuser',
  95                  'log_action'    => 'renameuser',
  96                  'log_namespace' => NS_USER,
  97                  'log_title'     => $oldTitle->getDBkey(),
  98                  'log_params'    => $newuser->getName()
  99                   ),
 100              __METHOD__
 101          );
 102          if (! $result || ! $result->numRows() ) {
 103              // try the old format
 104              $result = $dbr->select( 'logging', '*',
 105              array( 'log_type' => 'renameuser',
 106                  'log_action'    => 'renameuser',
 107                  'log_namespace' => NS_USER,
 108                  'log_title'     => $olduser->getName(),
 109                   ),
 110                  __METHOD__
 111              );
 112              if (! $result ||  ! $result->numRows() ) {
 113                  print "No log entry found for a rename of ".$olduser->getName()." to ".$newuser->getName().", proceed anyways??? [N/y] ";
 114                  $stdin = fopen ("php://stdin","rt");
 115                  $line = fgets($stdin);
 116                  fclose($stdin);
 117                  if ( $line[0] != "Y" && $line[0] != "y" ) {
 118                      print "Exiting at user's request\n";
 119                      exit(1);
 120                  }
 121              } else {
 122                  foreach ( $result as $row ) {
 123                      print "Found possible log entry of the rename, please check: ".$row->log_title." with comment ".$row->log_comment." on $row->log_timestamp\n";
 124                  }
 125              }
 126          } else {
 127              foreach ( $result as $row ) {
 128                  print "Found log entry of the rename: ".$olduser->getName()." to ".$newuser->getName()." on $row->log_timestamp\n";
 129              }
 130          }
 131          if ($result && $result->numRows() > 1) {
 132              print "More than one rename entry found in the log, not sure what to do. Continue anyways? [N/y]  ";
 133              $stdin = fopen ("php://stdin","rt");
 134              $line = fgets($stdin);
 135              fclose($stdin);
 136              if ( $line[0] != "Y" && $line[0] != "y" ) {
 137                  print "Exiting at user's request\n";
 138                  exit(1);
 139              }
 140          }
 141      }
 142  
 143      /**
 144       * @param $olduser User
 145       * @param $newuser User
 146       * @param $uid
 147       */
 148  	public function doUpdates( $olduser, $newuser, $uid ) {
 149          $this->updateTable( 'revision', 'rev_user_text', 'rev_user', 'rev_timestamp', $olduser, $newuser, $uid );
 150          $this->updateTable( 'archive', 'ar_user_text', 'ar_user', 'ar_timestamp',  $olduser, $newuser, $uid );
 151          $this->updateTable( 'logging', 'log_user_text', 'log_user', 'log_timestamp', $olduser, $newuser, $uid );
 152          $this->updateTable( 'image', 'img_user_text', 'img_user', 'img_timestamp', $olduser, $newuser, $uid );
 153          $this->updateTable( 'oldimage', 'oi_user_text', 'oi_user', 'oi_timestamp', $olduser, $newuser, $uid );
 154          $this->updateTable( 'filearchive', 'fa_user_text','fa_user', 'fa_timestamp', $olduser, $newuser, $uid );
 155      }
 156  
 157      /**
 158       * @param $table
 159       * @param $usernamefield
 160       * @param $useridfield
 161       * @param $timestampfield
 162       * @param $olduser User
 163       * @param $newuser User
 164       * @param $uid
 165       * @return int
 166       */
 167  	public function updateTable( $table, $usernamefield, $useridfield, $timestampfield, $olduser, $newuser, $uid ) {
 168          $dbw = wfGetDB( DB_MASTER );
 169  
 170          $contribs = $dbw->selectField( $table, 'count(*)',
 171              array( $usernamefield => $olduser->getName(), $useridfield => $uid ), __METHOD__ );
 172  
 173          if ( $contribs == 0 ) {
 174              print "No edits to be re-attributed from table $table for uid $uid\n" ;
 175              return(0);
 176          }
 177  
 178          print "Found $contribs edits to be re-attributed from table $table for uid $uid\n";
 179          if ( $uid != $newuser->getId() ) {
 180              print "If you proceed, the uid field will be set to that of the new user name (i.e. ".$newuser->getId().") in these rows.\n";
 181          }
 182  
 183          print "Proceed? [N/y]  ";
 184          $stdin = fopen ("php://stdin","rt");
 185          $line = fgets($stdin);
 186          fclose($stdin);
 187          if ( $line[0] != "Y" && $line[0] != "y" ) {
 188              print "Skipping at user's request\n";
 189              return(0);
 190          }
 191  
 192          $selectConds = array( $usernamefield => $olduser->getName(), $useridfield => $uid );
 193          $updateFields = array( $usernamefield => $newuser->getName(), $useridfield => $newuser->getId() );
 194  
 195          while ( $contribs > 0 ) {
 196              print "Doing batch of up to approximately ".$this->mBatchSize."\n";
 197              print "Do this batch? [N/y]  ";
 198              $stdin = fopen ("php://stdin","rt");
 199              $line = fgets($stdin);
 200              fclose($stdin);
 201              if ( $line[0] != "Y" && $line[0] != "y" ) {
 202                  print "Skipping at user's request\n";
 203                  return(0);
 204              }
 205              $dbw->begin();
 206              $result = $dbw->select( $table, $timestampfield, $selectConds , __METHOD__,
 207                  array( 'ORDER BY' => $timestampfield.' DESC', 'LIMIT' => $this->mBatchSize ) );
 208              if (! $result) {
 209                  print "There were rows for updating but now they are gone. Skipping.\n";
 210                  $dbw->rollback();
 211                  return(0);
 212              }
 213              $result->seek($result->numRows() -1 );
 214              $row = $result->fetchObject();
 215              $timestamp = $row->$timestampfield;
 216              $updateCondsWithTime = array_merge( $selectConds, array ("$timestampfield >= $timestamp") );
 217              $success = $dbw->update( $table, $updateFields, $updateCondsWithTime, __METHOD__ );
 218              if ( $success ) {
 219                  $rowsDone = $dbw->affectedRows();
 220                  $dbw->commit();
 221              } else {
 222                  print "Problem with the update, rolling back and exiting\n";
 223                  $dbw->rollback();
 224                  exit(1);
 225              }
 226              //$contribs = User::edits( $olduser->getId() );
 227              $contribs = $dbw->selectField( $table, 'count(*)', $selectConds, __METHOD__ );
 228              print "Updated $rowsDone edits; $contribs edits remaining to be re-attributed\n";
 229          }
 230          return(0);
 231      }
 232  
 233  }
 234  
 235  $maintClass = "RenameUserCleanup";
 236  require_once( RUN_MAINTENANCE_IF_MAIN );


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