| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Consequences; |
| 4 |
|
| 5 |
|
| 6 |
use MediaWiki\Extension\AbuseFilter\Consequences\Consequence\Consequence; |
| 7 |
use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner; |
| 8 |
use RuntimeException; |
| 9 |
|
| 10 |
class ConsequencesRegistry { |
| 11 |
public const SERVICE_NAME = 'AbuseFilterConsequencesRegistry'; |
| 12 |
|
| 13 |
private const DANGEROUS_ACTIONS = [ |
| 14 |
'block', |
| 15 |
'blockautopromote', |
| 16 |
'degroup', |
| 17 |
'rangeblock' |
| 18 |
]; |
| 19 |
|
| 20 |
|
| 21 |
private $hookRunner; |
| 22 |
|
| 23 |
private $configActions; |
| 24 |
|
| 25 |
|
| 26 |
private $dangerousActionsCache; |
| 27 |
|
| 28 |
private $customActionsCache; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function __construct( |
| 35 |
AbuseFilterHookRunner $hookRunner, |
| 36 |
array $configActions |
| 37 |
) { |
| 38 |
$this->hookRunner = $hookRunner; |
| 39 |
$this->configActions = $configActions; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function getDangerousActionNames(): array { |
| 48 |
if ( $this->dangerousActionsCache === null ) { |
| 49 |
$extActions = []; |
| 50 |
$this->hookRunner->onAbuseFilterGetDangerousActions( $extActions ); |
| 51 |
$this->dangerousActionsCache = array_unique( |
| 52 |
array_merge( $extActions, self::DANGEROUS_ACTIONS ) |
| 53 |
); |
| 54 |
} |
| 55 |
return $this->dangerousActionsCache; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function getAllActionNames(): array { |
| 62 |
return array_unique( |
| 63 |
array_merge( |
| 64 |
array_keys( $this->configActions ), |
| 65 |
array_keys( $this->getCustomActions() ) |
| 66 |
) |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function getCustomActions(): array { |
| 75 |
if ( $this->customActionsCache === null ) { |
| 76 |
$this->customActionsCache = []; |
| 77 |
$this->hookRunner->onAbuseFilterCustomActions( $this->customActionsCache ); |
| 78 |
$this->validateCustomActions(); |
| 79 |
} |
| 80 |
return $this->customActionsCache; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private function validateCustomActions(): void { |
| 87 |
foreach ( $this->customActionsCache as $name => $cb ) { |
| 88 |
if ( !is_string( $name ) ) { |
| 89 |
throw new RuntimeException( 'Custom actions keys should be strings!' ); |
| 90 |
} |
| 91 |
|
| 92 |
if ( !is_callable( $cb ) ) { |
| 93 |
throw new RuntimeException( 'Custom actions values should be callables!' ); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function getAllEnabledActionNames(): array { |
| 102 |
$disabledActions = array_keys( array_filter( |
| 103 |
$this->configActions, |
| 104 |
static function ( $el ) { |
| 105 |
return $el === false; |
| 106 |
} |
| 107 |
) ); |
| 108 |
return array_values( array_diff( $this->getAllActionNames(), $disabledActions ) ); |
| 109 |
} |
| 110 |
} |