MediaWiki  REL1_22
rollbackEdits.php
Go to the documentation of this file.
00001 <?php
00025 require_once __DIR__ . '/Maintenance.php';
00026 
00033 class RollbackEdits extends Maintenance {
00034     public function __construct() {
00035         parent::__construct();
00036         $this->mDescription = "Rollback all edits by a given user or IP provided they're the most recent edit";
00037         $this->addOption( 'titles', 'A list of titles, none means all titles where the given user is the most recent', false, true );
00038         $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
00039         $this->addOption( 'summary', 'Edit summary to use', false, true );
00040         $this->addOption( 'bot', 'Mark the edits as bot' );
00041     }
00042 
00043     public function execute() {
00044         $user = $this->getOption( 'user' );
00045         $username = User::isIP( $user ) ? $user : User::getCanonicalName( $user );
00046         if ( !$username ) {
00047             $this->error( 'Invalid username', true );
00048         }
00049 
00050         $bot = $this->hasOption( 'bot' );
00051         $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
00052         $titles = array();
00053         $results = array();
00054         if ( $this->hasOption( 'titles' ) ) {
00055             foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
00056                 $t = Title::newFromText( $title );
00057                 if ( !$t ) {
00058                     $this->error( 'Invalid title, ' . $title );
00059                 } else {
00060                     $titles[] = $t;
00061                 }
00062             }
00063         } else {
00064             $titles = $this->getRollbackTitles( $user );
00065         }
00066 
00067         if ( !$titles ) {
00068             $this->output( 'No suitable titles to be rolled back' );
00069             return;
00070         }
00071 
00072         $doer = User::newFromName( 'Maintenance script' );
00073 
00074         foreach ( $titles as $t ) {
00075             $page = WikiPage::factory( $t );
00076             $this->output( 'Processing ' . $t->getPrefixedText() . '... ' );
00077             if ( !$page->commitRollback( $user, $summary, $bot, $results, $doer ) ) {
00078                 $this->output( "done\n" );
00079             } else {
00080                 $this->output( "failed\n" );
00081             }
00082         }
00083     }
00084 
00090     private function getRollbackTitles( $user ) {
00091         $dbr = wfGetDB( DB_SLAVE );
00092         $titles = array();
00093         $results = $dbr->select(
00094             array( 'page', 'revision' ),
00095             array( 'page_namespace', 'page_title' ),
00096             array( 'page_latest = rev_id', 'rev_user_text' => $user ),
00097             __METHOD__
00098         );
00099         foreach ( $results as $row ) {
00100             $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
00101         }
00102         return $titles;
00103     }
00104 }
00105 
00106 $maintClass = 'RollbackEdits';
00107 require_once RUN_MAINTENANCE_IF_MAIN;