Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
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 / 38
0.00% covered (danger)
0.00%
0 / 4
306
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 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 setBackLinks
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 purgePagesThatQueryThisPage
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
56
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        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
31        $dbw = $lb->getConnectionRef( DB_PRIMARY );
32        if ( $dbw->tableExists( 'cargo_backlinks' ) ) {
33            $dbw->delete( 'cargo_backlinks', [
34                'cbl_query_page_id' => $pageId
35            ], __METHOD__ );
36        }
37    }
38
39    public static function setBackLinks( $title, $resultsPageIds ) {
40        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
41        $dbw = $lb->getConnectionRef( DB_PRIMARY );
42        if ( !$dbw->tableExists( 'cargo_backlinks' ) ) {
43            return;
44        }
45        // Sanity check
46        $resultsPageIds = array_unique( $resultsPageIds );
47
48        $pageId = $title->getArticleID();
49        $dbw->delete( 'cargo_backlinks', [
50            'cbl_query_page_id' => $pageId
51        ], __METHOD__ );
52
53        foreach ( $resultsPageIds as $resultPageId ) {
54            if ( $resultPageId ) {
55                $dbw->insert( 'cargo_backlinks', [
56                    'cbl_query_page_id' => $pageId,
57                    'cbl_result_page_id' => $resultPageId,
58                 ] );
59            }
60        }
61    }
62
63    public static function purgePagesThatQueryThisPage( $resultPageId ) {
64        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
65        $dbr = $lb->getConnectionRef( DB_REPLICA );
66        if ( !$dbr->tableExists( 'cargo_backlinks' ) ) {
67            return;
68        }
69
70        $res = $dbr->select( 'cargo_backlinks',
71            [ 'cbl_query_page_id' ],
72            [ 'cbl_result_page_id' => $resultPageId ] );
73        if ( method_exists( MediaWikiServices::class, 'getWikiPageFactory' ) ) {
74            // MW 1.36+
75            $wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
76        } else {
77            $wikiPageFactory = null;
78        }
79        foreach ( $res as $row ) {
80            $queryPageId = $row->cbl_query_page_id;
81            if ( $queryPageId ) {
82                if ( $wikiPageFactory !== null ) {
83                    // MW 1.36+
84                    $page = $wikiPageFactory->newFromID( $queryPageId );
85                } else {
86                    $page = \WikiPage::newFromID( $queryPageId );
87                }
88                if ( $page ) {
89                    $page->doPurge();
90                }
91            }
92        }
93    }
94}