Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
UpdateHitCountWatcher | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
run | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
updateHitCounts | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\AbuseFilter\Watcher; |
4 | |
5 | use MediaWiki\Deferred\DeferredUpdates; |
6 | use MediaWiki\Extension\AbuseFilter\CentralDBManager; |
7 | use Wikimedia\Rdbms\IDatabase; |
8 | use Wikimedia\Rdbms\LBFactory; |
9 | |
10 | /** |
11 | * Watcher that updates hit counts of filters |
12 | */ |
13 | class UpdateHitCountWatcher implements Watcher { |
14 | public const SERVICE_NAME = 'AbuseFilterUpdateHitCountWatcher'; |
15 | |
16 | /** @var LBFactory */ |
17 | private $lbFactory; |
18 | |
19 | /** @var CentralDBManager */ |
20 | private $centralDBManager; |
21 | |
22 | /** |
23 | * @param LBFactory $lbFactory |
24 | * @param CentralDBManager $centralDBManager |
25 | */ |
26 | public function __construct( |
27 | LBFactory $lbFactory, |
28 | CentralDBManager $centralDBManager |
29 | ) { |
30 | $this->lbFactory = $lbFactory; |
31 | $this->centralDBManager = $centralDBManager; |
32 | } |
33 | |
34 | /** |
35 | * @inheritDoc |
36 | */ |
37 | public function run( array $localFilters, array $globalFilters, string $group ): void { |
38 | // Run in a DeferredUpdate to avoid primary database queries on raw/view requests (T274455) |
39 | DeferredUpdates::addCallableUpdate( function () use ( $localFilters, $globalFilters ) { |
40 | if ( $localFilters ) { |
41 | $this->updateHitCounts( $this->lbFactory->getPrimaryDatabase(), $localFilters ); |
42 | } |
43 | |
44 | if ( $globalFilters ) { |
45 | $fdb = $this->centralDBManager->getConnection( DB_PRIMARY ); |
46 | $this->updateHitCounts( $fdb, $globalFilters ); |
47 | } |
48 | } ); |
49 | } |
50 | |
51 | /** |
52 | * @param IDatabase $dbw |
53 | * @param array $loggedFilters |
54 | */ |
55 | private function updateHitCounts( IDatabase $dbw, array $loggedFilters ): void { |
56 | $dbw->newUpdateQueryBuilder() |
57 | ->update( 'abuse_filter' ) |
58 | ->set( [ 'af_hit_count=af_hit_count+1' ] ) |
59 | ->where( [ 'af_id' => $loggedFilters ] ) |
60 | ->caller( __METHOD__ ) |
61 | ->execute(); |
62 | } |
63 | } |