Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 74 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| TableCleanup | |
0.00% |
0 / 74 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| init | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| progress | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
6 | |||
| runTable | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
56 | |||
| hexChar | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Generic class to cleanup a database table. |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | * @ingroup Maintenance |
| 8 | */ |
| 9 | |
| 10 | use MediaWiki\Maintenance\Maintenance; |
| 11 | use MediaWiki\WikiMap\WikiMap; |
| 12 | use Wikimedia\Timestamp\TimestampFormat as TS; |
| 13 | |
| 14 | // @codeCoverageIgnoreStart |
| 15 | require_once __DIR__ . '/Maintenance.php'; |
| 16 | // @codeCoverageIgnoreEnd |
| 17 | |
| 18 | /** |
| 19 | * Generic class to cleanup a database table. Already subclasses Maintenance. |
| 20 | * |
| 21 | * @ingroup Maintenance |
| 22 | */ |
| 23 | class TableCleanup extends Maintenance { |
| 24 | /** @var array */ |
| 25 | protected $defaultParams = [ |
| 26 | 'table' => 'page', |
| 27 | 'conds' => [], |
| 28 | 'index' => 'page_id', |
| 29 | 'callback' => 'processRow', |
| 30 | ]; |
| 31 | |
| 32 | /** @var bool */ |
| 33 | protected $dryrun = false; |
| 34 | /** @var int */ |
| 35 | protected $reportInterval = 100; |
| 36 | |
| 37 | protected int $processed; |
| 38 | protected int $updated; |
| 39 | protected int $count; |
| 40 | protected float $startTime; |
| 41 | protected string $table; |
| 42 | |
| 43 | public function __construct() { |
| 44 | parent::__construct(); |
| 45 | $this->addOption( 'dry-run', 'Perform a dry run' ); |
| 46 | $this->addOption( 'reporting-interval', 'How often to print status line' ); |
| 47 | $this->setBatchSize( 100 ); |
| 48 | } |
| 49 | |
| 50 | public function execute() { |
| 51 | $this->reportInterval = $this->getOption( 'reporting-interval', $this->reportInterval ); |
| 52 | |
| 53 | $this->dryrun = $this->hasOption( 'dry-run' ); |
| 54 | |
| 55 | if ( $this->dryrun ) { |
| 56 | $this->output( "Checking for bad titles...\n" ); |
| 57 | } else { |
| 58 | $this->output( "Checking and fixing bad titles...\n" ); |
| 59 | } |
| 60 | |
| 61 | $this->runTable( $this->defaultParams ); |
| 62 | } |
| 63 | |
| 64 | protected function init( int $count, string $table ) { |
| 65 | $this->processed = 0; |
| 66 | $this->updated = 0; |
| 67 | $this->count = $count; |
| 68 | $this->startTime = microtime( true ); |
| 69 | $this->table = $table; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param int $updated |
| 74 | */ |
| 75 | protected function progress( $updated ) { |
| 76 | $this->updated += $updated; |
| 77 | $this->processed++; |
| 78 | if ( $this->processed % $this->reportInterval != 0 ) { |
| 79 | return; |
| 80 | } |
| 81 | $portion = $this->processed / $this->count; |
| 82 | $updateRate = $this->updated / $this->processed; |
| 83 | |
| 84 | $now = microtime( true ); |
| 85 | $delta = $now - $this->startTime; |
| 86 | $estimatedTotalTime = $delta / $portion; |
| 87 | $eta = $this->startTime + $estimatedTotalTime; |
| 88 | |
| 89 | $this->output( |
| 90 | sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n", |
| 91 | WikiMap::getCurrentWikiDbDomain()->getId(), |
| 92 | wfTimestamp( TS::DB, intval( $now ) ), |
| 93 | $portion * 100.0, |
| 94 | $this->table, |
| 95 | wfTimestamp( TS::DB, intval( $eta ) ), |
| 96 | $this->processed, |
| 97 | $this->count, |
| 98 | $this->processed / $delta, |
| 99 | $updateRate * 100.0 |
| 100 | ) |
| 101 | ); |
| 102 | flush(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param array $params |
| 107 | */ |
| 108 | public function runTable( $params ) { |
| 109 | $dbr = $this->getReplicaDB(); |
| 110 | |
| 111 | if ( array_diff( array_keys( $params ), |
| 112 | [ 'table', 'conds', 'index', 'callback' ] ) |
| 113 | ) { |
| 114 | $this->fatalError( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) ); |
| 115 | } |
| 116 | |
| 117 | $table = $params['table']; |
| 118 | // count(*) would melt the DB for huge tables, we can estimate here |
| 119 | $count = $dbr->newSelectQueryBuilder() |
| 120 | ->table( $table ) |
| 121 | ->caller( __METHOD__ ) |
| 122 | ->estimateRowCount(); |
| 123 | $this->init( $count, $table ); |
| 124 | $this->output( "Processing $table...\n" ); |
| 125 | |
| 126 | $index = (array)$params['index']; |
| 127 | $indexConds = []; |
| 128 | $callback = [ $this, $params['callback'] ]; |
| 129 | |
| 130 | while ( true ) { |
| 131 | $conds = array_merge( $params['conds'], $indexConds ); |
| 132 | $res = $dbr->newSelectQueryBuilder() |
| 133 | ->select( '*' ) |
| 134 | ->from( $table ) |
| 135 | ->where( $conds ) |
| 136 | ->orderBy( implode( ',', $index ) ) |
| 137 | ->limit( $this->getBatchSize() ) |
| 138 | ->caller( __METHOD__ )->fetchResultSet(); |
| 139 | if ( !$res->numRows() ) { |
| 140 | // Done |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | foreach ( $res as $row ) { |
| 145 | $callback( $row ); |
| 146 | } |
| 147 | |
| 148 | if ( $res->numRows() < $this->getBatchSize() ) { |
| 149 | // Done |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | // Update the conditions to select the next batch. |
| 154 | $conds = []; |
| 155 | foreach ( $index as $field ) { |
| 156 | // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $res has at at least one item |
| 157 | $conds[ $field ] = $row->$field; |
| 158 | } |
| 159 | $indexConds = [ $dbr->buildComparison( '>', $conds ) ]; |
| 160 | } |
| 161 | |
| 162 | $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @param string[] $matches |
| 167 | * @return string |
| 168 | */ |
| 169 | protected function hexChar( $matches ) { |
| 170 | return sprintf( "\\x%02x", ord( $matches[1] ) ); |
| 171 | } |
| 172 | } |