2013-09-23 14:39:16 +00:00
|
|
|
<?php
|
2017-10-19 07:40:22 +00:00
|
|
|
|
2021-01-02 13:49:41 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Maintenance;
|
|
|
|
|
2021-01-17 11:54:43 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2022-09-29 16:54:36 +00:00
|
|
|
$IP = getenv( 'MW_INSTALL_PATH' );
|
|
|
|
if ( $IP === false ) {
|
2016-01-06 20:17:41 +00:00
|
|
|
$IP = __DIR__ . '/../../..';
|
2013-09-23 14:39:16 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
require_once "$IP/maintenance/Maintenance.php";
|
2021-01-17 11:54:43 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2013-09-23 14:39:16 +00:00
|
|
|
|
2021-01-02 13:49:41 +00:00
|
|
|
use LoggedUpdateMaintenance;
|
|
|
|
use ManualLogEntry;
|
2021-01-01 17:28:36 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseFilter;
|
2024-06-08 19:19:49 +00:00
|
|
|
use MediaWiki\User\UserIdentityValue;
|
2024-04-29 20:15:35 +00:00
|
|
|
use Wikimedia\Rdbms\IExpression;
|
|
|
|
use Wikimedia\Rdbms\LikeValue;
|
2019-03-29 10:00:18 +00:00
|
|
|
|
2013-09-23 14:39:16 +00:00
|
|
|
/**
|
2020-09-30 10:42:34 +00:00
|
|
|
* @codeCoverageIgnore
|
2022-09-29 16:54:36 +00:00
|
|
|
* No need to test old single-use script.
|
2013-09-23 14:39:16 +00:00
|
|
|
*/
|
2019-08-11 16:14:09 +00:00
|
|
|
class AddMissingLoggingEntries extends LoggedUpdateMaintenance {
|
2016-12-12 21:29:51 +00:00
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
|
2019-12-18 16:57:52 +00:00
|
|
|
$this->addDescription( 'Add missing logging entries for abusefilter-modify T54919' );
|
2019-08-11 16:14:09 +00:00
|
|
|
$this->addOption( 'dry-run', 'Perform a dry run' );
|
|
|
|
$this->addOption( 'verbose', 'Print a list of affected afh_id' );
|
2018-02-10 00:45:16 +00:00
|
|
|
$this->requireExtension( 'Abuse Filter' );
|
2016-12-12 21:29:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
2019-08-11 16:14:09 +00:00
|
|
|
* @inheritDoc
|
2018-04-04 21:14:25 +00:00
|
|
|
*/
|
2019-08-11 16:14:09 +00:00
|
|
|
public function getUpdateKey() {
|
2021-01-02 13:49:41 +00:00
|
|
|
return 'AddMissingLoggingEntries';
|
2019-08-11 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function doDBUpdates() {
|
|
|
|
$dryRun = $this->hasOption( 'dry-run' );
|
2017-06-15 14:23:34 +00:00
|
|
|
$logParams = [];
|
|
|
|
$afhRows = [];
|
2023-03-03 17:32:00 +00:00
|
|
|
$db = $this->getDB( DB_REPLICA, 'vslow' );
|
2013-09-23 14:39:16 +00:00
|
|
|
|
2019-08-11 16:14:09 +00:00
|
|
|
$logParamsConcat = $db->buildConcat( [ 'afh_id', $db->addQuotes( "\n" ) ] );
|
2024-04-29 20:15:35 +00:00
|
|
|
$legacyParamsLike = new LikeValue( $logParamsConcat, $db->anyString() );
|
2019-08-11 16:14:09 +00:00
|
|
|
// Non-legacy entries are a serialized array with 'newId' and 'historyId' keys
|
2024-04-29 20:15:35 +00:00
|
|
|
$newLogParamsLike = new LikeValue( $db->anyString(), 'historyId', $db->anyString() );
|
2015-09-28 18:03:35 +00:00
|
|
|
// Find all entries in abuse_filter_history without logging entry of same timestamp
|
2024-04-29 20:15:35 +00:00
|
|
|
$afhResult = $db->newSelectQueryBuilder()
|
2024-06-08 19:19:49 +00:00
|
|
|
->select( [ 'afh_id', 'afh_filter', 'afh_timestamp', 'afh_deleted', 'actor_user', 'actor_name' ] )
|
2024-04-29 20:15:35 +00:00
|
|
|
->from( 'abuse_filter_history' )
|
2024-06-08 19:19:49 +00:00
|
|
|
->join( 'actor', null, [ 'actor_id = afh_actor' ] )
|
2024-04-29 20:15:35 +00:00
|
|
|
->leftJoin( 'logging', null, [
|
|
|
|
'afh_timestamp = log_timestamp',
|
|
|
|
$db->expr( 'log_params', IExpression::LIKE, $legacyParamsLike ),
|
|
|
|
'log_type' => 'abusefilter',
|
|
|
|
] )
|
|
|
|
->where( [
|
|
|
|
'log_id' => null,
|
|
|
|
$db->expr( 'log_params', IExpression::NOT_LIKE, $newLogParamsLike ),
|
|
|
|
] )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchResultSet();
|
2013-09-23 14:39:16 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
// Because the timestamp matches aren't exact (sometimes a couple of
|
|
|
|
// seconds off), we need to check all our results and ignore those that
|
|
|
|
// do actually have log entries
|
2013-09-23 14:39:16 +00:00
|
|
|
foreach ( $afhResult as $row ) {
|
2015-08-22 22:28:18 +00:00
|
|
|
$logParams[] = $row->afh_id . "\n" . $row->afh_filter;
|
2013-09-23 14:39:16 +00:00
|
|
|
$afhRows[$row->afh_id] = $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !count( $afhRows ) ) {
|
2019-08-11 16:14:09 +00:00
|
|
|
$this->output( "Nothing to do.\n" );
|
|
|
|
return !$dryRun;
|
2013-09-23 14:39:16 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 20:15:35 +00:00
|
|
|
$logResult = $this->getDB( DB_REPLICA )->newSelectQueryBuilder()
|
|
|
|
->select( 'log_params' )
|
|
|
|
->from( 'logging' )
|
|
|
|
->where( [ 'log_type' => 'abusefilter', 'log_params' => $logParams ] )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchFieldValues();
|
2013-09-23 14:39:16 +00:00
|
|
|
|
2023-03-03 17:32:00 +00:00
|
|
|
foreach ( $logResult as $params ) {
|
2019-08-11 16:14:09 +00:00
|
|
|
// id . "\n" . filter
|
2023-03-03 17:32:00 +00:00
|
|
|
$afhId = explode( "\n", $params, 2 )[0];
|
2015-09-28 18:03:35 +00:00
|
|
|
// Forget this row had any issues - it just has a different timestamp in the log
|
|
|
|
unset( $afhRows[$afhId] );
|
2013-09-23 14:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !count( $afhRows ) ) {
|
2019-08-11 16:14:09 +00:00
|
|
|
$this->output( "Nothing to do.\n" );
|
|
|
|
return !$dryRun;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $dryRun ) {
|
|
|
|
$msg = count( $afhRows ) . " rows to insert.";
|
|
|
|
if ( $this->hasOption( 'verbose' ) ) {
|
|
|
|
$msg .= " Affected IDs (afh_id):\n" . implode( ', ', array_keys( $afhRows ) );
|
|
|
|
}
|
|
|
|
$this->output( "$msg\n" );
|
|
|
|
return false;
|
2013-09-23 14:39:16 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 17:32:00 +00:00
|
|
|
$dbw = $this->getDB( DB_PRIMARY );
|
2017-08-30 17:06:44 +00:00
|
|
|
|
2013-09-23 14:39:16 +00:00
|
|
|
$count = 0;
|
|
|
|
foreach ( $afhRows as $row ) {
|
2018-08-26 08:34:42 +00:00
|
|
|
if ( $count % 100 === 0 ) {
|
2023-03-03 17:32:00 +00:00
|
|
|
$this->waitForReplication();
|
2013-09-23 14:39:16 +00:00
|
|
|
}
|
2024-06-08 19:19:49 +00:00
|
|
|
$user = new UserIdentityValue( (int)( $row->actor_user ?? 0 ), $row->actor_name );
|
2019-12-18 16:57:52 +00:00
|
|
|
|
2021-01-03 11:12:16 +00:00
|
|
|
// This copies the code in FilterStore
|
2019-12-18 16:57:52 +00:00
|
|
|
$logEntry = new ManualLogEntry( 'abusefilter', 'modify' );
|
|
|
|
$logEntry->setPerformer( $user );
|
2020-10-03 17:13:32 +00:00
|
|
|
$logEntry->setTarget( SpecialAbuseFilter::getTitleForSubpage( $row->afh_filter ) );
|
2019-12-18 16:57:52 +00:00
|
|
|
// Use the new format!
|
|
|
|
$logEntry->setParameters( [
|
|
|
|
'historyId' => $row->afh_id,
|
|
|
|
'newId' => $row->afh_filter
|
|
|
|
] );
|
|
|
|
$logEntry->setTimestamp( $row->afh_timestamp );
|
|
|
|
$logEntry->insert( $dbw );
|
|
|
|
|
2013-09-23 14:39:16 +00:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
|
2019-08-11 16:14:09 +00:00
|
|
|
$this->output( "Inserted $count rows.\n" );
|
|
|
|
return true;
|
2013-09-23 14:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 00:41:40 +00:00
|
|
|
$maintClass = AddMissingLoggingEntries::class;
|
2013-09-23 14:39:16 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|