Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
CargoLinksUpdateHandler
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 onLinksUpdate
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3use MediaWiki\Deferred\LinksUpdate\LinksUpdate;
4use MediaWiki\Hook\LinksUpdateHook;
5
6class CargoLinksUpdateHandler implements LinksUpdateHook {
7
8    /**
9     * Update backlinks for Cargo queries when a page is saved.
10     * @param LinksUpdate $linksUpdate
11     */
12    public function onLinksUpdate( $linksUpdate ): void {
13        $parserOutput = $linksUpdate->getParserOutput();
14        $backlinks = array_keys(
15            $parserOutput->getExtensionData( CargoBackLinks::BACKLINKS_DATA_KEY ) ?? []
16        );
17
18        if ( count( $backlinks ) > 0 ) {
19            CargoBackLinks::setBackLinks( $linksUpdate->getTitle(), $backlinks );
20        } else {
21            $pageId = $linksUpdate->getPageId();
22            CargoBackLinks::removeBackLinks( $pageId );
23        }
24
25        // Don't save ephemeral data into the parser cache.
26        $parserOutput->setExtensionData( CargoBackLinks::BACKLINKS_DATA_KEY, null );
27    }
28}