Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.64% covered (warning)
88.64%
39 / 44
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoBackLinks
88.64% covered (warning)
88.64%
39 / 44
25.00% covered (danger)
25.00%
1 / 4
19.53
0.00% covered (danger)
0.00%
0 / 1
 managePageDeletion
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 removeBackLinks
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 setBackLinks
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
6.07
 purgePagesThatQueryThisPage
86.67% covered (warning)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 1
6.09
1<?php
2
3use MediaWiki\MediaWikiServices;
4
5class CargoBackLinks {
6
7    /**
8     * ParserOutput extension data key for backlinks.
9     */
10    public const BACKLINKS_DATA_KEY = 'ext-cargo-backlinks';
11
12    public static function managePageDeletion( $pageId ) {
13        $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromID( $pageId );
14        $pageTitle = $page ? $page->getTitle() : null;
15        if ( $pageTitle ) {
16            $pageId = $pageTitle->getArticleID();
17            // Purge the cache of all pages that may have this
18            // page in their displayed query results.
19            self::purgePagesThatQueryThisPage( $pageId );
20        }
21        // Remove all entries that are based on queries that were
22        // on this page.
23        self::removeBackLinks( $pageId );
24    }
25
26    public static function removeBackLinks( $pageId ) {
27        global $wgCargoIgnoreBacklinks;
28        if ( $wgCargoIgnoreBacklinks ) {
29            return;
30        }
31
32        $dbw = CargoUtils::getMainDBForWrite();
33        if ( $dbw->tableExists( 'cargo_backlinks' ) && !$dbw->isReadOnly() ) {
34            $dbw->delete( 'cargo_backlinks', [
35                'cbl_query_page_id' => $pageId
36            ], __METHOD__ );
37        }
38    }
39
40    public static function setBackLinks( $title, $resultsPageIds ) {
41        global $wgCargoIgnoreBacklinks;
42        if ( $wgCargoIgnoreBacklinks ) {
43            return;
44        }
45
46        $dbw = CargoUtils::getMainDBForWrite();
47        if ( !$dbw->tableExists( 'cargo_backlinks' ) || $dbw->isReadOnly() ) {
48            return;
49        }
50        // Sanity check
51        $resultsPageIds = array_unique( $resultsPageIds );
52
53        $pageId = $title->getArticleID();
54        $dbw->delete( 'cargo_backlinks', [
55            'cbl_query_page_id' => $pageId
56        ], __METHOD__ );
57
58        foreach ( $resultsPageIds as $resultPageId ) {
59            if ( $resultPageId ) {
60                $dbw->insert( 'cargo_backlinks', [
61                    'cbl_query_page_id' => $pageId,
62                    'cbl_result_page_id' => $resultPageId,
63                 ] );
64            }
65        }
66    }
67
68    public static function purgePagesThatQueryThisPage( $resultPageId ) {
69        global $wgCargoIgnoreBacklinks;
70        if ( $wgCargoIgnoreBacklinks ) {
71            return;
72        }
73
74        $dbr = CargoUtils::getMainDBForRead();
75        if ( !$dbr->tableExists( 'cargo_backlinks' ) ) {
76            return;
77        }
78
79        $res = $dbr->select( 'cargo_backlinks',
80            [ 'cbl_query_page_id' ],
81            [ 'cbl_result_page_id' => $resultPageId ] );
82        $wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
83        foreach ( $res as $row ) {
84            $queryPageId = $row->cbl_query_page_id;
85            if ( $queryPageId ) {
86                $page = $wikiPageFactory->newFromID( $queryPageId );
87                if ( $page ) {
88                    $page->doPurge();
89                }
90            }
91        }
92    }
93}