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 
84  $wikiPageFactory = $services->getWikiPageFactory();
85  $rollbackPageFactory = $services->getRollbackPageFactory();
86  foreach ( $titles as $t ) {
87  $page = $wikiPageFactory->newFromTitle( $t );
88  $this->output( 'Processing ' . $t->getPrefixedText() . '...' );
89  $rollbackResult = $rollbackPageFactory
90  ->newRollbackPage( $page, $doer, $user )
91  ->markAsBot( $bot )
92  ->setSummary( $summary )
93  ->rollback();
94  if ( $rollbackResult->isGood() ) {
95  $this->output( "Done!\n" );
96  } else {
97  $this->output( "Failed!\n" );
98  }
99  }
100  }
101 
107  private function getRollbackTitles( $user ) {
108  $dbr = $this->getDB( DB_REPLICA );
109  $titles = [];
110  $actorQuery = ActorMigration::newMigration()
111  ->getWhere( $dbr, 'rev_user', User::newFromName( $user, false ) );
112  $results = $dbr->select(
113  [ 'page', 'revision' ] + $actorQuery['tables'],
114  [ 'page_namespace', 'page_title' ],
115  $actorQuery['conds'],
116  __METHOD__,
117  [],
118  [ 'revision' => [ 'JOIN', 'page_latest = rev_id' ] ] + $actorQuery['joins']
119  );
120  foreach ( $results as $row ) {
121  $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
122  }
123 
124  return $titles;
125  }
126 }
127 
128 $maintClass = RollbackEdits::class;
129 require_once RUN_MAINTENANCE_IF_MAIN;
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:383
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
$maintClass
$maintClass
Definition: rollbackEdits.php:128
RollbackEdits\getRollbackTitles
getRollbackTitles( $user)
Get all pages that should be rolled back for a given user.
Definition: rollbackEdits.php:107
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:606
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
RollbackEdits
Maintenance script to rollback all edits by a given user or IP provided they're the most recent edit.
Definition: rollbackEdits.php:35
RollbackEdits\__construct
__construct()
Default constructor.
Definition: rollbackEdits.php:36
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:76
$dbr
$dbr
Definition: testCompression.php:54
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:809
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:651
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
RollbackEdits\execute
execute()
Do the actual work.
Definition: rollbackEdits.php:51
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1362
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:464
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
$t
$t
Definition: testCompression.php:74
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
User\MAINTENANCE_SCRIPT_USER
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition: User.php:121