Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
31 / 31 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
ContributionInfoRetriever | |
100.00% |
31 / 31 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
retrieveFor | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\IPInfo\InfoRetriever; |
4 | |
5 | use MediaWiki\IPInfo\Info\ContributionInfo; |
6 | use MediaWiki\User\ActorNormalization; |
7 | use MediaWiki\User\UserIdentity; |
8 | use Wikimedia\Rdbms\IConnectionProvider; |
9 | |
10 | class ContributionInfoRetriever extends BaseInfoRetriever { |
11 | public const NAME = 'ipinfo-source-contributions'; |
12 | |
13 | private IConnectionProvider $dbProvider; |
14 | private ActorNormalization $actorNormalization; |
15 | |
16 | public function __construct( |
17 | IConnectionProvider $dbProvider, |
18 | ActorNormalization $actorNormalization |
19 | ) { |
20 | $this->dbProvider = $dbProvider; |
21 | $this->actorNormalization = $actorNormalization; |
22 | } |
23 | |
24 | /** @inheritDoc */ |
25 | public function getName(): string { |
26 | return self::NAME; |
27 | } |
28 | |
29 | /** @inheritDoc */ |
30 | public function retrieveFor( UserIdentity $user, ?string $ip ): ContributionInfo { |
31 | $dbr = $this->dbProvider->getReplicaDatabase(); |
32 | $actorId = $this->actorNormalization->findActorId( $user, $dbr ); |
33 | |
34 | if ( $actorId === null ) { |
35 | return new ContributionInfo(); |
36 | } |
37 | |
38 | $numLocalEdits = $dbr->newSelectQueryBuilder() |
39 | ->from( 'revision' ) |
40 | ->where( [ |
41 | 'rev_actor' => $actorId, |
42 | ] ) |
43 | ->caller( __METHOD__ ) |
44 | ->fetchRowCount(); |
45 | |
46 | $oneDayTS = (int)wfTimestamp( TS_UNIX ) - ( 24 * 60 * 60 ); |
47 | $numRecentEdits = $dbr->newSelectQueryBuilder() |
48 | ->from( 'revision' ) |
49 | ->where( [ |
50 | 'rev_actor' => $actorId, |
51 | $dbr->expr( 'rev_timestamp', '>', $dbr->timestamp( $oneDayTS ) ), |
52 | ] ) |
53 | ->caller( __METHOD__ ) |
54 | ->fetchRowCount(); |
55 | |
56 | $numDeletedEdits = $dbr->newSelectQueryBuilder() |
57 | ->from( 'archive' ) |
58 | ->where( [ |
59 | 'ar_actor' => $actorId, |
60 | ] ) |
61 | ->caller( __METHOD__ ) |
62 | ->fetchRowCount(); |
63 | |
64 | return new ContributionInfo( $numLocalEdits, $numRecentEdits, $numDeletedEdits ); |
65 | } |
66 | } |