3 * Rollback all edits by a given user or IP provided they're the most
4 * recent edit (just like real rollback)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 require_once( dirname( __FILE__
) . '/Maintenance.php' );
26 class RollbackEdits
extends Maintenance
{
27 public function __construct() {
28 parent
::__construct();
29 $this->mDescription
= "Rollback all edits by a given user or IP provided they're the most recent edit";
30 $this->addOption( 'titles', 'A list of titles, none means all titles where the given user is the most recent', false, true );
31 $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
32 $this->addOption( 'summary', 'Edit summary to use', false, true );
33 $this->addOption( 'bot', 'Mark the edits as bot' );
36 public function execute() {
37 $user = $this->getOption( 'user' );
38 $username = User
::isIP( $user ) ?
$user : User
::getCanonicalName( $user );
40 $this->error( 'Invalid username', true );
43 $bot = $this->hasOption( 'bot' );
44 $summary = $this->getOption( 'summary', $this->mSelf
. ' mass rollback' );
47 if ( $this->hasOption( 'titles' ) ) {
48 foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
49 $t = Title
::newFromText( $title );
51 $this->error( 'Invalid title, ' . $title );
57 $titles = $this->getRollbackTitles( $user );
61 $this->output( 'No suitable titles to be rolled back' );
65 foreach ( $titles as $t ) {
66 $a = new Article( $t );
67 $this->output( 'Processing ' . $t->getPrefixedText() . '...' );
68 if ( !$a->commitRollback( $user, $summary, $bot, $results ) ) {
69 $this->output( "done\n" );
71 $this->output( "failed\n" );
77 * Get all pages that should be rolled back for a given user
78 * @param $user String a name to check against rev_user_text
81 private function getRollbackTitles( $user ) {
82 $dbr = wfGetDB( DB_SLAVE
);
84 $results = $dbr->select(
85 array( 'page', 'revision' ),
86 array( 'page_namespace', 'page_title' ),
87 array( 'page_latest = rev_id', 'rev_user_text' => $user ),
90 foreach ( $results as $row ) {
91 $titles[] = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
97 $maintClass = 'RollbackEdits';
98 require_once( RUN_MAINTENANCE_IF_MAIN
);