Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
AqsEditInfoService | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getEditsPerDay | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments; |
4 | |
5 | use DateTime; |
6 | use MediaWiki\Http\HttpRequestFactory; |
7 | use WANObjectCache; |
8 | use Wikimedia\Timestamp\ConvertibleTimestamp; |
9 | |
10 | /** |
11 | * Minimal client for the Wikimedia Analytics Query Service. |
12 | */ |
13 | class AqsEditInfoService extends EditInfoService { |
14 | |
15 | private HttpRequestFactory $requestFactory; |
16 | private WANObjectCache $cache; |
17 | |
18 | /** @var string Wiki name, in AQS format (domain prefix, e.g. 'en.wikipedia') */ |
19 | private string $wiki; |
20 | |
21 | /** |
22 | * @param HttpRequestFactory $requestFactory |
23 | * @param WANObjectCache $cache |
24 | * @param string $wiki Wiki name, in AQS format (domain prefix, e.g. 'en.wikipedia') |
25 | */ |
26 | public function __construct( |
27 | HttpRequestFactory $requestFactory, |
28 | WANObjectCache $cache, |
29 | string $wiki |
30 | ) { |
31 | $this->requestFactory = $requestFactory; |
32 | $this->cache = $cache; |
33 | $this->wiki = $wiki; |
34 | } |
35 | |
36 | /** @inheritDoc */ |
37 | public function getEditsPerDay() { |
38 | // AQS edit processing seems to be done monthly, but sometimes the processing lags, |
39 | // so use the day two months ago. |
40 | // The number shown above "edits in the last day" will be somewhat fake, |
41 | // but hopefully no one cares. |
42 | $day = new DateTime( '@' . ConvertibleTimestamp::time() . '-2 month -1 day' ); |
43 | $dayAfter = new DateTime( '@' . ConvertibleTimestamp::time() . '-2 month' ); |
44 | |
45 | return $this->cache->getWithSetCallback( |
46 | $this->cache->makeKey( 'GrowthExperiments', 'AQS', 'edits', |
47 | $this->wiki, $day->format( 'Ymd' ) ), |
48 | WANObjectCache::TTL_DAY, |
49 | function ( $oldValue, &$ttl, &$setOpts ) use ( $day, $dayAfter ) { |
50 | $url = 'https://wikimedia.org/api/rest_v1/metrics/edits/aggregate/' . $this->wiki |
51 | . '/user/content/daily/' . $day->format( 'Ymd' ) . '/' . $dayAfter->format( 'Ymd' ); |
52 | |
53 | $status = Util::getJsonUrl( $this->requestFactory, $url ); |
54 | if ( !$status->isOK() ) { |
55 | // Use short cache TTL for errors |
56 | $ttl = WANObjectCache::TTL_MINUTE; |
57 | return $status; |
58 | } |
59 | |
60 | $data = $status->getValue(); |
61 | $edits = 0; |
62 | // There should be 0 or 1 rows depending on whether there was any edit on the given day. |
63 | foreach ( $data['items'][0]['results'] ?? [] as $row ) { |
64 | $edits += $row['edits']; |
65 | } |
66 | return $edits; |
67 | } |
68 | ); |
69 | } |
70 | |
71 | } |