2020-11-07 11:40:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter;
|
|
|
|
|
2020-12-18 14:05:33 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesRegistry;
|
2020-11-07 11:40:26 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\Filter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This service allows comparing two versions of a filter.
|
|
|
|
* @todo We might want to expand this to cover the use case of ViewDiff
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
class FilterCompare {
|
|
|
|
public const SERVICE_NAME = 'AbuseFilterFilterCompare';
|
|
|
|
|
2020-11-29 23:10:14 +00:00
|
|
|
/** @var ConsequencesRegistry */
|
|
|
|
private $consequencesRegistry;
|
2020-11-07 11:40:26 +00:00
|
|
|
|
|
|
|
/**
|
2020-11-29 23:10:14 +00:00
|
|
|
* @param ConsequencesRegistry $consequencesRegistry
|
2020-11-07 11:40:26 +00:00
|
|
|
*/
|
2020-11-29 23:10:14 +00:00
|
|
|
public function __construct( ConsequencesRegistry $consequencesRegistry ) {
|
|
|
|
$this->consequencesRegistry = $consequencesRegistry;
|
2020-11-07 11:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Filter $firstFilter
|
|
|
|
* @param Filter $secondFilter
|
|
|
|
* @return array Fields that are different
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function compareVersions( Filter $firstFilter, Filter $secondFilter ): array {
|
2020-11-07 11:40:26 +00:00
|
|
|
// TODO: Avoid DB references here, re-add when saving the filter
|
|
|
|
$methods = [
|
|
|
|
'af_public_comments' => 'getName',
|
|
|
|
'af_pattern' => 'getRules',
|
|
|
|
'af_comments' => 'getComments',
|
|
|
|
'af_deleted' => 'isDeleted',
|
|
|
|
'af_enabled' => 'isEnabled',
|
|
|
|
'af_hidden' => 'isHidden',
|
|
|
|
'af_global' => 'isGlobal',
|
|
|
|
'af_group' => 'getGroup',
|
|
|
|
];
|
|
|
|
|
|
|
|
$differences = [];
|
|
|
|
|
|
|
|
foreach ( $methods as $field => $method ) {
|
|
|
|
if ( $firstFilter->$method() !== $secondFilter->$method() ) {
|
|
|
|
$differences[] = $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$firstActions = $firstFilter->getActions();
|
|
|
|
$secondActions = $secondFilter->getActions();
|
2020-11-29 23:10:14 +00:00
|
|
|
foreach ( $this->consequencesRegistry->getAllEnabledActionNames() as $action ) {
|
2020-11-07 11:40:26 +00:00
|
|
|
if ( !isset( $firstActions[$action] ) && !isset( $secondActions[$action] ) ) {
|
|
|
|
// They're both unset
|
|
|
|
} elseif ( isset( $firstActions[$action] ) && isset( $secondActions[$action] ) ) {
|
|
|
|
// They're both set. Double check needed, e.g. per T180194
|
|
|
|
if ( array_diff( $firstActions[$action], $secondActions[$action] ) ||
|
|
|
|
array_diff( $secondActions[$action], $firstActions[$action] ) ) {
|
|
|
|
// Different parameters
|
|
|
|
$differences[] = 'actions';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// One's unset, one's set.
|
|
|
|
$differences[] = 'actions';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique( $differences );
|
|
|
|
}
|
|
|
|
}
|