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