| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
use MediaWiki\Title\Title; |
| 26 |
use MediaWiki\User\User; |
| 27 |
|
| 28 |
|
| 29 |
require_once __DIR__ . '/Maintenance.php'; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class RollbackEdits extends Maintenance { |
| 39 |
public function __construct() { |
| 40 |
parent::__construct(); |
| 41 |
$this->addDescription( |
| 42 |
"Rollback all edits by a given user or IP provided they're the most recent edit" ); |
| 43 |
$this->addOption( |
| 44 |
'titles', |
| 45 |
'A list of titles, none means all titles where the given user is the most recent', |
| 46 |
false, |
| 47 |
true |
| 48 |
); |
| 49 |
$this->addOption( 'user', 'A user or IP to rollback all edits for', true, true ); |
| 50 |
$this->addOption( 'summary', 'Edit summary to use', false, true ); |
| 51 |
$this->addOption( 'bot', 'Mark the edits as bot' ); |
| 52 |
} |
| 53 |
|
| 54 |
public function execute() { |
| 55 |
$user = $this->getOption( 'user' ); |
| 56 |
$services = $this->getServiceContainer(); |
| 57 |
$userNameUtils = $services->getUserNameUtils(); |
| 58 |
$user = $userNameUtils->isIP( $user ) ? $user : $userNameUtils->getCanonical( $user ); |
| 59 |
if ( !$user ) { |
| 60 |
$this->fatalError( 'Invalid username' ); |
| 61 |
} |
| 62 |
|
| 63 |
$bot = $this->hasOption( 'bot' ); |
| 64 |
$summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' ); |
| 65 |
$titles = []; |
| 66 |
if ( $this->hasOption( 'titles' ) ) { |
| 67 |
foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) { |
| 68 |
$t = Title::newFromText( $title ); |
| 69 |
if ( !$t ) { |
| 70 |
$this->error( 'Invalid title, ' . $title ); |
| 71 |
} else { |
| 72 |
$titles[] = $t; |
| 73 |
} |
| 74 |
} |
| 75 |
} else { |
| 76 |
$titles = $this->getRollbackTitles( $user ); |
| 77 |
} |
| 78 |
|
| 79 |
if ( !$titles ) { |
| 80 |
$this->output( 'No suitable titles to be rolled back.' ); |
| 81 |
|
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$doer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
| 86 |
$byUser = $services->getUserIdentityLookup()->getUserIdentityByName( $user ); |
| 87 |
|
| 88 |
if ( !$byUser ) { |
| 89 |
$this->fatalError( 'Unknown user.' ); |
| 90 |
} |
| 91 |
|
| 92 |
$wikiPageFactory = $services->getWikiPageFactory(); |
| 93 |
$rollbackPageFactory = $services->getRollbackPageFactory(); |
| 94 |
foreach ( $titles as $t ) { |
| 95 |
$page = $wikiPageFactory->newFromTitle( $t ); |
| 96 |
$this->output( 'Processing ' . $t->getPrefixedText() . '...' ); |
| 97 |
$rollbackResult = $rollbackPageFactory |
| 98 |
->newRollbackPage( $page, $doer, $byUser ) |
| 99 |
->markAsBot( $bot ) |
| 100 |
->setSummary( $summary ) |
| 101 |
->rollback(); |
| 102 |
if ( $rollbackResult->isGood() ) { |
| 103 |
$this->output( "Done!\n" ); |
| 104 |
} else { |
| 105 |
$this->output( "Failed!\n" ); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
private function getRollbackTitles( $user ) { |
| 116 |
$dbr = $this->getReplicaDB(); |
| 117 |
$titles = []; |
| 118 |
|
| 119 |
$results = $dbr->newSelectQueryBuilder() |
| 120 |
->select( [ 'page_namespace', 'page_title' ] ) |
| 121 |
->from( 'page' ) |
| 122 |
->join( 'revision', null, 'page_latest = rev_id' ) |
| 123 |
->join( 'actor', null, 'rev_actor = actor_id' ) |
| 124 |
->where( [ 'actor_name' => $user ] ) |
| 125 |
->caller( __METHOD__ )->fetchResultSet(); |
| 126 |
foreach ( $results as $row ) { |
| 127 |
$titles[] = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 128 |
} |
| 129 |
|
| 130 |
return $titles; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
$maintClass = RollbackEdits::class; |
| 136 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 137 |
|