Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
EchoNotifier
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
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
 getDataForEvent
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 notifyForFilter
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
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\Extension\Notifications\Model\Event;
9use MediaWiki\Title\Title;
10
11/**
12 * Helper service for EmergencyWatcher to notify filter maintainers of throttled filters
13 * @todo DI not possible due to Echo
14 */
15class EchoNotifier {
16    public const SERVICE_NAME = 'AbuseFilterEchoNotifier';
17    public const EVENT_TYPE = 'throttled-filter';
18
19    /** @var FilterLookup */
20    private $filterLookup;
21    /** @var ConsequencesRegistry */
22    private $consequencesRegistry;
23    /** @var bool */
24    private $isEchoLoaded;
25
26    /**
27     * @param FilterLookup $filterLookup
28     * @param ConsequencesRegistry $consequencesRegistry
29     * @param bool $isEchoLoaded
30     */
31    public function __construct(
32        FilterLookup $filterLookup,
33        ConsequencesRegistry $consequencesRegistry,
34        bool $isEchoLoaded
35    ) {
36        $this->filterLookup = $filterLookup;
37        $this->consequencesRegistry = $consequencesRegistry;
38        $this->isEchoLoaded = $isEchoLoaded;
39    }
40
41    /**
42     * @param int $filter
43     * @return Title
44     */
45    private function getTitleForFilter( int $filter ): Title {
46        return SpecialAbuseFilter::getTitleForSubpage( (string)$filter );
47    }
48
49    /**
50     * @param int $filter
51     * @return ExistingFilter
52     */
53    private function getFilterObject( int $filter ): ExistingFilter {
54        return $this->filterLookup->getFilter( $filter, false );
55    }
56
57    /**
58     * @internal
59     * @param int $filter
60     * @return array
61     */
62    public function getDataForEvent( int $filter ): array {
63        $filterObj = $this->getFilterObject( $filter );
64        $throttledActionNames = array_intersect(
65            $filterObj->getActionsNames(),
66            $this->consequencesRegistry->getDangerousActionNames()
67        );
68        return [
69            'type' => self::EVENT_TYPE,
70            'title' => $this->getTitleForFilter( $filter ),
71            'extra' => [
72                'user' => $filterObj->getUserID(),
73                'throttled-actions' => $throttledActionNames,
74            ],
75        ];
76    }
77
78    /**
79     * Send notification about a filter being throttled
80     *
81     * @param int $filter
82     * @return Event|false
83     */
84    public function notifyForFilter( int $filter ) {
85        if ( $this->isEchoLoaded ) {
86            return Event::create( $this->getDataForEvent( $filter ) );
87        }
88        return false;
89    }
90
91}