| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\Deferred\DeferredUpdates; |
| 8 |
use MediaWiki\Extension\AbuseFilter\ChangeTags\ChangeTagger; |
| 9 |
use MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesExecutorFactory; |
| 10 |
use MediaWiki\Extension\AbuseFilter\Filter\ExistingFilter; |
| 11 |
use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner; |
| 12 |
use MediaWiki\Extension\AbuseFilter\Parser\FilterEvaluator; |
| 13 |
use MediaWiki\Extension\AbuseFilter\Parser\RuleCheckerFactory; |
| 14 |
use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGeneratorFactory; |
| 15 |
use MediaWiki\Extension\AbuseFilter\Variables\LazyVariableComputer; |
| 16 |
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder; |
| 17 |
use MediaWiki\Extension\AbuseFilter\Variables\VariablesManager; |
| 18 |
use MediaWiki\Extension\AbuseFilter\Watcher\Watcher; |
| 19 |
use MediaWiki\Status\Status; |
| 20 |
use MediaWiki\Title\Title; |
| 21 |
use MediaWiki\User\User; |
| 22 |
use Psr\Log\LoggerInterface; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class FilterRunner { |
| 30 |
public const CONSTRUCTOR_OPTIONS = [ |
| 31 |
'AbuseFilterValidGroups', |
| 32 |
'AbuseFilterCentralDB', |
| 33 |
'AbuseFilterIsCentral', |
| 34 |
'AbuseFilterConditionLimit', |
| 35 |
]; |
| 36 |
|
| 37 |
|
| 38 |
private $hookRunner; |
| 39 |
|
| 40 |
private $filterProfiler; |
| 41 |
|
| 42 |
private $changeTagger; |
| 43 |
|
| 44 |
private $filterLookup; |
| 45 |
|
| 46 |
private $ruleCheckerFactory; |
| 47 |
|
| 48 |
private $consExecutorFactory; |
| 49 |
|
| 50 |
private $abuseLoggerFactory; |
| 51 |
|
| 52 |
private $emergencyCache; |
| 53 |
|
| 54 |
private $watchers; |
| 55 |
|
| 56 |
private $stashCache; |
| 57 |
|
| 58 |
private $logger; |
| 59 |
|
| 60 |
private $varManager; |
| 61 |
|
| 62 |
private $varGeneratorFactory; |
| 63 |
|
| 64 |
private $options; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private $ruleChecker; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private $user; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private $title; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private $vars; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private $group; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private $action; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function __construct( |
| 114 |
AbuseFilterHookRunner $hookRunner, |
| 115 |
FilterProfiler $filterProfiler, |
| 116 |
ChangeTagger $changeTagger, |
| 117 |
FilterLookup $filterLookup, |
| 118 |
RuleCheckerFactory $ruleCheckerFactory, |
| 119 |
ConsequencesExecutorFactory $consExecutorFactory, |
| 120 |
AbuseLoggerFactory $abuseLoggerFactory, |
| 121 |
VariablesManager $varManager, |
| 122 |
VariableGeneratorFactory $varGeneratorFactory, |
| 123 |
EmergencyCache $emergencyCache, |
| 124 |
array $watchers, |
| 125 |
EditStashCache $stashCache, |
| 126 |
LoggerInterface $logger, |
| 127 |
ServiceOptions $options, |
| 128 |
User $user, |
| 129 |
Title $title, |
| 130 |
VariableHolder $vars, |
| 131 |
string $group |
| 132 |
) { |
| 133 |
$this->hookRunner = $hookRunner; |
| 134 |
$this->filterProfiler = $filterProfiler; |
| 135 |
$this->changeTagger = $changeTagger; |
| 136 |
$this->filterLookup = $filterLookup; |
| 137 |
$this->ruleCheckerFactory = $ruleCheckerFactory; |
| 138 |
$this->consExecutorFactory = $consExecutorFactory; |
| 139 |
$this->abuseLoggerFactory = $abuseLoggerFactory; |
| 140 |
$this->varManager = $varManager; |
| 141 |
$this->varGeneratorFactory = $varGeneratorFactory; |
| 142 |
$this->emergencyCache = $emergencyCache; |
| 143 |
$this->watchers = $watchers; |
| 144 |
$this->stashCache = $stashCache; |
| 145 |
$this->logger = $logger; |
| 146 |
|
| 147 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 148 |
if ( !in_array( $group, $options->get( 'AbuseFilterValidGroups' ), true ) ) { |
| 149 |
throw new InvalidArgumentException( "Group $group is not a valid group" ); |
| 150 |
} |
| 151 |
$this->options = $options; |
| 152 |
|
| 153 |
if ( !$vars->varIsSet( 'action' ) ) { |
| 154 |
throw new InvalidArgumentException( "The 'action' variable is not set." ); |
| 155 |
} |
| 156 |
$this->user = $user; |
| 157 |
$this->title = $title; |
| 158 |
$this->vars = $vars; |
| 159 |
$this->group = $group; |
| 160 |
$this->action = $vars->getComputedVariable( 'action' )->toString(); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private function init() { |
| 167 |
|
| 168 |
$this->hookRunner->onAbuseFilter_filterAction( |
| 169 |
$this->vars, |
| 170 |
$this->title |
| 171 |
); |
| 172 |
$this->hookRunner->onAbuseFilterAlterVariables( |
| 173 |
$this->vars, |
| 174 |
$this->title, |
| 175 |
$this->user |
| 176 |
); |
| 177 |
$generator = $this->varGeneratorFactory->newGenerator( $this->vars ); |
| 178 |
$this->vars = $generator->addGenericVars()->getVariableHolder(); |
| 179 |
$this->ruleChecker = $this->ruleCheckerFactory->newRuleChecker( $this->vars ); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function run( $allowStash = true ): Status { |
| 190 |
$this->init(); |
| 191 |
|
| 192 |
$skipReasons = []; |
| 193 |
$shouldFilter = $this->hookRunner->onAbuseFilterShouldFilterAction( |
| 194 |
$this->vars, $this->title, $this->user, $skipReasons |
| 195 |
); |
| 196 |
if ( !$shouldFilter ) { |
| 197 |
$this->logger->info( |
| 198 |
'Skipping action {action}. Reasons provided: {reasons}', |
| 199 |
[ 'action' => $this->action, 'reasons' => implode( ', ', $skipReasons ) ] |
| 200 |
); |
| 201 |
return Status::newGood(); |
| 202 |
} |
| 203 |
|
| 204 |
$useStash = $allowStash && $this->action === 'edit'; |
| 205 |
|
| 206 |
$runnerData = null; |
| 207 |
if ( $useStash ) { |
| 208 |
$cacheData = $this->stashCache->seek( $this->vars ); |
| 209 |
if ( $cacheData !== false ) { |
| 210 |
|
| 211 |
$this->vars = VariableHolder::newFromArray( $cacheData['vars'] ); |
| 212 |
$runnerData = RunnerData::fromArray( $cacheData['data'] ); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
$runnerData ??= $this->checkAllFiltersInternal(); |
| 217 |
|
| 218 |
DeferredUpdates::addCallableUpdate( function () use ( $runnerData ) { |
| 219 |
$this->profileExecution( $runnerData ); |
| 220 |
$this->updateEmergencyCache( $runnerData->getMatchesMap() ); |
| 221 |
} ); |
| 222 |
|
| 223 |
|
| 224 |
$accountname = $this->varManager->getVar( |
| 225 |
$this->vars, |
| 226 |
'accountname', |
| 227 |
VariablesManager::GET_BC |
| 228 |
)->toNative(); |
| 229 |
$spec = new ActionSpecifier( |
| 230 |
$this->action, |
| 231 |
$this->title, |
| 232 |
$this->user, |
| 233 |
$this->user->getRequest()->getIP(), |
| 234 |
$accountname |
| 235 |
); |
| 236 |
|
| 237 |
|
| 238 |
if ( $runnerData->getTotalConditions() > $this->options->get( 'AbuseFilterConditionLimit' ) ) { |
| 239 |
$this->changeTagger->addConditionsLimitTag( $spec ); |
| 240 |
} |
| 241 |
|
| 242 |
$matchedFilters = $runnerData->getMatchedFilters(); |
| 243 |
|
| 244 |
if ( count( $matchedFilters ) === 0 ) { |
| 245 |
return Status::newGood(); |
| 246 |
} |
| 247 |
|
| 248 |
$executor = $this->consExecutorFactory->newExecutor( $spec, $this->vars ); |
| 249 |
$status = $executor->executeFilterActions( $matchedFilters ); |
| 250 |
$actionsTaken = $status->getValue(); |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
$abuseLogger = $this->abuseLoggerFactory->newLogger( $this->title, $this->user, $this->vars ); |
| 255 |
[ |
| 256 |
'local' => $loggedLocalFilters, |
| 257 |
'global' => $loggedGlobalFilters |
| 258 |
] = $abuseLogger->addLogEntries( $actionsTaken ); |
| 259 |
|
| 260 |
foreach ( $this->watchers as $watcher ) { |
| 261 |
$watcher->run( $loggedLocalFilters, $loggedGlobalFilters, $this->group ); |
| 262 |
} |
| 263 |
|
| 264 |
return $status; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public function runForStash(): Status { |
| 276 |
if ( $this->action !== 'edit' ) { |
| 277 |
throw new InvalidArgumentException( |
| 278 |
__METHOD__ . " can only be called for edits, called for action {$this->action}." |
| 279 |
); |
| 280 |
} |
| 281 |
|
| 282 |
$this->init(); |
| 283 |
|
| 284 |
$skipReasons = []; |
| 285 |
$shouldFilter = $this->hookRunner->onAbuseFilterShouldFilterAction( |
| 286 |
$this->vars, $this->title, $this->user, $skipReasons |
| 287 |
); |
| 288 |
if ( !$shouldFilter ) { |
| 289 |
|
| 290 |
return Status::newGood(); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
$origVars = clone $this->vars; |
| 299 |
|
| 300 |
$runnerData = $this->checkAllFiltersInternal(); |
| 301 |
|
| 302 |
$cacheData = [ |
| 303 |
'vars' => $this->varManager->dumpAllVars( $this->vars ), |
| 304 |
'data' => $runnerData->toArray(), |
| 305 |
]; |
| 306 |
|
| 307 |
$this->stashCache->store( $origVars, $cacheData ); |
| 308 |
|
| 309 |
return Status::newGood(); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
private function checkAllFiltersInternal(): RunnerData { |
| 318 |
|
| 319 |
LazyVariableComputer::$profilingExtraTime = 0; |
| 320 |
|
| 321 |
$data = new RunnerData(); |
| 322 |
|
| 323 |
foreach ( $this->filterLookup->getAllActiveFiltersInGroup( $this->group, false ) as $filter ) { |
| 324 |
[ $status, $timeTaken ] = $this->checkFilter( $filter ); |
| 325 |
$data->record( $filter->getID(), false, $status, $timeTaken ); |
| 326 |
} |
| 327 |
|
| 328 |
if ( $this->options->get( 'AbuseFilterCentralDB' ) && !$this->options->get( 'AbuseFilterIsCentral' ) ) { |
| 329 |
foreach ( $this->filterLookup->getAllActiveFiltersInGroup( $this->group, true ) as $filter ) { |
| 330 |
[ $status, $timeTaken ] = $this->checkFilter( $filter, true ); |
| 331 |
$data->record( $filter->getID(), true, $status, $timeTaken ); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
return $data; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public function checkAllFilters(): array { |
| 345 |
$this->init(); |
| 346 |
return $this->checkAllFiltersInternal()->getMatchesMap(); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
private function checkFilter( ExistingFilter $filter, bool $global = false ): array { |
| 358 |
$filterName = GlobalNameUtils::buildGlobalName( $filter->getID(), $global ); |
| 359 |
|
| 360 |
$startTime = microtime( true ); |
| 361 |
$origExtraTime = LazyVariableComputer::$profilingExtraTime; |
| 362 |
|
| 363 |
$status = $this->ruleChecker->checkConditions( $filter->getRules(), $filterName ); |
| 364 |
|
| 365 |
$actualExtra = LazyVariableComputer::$profilingExtraTime - $origExtraTime; |
| 366 |
$timeTaken = 1000 * ( microtime( true ) - $startTime - $actualExtra ); |
| 367 |
|
| 368 |
return [ $status, $timeTaken ]; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
private function profileExecution( RunnerData $data ) { |
| 375 |
$allFilters = $data->getAllFilters(); |
| 376 |
$matchedFilters = $data->getMatchedFilters(); |
| 377 |
$this->filterProfiler->recordRuntimeProfilingResult( |
| 378 |
count( $allFilters ), |
| 379 |
$data->getTotalConditions(), |
| 380 |
$data->getTotalRuntime() |
| 381 |
); |
| 382 |
$this->filterProfiler->recordPerFilterProfiling( $this->title, $data->getProfilingData() ); |
| 383 |
$this->filterProfiler->recordStats( |
| 384 |
$this->group, |
| 385 |
$data->getTotalConditions(), |
| 386 |
$data->getTotalRuntime(), |
| 387 |
(bool)$matchedFilters |
| 388 |
); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
private function updateEmergencyCache( array $matches ): void { |
| 395 |
$filters = $this->emergencyCache->getFiltersToCheckInGroup( $this->group ); |
| 396 |
foreach ( $filters as $filter ) { |
| 397 |
if ( array_key_exists( "$filter", $matches ) ) { |
| 398 |
$this->emergencyCache->incrementForFilter( $filter, $matches["$filter"] ); |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
} |