MediaWiki master
deleteAutoPatrolLogs.php
Go to the documentation of this file.
1<?php
19require_once __DIR__ . '/Maintenance.php';
20
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;
array $params
The job parameters.
static newFromRow( $row)
Constructs new LogEntry from database result row.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
countDown( $seconds)
Count down from $seconds to zero on the terminal, with a one-second pause between showing each number...
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)