MediaWiki  REL1_31
reassignEdits.php
Go to the documentation of this file.
1 <?php
27 
28 require_once __DIR__ . '/Maintenance.php';
29 
36 class ReassignEdits extends Maintenance {
37  public function __construct() {
38  parent::__construct();
39  $this->addDescription( 'Reassign edits from one user to another' );
40  $this->addOption( "force", "Reassign even if the target user doesn't exist" );
41  $this->addOption( "norc", "Don't update the recent changes table" );
42  $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
43  $this->addArg( 'from', 'Old user to take edits from' );
44  $this->addArg( 'to', 'New user to give edits to' );
45  }
46 
47  public function execute() {
48  if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
49  # Set up the users involved
50  $from = $this->initialiseUser( $this->getArg( 0 ) );
51  $to = $this->initialiseUser( $this->getArg( 1 ) );
52 
53  # If the target doesn't exist, and --force is not set, stop here
54  if ( $to->getId() || $this->hasOption( 'force' ) ) {
55  # Reassign the edits
56  $report = $this->hasOption( 'report' );
57  $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
58  # If reporting, and there were items, advise the user to run without --report
59  if ( $report ) {
60  $this->output( "Run the script again without --report to update.\n" );
61  }
62  } else {
63  $ton = $to->getName();
64  $this->error( "User '{$ton}' not found." );
65  }
66  }
67  }
68 
78  private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
80 
81  $dbw = $this->getDB( DB_MASTER );
82  $this->beginTransaction( $dbw, __METHOD__ );
83 
84  # Count things
85  $this->output( "Checking current edits..." );
86  $revQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rev_user', $from );
87  $res = $dbw->select(
88  [ 'revision' ] + $revQueryInfo['tables'],
89  'COUNT(*) AS count',
90  $revQueryInfo['conds'],
91  __METHOD__,
92  [],
93  $revQueryInfo['joins']
94  );
95  $row = $dbw->fetchObject( $res );
96  $cur = $row->count;
97  $this->output( "found {$cur}.\n" );
98 
99  $this->output( "Checking deleted edits..." );
100  $arQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'ar_user', $from, false );
101  $res = $dbw->select(
102  [ 'archive' ] + $arQueryInfo['tables'],
103  'COUNT(*) AS count',
104  $arQueryInfo['conds'],
105  __METHOD__,
106  [],
107  $arQueryInfo['joins']
108  );
109  $row = $dbw->fetchObject( $res );
110  $del = $row->count;
111  $this->output( "found {$del}.\n" );
112 
113  # Don't count recent changes if we're not supposed to
114  if ( $rc ) {
115  $this->output( "Checking recent changes..." );
116  $rcQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rc_user', $from, false );
117  $res = $dbw->select(
118  [ 'recentchanges' ] + $rcQueryInfo['tables'],
119  'COUNT(*) AS count',
120  $rcQueryInfo['conds'],
121  __METHOD__,
122  [],
123  $rcQueryInfo['joins']
124  );
125  $row = $dbw->fetchObject( $res );
126  $rec = $row->count;
127  $this->output( "found {$rec}.\n" );
128  } else {
129  $rec = 0;
130  }
131 
132  $total = $cur + $del + $rec;
133  $this->output( "\nTotal entries to change: {$total}\n" );
134 
135  if ( !$report ) {
136  if ( $total ) {
137  # Reassign edits
138  $this->output( "\nReassigning current edits..." );
139  if ( $wgActorTableSchemaMigrationStage < MIGRATION_NEW ) {
140  $dbw->update(
141  'revision',
142  [
143  'rev_user' => $to->getId(),
144  'rev_user_text' =>
145  $wgActorTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ? $to->getName() : ''
146  ],
147  $from->isLoggedIn()
148  ? [ 'rev_user' => $from->getId() ] : [ 'rev_user_text' => $from->getName() ],
149  __METHOD__
150  );
151  }
153  $dbw->update(
154  'revision_actor_temp',
155  [ 'revactor_actor' => $to->getActorId( $dbw ) ],
156  [ 'revactor_actor' => $from->getActorId() ],
157  __METHOD__
158  );
159  }
160  $this->output( "done.\nReassigning deleted edits..." );
161  $dbw->update( 'archive',
162  $this->userSpecification( $dbw, $to, 'ar_user', 'ar_user_text', 'ar_actor' ),
163  [ $arQueryInfo['conds'] ], __METHOD__ );
164  $this->output( "done.\n" );
165  # Update recent changes if required
166  if ( $rc ) {
167  $this->output( "Updating recent changes..." );
168  $dbw->update( 'recentchanges',
169  $this->userSpecification( $dbw, $to, 'rc_user', 'rc_user_text', 'rc_actor' ),
170  [ $rcQueryInfo['conds'] ], __METHOD__ );
171  $this->output( "done.\n" );
172  }
173  }
174  }
175 
176  $this->commitTransaction( $dbw, __METHOD__ );
177 
178  return (int)$total;
179  }
180 
192  private function userSpecification( IDatabase $dbw, &$user, $idfield, $utfield, $acfield ) {
194 
195  $ret = [];
197  $ret += [
198  $idfield => $user->getId(),
199  $utfield => $wgActorTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ? $user->getName() : '',
200  ];
201  }
203  $ret += [ $acfield => $user->getActorId( $dbw ) ];
204  }
205  return $ret;
206  }
207 
214  private function initialiseUser( $username ) {
215  if ( User::isIP( $username ) ) {
216  $user = new User();
217  $user->setId( 0 );
218  $user->setName( $username );
219  } else {
221  if ( !$user ) {
222  $this->fatalError( "Invalid username" );
223  }
224  }
225  $user->load();
226 
227  return $user;
228  }
229 }
230 
232 require_once RUN_MAINTENANCE_IF_MAIN;
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:247
ReassignEdits
Maintenance script that reassigns edits from a user or IP address to another user.
Definition: reassignEdits.php:36
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:439
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:291
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:2005
MIGRATION_NEW
const MIGRATION_NEW
Definition: Defines.php:305
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance\getName
getName()
Get the script's name.
Definition: Maintenance.php:351
MIGRATION_WRITE_BOTH
const MIGRATION_WRITE_BOTH
Definition: Defines.php:303
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:591
Maintenance\hasArg
hasArg( $argId=0)
Does a given argument exist?
Definition: Maintenance.php:300
ReassignEdits\doReassignEdits
doReassignEdits(&$from, &$to, $rc=false, $report=false)
Reassign edits from one user to another.
Definition: reassignEdits.php:78
$res
$res
Definition: database.txt:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
User
User
Definition: All_system_messages.txt:425
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:89
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
ReassignEdits\initialiseUser
initialiseUser( $username)
Initialise the user object.
Definition: reassignEdits.php:214
ReassignEdits\__construct
__construct()
Default constructor.
Definition: reassignEdits.php:37
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1336
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:219
User\isIP
static isIP( $name)
Does the string match an anonymous IP address?
Definition: User.php:943
ReassignEdits\userSpecification
userSpecification(IDatabase $dbw, &$user, $idfield, $utfield, $acfield)
Return user specifications i.e.
Definition: reassignEdits.php:192
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
DB_MASTER
const DB_MASTER
Definition: defines.php:29
MIGRATION_OLD
const MIGRATION_OLD
Definition: Defines.php:302
$maintClass
$maintClass
Definition: reassignEdits.php:231
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1351
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:271
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1309
$username
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:785
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:416
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:388
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:240
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:310
ReassignEdits\execute
execute()
Do the actual work.
Definition: reassignEdits.php:47
$wgActorTableSchemaMigrationStage
int $wgActorTableSchemaMigrationStage
Actor table schema migration stage.
Definition: DefaultSettings.php:8881