2020-11-10 12:58:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter;
|
|
|
|
|
|
|
|
use EchoEvent;
|
2022-02-19 13:49:58 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesRegistry;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\ExistingFilter;
|
2021-01-01 17:28:36 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseFilter;
|
2020-11-10 12:58:11 +00:00
|
|
|
use Title;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper service for EmergencyWatcher to notify filter maintainers of throttled filters
|
|
|
|
* @todo DI not possible due to Echo
|
|
|
|
*/
|
|
|
|
class EchoNotifier {
|
|
|
|
public const SERVICE_NAME = 'AbuseFilterEchoNotifier';
|
|
|
|
public const EVENT_TYPE = 'throttled-filter';
|
|
|
|
|
|
|
|
/** @var FilterLookup */
|
|
|
|
private $filterLookup;
|
2022-02-19 13:49:58 +00:00
|
|
|
/** @var ConsequencesRegistry */
|
|
|
|
private $consequencesRegistry;
|
2020-11-10 12:58:11 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $isEchoLoaded;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FilterLookup $filterLookup
|
2022-02-19 13:49:58 +00:00
|
|
|
* @param ConsequencesRegistry $consequencesRegistry
|
2020-11-10 12:58:11 +00:00
|
|
|
* @param bool $isEchoLoaded
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
FilterLookup $filterLookup,
|
2022-02-19 13:49:58 +00:00
|
|
|
ConsequencesRegistry $consequencesRegistry,
|
2020-11-10 12:58:11 +00:00
|
|
|
bool $isEchoLoaded
|
|
|
|
) {
|
|
|
|
$this->filterLookup = $filterLookup;
|
2022-02-19 13:49:58 +00:00
|
|
|
$this->consequencesRegistry = $consequencesRegistry;
|
2020-11-10 12:58:11 +00:00
|
|
|
$this->isEchoLoaded = $isEchoLoaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $filter
|
|
|
|
* @return Title
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function getTitleForFilter( int $filter ): Title {
|
2020-11-10 12:58:11 +00:00
|
|
|
return SpecialAbuseFilter::getTitleForSubpage( (string)$filter );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $filter
|
2022-02-19 13:49:58 +00:00
|
|
|
* @return ExistingFilter
|
2020-11-10 12:58:11 +00:00
|
|
|
*/
|
2022-02-19 13:49:58 +00:00
|
|
|
private function getFilterObject( int $filter ): ExistingFilter {
|
|
|
|
return $this->filterLookup->getFilter( $filter, false );
|
2020-11-10 12:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @param int $filter
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function getDataForEvent( int $filter ): array {
|
2022-02-19 13:49:58 +00:00
|
|
|
$filterObj = $this->getFilterObject( $filter );
|
|
|
|
$throttledActionNames = array_intersect(
|
|
|
|
$filterObj->getActionsNames(),
|
|
|
|
$this->consequencesRegistry->getDangerousActionNames()
|
|
|
|
);
|
2020-11-10 12:58:11 +00:00
|
|
|
return [
|
|
|
|
'type' => self::EVENT_TYPE,
|
|
|
|
'title' => $this->getTitleForFilter( $filter ),
|
|
|
|
'extra' => [
|
2022-02-19 13:49:58 +00:00
|
|
|
'user' => $filterObj->getUserID(),
|
|
|
|
'throttled-actions' => $throttledActionNames,
|
2020-11-10 12:58:11 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send notification about a filter being throttled
|
|
|
|
*
|
|
|
|
* @param int $filter
|
2020-12-19 13:36:58 +00:00
|
|
|
* @return EchoEvent|false
|
2020-11-10 12:58:11 +00:00
|
|
|
*/
|
2020-12-19 13:36:58 +00:00
|
|
|
public function notifyForFilter( int $filter ) {
|
2020-11-10 12:58:11 +00:00
|
|
|
if ( $this->isEchoLoaded ) {
|
2020-12-19 13:36:58 +00:00
|
|
|
return EchoEvent::create( $this->getDataForEvent( $filter ) );
|
2020-11-10 12:58:11 +00:00
|
|
|
}
|
2020-12-19 13:36:58 +00:00
|
|
|
return false;
|
2020-11-10 12:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|