2009-09-01 23:08:54 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Rollback all edits by a given user or IP provided they're the most
|
|
|
|
|
* recent edit (just like real rollback)
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-08-11 20:48:09 +00:00
|
|
|
* @file
|
2009-09-01 23:08:54 +00:00
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2020-11-12 20:36:35 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2009-09-01 23:08:54 +00:00
|
|
|
|
2012-08-11 20:48:09 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script to rollback all edits by a given user or IP provided
|
|
|
|
|
* they're the most recent edit.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-09-01 23:08:54 +00:00
|
|
|
class RollbackEdits extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription(
|
|
|
|
|
"Rollback all edits by a given user or IP provided they're the most recent edit" );
|
2014-04-22 20:55:50 +00:00
|
|
|
$this->addOption(
|
|
|
|
|
'titles',
|
|
|
|
|
'A list of titles, none means all titles where the given user is the most recent',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
2009-09-01 23:08:54 +00:00
|
|
|
$this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
|
|
|
|
|
$this->addOption( 'summary', 'Edit summary to use', false, true );
|
|
|
|
|
$this->addOption( 'bot', 'Mark the edits as bot' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$user = $this->getOption( 'user' );
|
2023-08-31 09:21:12 +00:00
|
|
|
$services = $this->getServiceContainer();
|
2021-05-28 13:38:53 +00:00
|
|
|
$userNameUtils = $services->getUserNameUtils();
|
2024-08-15 09:59:57 +00:00
|
|
|
$user = $userNameUtils->isIP( $user ) ? $user : $userNameUtils->getCanonical( $user );
|
|
|
|
|
if ( !$user ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( 'Invalid username' );
|
2009-09-01 23:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bot = $this->hasOption( 'bot' );
|
|
|
|
|
$summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$titles = [];
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->hasOption( 'titles' ) ) {
|
|
|
|
|
foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
|
2009-09-01 23:08:54 +00:00
|
|
|
$t = Title::newFromText( $title );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$t ) {
|
2009-09-01 23:08:54 +00:00
|
|
|
$this->error( 'Invalid title, ' . $title );
|
|
|
|
|
} else {
|
|
|
|
|
$titles[] = $t;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$titles = $this->getRollbackTitles( $user );
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$titles ) {
|
2019-01-10 13:47:45 +00:00
|
|
|
$this->output( 'No suitable titles to be rolled back.' );
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2009-09-01 23:08:54 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 21:34:26 +00:00
|
|
|
$doer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
|
2024-08-15 09:59:57 +00:00
|
|
|
$byUser = $services->getUserIdentityLookup()->getUserIdentityByName( $user );
|
2022-06-28 07:18:21 +00:00
|
|
|
|
|
|
|
|
if ( !$byUser ) {
|
|
|
|
|
$this->fatalError( 'Unknown user.' );
|
|
|
|
|
}
|
2012-01-14 14:11:08 +00:00
|
|
|
|
2021-04-12 14:45:26 +00:00
|
|
|
$wikiPageFactory = $services->getWikiPageFactory();
|
|
|
|
|
$rollbackPageFactory = $services->getRollbackPageFactory();
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $titles as $t ) {
|
2020-11-12 20:36:35 +00:00
|
|
|
$page = $wikiPageFactory->newFromTitle( $t );
|
2021-07-21 16:34:05 +00:00
|
|
|
$this->output( 'Processing ' . $t->getPrefixedText() . '...' );
|
2021-03-26 22:56:39 +00:00
|
|
|
$rollbackResult = $rollbackPageFactory
|
2022-06-28 07:18:21 +00:00
|
|
|
->newRollbackPage( $page, $doer, $byUser )
|
2021-03-26 22:56:39 +00:00
|
|
|
->markAsBot( $bot )
|
|
|
|
|
->setSummary( $summary )
|
|
|
|
|
->rollback();
|
|
|
|
|
if ( $rollbackResult->isGood() ) {
|
2019-01-10 13:47:45 +00:00
|
|
|
$this->output( "Done!\n" );
|
2009-09-01 23:08:54 +00:00
|
|
|
} else {
|
2019-01-10 13:47:45 +00:00
|
|
|
$this->output( "Failed!\n" );
|
2009-09-01 23:08:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all pages that should be rolled back for a given user
|
2017-09-12 17:12:29 +00:00
|
|
|
* @param string $user A name to check against
|
2011-10-18 17:32:20 +00:00
|
|
|
* @return array
|
2009-09-01 23:08:54 +00:00
|
|
|
*/
|
|
|
|
|
private function getRollbackTitles( $user ) {
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbr = $this->getReplicaDB();
|
2016-02-17 09:09:32 +00:00
|
|
|
$titles = [];
|
2023-09-26 18:40:35 +00:00
|
|
|
|
|
|
|
|
$results = $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'page_namespace', 'page_title' ] )
|
|
|
|
|
->from( 'page' )
|
|
|
|
|
->join( 'revision', null, 'page_latest = rev_id' )
|
|
|
|
|
->join( 'actor', null, 'rev_actor = actor_id' )
|
|
|
|
|
->where( [ 'actor_name' => $user ] )
|
|
|
|
|
->caller( __METHOD__ )->fetchResultSet();
|
2018-09-18 18:21:20 +00:00
|
|
|
foreach ( $results as $row ) {
|
|
|
|
|
$titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
|
2009-09-01 23:08:54 +00:00
|
|
|
}
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2009-09-01 23:08:54 +00:00
|
|
|
return $titles;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = RollbackEdits::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|