MediaWiki  1.30.0
reassignEdits.php
Go to the documentation of this file.
1 <?php
26 require_once __DIR__ . '/Maintenance.php';
27 
34 class ReassignEdits extends Maintenance {
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription( 'Reassign edits from one user to another' );
38  $this->addOption( "force", "Reassign even if the target user doesn't exist" );
39  $this->addOption( "norc", "Don't update the recent changes table" );
40  $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
41  $this->addArg( 'from', 'Old user to take edits from' );
42  $this->addArg( 'to', 'New user to give edits to' );
43  }
44 
45  public function execute() {
46  if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
47  # Set up the users involved
48  $from = $this->initialiseUser( $this->getArg( 0 ) );
49  $to = $this->initialiseUser( $this->getArg( 1 ) );
50 
51  # If the target doesn't exist, and --force is not set, stop here
52  if ( $to->getId() || $this->hasOption( 'force' ) ) {
53  # Reassign the edits
54  $report = $this->hasOption( 'report' );
55  $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
56  # If reporting, and there were items, advise the user to run without --report
57  if ( $report ) {
58  $this->output( "Run the script again without --report to update.\n" );
59  }
60  } else {
61  $ton = $to->getName();
62  $this->error( "User '{$ton}' not found." );
63  }
64  }
65  }
66 
76  private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
77  $dbw = $this->getDB( DB_MASTER );
78  $this->beginTransaction( $dbw, __METHOD__ );
79 
80  # Count things
81  $this->output( "Checking current edits..." );
82  $res = $dbw->select(
83  'revision',
84  'COUNT(*) AS count',
85  $this->userConditions( $from, 'rev_user', 'rev_user_text' ),
86  __METHOD__
87  );
88  $row = $dbw->fetchObject( $res );
89  $cur = $row->count;
90  $this->output( "found {$cur}.\n" );
91 
92  $this->output( "Checking deleted edits..." );
93  $res = $dbw->select(
94  'archive',
95  'COUNT(*) AS count',
96  $this->userConditions( $from, 'ar_user', 'ar_user_text' ),
97  __METHOD__
98  );
99  $row = $dbw->fetchObject( $res );
100  $del = $row->count;
101  $this->output( "found {$del}.\n" );
102 
103  # Don't count recent changes if we're not supposed to
104  if ( $rc ) {
105  $this->output( "Checking recent changes..." );
106  $res = $dbw->select(
107  'recentchanges',
108  'COUNT(*) AS count',
109  $this->userConditions( $from, 'rc_user', 'rc_user_text' ),
110  __METHOD__
111  );
112  $row = $dbw->fetchObject( $res );
113  $rec = $row->count;
114  $this->output( "found {$rec}.\n" );
115  } else {
116  $rec = 0;
117  }
118 
119  $total = $cur + $del + $rec;
120  $this->output( "\nTotal entries to change: {$total}\n" );
121 
122  if ( !$report ) {
123  if ( $total ) {
124  # Reassign edits
125  $this->output( "\nReassigning current edits..." );
126  $dbw->update( 'revision', $this->userSpecification( $to, 'rev_user', 'rev_user_text' ),
127  $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
128  $this->output( "done.\nReassigning deleted edits..." );
129  $dbw->update( 'archive', $this->userSpecification( $to, 'ar_user', 'ar_user_text' ),
130  $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
131  $this->output( "done.\n" );
132  # Update recent changes if required
133  if ( $rc ) {
134  $this->output( "Updating recent changes..." );
135  $dbw->update( 'recentchanges', $this->userSpecification( $to, 'rc_user', 'rc_user_text' ),
136  $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
137  $this->output( "done.\n" );
138  }
139  }
140  }
141 
142  $this->commitTransaction( $dbw, __METHOD__ );
143 
144  return (int)$total;
145  }
146 
156  private function userConditions( &$user, $idfield, $utfield ) {
157  return $user->getId()
158  ? [ $idfield => $user->getId() ]
159  : [ $utfield => $user->getName() ];
160  }
161 
171  private function userSpecification( &$user, $idfield, $utfield ) {
172  return [ $idfield => $user->getId(), $utfield => $user->getName() ];
173  }
174 
181  private function initialiseUser( $username ) {
182  if ( User::isIP( $username ) ) {
183  $user = new User();
184  $user->setId( 0 );
185  $user->setName( $username );
186  } else {
188  if ( !$user ) {
189  $this->error( "Invalid username", true );
190  }
191  }
192  $user->load();
193 
194  return $user;
195  }
196 }
197 
198 $maintClass = "ReassignEdits";
199 require_once RUN_MAINTENANCE_IF_MAIN;
ReassignEdits
Maintenance script that reassigns edits from a user or IP address to another user.
Definition: reassignEdits.php:34
$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:244
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:550
Maintenance\hasArg
hasArg( $argId=0)
Does a given argument exist?
Definition: Maintenance.php:296
ReassignEdits\doReassignEdits
doReassignEdits(&$from, &$to, $rc=false, $report=false)
Reassign edits from one user to another.
Definition: reassignEdits.php:76
$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
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:35
ReassignEdits\initialiseUser
initialiseUser( $username)
Initialise the user object.
Definition: reassignEdits.php:181
ReassignEdits\__construct
__construct()
Default constructor.
Definition: reassignEdits.php:35
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1278
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
User\isIP
static isIP( $name)
Does the string match an anonymous IP address?
Definition: User.php:825
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$maintClass
$maintClass
Definition: reassignEdits.php:198
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1293
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:267
ReassignEdits\userSpecification
userSpecification(&$user, $idfield, $utfield)
Return user specifications i.e.
Definition: reassignEdits.php:171
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
ReassignEdits\userConditions
userConditions(&$user, $idfield, $utfield)
Return the most efficient set of user conditions i.e.
Definition: reassignEdits.php:156
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:236
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:306
$username
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:781
ReassignEdits\execute
execute()
Do the actual work.
Definition: reassignEdits.php:45