reassignEdits.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Reassign edits from a user or IP address to another user
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. * @author Rob Church <robchur@gmail.com>
  23. * @licence GNU General Public Licence 2.0 or later
  24. */
  25. require_once __DIR__ . '/Maintenance.php';
  26. /**
  27. * Maintenance script that reassigns edits from a user or IP address
  28. * to another user.
  29. *
  30. * @ingroup Maintenance
  31. */
  32. class ReassignEdits extends Maintenance {
  33. public function __construct() {
  34. parent::__construct();
  35. $this->addDescription( 'Reassign edits from one user to another' );
  36. $this->addOption( "force", "Reassign even if the target user doesn't exist" );
  37. $this->addOption( "norc", "Don't update the recent changes table" );
  38. $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
  39. $this->addArg( 'from', 'Old user to take edits from' );
  40. $this->addArg( 'to', 'New user to give edits to' );
  41. }
  42. public function execute() {
  43. if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
  44. # Set up the users involved
  45. $from = $this->initialiseUser( $this->getArg( 0 ) );
  46. $to = $this->initialiseUser( $this->getArg( 1 ) );
  47. # If the target doesn't exist, and --force is not set, stop here
  48. if ( $to->getId() || $this->hasOption( 'force' ) ) {
  49. # Reassign the edits
  50. $report = $this->hasOption( 'report' );
  51. $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
  52. # If reporting, and there were items, advise the user to run without --report
  53. if ( $report ) {
  54. $this->output( "Run the script again without --report to update.\n" );
  55. }
  56. } else {
  57. $ton = $to->getName();
  58. $this->error( "User '{$ton}' not found." );
  59. }
  60. }
  61. }
  62. /**
  63. * Reassign edits from one user to another
  64. *
  65. * @param User $from User to take edits from
  66. * @param User $to User to assign edits to
  67. * @param bool $rc Update the recent changes table
  68. * @param bool $report Don't change things; just echo numbers
  69. * @return int Number of entries changed, or that would be changed
  70. */
  71. private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
  72. $dbw = $this->getDB( DB_MASTER );
  73. $this->beginTransaction( $dbw, __METHOD__ );
  74. # Count things
  75. $this->output( "Checking current edits..." );
  76. $res = $dbw->select(
  77. 'revision',
  78. 'COUNT(*) AS count',
  79. $this->userConditions( $from, 'rev_user', 'rev_user_text' ),
  80. __METHOD__
  81. );
  82. $row = $dbw->fetchObject( $res );
  83. $cur = $row->count;
  84. $this->output( "found {$cur}.\n" );
  85. $this->output( "Checking deleted edits..." );
  86. $res = $dbw->select(
  87. 'archive',
  88. 'COUNT(*) AS count',
  89. $this->userConditions( $from, 'ar_user', 'ar_user_text' ),
  90. __METHOD__
  91. );
  92. $row = $dbw->fetchObject( $res );
  93. $del = $row->count;
  94. $this->output( "found {$del}.\n" );
  95. # Don't count recent changes if we're not supposed to
  96. if ( $rc ) {
  97. $this->output( "Checking recent changes..." );
  98. $res = $dbw->select(
  99. 'recentchanges',
  100. 'COUNT(*) AS count',
  101. $this->userConditions( $from, 'rc_user', 'rc_user_text' ),
  102. __METHOD__
  103. );
  104. $row = $dbw->fetchObject( $res );
  105. $rec = $row->count;
  106. $this->output( "found {$rec}.\n" );
  107. } else {
  108. $rec = 0;
  109. }
  110. $total = $cur + $del + $rec;
  111. $this->output( "\nTotal entries to change: {$total}\n" );
  112. if ( !$report ) {
  113. if ( $total ) {
  114. # Reassign edits
  115. $this->output( "\nReassigning current edits..." );
  116. $dbw->update( 'revision', $this->userSpecification( $to, 'rev_user', 'rev_user_text' ),
  117. $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
  118. $this->output( "done.\nReassigning deleted edits..." );
  119. $dbw->update( 'archive', $this->userSpecification( $to, 'ar_user', 'ar_user_text' ),
  120. $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
  121. $this->output( "done.\n" );
  122. # Update recent changes if required
  123. if ( $rc ) {
  124. $this->output( "Updating recent changes..." );
  125. $dbw->update( 'recentchanges', $this->userSpecification( $to, 'rc_user', 'rc_user_text' ),
  126. $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
  127. $this->output( "done.\n" );
  128. }
  129. }
  130. }
  131. $this->commitTransaction( $dbw, __METHOD__ );
  132. return (int)$total;
  133. }
  134. /**
  135. * Return the most efficient set of user conditions
  136. * i.e. a user => id mapping, or a user_text => text mapping
  137. *
  138. * @param User $user User for the condition
  139. * @param string $idfield Field name containing the identifier
  140. * @param string $utfield Field name containing the user text
  141. * @return array
  142. */
  143. private function userConditions( &$user, $idfield, $utfield ) {
  144. return $user->getId()
  145. ? [ $idfield => $user->getId() ]
  146. : [ $utfield => $user->getName() ];
  147. }
  148. /**
  149. * Return user specifications
  150. * i.e. user => id, user_text => text
  151. *
  152. * @param User $user User for the spec
  153. * @param string $idfield Field name containing the identifier
  154. * @param string $utfield Field name containing the user text
  155. * @return array
  156. */
  157. private function userSpecification( &$user, $idfield, $utfield ) {
  158. return [ $idfield => $user->getId(), $utfield => $user->getName() ];
  159. }
  160. /**
  161. * Initialise the user object
  162. *
  163. * @param string $username Username or IP address
  164. * @return User
  165. */
  166. private function initialiseUser( $username ) {
  167. if ( User::isIP( $username ) ) {
  168. $user = new User();
  169. $user->setId( 0 );
  170. $user->setName( $username );
  171. } else {
  172. $user = User::newFromName( $username );
  173. if ( !$user ) {
  174. $this->error( "Invalid username", true );
  175. }
  176. }
  177. $user->load();
  178. return $user;
  179. }
  180. }
  181. $maintClass = "ReassignEdits";
  182. require_once RUN_MAINTENANCE_IF_MAIN;