MediaWiki  REL1_19
rollbackEdits.php
Go to the documentation of this file.
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                 $doer = User::newFromName( 'Maintenance script' );
00066 
00067                 foreach ( $titles as $t ) {
00068                         $page = WikiPage::factory( $t );
00069                         $this->output( 'Processing ' . $t->getPrefixedText() . '... ' );
00070                         if ( !$page->commitRollback( $user, $summary, $bot, $results, $doer ) ) {
00071                                 $this->output( "done\n" );
00072                         } else {
00073                                 $this->output( "failed\n" );
00074                         }
00075                 }
00076         }
00077 
00083         private function getRollbackTitles( $user ) {
00084                 $dbr = wfGetDB( DB_SLAVE );
00085                 $titles = array();
00086                 $results = $dbr->select(
00087                         array( 'page', 'revision' ),
00088                         array( 'page_namespace', 'page_title' ),
00089                         array( 'page_latest = rev_id', 'rev_user_text' => $user ),
00090                         __METHOD__
00091                 );
00092                 foreach ( $results as $row ) {
00093                         $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
00094                 }
00095                 return $titles;
00096         }
00097 }
00098 
00099 $maintClass = 'RollbackEdits';
00100 require_once( RUN_MAINTENANCE_IF_MAIN );