Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
EchoNotifier
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
5 / 5
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTitleForFilter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFilterObject
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getThrottledActionNames
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 notifyForFilter
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter;
4
5use MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesRegistry;
6use MediaWiki\Extension\AbuseFilter\Filter\ExistingFilter;
7use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseFilter;
8use MediaWiki\Notification\NotificationService;
9use MediaWiki\Notification\RecipientSet;
10use MediaWiki\Notification\Types\TitleNotification;
11use MediaWiki\Title\Title;
12
13/**
14 * Helper service for EmergencyWatcher to notify filter maintainers of throttled filters
15 */
16class EchoNotifier {
17    public const SERVICE_NAME = ServiceNames::EchoNotifier;
18    public const EVENT_TYPE = 'throttled-filter';
19
20    public function __construct(
21        private readonly FilterLookup $filterLookup,
22        private readonly ConsequencesRegistry $consequencesRegistry,
23        private readonly NotificationService $notifications,
24    ) {
25    }
26
27    private function getTitleForFilter( int $filter ): Title {
28        return SpecialAbuseFilter::getTitleForSubpage( (string)$filter );
29    }
30
31    private function getFilterObject( int $filter ): ExistingFilter {
32        return $this->filterLookup->getFilter( $filter, false );
33    }
34
35    private function getThrottledActionNames( ExistingFilter $filterObj ): array {
36        return array_intersect(
37            $filterObj->getActionsNames(),
38            $this->consequencesRegistry->getDangerousActionNames()
39        );
40    }
41
42    /**
43     * Send notification about a filter being throttled
44     */
45    public function notifyForFilter( int $filter ): void {
46        $filterObj = $this->getFilterObject( $filter );
47        $this->notifications->notify(
48            new TitleNotification(
49                self::EVENT_TYPE,
50                $this->getTitleForFilter( $filter ),
51                [ 'throttled-actions' => $this->getThrottledActionNames( $filterObj ) ]
52            ),
53            new RecipientSet( $filterObj->getUserIdentity() )
54        );
55    }
56
57}