Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 67
CargoBackLinks
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
306
0.00% covered (danger)
0.00%
0 / 64
 managePageDeletion
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 12
 removeBackLinks
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 8
 setBackLinks
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 17
 purgePagesThatQueryThisPage
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 27
<?php
use MediaWiki\MediaWikiServices;
if ( !defined( 'DB_PRIMARY' ) ) {
    // MW < 1.37
    define( 'DB_PRIMARY', DB_MASTER );
}
class CargoBackLinks {
    public static function managePageDeletion( $pageId ) {
        if ( method_exists( MediaWikiServices::class, 'getWikiPageFactory' ) ) {
            $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromID( $pageId );
        } else {
            $page = \WikiPage::newFromID( $pageId );
        }
        $pageTitle = $page ? $page->getTitle() : null;
        if ( $pageTitle ) {
            $pageId = $pageTitle->getArticleID();
            // Purge the cache of all pages that may have this
            // page in their displayed query results.
            self::purgePagesThatQueryThisPage( $pageId );
        }
        // Remove all entries that are based on queries that were
        // on this page.
        self::removeBackLinks( $pageId );
    }
    public static function removeBackLinks( $pageId ) {
        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
        $dbw = $lb->getConnectionRef( DB_PRIMARY );
        if ( $dbw->tableExists( 'cargo_backlinks' ) ) {
            $dbw->delete( 'cargo_backlinks', [
                'cbl_query_page_id' => $pageId
            ], __METHOD__ );
        }
    }
    public static function setBackLinks( $title, $resultsPageIds ) {
        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
        $dbw = $lb->getConnectionRef( DB_PRIMARY );
        if ( !$dbw->tableExists( 'cargo_backlinks' ) ) {
            return;
        }
        // Sanity check
        $resultsPageIds = array_unique( $resultsPageIds );
        $pageId = $title->getArticleID();
        self::removeBackLinks( $pageId );
        foreach ( $resultsPageIds as $resultPageId ) {
            if ( $resultPageId ) {
                $dbw->insert( 'cargo_backlinks', [
                    'cbl_query_page_id' => $pageId,
                    'cbl_result_page_id' => $resultPageId,
                 ] );
            }
        }
    }
    public static function purgePagesThatQueryThisPage( $resultPageId ) {
        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
        $dbr = $lb->getConnectionRef( DB_REPLICA );
        if ( !$dbr->tableExists( 'cargo_backlinks' ) ) {
            return;
        }
        $res = $dbr->select( 'cargo_backlinks',
            [ 'cbl_query_page_id' ],
            [ 'cbl_result_page_id' => $resultPageId ] );
        if ( method_exists( MediaWikiServices::class, 'getWikiPageFactory' ) ) {
            // MW 1.36+
            $wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
        } else {
            $wikiPageFactory = null;
        }
        foreach ( $res as $row ) {
            $queryPageId = $row->cbl_query_page_id;
            if ( $queryPageId ) {
                if ( $wikiPageFactory !== null ) {
                    // MW 1.36+
                    $page = $wikiPageFactory->newFromID( $queryPageId );
                } else {
                    $page = \WikiPage::newFromID( $queryPageId );
                }
                if ( $page ) {
                    $page->doPurge();
                }
            }
        }
    }
}