MediaWiki  1.34.0
deleteAutoPatrolLogs.php
Go to the documentation of this file.
1 <?php
19 require_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 = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(
100  DB_REPLICA
101  );
102  $before = $this->getOption( 'before', false );
103 
104  $conds = [
105  'log_type' => 'patrol',
106  'log_action' => 'autopatrol',
107  ];
108 
109  if ( $fromId ) {
110  $conds[] = 'log_id > ' . $dbr->addQuotes( $fromId );
111  }
112 
113  if ( $before ) {
114  $conds[] = 'log_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $before ) );
115  }
116 
117  return $dbr->selectFieldValues(
118  'logging',
119  'log_id',
120  $conds,
121  __METHOD__,
122  [ 'LIMIT' => $this->getBatchSize() ]
123  );
124  }
125 
126  private function getRowsOld( $fromId ) {
127  $dbr = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(
128  DB_REPLICA
129  );
130  $batchSize = $this->getBatchSize();
131  $before = $this->getOption( 'before', false );
132 
133  $conds = [
134  'log_type' => 'patrol',
135  'log_action' => 'patrol',
136  ];
137 
138  if ( $fromId ) {
139  $conds[] = 'log_id > ' . $dbr->addQuotes( $fromId );
140  }
141 
142  if ( $before ) {
143  $conds[] = 'log_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $before ) );
144  }
145 
146  $result = $dbr->select(
147  'logging',
148  [ 'log_id', 'log_params' ],
149  $conds,
150  __METHOD__,
151  [ 'LIMIT' => $batchSize ]
152  );
153 
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;
162  }
163 
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;
176  }
177 
178  if ( $auto === true ) {
179  $autopatrols[] = $row->log_id;
180  }
181  }
182 
183  if ( $last === null ) {
184  return null;
185  }
186 
187  return [ 'rows' => $autopatrols, 'lastId' => $last ];
188  }
189 
190  private function deleteRows( array $rows ) {
191  $dbw = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(
192  DB_MASTER
193  );
194 
195  $dbw->delete(
196  'logging',
197  [ 'log_id' => $rows ],
198  __METHOD__
199  );
200 
201  MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
202  }
203 
204 }
205 
206 $maintClass = DeleteAutoPatrolLogs::class;
207 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
DeleteAutoPatrolLogs\getRows
getRows( $fromId)
Definition: deleteAutoPatrolLogs.php:98
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
DeleteAutoPatrolLogs
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: deleteAutoPatrolLogs.php:26
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
$last
$last
Definition: profileinfo.php:419
$dbr
$dbr
Definition: testCompression.php:50
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:138
DeleteAutoPatrolLogs\execute
execute()
Do the actual work.
Definition: deleteAutoPatrolLogs.php:57
DatabaseLogEntry\newFromRow
static newFromRow( $row)
Constructs new LogEntry from database result row.
Definition: DatabaseLogEntry.php:78
DeleteAutoPatrolLogs\__construct
__construct()
Default constructor.
Definition: deleteAutoPatrolLogs.php:28
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Maintenance\countDown
countDown( $seconds)
Count down from $seconds to zero on the terminal, with a one-second pause between showing each number...
Definition: Maintenance.php:1572
DeleteAutoPatrolLogs\getRowsOld
getRowsOld( $fromId)
Definition: deleteAutoPatrolLogs.php:126
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
$maintClass
$maintClass
Definition: deleteAutoPatrolLogs.php:206
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
DeleteAutoPatrolLogs\deleteRows
deleteRows(array $rows)
Definition: deleteAutoPatrolLogs.php:190
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:394