Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
42.86% covered (danger)
42.86%
6 / 14
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
EventPageCacheUpdater
42.86% covered (danger)
42.86%
6 / 14
33.33% covered (danger)
33.33%
1 / 3
9.66
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 adjustCacheForPageWithRegistration
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 purgeEventPageCache
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare( strict_types=1 );
4
5namespace MediaWiki\Extension\CampaignEvents\EventPage;
6
7use HtmlCacheUpdater;
8use MediaWiki\Extension\CampaignEvents\Event\EventRegistration;
9use MediaWiki\Extension\CampaignEvents\Event\ExistingEventRegistration;
10use MediaWiki\Extension\CampaignEvents\MWEntity\MWPageProxy;
11use MediaWiki\Output\OutputPage;
12use MediaWiki\Utils\MWTimestamp;
13use RuntimeException;
14
15/**
16 * This class is responsible of managing the cache of event pages, to avoid issues like T326593.
17 */
18class EventPageCacheUpdater {
19    public const SERVICE_NAME = 'CampaignEventsEventPageCacheUpdater';
20
21    private HtmlCacheUpdater $htmlCacheUpdater;
22
23    /**
24     * @param HtmlCacheUpdater $htmlCacheUpdater
25     */
26    public function __construct( HtmlCacheUpdater $htmlCacheUpdater ) {
27        $this->htmlCacheUpdater = $htmlCacheUpdater;
28    }
29
30    /**
31     * @param OutputPage $out
32     * @param ExistingEventRegistration $registration
33     */
34    public function adjustCacheForPageWithRegistration(
35        OutputPage $out,
36        ExistingEventRegistration $registration
37    ): void {
38        $endTSUnix = (int)wfTimestamp( TS_UNIX, $registration->getEndUTCTimestamp() );
39        $now = (int)MWTimestamp::now( TS_UNIX );
40        if ( $endTSUnix < $now ) {
41            // The event has ended, so it's presumably safe to allow normal caching.
42            return;
43        }
44
45        // Do not let the cached version persist after the end date.
46        $secondsToEventEnd = $endTSUnix - $now;
47        $out->lowerCdnMaxage( $secondsToEventEnd );
48    }
49
50    /**
51     * @param EventRegistration $registration
52     * @return void
53     */
54    public function purgeEventPageCache( EventRegistration $registration ): void {
55        $eventPage = $registration->getPage();
56        if ( !$eventPage instanceof MWPageProxy ) {
57            throw new RuntimeException( 'Unexpected ICampaignsPage implementation.' );
58        }
59        $this->htmlCacheUpdater->purgeTitleUrls(
60            [ $eventPage->getPageIdentity() ],
61            HtmlCacheUpdater::PURGE_INTENT_TXROUND_REFLECTED
62        );
63    }
64}