Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.27% |
107 / 110 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| DeleteAutoPatrolLogs | |
97.27% |
107 / 110 |
|
60.00% |
3 / 5 |
23 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
95.65% |
22 / 23 |
|
0.00% |
0 / 1 |
8 | |||
| getRows | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
3 | |||
| getRowsOld | |
94.59% |
35 / 37 |
|
0.00% |
0 / 1 |
10.02 | |||
| deleteRows | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | use MediaWiki\Logging\DatabaseLogEntry; |
| 7 | use MediaWiki\Maintenance\Maintenance; |
| 8 | |
| 9 | // @codeCoverageIgnoreStart |
| 10 | require_once __DIR__ . '/Maintenance.php'; |
| 11 | // @codeCoverageIgnoreEnd |
| 12 | |
| 13 | /** |
| 14 | * Remove autopatrol logs in the logging table. |
| 15 | * |
| 16 | * @ingroup Maintenance |
| 17 | */ |
| 18 | class DeleteAutoPatrolLogs extends Maintenance { |
| 19 | |
| 20 | public function __construct() { |
| 21 | parent::__construct(); |
| 22 | $this->addDescription( 'Remove autopatrol logs in the logging table' ); |
| 23 | $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' ); |
| 24 | $this->addOption( |
| 25 | 'check-old', |
| 26 | 'Check old patrol logs (for deleting old format autopatrols).' |
| 27 | ); |
| 28 | $this->addOption( |
| 29 | 'before', |
| 30 | 'Timestamp to delete only before that time, all MediaWiki timestamp formats are accepted', |
| 31 | false, |
| 32 | true |
| 33 | ); |
| 34 | $this->addOption( |
| 35 | 'from-id', |
| 36 | 'First row (log id) to start updating from', |
| 37 | false, |
| 38 | true |
| 39 | ); |
| 40 | $this->addOption( |
| 41 | 'sleep', |
| 42 | 'Sleep time (in seconds) between every batch', |
| 43 | false, |
| 44 | true |
| 45 | ); |
| 46 | $this->setBatchSize( 1000 ); |
| 47 | } |
| 48 | |
| 49 | public function execute() { |
| 50 | $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) ); |
| 51 | |
| 52 | $sleep = (int)$this->getOption( 'sleep', 10 ); |
| 53 | $fromId = $this->getOption( 'from-id', null ); |
| 54 | $this->countDown( 5 ); |
| 55 | while ( true ) { |
| 56 | if ( $this->hasOption( 'check-old' ) ) { |
| 57 | $rowsData = $this->getRowsOld( $fromId ); |
| 58 | // We reached end of the table |
| 59 | if ( !$rowsData ) { |
| 60 | break; |
| 61 | } |
| 62 | $rows = $rowsData['rows']; |
| 63 | $fromId = $rowsData['lastId']; |
| 64 | |
| 65 | // There is nothing to delete in this batch |
| 66 | if ( !$rows ) { |
| 67 | continue; |
| 68 | } |
| 69 | } else { |
| 70 | $rows = $this->getRows( $fromId ); |
| 71 | if ( !$rows ) { |
| 72 | break; |
| 73 | } |
| 74 | $fromId = end( $rows ); |
| 75 | } |
| 76 | |
| 77 | if ( $this->hasOption( 'dry-run' ) ) { |
| 78 | $this->output( 'These rows will get deleted: ' . implode( ', ', $rows ) . "\n" ); |
| 79 | } else { |
| 80 | $this->deleteRows( $rows ); |
| 81 | $this->output( 'Processed up to row id ' . end( $rows ) . "\n" ); |
| 82 | } |
| 83 | |
| 84 | if ( $sleep > 0 ) { |
| 85 | sleep( $sleep ); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private function getRows( ?int $fromId ): array { |
| 91 | $dbr = $this->getReplicaDB(); |
| 92 | $before = $this->getOption( 'before', false ); |
| 93 | |
| 94 | $conds = [ |
| 95 | 'log_type' => 'patrol', |
| 96 | 'log_action' => 'autopatrol', |
| 97 | ]; |
| 98 | |
| 99 | if ( $fromId ) { |
| 100 | $conds[] = $dbr->expr( 'log_id', '>', $fromId ); |
| 101 | } |
| 102 | |
| 103 | if ( $before ) { |
| 104 | $conds[] = $dbr->expr( 'log_timestamp', '<', $dbr->timestamp( $before ) ); |
| 105 | } |
| 106 | |
| 107 | return $dbr->newSelectQueryBuilder() |
| 108 | ->select( 'log_id' ) |
| 109 | ->from( 'logging' ) |
| 110 | ->where( $conds ) |
| 111 | ->orderBy( 'log_id' ) |
| 112 | ->limit( $this->getBatchSize() ) |
| 113 | ->caller( __METHOD__ ) |
| 114 | ->fetchFieldValues(); |
| 115 | } |
| 116 | |
| 117 | private function getRowsOld( ?int $fromId ): ?array { |
| 118 | $dbr = $this->getReplicaDB(); |
| 119 | $batchSize = $this->getBatchSize(); |
| 120 | $before = $this->getOption( 'before', false ); |
| 121 | |
| 122 | $conds = [ |
| 123 | 'log_type' => 'patrol', |
| 124 | 'log_action' => 'patrol', |
| 125 | ]; |
| 126 | |
| 127 | if ( $fromId ) { |
| 128 | $conds[] = $dbr->expr( 'log_id', '>', $fromId ); |
| 129 | } |
| 130 | |
| 131 | if ( $before ) { |
| 132 | $conds[] = $dbr->expr( 'log_timestamp', '<', $dbr->timestamp( $before ) ); |
| 133 | } |
| 134 | |
| 135 | $result = $dbr->newSelectQueryBuilder() |
| 136 | ->select( [ 'log_id', 'log_params' ] ) |
| 137 | ->from( 'logging' ) |
| 138 | ->where( $conds ) |
| 139 | ->orderBy( 'log_id' ) |
| 140 | ->limit( $batchSize ) |
| 141 | ->caller( __METHOD__ ) |
| 142 | ->fetchResultSet(); |
| 143 | |
| 144 | $last = null; |
| 145 | $autopatrols = []; |
| 146 | foreach ( $result as $row ) { |
| 147 | $last = $row->log_id; |
| 148 | $logEntry = DatabaseLogEntry::newFromRow( $row ); |
| 149 | $params = $logEntry->getParameters(); |
| 150 | if ( !is_array( $params ) ) { |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | // This logic belongs to PatrolLogFormatter::getMessageKey |
| 155 | // and LogFormatter::extractParameters the 'auto' value is logically presented as key [5]. |
| 156 | // For legacy case the logical key is index + 3, meaning [2]. |
| 157 | // For the modern case, the logical key is index - 1 meaning [6]. |
| 158 | if ( array_key_exists( '6::auto', $params ) ) { |
| 159 | // Between 2011-2016 autopatrol logs |
| 160 | $auto = $params['6::auto'] === true; |
| 161 | } elseif ( $logEntry->isLegacy() && array_key_exists( 2, $params ) ) { |
| 162 | // Pre-2011 autopatrol logs |
| 163 | $auto = $params[2] === '1'; |
| 164 | } else { |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | if ( $auto ) { |
| 169 | $autopatrols[] = $row->log_id; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if ( $last === null ) { |
| 174 | return null; |
| 175 | } |
| 176 | |
| 177 | return [ 'rows' => $autopatrols, 'lastId' => $last ]; |
| 178 | } |
| 179 | |
| 180 | private function deleteRows( array $rows ) { |
| 181 | $dbw = $this->getPrimaryDB(); |
| 182 | |
| 183 | $dbw->newDeleteQueryBuilder() |
| 184 | ->deleteFrom( 'logging' ) |
| 185 | ->where( [ 'log_id' => $rows ] ) |
| 186 | ->caller( __METHOD__ )->execute(); |
| 187 | |
| 188 | $this->waitForReplication(); |
| 189 | } |
| 190 | |
| 191 | } |
| 192 | |
| 193 | // @codeCoverageIgnoreStart |
| 194 | $maintClass = DeleteAutoPatrolLogs::class; |
| 195 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 196 | // @codeCoverageIgnoreEnd |