MediaWiki  master
rollbackEdits.php
Go to the documentation of this file.
1 <?php
26 
27 require_once __DIR__ . '/Maintenance.php';
28 
35 class RollbackEdits extends Maintenance {
36  public function __construct() {
37  parent::__construct();
38  $this->addDescription(
39  "Rollback all edits by a given user or IP provided they're the most recent edit" );
40  $this->addOption(
41  'titles',
42  'A list of titles, none means all titles where the given user is the most recent',
43  false,
44  true
45  );
46  $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
47  $this->addOption( 'summary', 'Edit summary to use', false, true );
48  $this->addOption( 'bot', 'Mark the edits as bot' );
49  }
50 
51  public function execute() {
52  $user = $this->getOption( 'user' );
53  $services = MediaWikiServices::getInstance();
54  $userNameUtils = $services->getUserNameUtils();
55  $username = $userNameUtils->isIP( $user ) ? $user : $userNameUtils->getCanonical( $user );
56  if ( !$username ) {
57  $this->fatalError( 'Invalid username' );
58  }
59 
60  $bot = $this->hasOption( 'bot' );
61  $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
62  $titles = [];
63  if ( $this->hasOption( 'titles' ) ) {
64  foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
66  if ( !$t ) {
67  $this->error( 'Invalid title, ' . $title );
68  } else {
69  $titles[] = $t;
70  }
71  }
72  } else {
73  $titles = $this->getRollbackTitles( $user );
74  }
75 
76  if ( !$titles ) {
77  $this->output( 'No suitable titles to be rolled back.' );
78 
79  return;
80  }
81 
82  $doer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
83  $byUser = $services->getUserIdentityLookup()->getUserIdentityByName( $username );
84 
85  if ( !$byUser ) {
86  $this->fatalError( 'Unknown user.' );
87  }
88 
89  $wikiPageFactory = $services->getWikiPageFactory();
90  $rollbackPageFactory = $services->getRollbackPageFactory();
91  foreach ( $titles as $t ) {
92  $page = $wikiPageFactory->newFromTitle( $t );
93  $this->output( 'Processing ' . $t->getPrefixedText() . '...' );
94  $rollbackResult = $rollbackPageFactory
95  ->newRollbackPage( $page, $doer, $byUser )
96  ->markAsBot( $bot )
97  ->setSummary( $summary )
98  ->rollback();
99  if ( $rollbackResult->isGood() ) {
100  $this->output( "Done!\n" );
101  } else {
102  $this->output( "Failed!\n" );
103  }
104  }
105  }
106 
112  private function getRollbackTitles( $user ) {
113  $dbr = $this->getDB( DB_REPLICA );
114  $titles = [];
115  $actorQuery = ActorMigration::newMigration()
116  ->getWhere( $dbr, 'rev_user', User::newFromName( $user, false ) );
117  $results = $dbr->select(
118  [ 'page', 'revision' ] + $actorQuery['tables'],
119  [ 'page_namespace', 'page_title' ],
120  $actorQuery['conds'],
121  __METHOD__,
122  [],
123  [ 'revision' => [ 'JOIN', 'page_latest = rev_id' ] ] + $actorQuery['joins']
124  );
125  foreach ( $results as $row ) {
126  $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
127  }
128 
129  return $titles;
130  }
131 }
132 
133 $maintClass = RollbackEdits::class;
134 require_once RUN_MAINTENANCE_IF_MAIN;
static newMigration()
Static constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
Maintenance script to rollback all edits by a given user or IP provided they're the most recent edit.
__construct()
Default constructor.
execute()
Do the actual work.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:373
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:641
static newFromName( $name, $validate='valid')
Definition: User.php:587
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:795
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition: User.php:116
const DB_REPLICA
Definition: defines.php:26
$maintClass