Use TOCData methods to process new headings
[mediawiki.git] / maintenance / deleteAutoPatrolLogs.php
blob48e67b5f4aa0c293551c7f0fb9ac2a92a67e306c
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.
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.
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
19 require_once __DIR__ . '/Maintenance.php';
21 /**
22 * Remove autopatrol logs in the logging table.
24 * @ingroup Maintenance
26 class DeleteAutoPatrolLogs extends Maintenance {
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).'
36 $this->addOption(
37 'before',
38 'Timestamp to delete only before that time, all MediaWiki timestamp formats are accepted',
39 false,
40 true
42 $this->addOption(
43 'from-id',
44 'First row (log id) to start updating from',
45 false,
46 true
48 $this->addOption(
49 'sleep',
50 'Sleep time (in seconds) between every batch',
51 false,
52 true
54 $this->setBatchSize( 1000 );
57 public function execute() {
58 $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
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;
70 $rows = $rowsData['rows'];
71 $fromId = $rowsData['lastId'];
73 // There is nothing to delete in this batch
74 if ( !$rows ) {
75 continue;
77 } else {
78 $rows = $this->getRows( $fromId );
79 if ( !$rows ) {
80 break;
82 $fromId = end( $rows );
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" );
92 if ( $sleep > 0 ) {
93 sleep( $sleep );
98 private function getRows( $fromId ) {
99 $lb = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer();
100 $dbr = $lb->getConnectionRef( DB_REPLICA );
101 $before = $this->getOption( 'before', false );
103 $conds = [
104 'log_type' => 'patrol',
105 'log_action' => 'autopatrol',
108 if ( $fromId ) {
109 $conds[] = 'log_id > ' . $dbr->addQuotes( $fromId );
112 if ( $before ) {
113 $conds[] = 'log_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $before ) );
116 return $dbr->newSelectQueryBuilder()
117 ->select( 'log_id' )
118 ->from( 'logging' )
119 ->where( $conds )
120 ->orderBy( 'log_id' )
121 ->limit( $this->getBatchSize() )
122 ->caller( __METHOD__ )
123 ->fetchFieldValues();
126 private function getRowsOld( $fromId ) {
127 $lb = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer();
128 $dbr = $lb->getConnectionRef( DB_REPLICA );
129 $batchSize = $this->getBatchSize();
130 $before = $this->getOption( 'before', false );
132 $conds = [
133 'log_type' => 'patrol',
134 'log_action' => 'patrol',
137 if ( $fromId ) {
138 $conds[] = 'log_id > ' . $dbr->addQuotes( $fromId );
141 if ( $before ) {
142 $conds[] = 'log_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $before ) );
145 $result = $dbr->newSelectQueryBuilder()
146 ->select( [ 'log_id', 'log_params' ] )
147 ->from( 'logging' )
148 ->where( $conds )
149 ->orderBy( 'log_id' )
150 ->limit( $batchSize )
151 ->caller( __METHOD__ )
152 ->fetchResultSet();
154 $last = null;
155 $autopatrols = [];
156 foreach ( $result as $row ) {
157 $last = $row->log_id;
158 $logEntry = DatabaseLogEntry::newFromRow( $row );
159 $params = $logEntry->getParameters();
160 if ( !is_array( $params ) ) {
161 continue;
164 // This logic belongs to PatrolLogFormatter::getMessageKey
165 // and LogFormatter::extractParameters the 'auto' value is logically presented as key [5].
166 // For legacy case the logical key is index + 3, meaning [2].
167 // For the modern case, the logical key is index - 1 meaning [6].
168 if ( array_key_exists( '6::auto', $params ) ) {
169 // Between 2011-2016 autopatrol logs
170 $auto = $params['6::auto'] === true;
171 } elseif ( $logEntry->isLegacy() === true && array_key_exists( 2, $params ) ) {
172 // Pre-2011 autopatrol logs
173 $auto = $params[2] === '1';
174 } else {
175 continue;
178 if ( $auto ) {
179 $autopatrols[] = $row->log_id;
183 if ( $last === null ) {
184 return null;
187 return [ 'rows' => $autopatrols, 'lastId' => $last ];
190 private function deleteRows( array $rows ) {
191 $lb = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer();
192 $dbw = $lb->getConnectionRef( DB_PRIMARY );
194 $dbw->delete(
195 'logging',
196 [ 'log_id' => $rows ],
197 __METHOD__
200 $this->waitForReplication();
205 $maintClass = DeleteAutoPatrolLogs::class;
206 require_once RUN_MAINTENANCE_IF_MAIN;