* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
+ * @file
* @ingroup Maintenance
*/
-require_once( dirname(__FILE__) . '/Maintenance.php' );
+require_once __DIR__ . '/Maintenance.php';
+/**
+ * Maintenance script to rollback all edits by a given user or IP provided
+ * they're the most recent edit.
+ *
+ * @ingroup Maintenance
+ */
class RollbackEdits extends Maintenance {
public function __construct() {
parent::__construct();
public function execute() {
$user = $this->getOption( 'user' );
$username = User::isIP( $user ) ? $user : User::getCanonicalName( $user );
- if( !$username ) {
+ if ( !$username ) {
$this->error( 'Invalid username', true );
}
$summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
$titles = array();
$results = array();
- if( $this->hasOption( 'titles' ) ) {
- foreach( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
+ if ( $this->hasOption( 'titles' ) ) {
+ foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
$t = Title::newFromText( $title );
- if( !$t ) {
+ if ( !$t ) {
$this->error( 'Invalid title, ' . $title );
} else {
$titles[] = $t;
$titles = $this->getRollbackTitles( $user );
}
- if( !$titles ) {
+ 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 ) ) {
+ $doer = User::newFromName( 'Maintenance script' );
+
+ foreach ( $titles as $t ) {
+ $page = WikiPage::factory( $t );
+ $this->output( 'Processing ' . $t->getPrefixedText() . '... ' );
+ if ( !$page->commitRollback( $user, $summary, $bot, $results, $doer ) ) {
$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
+ * @return array
*/
private function getRollbackTitles( $user ) {
$dbr = wfGetDB( DB_SLAVE );
$results = $dbr->select(
array( 'page', 'revision' ),
array( 'page_namespace', 'page_title' ),
- array( 'page.page_latest = revision.rev_id', 'revision.rev_user_text' => $user ),
+ array( 'page_latest = rev_id', 'rev_user_text' => $user ),
__METHOD__
);
- var_dump( __METHOD__ );
- while( $row = $dbr->fetchObject( $results ) ) {
+ foreach ( $results as $row ) {
$titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
}
return $titles;
}
$maintClass = 'RollbackEdits';
-require_once( DO_MAINTENANCE );
+require_once RUN_MAINTENANCE_IF_MAIN;