Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
PruneOldBounceRecords
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 pruneOldRecords
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 getOldRecords
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace MediaWiki\Extension\BounceHandler;
4
5/**
6 * Class PruneOldBounceRecords
7 *
8 * Prune old bounce records from the 'bounce_records' table
9 *
10 * @file
11 * @ingroup Extensions
12 * @author Tony Thomas <01tonythomas@gmail.com>
13 * @license GPL-2.0-or-later
14 */
15class PruneOldBounceRecords {
16
17    private int $bounceRecordMaxAge;
18
19    public function __construct( int $bounceRecordMaxAge ) {
20        $this->bounceRecordMaxAge = $bounceRecordMaxAge;
21    }
22
23    /**
24     * Prune old bounce records
25     */
26    public function pruneOldRecords() {
27        $idArray = $this->getOldRecords();
28        $idArrayCount = count( $idArray );
29        if ( $idArrayCount > 0 ) {
30            $dbw = ProcessBounceEmails::getBounceRecordPrimaryDB();
31            $dbw->newDeleteQueryBuilder()
32                ->deleteFrom( 'bounce_records' )
33                ->where( [
34                    'br_id' => $idArray
35                ] )
36                ->caller( __METHOD__ )
37                ->execute();
38            wfDebugLog( 'BounceHandler', "Pruned $idArrayCount bounce records." );
39        }
40    }
41
42    /**
43     * Get Old bounce records from DB
44     *
45     * @return int[]
46     */
47    private function getOldRecords() {
48        $idArray = [];
49        $maximumRecordAge = time() - $this->bounceRecordMaxAge;
50        $dbr = ProcessBounceEmails::getBounceRecordReplicaDB();
51        $res = $dbr->newSelectQueryBuilder()
52            ->select( [ 'br_id' ] )
53            ->from( 'bounce_records' )
54            ->where( $dbr->expr( 'br_timestamp', '<', $dbr->timestamp( $maximumRecordAge ) ) )
55            ->limit( 100 )
56            ->caller( __METHOD__ )->fetchResultSet();
57
58        foreach ( $res as $row ) {
59            $idArray[] = (int)$row->br_id;
60        }
61
62        return $idArray;
63    }
64
65}