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    /** @inheritDoc */
40    protected function getUpdateKey() {
41        return __CLASS__;
42    }
43
44    /** @inheritDoc */
45    protected function doDBUpdates() {
46        $this->dbw = $this->getDB( DB_PRIMARY );
47        if (
48            !$this->dbw->tableExists( 'block', __METHOD__ ) ||
49            !$this->dbw->tableExists( 'block_target', __METHOD__ )
50        ) {
51            $this->fatalError( "Run update.php to create the block and block_target tables." );
52        }
53        if ( !$this->dbw->tableExists( 'ipblocks', __METHOD__ ) ) {
54            $this->output( "No ipblocks table, skipping migration to block_target.\n" );
55            return true;
56        }
57
58        $this->output( "Populating the block and block_target tables\n" );
59        $migratedCount = 0;
60
61        $id = 0;
62        while ( $id !== null ) {
63            $this->output( "Migrating ipblocks with ID > $id...\n" );
64            [ $numBlocks, $id ] = $this->handleBatch( $id );
65            $migratedCount += $numBlocks;
66        }
67
68        $this->output( "Completed migration of $migratedCount ipblocks to block and block_target.\n" );
69
70        return true;
71    }
72
73    /**
74     * Handle up to $this->getBatchSize() pairs of INSERTs,
75     * one for block and one for block_target.
76     *
77     * @param int $lowId
78     * @return array [ number of blocks migrated, last ipb_id or null ]
79     */
80    private function handleBatch( int $lowId ): array {
81        $migratedCount = 0;
82        $res = $this->dbw->newSelectQueryBuilder()
83            ->select( '*' )
84            ->from( 'ipblocks' )
85            ->leftJoin( 'block', null, 'bl_id=ipb_id' )
86            ->where( [
87                $this->dbw->expr( 'ipb_id', '>', $lowId ),
88                'bl_id' => null
89            ] )
90            ->orderBy( 'ipb_id' )
91            ->limit( $this->getBatchSize() )
92            ->caller( __METHOD__ )
93            ->fetchResultSet();
94
95        if ( !$res->numRows() ) {
96            return [ $migratedCount, null ];
97        }
98
99        $highestId = $lowId;
100        foreach ( $res as $row ) {
101            $highestId = $row->ipb_id;
102            $isIP = IPUtils::isValid( $row->ipb_address );
103            $isRange = IPUtils::isValidRange( $row->ipb_address );
104            $isIPOrRange = $isIP || $isRange;
105            $ipHex = null;
106            if ( $isIP ) {
107                $ipHex = IPUtils::toHex( $row->ipb_address );
108            } elseif ( $isRange ) {
109                $ipHex = $row->ipb_range_start;
110            } elseif ( (int)$row->ipb_user === 0 ) {
111                // There was data corruption circa 2006 and 2011 where some accounts were
112                // blocked as if they were logged out users. Here we'll prune the erroneous
113                // data by simply not copying it to the new schema.
114                $this->output( "ipblock with ID $row->ipb_id: account block with ipb_user=0, skipping…\n" );
115                continue;
116            }
117
118            // Insert into block_target
119            $blockTarget = [
120                'bt_address'     => $isIPOrRange ? $row->ipb_address : null,
121                'bt_user'        => $isIPOrRange ? null : $row->ipb_user,
122                'bt_user_text'   => $isIPOrRange ? null : $row->ipb_address,
123                'bt_auto'        => $row->ipb_auto,
124                'bt_range_start' => $isRange ? $row->ipb_range_start : null,
125                'bt_range_end'   => $isRange ? $row->ipb_range_end : null,
126                'bt_ip_hex'      => $ipHex,
127                'bt_count'       => 1
128            ];
129            $this->dbw->newInsertQueryBuilder()
130                ->insertInto( 'block_target' )
131                ->row( $blockTarget )
132                ->caller( __METHOD__ )
133                ->execute();
134            $insertId = $this->dbw->insertId();
135            if ( !$insertId ) {
136                $this->fatalError(
137                    "ipblock with ID $row->ipb_id: Failed to create block_target. Insert ID is falsy!"
138                );
139            }
140
141            // Insert into block
142            $block = [
143                'bl_id'               => $row->ipb_id,
144                'bl_target'           => $insertId,
145                'bl_by_actor'         => $row->ipb_by_actor,
146                'bl_reason_id'        => $row->ipb_reason_id,
147                'bl_timestamp'        => $row->ipb_timestamp,
148                'bl_anon_only'        => $row->ipb_anon_only,
149                'bl_create_account'   => $row->ipb_create_account,
150                'bl_enable_autoblock' => $row->ipb_enable_autoblock,
151                'bl_expiry'           => $row->ipb_expiry,
152                'bl_deleted'          => $row->ipb_deleted,
153                'bl_block_email'      => $row->ipb_block_email,
154                'bl_allow_usertalk'   => $row->ipb_allow_usertalk,
155                // See T282890
156                'bl_parent_block_id'  => (int)$row->ipb_parent_block_id === 0 ? null : $row->ipb_parent_block_id,
157                'bl_sitewide'         => $row->ipb_sitewide,
158            ];
159            $this->dbw->newInsertQueryBuilder()
160                ->insertInto( 'block' )
161                ->ignore()
162                ->row( $block )
163                ->caller( __METHOD__ )
164                ->execute();
165            if ( $this->dbw->affectedRows() ) {
166                $migratedCount++;
167            }
168        }
169
170        $this->output( "Migrated $migratedCount blocks\n" );
171
172        // Sleep between batches for replication to catch up
173        $this->waitForReplication();
174        $sleep = (int)$this->getOption( 'sleep', 0 );
175        if ( $sleep > 0 ) {
176            sleep( $sleep );
177        }
178
179        return [ $migratedCount, $highestId ];
180    }
181}
182
183// @codeCoverageIgnoreStart
184$maintClass = MigrateBlocks::class;
185require_once RUN_MAINTENANCE_IF_MAIN;
186// @codeCoverageIgnoreEnd