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