| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Watcher; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\Deferred\AutoCommitUpdate; |
| 8 |
use MediaWiki\Deferred\DeferredUpdates; |
| 9 |
use MediaWiki\Extension\AbuseFilter\EchoNotifier; |
| 10 |
use MediaWiki\Extension\AbuseFilter\EmergencyCache; |
| 11 |
use MediaWiki\Extension\AbuseFilter\FilterLookup; |
| 12 |
use Wikimedia\Rdbms\IDatabase; |
| 13 |
use Wikimedia\Rdbms\LBFactory; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class EmergencyWatcher implements Watcher { |
| 22 |
public const SERVICE_NAME = 'AbuseFilterEmergencyWatcher'; |
| 23 |
|
| 24 |
public const CONSTRUCTOR_OPTIONS = [ |
| 25 |
'AbuseFilterEmergencyDisableAge', |
| 26 |
'AbuseFilterEmergencyDisableCount', |
| 27 |
'AbuseFilterEmergencyDisableThreshold', |
| 28 |
]; |
| 29 |
|
| 30 |
|
| 31 |
private $cache; |
| 32 |
|
| 33 |
|
| 34 |
private $lbFactory; |
| 35 |
|
| 36 |
|
| 37 |
private $filterLookup; |
| 38 |
|
| 39 |
|
| 40 |
private $notifier; |
| 41 |
|
| 42 |
|
| 43 |
private $options; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function __construct( |
| 53 |
EmergencyCache $cache, |
| 54 |
LBFactory $lbFactory, |
| 55 |
FilterLookup $filterLookup, |
| 56 |
EchoNotifier $notifier, |
| 57 |
ServiceOptions $options |
| 58 |
) { |
| 59 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 60 |
$this->cache = $cache; |
| 61 |
$this->lbFactory = $lbFactory; |
| 62 |
$this->filterLookup = $filterLookup; |
| 63 |
$this->notifier = $notifier; |
| 64 |
$this->options = $options; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function getFiltersToThrottle( array $filters, string $group ): array { |
| 76 |
$filters = array_intersect( |
| 77 |
$filters, |
| 78 |
$this->cache->getFiltersToCheckInGroup( $group ) |
| 79 |
); |
| 80 |
if ( $filters === [] ) { |
| 81 |
return []; |
| 82 |
} |
| 83 |
|
| 84 |
$threshold = $this->getEmergencyValue( 'threshold', $group ); |
| 85 |
$hitCountLimit = $this->getEmergencyValue( 'count', $group ); |
| 86 |
$maxAge = $this->getEmergencyValue( 'age', $group ); |
| 87 |
|
| 88 |
$time = (int)wfTimestamp( TS_UNIX ); |
| 89 |
|
| 90 |
$throttleFilters = []; |
| 91 |
foreach ( $filters as $filter ) { |
| 92 |
$filterObj = $this->filterLookup->getFilter( $filter, false ); |
| 93 |
|
| 94 |
|
| 95 |
if ( $filterObj->isThrottled() ) { |
| 96 |
continue; |
| 97 |
} |
| 98 |
|
| 99 |
$filterAge = (int)wfTimestamp( TS_UNIX, $filterObj->getTimestamp() ); |
| 100 |
$exemptTime = $filterAge + $maxAge; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
if ( $exemptTime <= $time ) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
$cacheValue = $this->cache->getForFilter( $filter ); |
| 112 |
if ( $cacheValue === false ) { |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
[ 'total' => $totalActions, 'matches' => $matchCount ] = $cacheValue; |
| 117 |
|
| 118 |
if ( $matchCount > $hitCountLimit && ( $matchCount / $totalActions ) > $threshold ) { |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
$throttleFilters[] = $filter; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $throttleFilters; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function run( array $localFilters, array $globalFilters, string $group ): void { |
| 135 |
$throttleFilters = $this->getFiltersToThrottle( $localFilters, $group ); |
| 136 |
if ( !$throttleFilters ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
DeferredUpdates::addUpdate( |
| 141 |
new AutoCommitUpdate( |
| 142 |
$this->lbFactory->getPrimaryDatabase(), |
| 143 |
__METHOD__, |
| 144 |
static function ( IDatabase $dbw, $fname ) use ( $throttleFilters ) { |
| 145 |
$dbw->newUpdateQueryBuilder() |
| 146 |
->update( 'abuse_filter' ) |
| 147 |
->set( [ 'af_throttled' => 1 ] ) |
| 148 |
->where( [ 'af_id' => $throttleFilters ] ) |
| 149 |
->caller( $fname ) |
| 150 |
->execute(); |
| 151 |
} |
| 152 |
) |
| 153 |
); |
| 154 |
DeferredUpdates::addCallableUpdate( function () use ( $throttleFilters ) { |
| 155 |
foreach ( $throttleFilters as $filter ) { |
| 156 |
$this->notifier->notifyForFilter( $filter ); |
| 157 |
} |
| 158 |
} ); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private function getEmergencyValue( string $type, string $group ) { |
| 167 |
switch ( $type ) { |
| 168 |
case 'threshold': |
| 169 |
$opt = 'AbuseFilterEmergencyDisableThreshold'; |
| 170 |
break; |
| 171 |
case 'count': |
| 172 |
$opt = 'AbuseFilterEmergencyDisableCount'; |
| 173 |
break; |
| 174 |
case 'age': |
| 175 |
$opt = 'AbuseFilterEmergencyDisableAge'; |
| 176 |
break; |
| 177 |
default: |
| 178 |
|
| 179 |
throw new InvalidArgumentException( '$type must be either "threshold", "count" or "age"' ); |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
$value = $this->options->get( $opt ); |
| 184 |
return $value[$group] ?? $value['default']; |
| 185 |
} |
| 186 |
} |