| 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 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class FilterCompare { |
| 14 |
public const SERVICE_NAME = 'AbuseFilterFilterCompare'; |
| 15 |
|
| 16 |
|
| 17 |
private $consequencesRegistry; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public function __construct( ConsequencesRegistry $consequencesRegistry ) { |
| 23 |
$this->consequencesRegistry = $consequencesRegistry; |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public function compareVersions( Filter $firstFilter, Filter $secondFilter ): array { |
| 32 |
|
| 33 |
$methods = [ |
| 34 |
'af_public_comments' => 'getName', |
| 35 |
'af_pattern' => 'getRules', |
| 36 |
'af_comments' => 'getComments', |
| 37 |
'af_deleted' => 'isDeleted', |
| 38 |
'af_enabled' => 'isEnabled', |
| 39 |
'af_hidden' => 'getPrivacyLevel', |
| 40 |
'af_global' => 'isGlobal', |
| 41 |
'af_group' => 'getGroup', |
| 42 |
]; |
| 43 |
|
| 44 |
$differences = []; |
| 45 |
|
| 46 |
foreach ( $methods as $field => $method ) { |
| 47 |
if ( $firstFilter->$method() !== $secondFilter->$method() ) { |
| 48 |
$differences[] = $field; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
$firstActions = $firstFilter->getActions(); |
| 53 |
$secondActions = $secondFilter->getActions(); |
| 54 |
foreach ( $this->consequencesRegistry->getAllEnabledActionNames() as $action ) { |
| 55 |
if ( !isset( $firstActions[$action] ) && !isset( $secondActions[$action] ) ) { |
| 56 |
|
| 57 |
} elseif ( isset( $firstActions[$action] ) && isset( $secondActions[$action] ) ) { |
| 58 |
|
| 59 |
if ( array_diff( $firstActions[$action], $secondActions[$action] ) || |
| 60 |
array_diff( $secondActions[$action], $firstActions[$action] ) ) { |
| 61 |
|
| 62 |
$differences[] = 'actions'; |
| 63 |
} |
| 64 |
} else { |
| 65 |
|
| 66 |
$differences[] = 'actions'; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return array_unique( $differences ); |
| 71 |
} |
| 72 |
} |