MediaWiki REL1_40
rollbackEdits.php
Go to the documentation of this file.
1<?php
28
29require_once __DIR__ . '/Maintenance.php';
30
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription(
41 "Rollback all edits by a given user or IP provided they're the most recent edit" );
42 $this->addOption(
43 'titles',
44 'A list of titles, none means all titles where the given user is the most recent',
45 false,
46 true
47 );
48 $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
49 $this->addOption( 'summary', 'Edit summary to use', false, true );
50 $this->addOption( 'bot', 'Mark the edits as bot' );
51 }
52
53 public function execute() {
54 $user = $this->getOption( 'user' );
55 $services = MediaWikiServices::getInstance();
56 $userNameUtils = $services->getUserNameUtils();
57 $username = $userNameUtils->isIP( $user ) ? $user : $userNameUtils->getCanonical( $user );
58 if ( !$username ) {
59 $this->fatalError( 'Invalid username' );
60 }
61
62 $bot = $this->hasOption( 'bot' );
63 $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
64 $titles = [];
65 if ( $this->hasOption( 'titles' ) ) {
66 foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
67 $t = Title::newFromText( $title );
68 if ( !$t ) {
69 $this->error( 'Invalid title, ' . $title );
70 } else {
71 $titles[] = $t;
72 }
73 }
74 } else {
75 $titles = $this->getRollbackTitles( $user );
76 }
77
78 if ( !$titles ) {
79 $this->output( 'No suitable titles to be rolled back.' );
80
81 return;
82 }
83
84 $doer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
85 $byUser = $services->getUserIdentityLookup()->getUserIdentityByName( $username );
86
87 if ( !$byUser ) {
88 $this->fatalError( 'Unknown user.' );
89 }
90
91 $wikiPageFactory = $services->getWikiPageFactory();
92 $rollbackPageFactory = $services->getRollbackPageFactory();
93 foreach ( $titles as $t ) {
94 $page = $wikiPageFactory->newFromTitle( $t );
95 $this->output( 'Processing ' . $t->getPrefixedText() . '...' );
96 $rollbackResult = $rollbackPageFactory
97 ->newRollbackPage( $page, $doer, $byUser )
98 ->markAsBot( $bot )
99 ->setSummary( $summary )
100 ->rollback();
101 if ( $rollbackResult->isGood() ) {
102 $this->output( "Done!\n" );
103 } else {
104 $this->output( "Failed!\n" );
105 }
106 }
107 }
108
114 private function getRollbackTitles( $user ) {
115 $dbr = $this->getDB( DB_REPLICA );
116 $titles = [];
117 $actorQuery = ActorMigration::newMigration()
118 ->getWhere( $dbr, 'rev_user', User::newFromName( $user, false ) );
119 $results = $dbr->select(
120 [ 'page', 'revision' ] + $actorQuery['tables'],
121 [ 'page_namespace', 'page_title' ],
122 $actorQuery['conds'],
123 __METHOD__,
124 [],
125 [ 'revision' => [ 'JOIN', 'page_latest = rev_id' ] ] + $actorQuery['joins']
126 );
127 foreach ( $results as $row ) {
128 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
129 }
130
131 return $titles;
132 }
133}
134
135$maintClass = RollbackEdits::class;
136require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
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.
Represents a title within MediaWiki.
Definition Title.php:82
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
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 newFromName( $name, $validate='valid')
Definition User.php:592
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:793
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition User.php:117
const DB_REPLICA
Definition defines.php:26
$maintClass