Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoBackLinks
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 4
506
0.00% covered (danger)
0.00%
0 / 1
 managePageDeletion
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 removeBackLinks
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 setBackLinks
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
42
 purgePagesThatQueryThisPage
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2
3use MediaWiki\MediaWikiServices;
4
5if ( !defined( 'DB_PRIMARY' ) ) {
6    // MW < 1.37
7    define( 'DB_PRIMARY', DB_MASTER );
8}
9
10class CargoBackLinks {
11    public static function managePageDeletion( $pageId ) {
12        if ( method_exists( MediaWikiServices::class, 'getWikiPageFactory' ) ) {
13            $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromID( $pageId );
14        } else {
15            $page = \WikiPage::newFromID( $pageId );
16        }
17        $pageTitle = $page ? $page->getTitle() : null;
18        if ( $pageTitle ) {
19            $pageId = $pageTitle->getArticleID();
20            // Purge the cache of all pages that may have this
21            // page in their displayed query results.
22            self::purgePagesThatQueryThisPage( $pageId );
23        }
24        // Remove all entries that are based on queries that were
25        // on this page.
26        self::removeBackLinks( $pageId );
27    }
28
29    public static function removeBackLinks( $pageId ) {
30        global $wgCargoIgnoreBacklinks;
31        if ( $wgCargoIgnoreBacklinks ) {
32            return;
33        }
34
35        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
36        $dbw = $lb->getConnectionRef( DB_PRIMARY );
37        if ( $dbw->tableExists( 'cargo_backlinks' ) && !$dbw->isReadOnly() ) {
38            $dbw->delete( 'cargo_backlinks', [
39                'cbl_query_page_id' => $pageId
40            ], __METHOD__ );
41        }
42    }
43
44    public static function setBackLinks( $title, $resultsPageIds ) {
45        global $wgCargoIgnoreBacklinks;
46        if ( $wgCargoIgnoreBacklinks ) {
47            return;
48        }
49
50        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
51        $dbw = $lb->getConnectionRef( DB_PRIMARY );
52        if ( !$dbw->tableExists( 'cargo_backlinks' ) || $dbw->isReadOnly() ) {
53            return;
54        }
55        // Sanity check
56        $resultsPageIds = array_unique( $resultsPageIds );
57
58        $pageId = $title->getArticleID();
59        $dbw->delete( 'cargo_backlinks', [
60            'cbl_query_page_id' => $pageId
61        ], __METHOD__ );
62
63        foreach ( $resultsPageIds as $resultPageId ) {
64            if ( $resultPageId ) {
65                $dbw->insert( 'cargo_backlinks', [
66                    'cbl_query_page_id' => $pageId,
67                    'cbl_result_page_id' => $resultPageId,
68                 ] );
69            }
70        }
71    }
72
73    public static function purgePagesThatQueryThisPage( $resultPageId ) {
74        global $wgCargoIgnoreBacklinks;
75        if ( $wgCargoIgnoreBacklinks ) {
76            return;
77        }
78
79        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
80        $dbr = $lb->getConnectionRef( DB_REPLICA );
81        if ( !$dbr->tableExists( 'cargo_backlinks' ) ) {
82            return;
83        }
84
85        $res = $dbr->select( 'cargo_backlinks',
86            [ 'cbl_query_page_id' ],
87            [ 'cbl_result_page_id' => $resultPageId ] );
88        if ( method_exists( MediaWikiServices::class, 'getWikiPageFactory' ) ) {
89            // MW 1.36+
90            $wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
91        } else {
92            $wikiPageFactory = null;
93        }
94        foreach ( $res as $row ) {
95            $queryPageId = $row->cbl_query_page_id;
96            if ( $queryPageId ) {
97                if ( $wikiPageFactory !== null ) {
98                    // MW 1.36+
99                    $page = $wikiPageFactory->newFromID( $queryPageId );
100                } else {
101                    $page = \WikiPage::newFromID( $queryPageId );
102                }
103                if ( $page ) {
104                    $page->doPurge();
105                }
106            }
107        }
108    }
109}