| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter; |
| 4 |
|
| 5 |
use Wikimedia\ObjectCache\BagOStuff; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class EmergencyCache { |
| 12 |
|
| 13 |
public const SERVICE_NAME = 'AbuseFilterEmergencyCache'; |
| 14 |
|
| 15 |
|
| 16 |
private $stash; |
| 17 |
|
| 18 |
|
| 19 |
private $ttlPerGroup; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public function __construct( BagOStuff $stash, array $ttlPerGroup ) { |
| 26 |
$this->stash = $stash; |
| 27 |
$this->ttlPerGroup = $ttlPerGroup; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function getFiltersToCheckInGroup( string $group ): array { |
| 38 |
$filterToExpiry = $this->stash->get( $this->createGroupKey( $group ) ); |
| 39 |
if ( $filterToExpiry === false ) { |
| 40 |
return []; |
| 41 |
} |
| 42 |
$time = (int)round( $this->stash->getCurrentTime() ); |
| 43 |
return array_keys( array_filter( |
| 44 |
$filterToExpiry, |
| 45 |
static function ( $exp ) use ( $time ) { |
| 46 |
return $exp > $time; |
| 47 |
} |
| 48 |
) ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function setNewForFilter( int $filter, string $group ): bool { |
| 60 |
$ttl = $this->ttlPerGroup[$group] ?? $this->ttlPerGroup['default']; |
| 61 |
$expiry = (int)round( $this->stash->getCurrentTime() + $ttl ); |
| 62 |
$this->stash->merge( |
| 63 |
$this->createGroupKey( $group ), |
| 64 |
static function ( $cache, $key, $value ) use ( $filter, $expiry ) { |
| 65 |
if ( $value === false ) { |
| 66 |
$value = []; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
$value[$filter] = $expiry; |
| 72 |
return $value; |
| 73 |
}, |
| 74 |
$expiry |
| 75 |
); |
| 76 |
return $this->stash->set( |
| 77 |
$this->createFilterKey( $filter ), |
| 78 |
[ 'total' => 0, 'matches' => 0, 'expiry' => $expiry ], |
| 79 |
$expiry |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function incrementForFilter( int $filter, bool $matched ): bool { |
| 91 |
return $this->stash->merge( |
| 92 |
$this->createFilterKey( $filter ), |
| 93 |
static function ( $cache, $key, $value, &$expiry ) use ( $matched ) { |
| 94 |
if ( $value === false ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
$value['total']++; |
| 98 |
if ( $matched ) { |
| 99 |
$value['matches']++; |
| 100 |
} |
| 101 |
|
| 102 |
$expiry = $value['expiry']; |
| 103 |
return $value; |
| 104 |
} |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function getForFilter( int $filter ) { |
| 117 |
$value = $this->stash->get( $this->createFilterKey( $filter ) ); |
| 118 |
if ( $value !== false ) { |
| 119 |
unset( $value['expiry'] ); |
| 120 |
} |
| 121 |
return $value; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
private function createGroupKey( string $group ): string { |
| 129 |
return $this->stash->makeKey( 'abusefilter', 'emergency', 'group', $group ); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private function createFilterKey( int $filter ): string { |
| 137 |
return $this->stash->makeKey( 'abusefilter', 'emergency', 'filter', $filter ); |
| 138 |
} |
| 139 |
|
| 140 |
} |