Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
25 / 25 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| FilterCompare | |
100.00% |
25 / 25 |
|
100.00% |
2 / 2 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| compareVersions | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
9 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\AbuseFilter; |
| 4 | |
| 5 | use MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesRegistry; |
| 6 | use MediaWiki\Extension\AbuseFilter\Filter\Filter; |
| 7 | |
| 8 | /** |
| 9 | * This service allows comparing two versions of a filter. |
| 10 | * @todo We might want to expand this to cover the use case of ViewDiff |
| 11 | * @internal |
| 12 | */ |
| 13 | class FilterCompare { |
| 14 | public const SERVICE_NAME = 'AbuseFilterFilterCompare'; |
| 15 | |
| 16 | public function __construct( private readonly ConsequencesRegistry $consequencesRegistry ) { |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @param Filter $firstFilter |
| 21 | * @param Filter $secondFilter |
| 22 | * @return string[] Fields that are different |
| 23 | */ |
| 24 | public function compareVersions( Filter $firstFilter, Filter $secondFilter ): array { |
| 25 | // TODO: Avoid DB references here, re-add when saving the filter |
| 26 | $methods = [ |
| 27 | 'af_public_comments' => 'getName', |
| 28 | 'af_pattern' => 'getRules', |
| 29 | 'af_comments' => 'getComments', |
| 30 | 'af_deleted' => 'isDeleted', |
| 31 | 'af_enabled' => 'isEnabled', |
| 32 | 'af_hidden' => 'getPrivacyLevel', |
| 33 | 'af_global' => 'isGlobal', |
| 34 | 'af_group' => 'getGroup', |
| 35 | ]; |
| 36 | |
| 37 | $differences = []; |
| 38 | |
| 39 | foreach ( $methods as $field => $method ) { |
| 40 | if ( $firstFilter->$method() !== $secondFilter->$method() ) { |
| 41 | $differences[] = $field; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | $firstActions = $firstFilter->getActions(); |
| 46 | $secondActions = $secondFilter->getActions(); |
| 47 | foreach ( $this->consequencesRegistry->getAllEnabledActionNames() as $action ) { |
| 48 | if ( isset( $firstActions[$action] ) && isset( $secondActions[$action] ) ) { |
| 49 | // They're both set. Double check needed, e.g. per T180194 |
| 50 | if ( array_diff( $firstActions[$action], $secondActions[$action] ) || |
| 51 | array_diff( $secondActions[$action], $firstActions[$action] ) ) { |
| 52 | // Different parameters |
| 53 | $differences[] = 'actions'; |
| 54 | } |
| 55 | } elseif ( isset( $firstActions[$action] ) !== isset( $secondActions[$action] ) ) { |
| 56 | // One's unset, one's set. |
| 57 | $differences[] = 'actions'; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return array_unique( $differences ); |
| 62 | } |
| 63 | } |