File: EmergencyCache.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (140 lines) | stat: -rw-r--r-- 3,744 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

namespace MediaWiki\Extension\AbuseFilter;

use Wikimedia\ObjectCache\BagOStuff;

/**
 * Helper class for EmergencyWatcher. Wrapper around cache which tracks hits of recently
 * modified filters.
 */
class EmergencyCache {

	public const SERVICE_NAME = 'AbuseFilterEmergencyCache';

	/** @var BagOStuff */
	private $stash;

	/** @var int[] */
	private $ttlPerGroup;

	/**
	 * @param BagOStuff $stash
	 * @param int[] $ttlPerGroup
	 */
	public function __construct( BagOStuff $stash, array $ttlPerGroup ) {
		$this->stash = $stash;
		$this->ttlPerGroup = $ttlPerGroup;
	}

	/**
	 * Get recently modified filters in the group. Thanks to this, performance can be improved,
	 * because only a small subset of filters will need an update.
	 *
	 * @param string $group
	 * @return int[]
	 */
	public function getFiltersToCheckInGroup( string $group ): array {
		$filterToExpiry = $this->stash->get( $this->createGroupKey( $group ) );
		if ( $filterToExpiry === false ) {
			return [];
		}
		$time = (int)round( $this->stash->getCurrentTime() );
		return array_keys( array_filter(
			$filterToExpiry,
			static function ( $exp ) use ( $time ) {
				return $exp > $time;
			}
		) );
	}

	/**
	 * Create a new entry in cache for a filter and update the entry for the group.
	 * This method is usually called after the filter has been updated.
	 *
	 * @param int $filter
	 * @param string $group
	 * @return bool
	 */
	public function setNewForFilter( int $filter, string $group ): bool {
		$ttl = $this->ttlPerGroup[$group] ?? $this->ttlPerGroup['default'];
		$expiry = (int)round( $this->stash->getCurrentTime() + $ttl );
		$this->stash->merge(
			$this->createGroupKey( $group ),
			static function ( $cache, $key, $value ) use ( $filter, $expiry ) {
				if ( $value === false ) {
					$value = [];
				}
				// note that some filters may have already had their keys expired
				// we are currently filtering them out in getFiltersToCheckInGroup
				// but if necessary, it can be done here
				$value[$filter] = $expiry;
				return $value;
			},
			$expiry
		);
		return $this->stash->set(
			$this->createFilterKey( $filter ),
			[ 'total' => 0, 'matches' => 0, 'expiry' => $expiry ],
			$expiry
		);
	}

	/**
	 * Increase the filter's 'total' value by one and possibly also the 'matched' value.
	 *
	 * @param int $filter
	 * @param bool $matched Whether the filter matched the action
	 * @return bool
	 */
	public function incrementForFilter( int $filter, bool $matched ): bool {
		return $this->stash->merge(
			$this->createFilterKey( $filter ),
			static function ( $cache, $key, $value, &$expiry ) use ( $matched ) {
				if ( $value === false ) {
					return false;
				}
				$value['total']++;
				if ( $matched ) {
					$value['matches']++;
				}
				// enforce the prior TTL
				$expiry = $value['expiry'];
				return $value;
			}
		);
	}

	/**
	 * Get the cache entry for the filter. Returns false when the key has already expired.
	 * Otherwise it returns the entry formatted as [ 'total' => number of actions,
	 * 'matches' => number of hits ] (since the last filter modification).
	 *
	 * @param int $filter
	 * @return array|false
	 */
	public function getForFilter( int $filter ) {
		$value = $this->stash->get( $this->createFilterKey( $filter ) );
		if ( $value !== false ) {
			unset( $value['expiry'] );
		}
		return $value;
	}

	/**
	 * @param string $group
	 * @return string
	 */
	private function createGroupKey( string $group ): string {
		return $this->stash->makeKey( 'abusefilter', 'emergency', 'group', $group );
	}

	/**
	 * @param int $filter
	 * @return string
	 */
	private function createFilterKey( int $filter ): string {
		return $this->stash->makeKey( 'abusefilter', 'emergency', 'filter', $filter );
	}

}