00001 <?php
00024 require_once( dirname(__FILE__) . '/Maintenance.php' );
00025
00026 class RollbackEdits extends Maintenance {
00027 public function __construct() {
00028 parent::__construct();
00029 $this->mDescription = "Rollback all edits by a given user or IP provided they're the most recent edit";
00030 $this->addOption( 'titles', 'A list of titles, none means all titles where the given user is the most recent', false, true );
00031 $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
00032 $this->addOption( 'summary', 'Edit summary to use', false, true );
00033 $this->addOption( 'bot', 'Mark the edits as bot' );
00034 }
00035
00036 public function execute() {
00037 $user = $this->getOption( 'user' );
00038 $username = User::isIP( $user ) ? $user : User::getCanonicalName( $user );
00039 if( !$username ) {
00040 $this->error( 'Invalid username', true );
00041 }
00042
00043 $bot = $this->hasOption( 'bot' );
00044 $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
00045 $titles = array();
00046 $results = array();
00047 if( $this->hasOption( 'titles' ) ) {
00048 foreach( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
00049 $t = Title::newFromText( $title );
00050 if( !$t ) {
00051 $this->error( 'Invalid title, ' . $title );
00052 } else {
00053 $titles[] = $t;
00054 }
00055 }
00056 } else {
00057 $titles = $this->getRollbackTitles( $user );
00058 }
00059
00060 if( !$titles ) {
00061 $this->output( 'No suitable titles to be rolled back' );
00062 return;
00063 }
00064
00065 foreach( $titles as $t ) {
00066 $a = new Article( $t );
00067 $this->output( 'Processing ' . $t->getPrefixedText() . '...' );
00068 if( !$a->commitRollback( $user, $summary, $bot, $results ) ) {
00069 $this->output( "done\n" );
00070 } else {
00071 $this->output( "failed\n" );
00072 }
00073 }
00074 }
00075
00080 private function getRollbackTitles( $user ) {
00081 $dbr = wfGetDB( DB_SLAVE );
00082 $titles = array();
00083 $results = $dbr->select(
00084 array( 'page', 'revision' ),
00085 array( 'page_namespace', 'page_title' ),
00086 array( 'page_latest = rev_id', 'rev_user_text' => $user ),
00087 __METHOD__
00088 );
00089 while( $row = $dbr->fetchObject( $results ) ) {
00090 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
00091 }
00092 return $titles;
00093 }
00094 }
00095
00096 $maintClass = 'RollbackEdits';
00097 require_once( DO_MAINTENANCE );