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