Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.69% covered (success)
94.69%
107 / 113
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
19require_once __DIR__ . '/Maintenance.php';
20
21/**
22 * Remove autopatrol logs in the logging table.
23 *
24 * @ingroup Maintenance
25 */
26class DeleteAutoPatrolLogs extends Maintenance {
27
28    public function __construct() {
29        parent::__construct();
30        $this->addDescription( 'Remove autopatrol logs in the logging table' );
31        $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' );
32        $this->addOption(
33            'check-old',
34            'Check old patrol logs (for deleting old format autopatrols).'
35        );
36        $this->addOption(
37            'before',
38            'Timestamp to delete only before that time, all MediaWiki timestamp formats are accepted',
39            false,
40            true
41        );
42        $this->addOption(
43            'from-id',
44            'First row (log id) to start updating from',
45            false,
46            true
47        );
48        $this->addOption(
49            'sleep',
50            'Sleep time (in seconds) between every batch',
51            false,
52            true
53        );
54        $this->setBatchSize( 1000 );
55    }
56
57    public function execute() {
58        $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
59
60        $sleep = (int)$this->getOption( 'sleep', 10 );
61        $fromId = $this->getOption( 'from-id', null );
62        $this->countDown( 5 );
63        while ( true ) {
64            if ( $this->hasOption( 'check-old' ) ) {
65                $rowsData = $this->getRowsOld( $fromId );
66                // We reached end of the table
67                if ( !$rowsData ) {
68                    break;
69                }
70                $rows = $rowsData['rows'];
71                $fromId = $rowsData['lastId'];
72
73                // There is nothing to delete in this batch
74                if ( !$rows ) {
75                    continue;
76                }
77            } else {
78                $rows = $this->getRows( $fromId );
79                if ( !$rows ) {
80                    break;
81                }
82                $fromId = end( $rows );
83            }
84
85            if ( $this->hasOption( 'dry-run' ) ) {
86                $this->output( 'These rows will get deleted: ' . implode( ', ', $rows ) . "\n" );
87            } else {
88                $this->deleteRows( $rows );
89                $this->output( 'Processed up to row id ' . end( $rows ) . "\n" );
90            }
91
92            if ( $sleep > 0 ) {
93                sleep( $sleep );
94            }
95        }
96    }
97
98    private function getRows( $fromId ) {
99        $dbr = $this->getReplicaDB();
100        $before = $this->getOption( 'before', false );
101
102        $conds = [
103            'log_type' => 'patrol',
104            'log_action' => 'autopatrol',
105        ];
106
107        if ( $fromId ) {
108            $conds[] = $dbr->expr( 'log_id', '>', $fromId );
109        }
110
111        if ( $before ) {
112            $conds[] = $dbr->expr( 'log_timestamp', '<', $dbr->timestamp( $before ) );
113        }
114
115        return $dbr->newSelectQueryBuilder()
116            ->select( 'log_id' )
117            ->from( 'logging' )
118            ->where( $conds )
119            ->orderBy( 'log_id' )
120            ->limit( $this->getBatchSize() )
121            ->caller( __METHOD__ )
122            ->fetchFieldValues();
123    }
124
125    private function getRowsOld( $fromId ) {
126        $dbr = $this->getReplicaDB();
127        $batchSize = $this->getBatchSize();
128        $before = $this->getOption( 'before', false );
129
130        $conds = [
131            'log_type' => 'patrol',
132            'log_action' => 'patrol',
133        ];
134
135        if ( $fromId ) {
136            $conds[] = $dbr->expr( 'log_id', '>', $fromId );
137        }
138
139        if ( $before ) {
140            $conds[] = $dbr->expr( 'log_timestamp', '<', $dbr->timestamp( $before ) );
141        }
142
143        $result = $dbr->newSelectQueryBuilder()
144            ->select( [ 'log_id', 'log_params' ] )
145            ->from( 'logging' )
146            ->where( $conds )
147            ->orderBy( 'log_id' )
148            ->limit( $batchSize )
149            ->caller( __METHOD__ )
150            ->fetchResultSet();
151
152        $last = null;
153        $autopatrols = [];
154        foreach ( $result as $row ) {
155            $last = $row->log_id;
156            $logEntry = DatabaseLogEntry::newFromRow( $row );
157            $params = $logEntry->getParameters();
158            if ( !is_array( $params ) ) {
159                continue;
160            }
161
162            // This logic belongs to PatrolLogFormatter::getMessageKey
163            // and LogFormatter::extractParameters the 'auto' value is logically presented as key [5].
164            // For legacy case the logical key is index + 3, meaning [2].
165            // For the modern case, the logical key is index - 1 meaning [6].
166            if ( array_key_exists( '6::auto', $params ) ) {
167                // Between 2011-2016 autopatrol logs
168                $auto = $params['6::auto'] === true;
169            } elseif ( $logEntry->isLegacy() === true && array_key_exists( 2, $params ) ) {
170                // Pre-2011 autopatrol logs
171                $auto = $params[2] === '1';
172            } else {
173                continue;
174            }
175
176            if ( $auto ) {
177                $autopatrols[] = $row->log_id;
178            }
179        }
180
181        if ( $last === null ) {
182            return null;
183        }
184
185        return [ 'rows' => $autopatrols, 'lastId' => $last ];
186    }
187
188    private function deleteRows( array $rows ) {
189        $dbw = $this->getPrimaryDB();
190
191        $dbw->newDeleteQueryBuilder()
192            ->deleteFrom( 'logging' )
193            ->where( [ 'log_id' => $rows ] )
194            ->caller( __METHOD__ )->execute();
195
196        $this->waitForReplication();
197    }
198
199}
200
201$maintClass = DeleteAutoPatrolLogs::class;
202require_once RUN_MAINTENANCE_IF_MAIN;