1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
<?php
use Wikimedia\IPUtils;
use Wikimedia\Rdbms\IMaintainableDatabase;
require_once __DIR__ . "/Maintenance.php";
/**
* 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 );
if (
!$this->dbw->tableExists( 'block', __METHOD__ ) ||
!$this->dbw->tableExists( 'block_target', __METHOD__ )
) {
$this->fatalError( "Run update.php to create the block and block_target tables." );
}
if ( !$this->dbw->tableExists( 'ipblocks', __METHOD__ ) ) {
$this->output( "No ipblocks table, skipping migration to block_target.\n" );
return true;
}
$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' )
->leftJoin( 'block', null, 'bl_id=ipb_id' )
->where( [
$this->dbw->expr( 'ipb_id', '>', $lowId ),
'bl_id' => null
] )
->orderBy( 'ipb_id' )
->limit( $this->getBatchSize() )
->caller( __METHOD__ )
->fetchResultSet();
if ( !$res->numRows() ) {
return [ $migratedCount, null ];
}
$highestId = $lowId;
foreach ( $res as $row ) {
$highestId = $row->ipb_id;
$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' )
->ignore()
->row( $block )
->caller( __METHOD__ )
->execute();
if ( $this->dbw->affectedRows() ) {
$migratedCount++;
}
}
$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 ];
}
}
$maintClass = MigrateBlocks::class;
require_once RUN_MAINTENANCE_IF_MAIN;
|