| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder; |
| 7 |
use MediaWiki\Extension\AbuseFilter\Variables\VariablesManager; |
| 8 |
use MediaWiki\Linker\LinkTarget; |
| 9 |
use Psr\Log\LoggerInterface; |
| 10 |
use Wikimedia\ObjectCache\BagOStuff; |
| 11 |
use Wikimedia\Stats\IBufferingStatsdDataFactory; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class EditStashCache { |
| 17 |
|
| 18 |
private const CACHE_VERSION = 'v5'; |
| 19 |
|
| 20 |
|
| 21 |
private $cache; |
| 22 |
|
| 23 |
|
| 24 |
private $statsdDataFactory; |
| 25 |
|
| 26 |
|
| 27 |
private $variablesManager; |
| 28 |
|
| 29 |
|
| 30 |
private $logger; |
| 31 |
|
| 32 |
|
| 33 |
private $target; |
| 34 |
|
| 35 |
|
| 36 |
private $group; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function __construct( |
| 47 |
BagOStuff $cache, |
| 48 |
IBufferingStatsdDataFactory $statsdDataFactory, |
| 49 |
VariablesManager $variablesManager, |
| 50 |
LoggerInterface $logger, |
| 51 |
LinkTarget $target, |
| 52 |
string $group |
| 53 |
) { |
| 54 |
$this->cache = $cache; |
| 55 |
$this->statsdDataFactory = $statsdDataFactory; |
| 56 |
$this->variablesManager = $variablesManager; |
| 57 |
$this->logger = $logger; |
| 58 |
$this->target = $target; |
| 59 |
$this->group = $group; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function store( VariableHolder $vars, array $data ): void { |
| 67 |
$key = $this->getStashKey( $vars ); |
| 68 |
$this->cache->set( $key, $data, BagOStuff::TTL_MINUTE ); |
| 69 |
$this->logCache( 'store', $key ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function seek( VariableHolder $vars ) { |
| 79 |
$key = $this->getStashKey( $vars ); |
| 80 |
$value = $this->cache->get( $key ); |
| 81 |
$status = $value !== false ? 'hit' : 'miss'; |
| 82 |
$this->logCache( $status, $key ); |
| 83 |
return $value; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
private function logCache( string $type, string $key ): void { |
| 94 |
if ( !in_array( $type, [ 'store', 'hit', 'miss' ] ) ) { |
| 95 |
|
| 96 |
throw new InvalidArgumentException( '$type must be either "store", "hit" or "miss"' ); |
| 97 |
|
| 98 |
} |
| 99 |
$this->logger->debug( |
| 100 |
__METHOD__ . ": cache {logtype} for '{target}' (key {key}).", |
| 101 |
[ 'logtype' => $type, 'target' => $this->target, 'key' => $key ] |
| 102 |
); |
| 103 |
$this->statsdDataFactory->increment( "abusefilter.check-stash.$type" ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private function getStashKey( VariableHolder $vars ): string { |
| 113 |
$inputVars = $this->variablesManager->exportNonLazyVars( $vars ); |
| 114 |
|
| 115 |
$excludedVars = [ |
| 116 |
'old_html' => true, |
| 117 |
'new_html' => true, |
| 118 |
'user_age' => true, |
| 119 |
'timestamp' => true, |
| 120 |
'page_age' => true, |
| 121 |
'page_last_edit_age' => true, |
| 122 |
]; |
| 123 |
|
| 124 |
$inputVars = array_diff_key( $inputVars, $excludedVars ); |
| 125 |
ksort( $inputVars ); |
| 126 |
$hash = md5( serialize( $inputVars ) ); |
| 127 |
|
| 128 |
return $this->cache->makeKey( |
| 129 |
'abusefilter', |
| 130 |
'check-stash', |
| 131 |
$this->group, |
| 132 |
$hash, |
| 133 |
self::CACHE_VERSION |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
} |