Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.27% covered (success)
97.27%
107 / 110
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteAutoPatrolLogs
97.27% covered (success)
97.27%
107 / 110
60.00% covered (warning)
60.00%
3 / 5
23
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
1
 execute
95.65% covered (success)
95.65%
22 / 23
0.00% covered (danger)
0.00%
0 / 1
8
 getRows
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
3
 getRowsOld
94.59% covered (success)
94.59%
35 / 37
0.00% covered (danger)
0.00%
0 / 1
10.02
 deleteRows
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19use MediaWiki\Maintenance\Maintenance;
20
21// @codeCoverageIgnoreStart
22require_once __DIR__ . '/Maintenance.php';
23// @codeCoverageIgnoreEnd
24
25/**
26 * Remove autopatrol logs in the logging table.
27 *
28 * @ingroup Maintenance
29 */
30class DeleteAutoPatrolLogs extends Maintenance {
31
32    public function __construct() {
33        parent::__construct();
34        $this->addDescription( 'Remove autopatrol logs in the logging table' );
35        $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' );
36        $this->addOption(
37            'check-old',
38            'Check old patrol logs (for deleting old format autopatrols).'
39        );
40        $this->addOption(
41            'before',
42            'Timestamp to delete only before that time, all MediaWiki timestamp formats are accepted',
43            false,
44            true
45        );
46        $this->addOption(
47            'from-id',
48            'First row (log id) to start updating from',
49            false,
50            true
51        );
52        $this->addOption(
53            'sleep',
54            'Sleep time (in seconds) between every batch',
55            false,
56            true
57        );
58        $this->setBatchSize( 1000 );
59    }
60
61    public function execute() {
62        $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
63
64        $sleep = (int)$this->getOption( 'sleep', 10 );
65        $fromId = $this->getOption( 'from-id', null );
66        $this->countDown( 5 );
67        while ( true ) {
68            if ( $this->hasOption( 'check-old' ) ) {
69                $rowsData = $this->getRowsOld( $fromId );
70                // We reached end of the table
71                if ( !$rowsData ) {
72                    break;
73                }
74                $rows = $rowsData['rows'];
75                $fromId = $rowsData['lastId'];
76
77                // There is nothing to delete in this batch
78                if ( !$rows ) {
79                    continue;
80                }
81            } else {
82                $rows = $this->getRows( $fromId );
83                if ( !$rows ) {
84                    break;
85                }
86                $fromId = end( $rows );
87            }
88
89            if ( $this->hasOption( 'dry-run' ) ) {
90                $this->output( 'These rows will get deleted: ' . implode( ', ', $rows ) . "\n" );
91            } else {
92                $this->deleteRows( $rows );
93                $this->output( 'Processed up to row id ' . end( $rows ) . "\n" );
94            }
95
96            if ( $sleep > 0 ) {
97                sleep( $sleep );
98            }
99        }
100    }
101
102    private function getRows( $fromId ) {
103        $dbr = $this->getReplicaDB();
104        $before = $this->getOption( 'before', false );
105
106        $conds = [
107            'log_type' => 'patrol',
108            'log_action' => 'autopatrol',
109        ];
110
111        if ( $fromId ) {
112            $conds[] = $dbr->expr( 'log_id', '>', $fromId );
113        }
114
115        if ( $before ) {
116            $conds[] = $dbr->expr( 'log_timestamp', '<', $dbr->timestamp( $before ) );
117        }
118
119        return $dbr->newSelectQueryBuilder()
120            ->select( 'log_id' )
121            ->from( 'logging' )
122            ->where( $conds )
123            ->orderBy( 'log_id' )
124            ->limit( $this->getBatchSize() )
125            ->caller( __METHOD__ )
126            ->fetchFieldValues();
127    }
128
129    private function getRowsOld( $fromId ) {
130        $dbr = $this->getReplicaDB();
131        $batchSize = $this->getBatchSize();
132        $before = $this->getOption( 'before', false );
133
134        $conds = [
135            'log_type' => 'patrol',
136            'log_action' => 'patrol',
137        ];
138
139        if ( $fromId ) {
140            $conds[] = $dbr->expr( 'log_id', '>', $fromId );
141        }
142
143        if ( $before ) {
144            $conds[] = $dbr->expr( 'log_timestamp', '<', $dbr->timestamp( $before ) );
145        }
146
147        $result = $dbr->newSelectQueryBuilder()
148            ->select( [ 'log_id', 'log_params' ] )
149            ->from( 'logging' )
150            ->where( $conds )
151            ->orderBy( 'log_id' )
152            ->limit( $batchSize )
153            ->caller( __METHOD__ )
154            ->fetchResultSet();
155
156        $last = null;
157        $autopatrols = [];
158        foreach ( $result as $row ) {
159            $last = $row->log_id;
160            $logEntry = DatabaseLogEntry::newFromRow( $row );
161            $params = $logEntry->getParameters();
162            if ( !is_array( $params ) ) {
163                continue;
164            }
165
166            // This logic belongs to PatrolLogFormatter::getMessageKey
167            // and LogFormatter::extractParameters the 'auto' value is logically presented as key [5].
168            // For legacy case the logical key is index + 3, meaning [2].
169            // For the modern case, the logical key is index - 1 meaning [6].
170            if ( array_key_exists( '6::auto', $params ) ) {
171                // Between 2011-2016 autopatrol logs
172                $auto = $params['6::auto'] === true;
173            } elseif ( $logEntry->isLegacy() === true && array_key_exists( 2, $params ) ) {
174                // Pre-2011 autopatrol logs
175                $auto = $params[2] === '1';
176            } else {
177                continue;
178            }
179
180            if ( $auto ) {
181                $autopatrols[] = $row->log_id;
182            }
183        }
184
185        if ( $last === null ) {
186            return null;
187        }
188
189        return [ 'rows' => $autopatrols, 'lastId' => $last ];
190    }
191
192    private function deleteRows( array $rows ) {
193        $dbw = $this->getPrimaryDB();
194
195        $dbw->newDeleteQueryBuilder()
196            ->deleteFrom( 'logging' )
197            ->where( [ 'log_id' => $rows ] )
198            ->caller( __METHOD__ )->execute();
199
200        $this->waitForReplication();
201    }
202
203}
204
205// @codeCoverageIgnoreStart
206$maintClass = DeleteAutoPatrolLogs::class;
207require_once RUN_MAINTENANCE_IF_MAIN;
208// @codeCoverageIgnoreEnd