| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
require_once __DIR__ . '/Maintenance.php'; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class DeleteAutoPatrolLogs extends Maintenance { |
| 29 |
|
| 30 |
public function __construct() { |
| 31 |
parent::__construct(); |
| 32 |
$this->addDescription( 'Remove autopatrol logs in the logging table' ); |
| 33 |
$this->addOption( 'dry-run', 'Print debug info instead of actually deleting' ); |
| 34 |
$this->addOption( |
| 35 |
'check-old', |
| 36 |
'Check old patrol logs (for deleting old format autopatrols).' |
| 37 |
); |
| 38 |
$this->addOption( |
| 39 |
'before', |
| 40 |
'Timestamp to delete only before that time, all MediaWiki timestamp formats are accepted', |
| 41 |
false, |
| 42 |
true |
| 43 |
); |
| 44 |
$this->addOption( |
| 45 |
'from-id', |
| 46 |
'First row (log id) to start updating from', |
| 47 |
false, |
| 48 |
true |
| 49 |
); |
| 50 |
$this->addOption( |
| 51 |
'sleep', |
| 52 |
'Sleep time (in seconds) between every batch', |
| 53 |
false, |
| 54 |
true |
| 55 |
); |
| 56 |
$this->setBatchSize( 1000 ); |
| 57 |
} |
| 58 |
|
| 59 |
public function execute() { |
| 60 |
$this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) ); |
| 61 |
|
| 62 |
$sleep = (int)$this->getOption( 'sleep', 10 ); |
| 63 |
$fromId = $this->getOption( 'from-id', null ); |
| 64 |
$this->countDown( 5 ); |
| 65 |
while ( true ) { |
| 66 |
if ( $this->hasOption( 'check-old' ) ) { |
| 67 |
$rowsData = $this->getRowsOld( $fromId ); |
| 68 |
|
| 69 |
if ( !$rowsData ) { |
| 70 |
break; |
| 71 |
} |
| 72 |
$rows = $rowsData['rows']; |
| 73 |
$fromId = $rowsData['lastId']; |
| 74 |
|
| 75 |
|
| 76 |
if ( !$rows ) { |
| 77 |
continue; |
| 78 |
} |
| 79 |
} else { |
| 80 |
$rows = $this->getRows( $fromId ); |
| 81 |
if ( !$rows ) { |
| 82 |
break; |
| 83 |
} |
| 84 |
$fromId = end( $rows ); |
| 85 |
} |
| 86 |
|
| 87 |
if ( $this->hasOption( 'dry-run' ) ) { |
| 88 |
$this->output( 'These rows will get deleted: ' . implode( ', ', $rows ) . "\n" ); |
| 89 |
} else { |
| 90 |
$this->deleteRows( $rows ); |
| 91 |
$this->output( 'Processed up to row id ' . end( $rows ) . "\n" ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( $sleep > 0 ) { |
| 95 |
sleep( $sleep ); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
private function getRows( $fromId ) { |
| 101 |
$dbr = $this->getReplicaDB(); |
| 102 |
$before = $this->getOption( 'before', false ); |
| 103 |
|
| 104 |
$conds = [ |
| 105 |
'log_type' => 'patrol', |
| 106 |
'log_action' => 'autopatrol', |
| 107 |
]; |
| 108 |
|
| 109 |
if ( $fromId ) { |
| 110 |
$conds[] = $dbr->expr( 'log_id', '>', $fromId ); |
| 111 |
} |
| 112 |
|
| 113 |
if ( $before ) { |
| 114 |
$conds[] = $dbr->expr( 'log_timestamp', '<', $dbr->timestamp( $before ) ); |
| 115 |
} |
| 116 |
|
| 117 |
return $dbr->newSelectQueryBuilder() |
| 118 |
->select( 'log_id' ) |
| 119 |
->from( 'logging' ) |
| 120 |
->where( $conds ) |
| 121 |
->orderBy( 'log_id' ) |
| 122 |
->limit( $this->getBatchSize() ) |
| 123 |
->caller( __METHOD__ ) |
| 124 |
->fetchFieldValues(); |
| 125 |
} |
| 126 |
|
| 127 |
private function getRowsOld( $fromId ) { |
| 128 |
$dbr = $this->getReplicaDB(); |
| 129 |
$batchSize = $this->getBatchSize(); |
| 130 |
$before = $this->getOption( 'before', false ); |
| 131 |
|
| 132 |
$conds = [ |
| 133 |
'log_type' => 'patrol', |
| 134 |
'log_action' => 'patrol', |
| 135 |
]; |
| 136 |
|
| 137 |
if ( $fromId ) { |
| 138 |
$conds[] = $dbr->expr( 'log_id', '>', $fromId ); |
| 139 |
} |
| 140 |
|
| 141 |
if ( $before ) { |
| 142 |
$conds[] = $dbr->expr( 'log_timestamp', '<', $dbr->timestamp( $before ) ); |
| 143 |
} |
| 144 |
|
| 145 |
$result = $dbr->newSelectQueryBuilder() |
| 146 |
->select( [ 'log_id', 'log_type', 'log_action', 'log_params' ] ) |
| 147 |
->from( 'logging' ) |
| 148 |
->where( $conds ) |
| 149 |
->orderBy( 'log_id' ) |
| 150 |
->limit( $batchSize ) |
| 151 |
->caller( __METHOD__ ) |
| 152 |
->fetchResultSet(); |
| 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 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
if ( array_key_exists( '6::auto', $params ) ) { |
| 169 |
|
| 170 |
$auto = $params['6::auto'] === true; |
| 171 |
} elseif ( $logEntry->isLegacy() === true && array_key_exists( 2, $params ) ) { |
| 172 |
|
| 173 |
$auto = $params[2] === '1'; |
| 174 |
} else { |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
if ( $auto ) { |
| 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 = $this->getPrimaryDB(); |
| 192 |
|
| 193 |
$dbw->newDeleteQueryBuilder() |
| 194 |
->deleteFrom( 'logging' ) |
| 195 |
->where( [ 'log_id' => $rows ] ) |
| 196 |
->caller( __METHOD__ )->execute(); |
| 197 |
|
| 198 |
$this->waitForReplication(); |
| 199 |
} |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
$maintClass = DeleteAutoPatrolLogs::class; |
| 205 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 206 |
|