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    /**
18     * @var int
19     */
20    private $bounceRecordMaxAge;
21
22    /**
23     * @param int $bounceRecordMaxAge
24     */
25    public function __construct( $bounceRecordMaxAge ) {
26        $this->bounceRecordMaxAge = $bounceRecordMaxAge;
27    }
28
29    /**
30     * Prune old bounce records
31     *
32     * @param string $wikiId
33     *
34     */
35    public function pruneOldRecords( $wikiId ) {
36        $idArray = $this->getOldRecords( $wikiId );
37        $idArrayCount = count( $idArray );
38        if ( $idArrayCount > 0 ) {
39            $dbw = ProcessBounceEmails::getBounceRecordDB( DB_PRIMARY, $wikiId );
40            $dbw->newDeleteQueryBuilder()
41                ->deleteFrom( 'bounce_records' )
42                ->where( [
43                    'br_id' => $idArray
44                ] )
45                ->caller( __METHOD__ )
46                ->execute();
47            wfDebugLog( 'BounceHandler', "Pruned $idArrayCount bounce records from $wikiId wiki." );
48        }
49    }
50
51    /**
52     * Get Old bounce records from DB
53     *
54     * @param string $wikiId
55     * @return int[]
56     */
57    private function getOldRecords( $wikiId ) {
58        $idArray = [];
59        $maximumRecordAge = time() - $this->bounceRecordMaxAge;
60        $dbr = ProcessBounceEmails::getBounceRecordDB( DB_REPLICA, $wikiId );
61        $res = $dbr->newSelectQueryBuilder()
62            ->select( [ 'br_id' ] )
63            ->from( 'bounce_records' )
64            ->where( $dbr->expr( 'br_timestamp', '<', $dbr->timestamp( $maximumRecordAge ) ) )
65            ->limit( 100 )
66            ->caller( __METHOD__ )->fetchResultSet();
67
68        foreach ( $res as $row ) {
69            $idArray[] = (int)$row->br_id;
70        }
71
72        return $idArray;
73    }
74
75}