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 $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
61 $sleep = (int)$this->getOption( 'sleep', 10 );
62 $fromId = $this->getOption( 'from-id', null );
63 $this->countDown( 5 );
65 if ( $this->hasOption( 'check-old' ) ) {
66 $rowsData = $this->getRowsOld( $fromId );
67 // We reached end of the table
71 $rows = $rowsData['rows'];
72 $fromId = $rowsData['lastId'];
74 // There is nothing to delete in this batch
79 $rows = $this->getRows( $fromId );
83 $fromId = end( $rows );
86 if ( $this->hasOption( 'dry-run' ) ) {
87 $this->output( 'These rows will get deleted: ' . implode( ', ', $rows ) . "\n" );
89 $this->deleteRows( $rows );
90 $this->output( 'Processed up to row id ' . end( $rows ) . "\n" );
99 private function getRows( $fromId ) {
100 $dbr = MediaWiki\MediaWikiServices
::getInstance()->getDBLoadBalancer()->getConnection(
103 $before = $this->getOption( 'before', false );
106 'log_type' => 'patrol',
107 'log_action' => 'autopatrol',
111 $conds[] = 'log_id > ' . $dbr->addQuotes( $fromId );
115 $conds[] = 'log_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $before ) );
118 return $dbr->selectFieldValues(
123 [ 'LIMIT' => $this->getBatchSize() ]
127 private function getRowsOld( $fromId ) {
128 $dbr = MediaWiki\MediaWikiServices
::getInstance()->getDBLoadBalancer()->getConnection(
131 $batchSize = $this->getBatchSize();
132 $before = $this->getOption( 'before', false );
135 'log_type' => 'patrol',
136 'log_action' => 'patrol',
140 $conds[] = 'log_id > ' . $dbr->addQuotes( $fromId );
144 $conds[] = 'log_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $before ) );
147 $result = $dbr->select(
149 [ 'log_id', 'log_params' ],
152 [ 'LIMIT' => $batchSize ]
157 foreach ( $result as $row ) {
158 $last = $row->log_id
;
159 Wikimedia\
suppressWarnings();
160 $params = unserialize( $row->log_params
);
161 Wikimedia\restoreWarnings
();
163 // Skipping really old rows, before 2011
164 if ( !is_array( $params ) ||
!array_key_exists( '6::auto', $params ) ) {
168 $auto = $params['6::auto'];
170 $autopatrols[] = $row->log_id
;
174 if ( $last === null ) {
178 return [ 'rows' => $autopatrols, 'lastId' => $last ];
181 private function deleteRows( array $rows ) {
182 $dbw = MediaWiki\MediaWikiServices
::getInstance()->getDBLoadBalancer()->getConnection(
188 [ 'log_id' => $rows ],
192 MediaWiki\MediaWikiServices
::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
197 $maintClass = DeleteAutoPatrolLogs
::class;
198 require_once RUN_MAINTENANCE_IF_MAIN
;