MediaWiki master
deleteAutoPatrolLogs.php
Go to the documentation of this file.
1<?php
20
21// @codeCoverageIgnoreStart
22require_once __DIR__ . '/Maintenance.php';
23// @codeCoverageIgnoreEnd
24
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
array $params
The job parameters.
static newFromRow( $row)
Constructs new LogEntry from database result row.
Remove autopatrol logs in the logging table.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
countDown( $seconds)
Count down from $seconds to zero on the terminal, with a one-second pause between showing each number...
addDescription( $text)
Set the description text.