Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.36% covered (warning)
89.36%
42 / 47
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoBackLinks
89.36% covered (warning)
89.36%
42 / 47
25.00% covered (danger)
25.00%
1 / 4
19.43
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
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
4.03
 setBackLinks
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
0.00%
0 / 1
6.06
 purgePagesThatQueryThisPage
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
6.07
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        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
33        $dbw = $lb->getConnectionRef( DB_PRIMARY );
34        if ( $dbw->tableExists( 'cargo_backlinks' ) && !$dbw->isReadOnly() ) {
35            $dbw->delete( 'cargo_backlinks', [
36                'cbl_query_page_id' => $pageId
37            ], __METHOD__ );
38        }
39    }
40
41    public static function setBackLinks( $title, $resultsPageIds ) {
42        global $wgCargoIgnoreBacklinks;
43        if ( $wgCargoIgnoreBacklinks ) {
44            return;
45        }
46
47        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
48        $dbw = $lb->getConnectionRef( DB_PRIMARY );
49        if ( !$dbw->tableExists( 'cargo_backlinks' ) || $dbw->isReadOnly() ) {
50            return;
51        }
52        // Sanity check
53        $resultsPageIds = array_unique( $resultsPageIds );
54
55        $pageId = $title->getArticleID();
56        $dbw->delete( 'cargo_backlinks', [
57            'cbl_query_page_id' => $pageId
58        ], __METHOD__ );
59
60        foreach ( $resultsPageIds as $resultPageId ) {
61            if ( $resultPageId ) {
62                $dbw->insert( 'cargo_backlinks', [
63                    'cbl_query_page_id' => $pageId,
64                    'cbl_result_page_id' => $resultPageId,
65                 ] );
66            }
67        }
68    }
69
70    public static function purgePagesThatQueryThisPage( $resultPageId ) {
71        global $wgCargoIgnoreBacklinks;
72        if ( $wgCargoIgnoreBacklinks ) {
73            return;
74        }
75
76        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
77        $dbr = $lb->getConnectionRef( DB_REPLICA );
78        if ( !$dbr->tableExists( 'cargo_backlinks' ) ) {
79            return;
80        }
81
82        $res = $dbr->select( 'cargo_backlinks',
83            [ 'cbl_query_page_id' ],
84            [ 'cbl_result_page_id' => $resultPageId ] );
85        $wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
86        foreach ( $res as $row ) {
87            $queryPageId = $row->cbl_query_page_id;
88            if ( $queryPageId ) {
89                $page = $wikiPageFactory->newFromID( $queryPageId );
90                if ( $page ) {
91                    $page->doPurge();
92                }
93            }
94        }
95    }
96}