2023-12-05 18:24:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use Wikimedia\IPUtils;
|
2024-02-20 18:14:22 +00:00
|
|
|
use Wikimedia\Rdbms\IMaintainableDatabase;
|
2023-12-05 18:24:56 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2023-12-05 18:24:56 +00:00
|
|
|
require_once __DIR__ . "/Maintenance.php";
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2023-12-05 18:24:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maintenance script that migrates rows from ipblocks to block and block_target.
|
|
|
|
|
* The data is normalized to match the new schema. Any corrupt data that is
|
|
|
|
|
* encountered may be skipped, but will be logged.
|
|
|
|
|
*
|
|
|
|
|
* The old ipblocks table is left touched.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
* @since 1.42
|
|
|
|
|
*/
|
|
|
|
|
class MigrateBlocks extends LoggedUpdateMaintenance {
|
|
|
|
|
private IMaintainableDatabase $dbw;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->addDescription(
|
|
|
|
|
'Copy data from the ipblocks table into the new block and block_target tables'
|
|
|
|
|
);
|
|
|
|
|
$this->addOption(
|
|
|
|
|
'sleep',
|
|
|
|
|
'Sleep time (in seconds) between every batch. Default: 0',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
// Batch size is typically 1000, but we'll do 500 since there are 2 writes for each ipblock.
|
|
|
|
|
$this->setBatchSize( 500 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getUpdateKey() {
|
|
|
|
|
return __CLASS__;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function doDBUpdates() {
|
|
|
|
|
$this->dbw = $this->getDB( DB_PRIMARY );
|
2024-02-09 17:46:37 +00:00
|
|
|
if (
|
|
|
|
|
!$this->dbw->tableExists( 'block', __METHOD__ ) ||
|
|
|
|
|
!$this->dbw->tableExists( 'block_target', __METHOD__ )
|
|
|
|
|
) {
|
2023-12-05 18:24:56 +00:00
|
|
|
$this->fatalError( "Run update.php to create the block and block_target tables." );
|
|
|
|
|
}
|
2024-02-09 17:46:37 +00:00
|
|
|
if ( !$this->dbw->tableExists( 'ipblocks', __METHOD__ ) ) {
|
2024-04-09 02:49:33 +00:00
|
|
|
$this->output( "No ipblocks table, skipping migration to block_target.\n" );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-12-05 18:24:56 +00:00
|
|
|
|
|
|
|
|
$this->output( "Populating the block and block_target tables\n" );
|
|
|
|
|
$migratedCount = 0;
|
|
|
|
|
|
|
|
|
|
$id = 0;
|
|
|
|
|
while ( $id !== null ) {
|
|
|
|
|
$this->output( "Migrating ipblocks with ID > $id...\n" );
|
|
|
|
|
[ $numBlocks, $id ] = $this->handleBatch( $id );
|
|
|
|
|
$migratedCount += $numBlocks;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->output( "Completed migration of $migratedCount ipblocks to block and block_target.\n" );
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle up to $this->getBatchSize() pairs of INSERTs,
|
|
|
|
|
* one for block and one for block_target.
|
|
|
|
|
*
|
|
|
|
|
* @param int $lowId
|
|
|
|
|
* @return array [ number of blocks migrated, last ipb_id or null ]
|
|
|
|
|
*/
|
|
|
|
|
private function handleBatch( int $lowId ): array {
|
|
|
|
|
$migratedCount = 0;
|
|
|
|
|
$res = $this->dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( '*' )
|
|
|
|
|
->from( 'ipblocks' )
|
2024-03-11 01:25:11 +00:00
|
|
|
->leftJoin( 'block', null, 'bl_id=ipb_id' )
|
|
|
|
|
->where( [
|
|
|
|
|
$this->dbw->expr( 'ipb_id', '>', $lowId ),
|
|
|
|
|
'bl_id' => null
|
|
|
|
|
] )
|
2023-12-05 18:24:56 +00:00
|
|
|
->orderBy( 'ipb_id' )
|
|
|
|
|
->limit( $this->getBatchSize() )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->fetchResultSet();
|
|
|
|
|
|
|
|
|
|
if ( !$res->numRows() ) {
|
|
|
|
|
return [ $migratedCount, null ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$highestId = $lowId;
|
|
|
|
|
foreach ( $res as $row ) {
|
2024-03-13 00:02:33 +00:00
|
|
|
$highestId = $row->ipb_id;
|
2023-12-05 18:24:56 +00:00
|
|
|
$isIP = IPUtils::isValid( $row->ipb_address );
|
|
|
|
|
$isRange = IPUtils::isValidRange( $row->ipb_address );
|
|
|
|
|
$isIPOrRange = $isIP || $isRange;
|
|
|
|
|
$ipHex = null;
|
|
|
|
|
if ( $isIP ) {
|
|
|
|
|
$ipHex = IPUtils::toHex( $row->ipb_address );
|
|
|
|
|
} elseif ( $isRange ) {
|
|
|
|
|
$ipHex = $row->ipb_range_start;
|
|
|
|
|
} elseif ( (int)$row->ipb_user === 0 ) {
|
|
|
|
|
// There was data corruption circa 2006 and 2011 where some accounts were
|
|
|
|
|
// blocked as if they were logged out users. Here we'll prune the erroneous
|
|
|
|
|
// data by simply not copying it to the new schema.
|
|
|
|
|
$this->output( "ipblock with ID $row->ipb_id: account block with ipb_user=0, skipping…\n" );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert into block_target
|
|
|
|
|
$blockTarget = [
|
|
|
|
|
'bt_address' => $isIPOrRange ? $row->ipb_address : null,
|
|
|
|
|
'bt_user' => $isIPOrRange ? null : $row->ipb_user,
|
|
|
|
|
'bt_user_text' => $isIPOrRange ? null : $row->ipb_address,
|
|
|
|
|
'bt_auto' => $row->ipb_auto,
|
|
|
|
|
'bt_range_start' => $isRange ? $row->ipb_range_start : null,
|
|
|
|
|
'bt_range_end' => $isRange ? $row->ipb_range_end : null,
|
|
|
|
|
'bt_ip_hex' => $ipHex,
|
|
|
|
|
'bt_count' => 1
|
|
|
|
|
];
|
|
|
|
|
$this->dbw->newInsertQueryBuilder()
|
|
|
|
|
->insertInto( 'block_target' )
|
|
|
|
|
->row( $blockTarget )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->execute();
|
|
|
|
|
$insertId = $this->dbw->insertId();
|
|
|
|
|
if ( !$insertId ) {
|
|
|
|
|
$this->fatalError(
|
|
|
|
|
"ipblock with ID $row->ipb_id: Failed to create block_target. Insert ID is falsy!"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert into block
|
|
|
|
|
$block = [
|
|
|
|
|
'bl_id' => $row->ipb_id,
|
|
|
|
|
'bl_target' => $insertId,
|
|
|
|
|
'bl_by_actor' => $row->ipb_by_actor,
|
|
|
|
|
'bl_reason_id' => $row->ipb_reason_id,
|
|
|
|
|
'bl_timestamp' => $row->ipb_timestamp,
|
|
|
|
|
'bl_anon_only' => $row->ipb_anon_only,
|
|
|
|
|
'bl_create_account' => $row->ipb_create_account,
|
|
|
|
|
'bl_enable_autoblock' => $row->ipb_enable_autoblock,
|
|
|
|
|
'bl_expiry' => $row->ipb_expiry,
|
|
|
|
|
'bl_deleted' => $row->ipb_deleted,
|
|
|
|
|
'bl_block_email' => $row->ipb_block_email,
|
|
|
|
|
'bl_allow_usertalk' => $row->ipb_allow_usertalk,
|
|
|
|
|
// See T282890
|
|
|
|
|
'bl_parent_block_id' => (int)$row->ipb_parent_block_id === 0 ? null : $row->ipb_parent_block_id,
|
|
|
|
|
'bl_sitewide' => $row->ipb_sitewide,
|
|
|
|
|
];
|
|
|
|
|
$this->dbw->newInsertQueryBuilder()
|
|
|
|
|
->insertInto( 'block' )
|
2024-03-11 01:25:11 +00:00
|
|
|
->ignore()
|
2023-12-05 18:24:56 +00:00
|
|
|
->row( $block )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->execute();
|
2024-03-11 01:25:11 +00:00
|
|
|
if ( $this->dbw->affectedRows() ) {
|
|
|
|
|
$migratedCount++;
|
|
|
|
|
}
|
2023-12-05 18:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->output( "Migrated $migratedCount blocks\n" );
|
|
|
|
|
|
|
|
|
|
// Sleep between batches for replication to catch up
|
|
|
|
|
$this->waitForReplication();
|
|
|
|
|
$sleep = (int)$this->getOption( 'sleep', 0 );
|
|
|
|
|
if ( $sleep > 0 ) {
|
|
|
|
|
sleep( $sleep );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [ $migratedCount, $highestId ];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2023-12-05 18:24:56 +00:00
|
|
|
$maintClass = MigrateBlocks::class;
|
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|