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';
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' );
34 'Check old patrol logs (for deleting old format autopatrols).' .
35 'Note that this will not delete rows older than 2011 (MediaWiki 1.18).'
39 'Timestamp to delete only before that time, all MediaWiki timestamp formats are accepted',
45 'First row (log id) to start updating from',
51 'Sleep time (in seconds) between every batch',
55 $this->setBatchSize( 1000 );
58 public function execute() {
59 $sleep = (int)$this->getOption( 'sleep', 10 );
60 $fromId = $this->getOption( 'from-id', null );
61 $this->countDown( 5 );
63 if ( $this->hasOption( 'check-old' ) ) {
64 $rowsData = $this->getRowsOld( $fromId );
65 // We reached end of the table
69 $rows = $rowsData['rows'];
70 $fromId = $rowsData['lastId'];
72 // There is nothing to delete in this batch
77 $rows = $this->getRows( $fromId );
81 $fromId = end( $rows );
84 if ( $this->hasOption( 'dry-run' ) ) {
85 $this->output( 'These rows will get deleted: ' . implode( ', ', $rows ) . "\n" );
87 $this->deleteRows( $rows );
88 $this->output( 'Processed up to row id ' . end( $rows ) . "\n" );
97 private function getRows( $fromId ) {
98 $dbr = MediaWiki\MediaWikiServices
::getInstance()->getDBLoadBalancer()->getConnection(
101 $before = $this->getOption( 'before', false );
104 'log_type' => 'patrol',
105 'log_action' => 'autopatrol',
109 $conds[] = 'log_id > ' . $dbr->addQuotes( $fromId );
113 $conds[] = 'log_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $before ) );
116 return $dbr->selectFieldValues(
121 [ 'LIMIT' => $this->getBatchSize() ]
125 private function getRowsOld( $fromId ) {
126 $dbr = MediaWiki\MediaWikiServices
::getInstance()->getDBLoadBalancer()->getConnection(
129 $batchSize = $this->getBatchSize();
130 $before = $this->getOption( 'before', false );
133 'log_type' => 'patrol',
134 'log_action' => 'patrol',
138 $conds[] = 'log_id > ' . $dbr->addQuotes( $fromId );
142 $conds[] = 'log_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $before ) );
145 $result = $dbr->select(
147 [ 'log_id', 'log_params' ],
150 [ 'LIMIT' => $batchSize ]
155 foreach ( $result as $row ) {
156 $last = $row->log_id
;
157 Wikimedia\
suppressWarnings();
158 $params = unserialize( $row->log_params
);
159 Wikimedia\restoreWarnings
();
161 // Skipping really old rows, before 2011
162 if ( is_array( $params ) && !array_key_exists( '6::auto', $params ) ) {
166 $auto = $params['6::auto'];
168 $autopatrolls[] = $row->log_id
;
172 if ( $last === null ) {
176 return [ 'rows' => $autopatrolls, 'lastId' => $last ];
179 private function deleteRows( array $rows ) {
180 $dbw = MediaWiki\MediaWikiServices
::getInstance()->getDBLoadBalancer()->getConnection(
186 [ 'log_id' => $rows ],
190 MediaWiki\MediaWikiServices
::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
195 $maintClass = DeleteAutoPatrolLogs
::class;
196 require_once RUN_MAINTENANCE_IF_MAIN
;