Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
AbstractTemporaryAccountsInstrumentation | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getUserType | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | namespace WikimediaEvents\TemporaryAccounts; |
3 | |
4 | use MediaWiki\User\UserFactory; |
5 | use MediaWiki\User\UserIdentity; |
6 | use MediaWiki\User\UserIdentityUtils; |
7 | use Wikimedia\IPUtils; |
8 | |
9 | /** |
10 | * Abstract class used a as base for temporary account instrumentation hook handler classes that have need |
11 | * to label an event by the type of user that performed the event and/or was targeted in the event. |
12 | */ |
13 | abstract class AbstractTemporaryAccountsInstrumentation { |
14 | |
15 | public const ACCOUNT_TYPE_TEMPORARY = 'temp'; |
16 | public const ACCOUNT_TYPE_ANON = 'anon'; |
17 | public const ACCOUNT_TYPE_IP_RANGE = 'iprange'; |
18 | public const ACCOUNT_TYPE_BOT = 'bot'; |
19 | public const ACCOUNT_TYPE_NORMAL = 'normal'; |
20 | |
21 | protected UserIdentityUtils $userIdentityUtils; |
22 | protected UserFactory $userFactory; |
23 | |
24 | public function __construct( |
25 | UserIdentityUtils $userIdentityUtils, |
26 | UserFactory $userFactory |
27 | ) { |
28 | $this->userIdentityUtils = $userIdentityUtils; |
29 | $this->userFactory = $userFactory; |
30 | } |
31 | |
32 | /** |
33 | * Get the type of user for use as a Prometheus label. |
34 | * @param UserIdentity|string $user For single IP addresses, temporary accounts, named accounts, |
35 | * and bot accounts, this will be a user identity. For IP ranges, this will be a string. |
36 | * @return string One of the TemporaryAccountsInstrumentation::ACCOUNT_TYPE_* constants |
37 | */ |
38 | protected function getUserType( $user ): string { |
39 | if ( !$user instanceof UserIdentity ) { |
40 | // Must be an IP range. |
41 | return self::ACCOUNT_TYPE_IP_RANGE; |
42 | } |
43 | if ( $this->userIdentityUtils->isTemp( $user ) ) { |
44 | return self::ACCOUNT_TYPE_TEMPORARY; |
45 | } |
46 | |
47 | if ( IPUtils::isIPAddress( $user->getName() ) ) { |
48 | return self::ACCOUNT_TYPE_ANON; |
49 | } |
50 | |
51 | $user = $this->userFactory->newFromUserIdentity( $user ); |
52 | if ( $user->isBot() ) { |
53 | return self::ACCOUNT_TYPE_BOT; |
54 | } |
55 | |
56 | return self::ACCOUNT_TYPE_NORMAL; |
57 | } |
58 | } |