MediaWiki master
rollbackEdits.php
Go to the documentation of this file.
1<?php
28
29// @codeCoverageIgnoreStart
30require_once __DIR__ . '/Maintenance.php';
31// @codeCoverageIgnoreEnd
32
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription(
43 "Rollback all edits by a given user or IP provided they're the most recent edit" );
44 $this->addOption(
45 'titles',
46 'A list of titles, none means all titles where the given user is the most recent',
47 false,
48 true
49 );
50 $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
51 $this->addOption( 'summary', 'Edit summary to use', false, true );
52 $this->addOption( 'bot', 'Mark the edits as bot' );
53 $this->setBatchSize( 10 );
54 }
55
56 public function execute() {
57 $user = $this->getOption( 'user' );
58 $services = $this->getServiceContainer();
59 $userNameUtils = $services->getUserNameUtils();
60 $user = $userNameUtils->isIP( $user ) ? $user : $userNameUtils->getCanonical( $user );
61 if ( !$user ) {
62 $this->fatalError( 'Invalid username' );
63 }
64
65 $bot = $this->hasOption( 'bot' );
66 $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
67 $titles = [];
68 if ( $this->hasOption( 'titles' ) ) {
69 foreach ( explode( '|', $this->getOption( 'titles' ) ) as $text ) {
70 $title = Title::newFromText( $text );
71 if ( !$title ) {
72 $this->error( 'Invalid title, ' . $text );
73 } else {
74 $titles[] = $title;
75 }
76 }
77 } else {
78 $titles = $this->getRollbackTitles( $user );
79 }
80
81 if ( !$titles ) {
82 $this->output( 'No suitable titles to be rolled back.' );
83
84 return;
85 }
86
87 $doer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
88 $byUser = $services->getUserIdentityLookup()->getUserIdentityByName( $user );
89
90 if ( !$byUser ) {
91 $this->fatalError( 'Unknown user.' );
92 }
93
94 $wikiPageFactory = $services->getWikiPageFactory();
95 $rollbackPageFactory = $services->getRollbackPageFactory();
96
98 $titleBatches = $this->newBatchIterator( $titles );
99
100 foreach ( $titleBatches as $titleBatch ) {
101 foreach ( $titleBatch as $title ) {
102 $page = $wikiPageFactory->newFromTitle( $title );
103 $this->output( 'Processing ' . $title->getPrefixedText() . '...' );
104
105 $this->beginTransactionRound( __METHOD__ );
106 $rollbackResult = $rollbackPageFactory
107 ->newRollbackPage( $page, $doer, $byUser )
108 ->markAsBot( $bot )
109 ->setSummary( $summary )
110 ->rollback();
111 $this->commitTransactionRound( __METHOD__ );
112
113 if ( $rollbackResult->isGood() ) {
114 $this->output( "Done!\n" );
115 } else {
116 $this->output( "Failed!\n" );
117 }
118 }
119 }
120 }
121
127 private function getRollbackTitles( $user ) {
128 $dbr = $this->getReplicaDB();
129 $titles = [];
130
131 $results = $dbr->newSelectQueryBuilder()
132 ->select( [ 'page_namespace', 'page_title' ] )
133 ->from( 'page' )
134 ->join( 'revision', null, 'page_latest = rev_id' )
135 ->join( 'actor', null, 'rev_actor = actor_id' )
136 ->where( [ 'actor_name' => $user ] )
137 ->caller( __METHOD__ )->fetchResultSet();
138 foreach ( $results as $row ) {
139 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
140 }
141
142 return $titles;
143 }
144}
145
146// @codeCoverageIgnoreStart
147$maintClass = RollbackEdits::class;
148require_once RUN_MAINTENANCE_IF_MAIN;
149// @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.
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:78
internal since 1.36
Definition User.php:93
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