addDescription( "Rollback all edits by a given user or IP provided they're the most recent edit" ); $this->addOption( 'titles', 'A list of titles, none means all titles where the given user is the most recent', false, true ); $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true ); $this->addOption( 'summary', 'Edit summary to use', false, true ); $this->addOption( 'bot', 'Mark the edits as bot' ); } public function execute() { $user = $this->getOption( 'user' ); $username = User::isIP( $user ) ? $user : User::getCanonicalName( $user ); if ( !$username ) { $this->fatalError( 'Invalid username' ); } $bot = $this->hasOption( 'bot' ); $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' ); $titles = []; $results = []; if ( $this->hasOption( 'titles' ) ) { foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) { $t = Title::newFromText( $title ); if ( !$t ) { $this->error( 'Invalid title, ' . $title ); } else { $titles[] = $t; } } } else { $titles = $this->getRollbackTitles( $user ); } if ( !$titles ) { $this->output( 'No suitable titles to be rolled back' ); return; } $doer = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] ); foreach ( $titles as $t ) { $page = WikiPage::factory( $t ); $this->output( 'Processing ' . $t->getPrefixedText() . '... ' ); if ( !$page->commitRollback( $user, $summary, $bot, $results, $doer ) ) { $this->output( "done\n" ); } else { $this->output( "failed\n" ); } } } /** * Get all pages that should be rolled back for a given user * @param string $user A name to check against rev_user_text * @return array */ private function getRollbackTitles( $user ) { $dbr = $this->getDB( DB_REPLICA ); $titles = []; $results = $dbr->select( [ 'page', 'revision' ], [ 'page_namespace', 'page_title' ], [ 'page_latest = rev_id', 'rev_user_text' => $user ], __METHOD__ ); foreach ( $results as $row ) { $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title ); } return $titles; } } $maintClass = RollbackEdits::class; require_once RUN_MAINTENANCE_IF_MAIN;