X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/8989532d3de45b196373107c7a812a68ac0ff2d9..d75ce11339b35963b5f8c3d53190819c1c025716:/maintenance/rollbackEdits.php diff --git a/maintenance/rollbackEdits.php b/maintenance/rollbackEdits.php new file mode 100644 index 00000000..5d6a80a4 --- /dev/null +++ b/maintenance/rollbackEdits.php @@ -0,0 +1,97 @@ +mDescription = "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->error( 'Invalid username', true ); + } + + $bot = $this->hasOption( 'bot' ); + $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' ); + $titles = array(); + $results = array(); + 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; + } + + foreach( $titles as $t ) { + $a = new Article( $t ); + $this->output( 'Processing ' . $t->getPrefixedText() . '...' ); + if( !$a->commitRollback( $user, $summary, $bot, $results ) ) { + $this->output( "done\n" ); + } else { + $this->output( "failed\n" ); + } + } + } + + /** + * Get all pages that should be rolled back for a given user + * @param $user String a name to check against rev_user_text + */ + private function getRollbackTitles( $user ) { + $dbr = wfGetDB( DB_SLAVE ); + $titles = array(); + $results = $dbr->select( + array( 'page', 'revision' ), + array( 'page_namespace', 'page_title' ), + array( 'page_latest = rev_id', 'rev_user_text' => $user ), + __METHOD__ + ); + while( $row = $dbr->fetchObject( $results ) ) { + $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title ); + } + return $titles; + } +} + +$maintClass = 'RollbackEdits'; +require_once( DO_MAINTENANCE );