Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 106 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
MigrateBlocks | |
0.00% |
0 / 103 |
|
0.00% |
0 / 4 |
506 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
getUpdateKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doDBUpdates | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
handleBatch | |
0.00% |
0 / 79 |
|
0.00% |
0 / 1 |
272 |
1 | <?php |
2 | |
3 | use Wikimedia\IPUtils; |
4 | use Wikimedia\Rdbms\IMaintainableDatabase; |
5 | |
6 | require_once __DIR__ . "/Maintenance.php"; |
7 | |
8 | /** |
9 | * Maintenance script that migrates rows from ipblocks to block and block_target. |
10 | * The data is normalized to match the new schema. Any corrupt data that is |
11 | * encountered may be skipped, but will be logged. |
12 | * |
13 | * The old ipblocks table is left touched. |
14 | * |
15 | * @ingroup Maintenance |
16 | * @since 1.42 |
17 | */ |
18 | class MigrateBlocks extends LoggedUpdateMaintenance { |
19 | private IMaintainableDatabase $dbw; |
20 | |
21 | public function __construct() { |
22 | parent::__construct(); |
23 | $this->addDescription( |
24 | 'Copy data from the ipblocks table into the new block and block_target tables' |
25 | ); |
26 | $this->addOption( |
27 | 'sleep', |
28 | 'Sleep time (in seconds) between every batch. Default: 0', |
29 | false, |
30 | true |
31 | ); |
32 | // Batch size is typically 1000, but we'll do 500 since there are 2 writes for each ipblock. |
33 | $this->setBatchSize( 500 ); |
34 | } |
35 | |
36 | protected function getUpdateKey() { |
37 | return __CLASS__; |
38 | } |
39 | |
40 | protected function doDBUpdates() { |
41 | $this->dbw = $this->getDB( DB_PRIMARY ); |
42 | if ( !$this->dbw->tableExists( 'block', __METHOD__ ) || !$this->dbw->tableExists( 'block_target' ) ) { |
43 | $this->fatalError( "Run update.php to create the block and block_target tables." ); |
44 | } |
45 | |
46 | $this->output( "Populating the block and block_target tables\n" ); |
47 | $migratedCount = 0; |
48 | |
49 | $id = 0; |
50 | while ( $id !== null ) { |
51 | $this->output( "Migrating ipblocks with ID > $id...\n" ); |
52 | [ $numBlocks, $id ] = $this->handleBatch( $id ); |
53 | $migratedCount += $numBlocks; |
54 | } |
55 | |
56 | $this->output( "Completed migration of $migratedCount ipblocks to block and block_target.\n" ); |
57 | |
58 | return true; |
59 | } |
60 | |
61 | /** |
62 | * Handle up to $this->getBatchSize() pairs of INSERTs, |
63 | * one for block and one for block_target. |
64 | * |
65 | * @param int $lowId |
66 | * @return array [ number of blocks migrated, last ipb_id or null ] |
67 | */ |
68 | private function handleBatch( int $lowId ): array { |
69 | $migratedCount = 0; |
70 | $res = $this->dbw->newSelectQueryBuilder() |
71 | ->select( '*' ) |
72 | ->from( 'ipblocks' ) |
73 | ->leftJoin( 'block', null, 'bl_id=ipb_id' ) |
74 | ->where( [ |
75 | $this->dbw->expr( 'ipb_id', '>', $lowId ), |
76 | 'bl_id' => null |
77 | ] ) |
78 | ->orderBy( 'ipb_id' ) |
79 | ->limit( $this->getBatchSize() ) |
80 | ->caller( __METHOD__ ) |
81 | ->fetchResultSet(); |
82 | |
83 | if ( !$res->numRows() ) { |
84 | return [ $migratedCount, null ]; |
85 | } |
86 | |
87 | $highestId = $lowId; |
88 | foreach ( $res as $row ) { |
89 | $highestId = $row->ipb_id; |
90 | $isIP = IPUtils::isValid( $row->ipb_address ); |
91 | $isRange = IPUtils::isValidRange( $row->ipb_address ); |
92 | $isIPOrRange = $isIP || $isRange; |
93 | $ipHex = null; |
94 | if ( $isIP ) { |
95 | $ipHex = IPUtils::toHex( $row->ipb_address ); |
96 | } elseif ( $isRange ) { |
97 | $ipHex = $row->ipb_range_start; |
98 | } elseif ( (int)$row->ipb_user === 0 ) { |
99 | // There was data corruption circa 2006 and 2011 where some accounts were |
100 | // blocked as if they were logged out users. Here we'll prune the erroneous |
101 | // data by simply not copying it to the new schema. |
102 | $this->output( "ipblock with ID $row->ipb_id: account block with ipb_user=0, skipping…\n" ); |
103 | continue; |
104 | } |
105 | |
106 | // Insert into block_target |
107 | $blockTarget = [ |
108 | 'bt_address' => $isIPOrRange ? $row->ipb_address : null, |
109 | 'bt_user' => $isIPOrRange ? null : $row->ipb_user, |
110 | 'bt_user_text' => $isIPOrRange ? null : $row->ipb_address, |
111 | 'bt_auto' => $row->ipb_auto, |
112 | 'bt_range_start' => $isRange ? $row->ipb_range_start : null, |
113 | 'bt_range_end' => $isRange ? $row->ipb_range_end : null, |
114 | 'bt_ip_hex' => $ipHex, |
115 | 'bt_count' => 1 |
116 | ]; |
117 | $this->dbw->newInsertQueryBuilder() |
118 | ->insertInto( 'block_target' ) |
119 | ->row( $blockTarget ) |
120 | ->caller( __METHOD__ ) |
121 | ->execute(); |
122 | $insertId = $this->dbw->insertId(); |
123 | if ( !$insertId ) { |
124 | $this->fatalError( |
125 | "ipblock with ID $row->ipb_id: Failed to create block_target. Insert ID is falsy!" |
126 | ); |
127 | } |
128 | |
129 | // Insert into block |
130 | $block = [ |
131 | 'bl_id' => $row->ipb_id, |
132 | 'bl_target' => $insertId, |
133 | 'bl_by_actor' => $row->ipb_by_actor, |
134 | 'bl_reason_id' => $row->ipb_reason_id, |
135 | 'bl_timestamp' => $row->ipb_timestamp, |
136 | 'bl_anon_only' => $row->ipb_anon_only, |
137 | 'bl_create_account' => $row->ipb_create_account, |
138 | 'bl_enable_autoblock' => $row->ipb_enable_autoblock, |
139 | 'bl_expiry' => $row->ipb_expiry, |
140 | 'bl_deleted' => $row->ipb_deleted, |
141 | 'bl_block_email' => $row->ipb_block_email, |
142 | 'bl_allow_usertalk' => $row->ipb_allow_usertalk, |
143 | // See T282890 |
144 | 'bl_parent_block_id' => (int)$row->ipb_parent_block_id === 0 ? null : $row->ipb_parent_block_id, |
145 | 'bl_sitewide' => $row->ipb_sitewide, |
146 | ]; |
147 | $this->dbw->newInsertQueryBuilder() |
148 | ->insertInto( 'block' ) |
149 | ->ignore() |
150 | ->row( $block ) |
151 | ->caller( __METHOD__ ) |
152 | ->execute(); |
153 | if ( $this->dbw->affectedRows() ) { |
154 | $migratedCount++; |
155 | } |
156 | } |
157 | |
158 | $this->output( "Migrated $migratedCount blocks\n" ); |
159 | |
160 | // Sleep between batches for replication to catch up |
161 | $this->waitForReplication(); |
162 | $sleep = (int)$this->getOption( 'sleep', 0 ); |
163 | if ( $sleep > 0 ) { |
164 | sleep( $sleep ); |
165 | } |
166 | |
167 | return [ $migratedCount, $highestId ]; |
168 | } |
169 | } |
170 | |
171 | $maintClass = MigrateBlocks::class; |
172 | require_once RUN_MAINTENANCE_IF_MAIN; |