MediaWiki master
rollbackEdits.php
Go to the documentation of this file.
1<?php
14
15// @codeCoverageIgnoreStart
16require_once __DIR__ . '/Maintenance.php';
17// @codeCoverageIgnoreEnd
18
26 public function __construct() {
27 parent::__construct();
28 $this->addDescription(
29 "Rollback all edits by a given user or IP provided they're the most recent edit" );
30 $this->addOption(
31 'titles',
32 'A list of titles, none means all titles where the given user is the most recent',
33 false,
34 true
35 );
36 $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
37 $this->addOption( 'summary', 'Edit summary to use', false, true );
38 $this->addOption( 'bot', 'Mark the edits as bot' );
39 $this->setBatchSize( 10 );
40 }
41
42 public function execute() {
43 $user = $this->getOption( 'user' );
44 $services = $this->getServiceContainer();
45 $userNameUtils = $services->getUserNameUtils();
46 $user = $userNameUtils->isIP( $user ) ? $user : $userNameUtils->getCanonical( $user );
47 if ( !$user ) {
48 $this->fatalError( 'Invalid username' );
49 }
50
51 $bot = $this->hasOption( 'bot' );
52 $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
53 $titles = [];
54 if ( $this->hasOption( 'titles' ) ) {
55 foreach ( explode( '|', $this->getOption( 'titles' ) ) as $text ) {
56 $title = Title::newFromText( $text );
57 if ( !$title ) {
58 $this->error( 'Invalid title, ' . $text );
59 } else {
60 $titles[] = $title;
61 }
62 }
63 } else {
64 $titles = $this->getRollbackTitles( $user );
65 }
66
67 if ( !$titles ) {
68 $this->output( 'No suitable titles to be rolled back.' );
69
70 return;
71 }
72
73 $doer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
74 $byUser = $services->getUserIdentityLookup()->getUserIdentityByName( $user );
75
76 if ( !$byUser ) {
77 $this->fatalError( 'Unknown user.' );
78 }
79
80 $wikiPageFactory = $services->getWikiPageFactory();
81 $rollbackPageFactory = $services->getRollbackPageFactory();
82
84 $titleBatches = $this->newBatchIterator( $titles );
85
86 foreach ( $titleBatches as $titleBatch ) {
87 foreach ( $titleBatch as $title ) {
88 $page = $wikiPageFactory->newFromTitle( $title );
89 $this->output( 'Processing ' . $title->getPrefixedText() . '...' );
90
91 $this->beginTransactionRound( __METHOD__ );
92 $rollbackResult = $rollbackPageFactory
93 ->newRollbackPage( $page, $doer, $byUser )
94 ->markAsBot( $bot )
95 ->setSummary( $summary )
96 ->rollback();
97 $this->commitTransactionRound( __METHOD__ );
98
99 if ( $rollbackResult->isGood() ) {
100 $this->output( "Done!\n" );
101 } else {
102 $this->output( "Failed!\n" );
103 }
104 }
105 }
106 }
107
113 private function getRollbackTitles( $user ) {
114 $dbr = $this->getReplicaDB();
115 $titles = [];
116
117 $results = $dbr->newSelectQueryBuilder()
118 ->select( [ 'page_namespace', 'page_title' ] )
119 ->from( 'page' )
120 ->join( 'revision', null, 'page_latest = rev_id' )
121 ->join( 'actor', null, 'rev_actor = actor_id' )
122 ->where( [ 'actor_name' => $user ] )
123 ->caller( __METHOD__ )->fetchResultSet();
124 foreach ( $results as $row ) {
125 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
126 }
127
128 return $titles;
129 }
130}
131
132// @codeCoverageIgnoreStart
133$maintClass = RollbackEdits::class;
134require_once RUN_MAINTENANCE_IF_MAIN;
135// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
newBatchIterator( $source)
Wrap an entry iterator into a generator that returns batches of said entries.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
getReplicaDB(string|false $virtualDomain=false)
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:69
User class for the MediaWiki software.
Definition User.php:130
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.
$maintClass