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