| 1 |
<?php |
| 2 |
|
| 3 |
use Wikimedia\IPUtils; |
| 4 |
use Wikimedia\Rdbms\IMaintainableDatabase; |
| 5 |
|
| 6 |
|
| 7 |
require_once __DIR__ . "/Maintenance.php"; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class MigrateBlocks extends LoggedUpdateMaintenance { |
| 21 |
private IMaintainableDatabase $dbw; |
| 22 |
|
| 23 |
public function __construct() { |
| 24 |
parent::__construct(); |
| 25 |
$this->addDescription( |
| 26 |
'Copy data from the ipblocks table into the new block and block_target tables' |
| 27 |
); |
| 28 |
$this->addOption( |
| 29 |
'sleep', |
| 30 |
'Sleep time (in seconds) between every batch. Default: 0', |
| 31 |
false, |
| 32 |
true |
| 33 |
); |
| 34 |
|
| 35 |
$this->setBatchSize( 500 ); |
| 36 |
} |
| 37 |
|
| 38 |
protected function getUpdateKey() { |
| 39 |
return __CLASS__; |
| 40 |
} |
| 41 |
|
| 42 |
protected function doDBUpdates() { |
| 43 |
$this->dbw = $this->getDB( DB_PRIMARY ); |
| 44 |
if ( |
| 45 |
!$this->dbw->tableExists( 'block', __METHOD__ ) || |
| 46 |
!$this->dbw->tableExists( 'block_target', __METHOD__ ) |
| 47 |
) { |
| 48 |
$this->fatalError( "Run update.php to create the block and block_target tables." ); |
| 49 |
} |
| 50 |
if ( !$this->dbw->tableExists( 'ipblocks', __METHOD__ ) ) { |
| 51 |
$this->output( "No ipblocks table, skipping migration to block_target.\n" ); |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
$this->output( "Populating the block and block_target tables\n" ); |
| 56 |
$migratedCount = 0; |
| 57 |
|
| 58 |
$id = 0; |
| 59 |
while ( $id !== null ) { |
| 60 |
$this->output( "Migrating ipblocks with ID > $id...\n" ); |
| 61 |
[ $numBlocks, $id ] = $this->handleBatch( $id ); |
| 62 |
$migratedCount += $numBlocks; |
| 63 |
} |
| 64 |
|
| 65 |
$this->output( "Completed migration of $migratedCount ipblocks to block and block_target.\n" ); |
| 66 |
|
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
private function handleBatch( int $lowId ): array { |
| 78 |
$migratedCount = 0; |
| 79 |
$res = $this->dbw->newSelectQueryBuilder() |
| 80 |
->select( '*' ) |
| 81 |
->from( 'ipblocks' ) |
| 82 |
->leftJoin( 'block', null, 'bl_id=ipb_id' ) |
| 83 |
->where( [ |
| 84 |
$this->dbw->expr( 'ipb_id', '>', $lowId ), |
| 85 |
'bl_id' => null |
| 86 |
] ) |
| 87 |
->orderBy( 'ipb_id' ) |
| 88 |
->limit( $this->getBatchSize() ) |
| 89 |
->caller( __METHOD__ ) |
| 90 |
->fetchResultSet(); |
| 91 |
|
| 92 |
if ( !$res->numRows() ) { |
| 93 |
return [ $migratedCount, null ]; |
| 94 |
} |
| 95 |
|
| 96 |
$highestId = $lowId; |
| 97 |
foreach ( $res as $row ) { |
| 98 |
$highestId = $row->ipb_id; |
| 99 |
$isIP = IPUtils::isValid( $row->ipb_address ); |
| 100 |
$isRange = IPUtils::isValidRange( $row->ipb_address ); |
| 101 |
$isIPOrRange = $isIP || $isRange; |
| 102 |
$ipHex = null; |
| 103 |
if ( $isIP ) { |
| 104 |
$ipHex = IPUtils::toHex( $row->ipb_address ); |
| 105 |
} elseif ( $isRange ) { |
| 106 |
$ipHex = $row->ipb_range_start; |
| 107 |
} elseif ( (int)$row->ipb_user === 0 ) { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
$this->output( "ipblock with ID $row->ipb_id: account block with ipb_user=0, skipping…\n" ); |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
$blockTarget = [ |
| 117 |
'bt_address' => $isIPOrRange ? $row->ipb_address : null, |
| 118 |
'bt_user' => $isIPOrRange ? null : $row->ipb_user, |
| 119 |
'bt_user_text' => $isIPOrRange ? null : $row->ipb_address, |
| 120 |
'bt_auto' => $row->ipb_auto, |
| 121 |
'bt_range_start' => $isRange ? $row->ipb_range_start : null, |
| 122 |
'bt_range_end' => $isRange ? $row->ipb_range_end : null, |
| 123 |
'bt_ip_hex' => $ipHex, |
| 124 |
'bt_count' => 1 |
| 125 |
]; |
| 126 |
$this->dbw->newInsertQueryBuilder() |
| 127 |
->insertInto( 'block_target' ) |
| 128 |
->row( $blockTarget ) |
| 129 |
->caller( __METHOD__ ) |
| 130 |
->execute(); |
| 131 |
$insertId = $this->dbw->insertId(); |
| 132 |
if ( !$insertId ) { |
| 133 |
$this->fatalError( |
| 134 |
"ipblock with ID $row->ipb_id: Failed to create block_target. Insert ID is falsy!" |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
$block = [ |
| 140 |
'bl_id' => $row->ipb_id, |
| 141 |
'bl_target' => $insertId, |
| 142 |
'bl_by_actor' => $row->ipb_by_actor, |
| 143 |
'bl_reason_id' => $row->ipb_reason_id, |
| 144 |
'bl_timestamp' => $row->ipb_timestamp, |
| 145 |
'bl_anon_only' => $row->ipb_anon_only, |
| 146 |
'bl_create_account' => $row->ipb_create_account, |
| 147 |
'bl_enable_autoblock' => $row->ipb_enable_autoblock, |
| 148 |
'bl_expiry' => $row->ipb_expiry, |
| 149 |
'bl_deleted' => $row->ipb_deleted, |
| 150 |
'bl_block_email' => $row->ipb_block_email, |
| 151 |
'bl_allow_usertalk' => $row->ipb_allow_usertalk, |
| 152 |
|
| 153 |
'bl_parent_block_id' => (int)$row->ipb_parent_block_id === 0 ? null : $row->ipb_parent_block_id, |
| 154 |
'bl_sitewide' => $row->ipb_sitewide, |
| 155 |
]; |
| 156 |
$this->dbw->newInsertQueryBuilder() |
| 157 |
->insertInto( 'block' ) |
| 158 |
->ignore() |
| 159 |
->row( $block ) |
| 160 |
->caller( __METHOD__ ) |
| 161 |
->execute(); |
| 162 |
if ( $this->dbw->affectedRows() ) { |
| 163 |
$migratedCount++; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
$this->output( "Migrated $migratedCount blocks\n" ); |
| 168 |
|
| 169 |
|
| 170 |
$this->waitForReplication(); |
| 171 |
$sleep = (int)$this->getOption( 'sleep', 0 ); |
| 172 |
if ( $sleep > 0 ) { |
| 173 |
sleep( $sleep ); |
| 174 |
} |
| 175 |
|
| 176 |
return [ $migratedCount, $highestId ]; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
$maintClass = MigrateBlocks::class; |
| 182 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 183 |
|