MediaWiki master
migrateBlocks.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\IPUtils;
5
6require_once __DIR__ . "/Maintenance.php";
7
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 if ( !$this->dbw->tableExists( 'ipblocks' ) ) {
46 $this->output( "No ipblocks table, skipping migration to block_target.\n" );
47 return true;
48 }
49
50 $this->output( "Populating the block and block_target tables\n" );
51 $migratedCount = 0;
52
53 $id = 0;
54 while ( $id !== null ) {
55 $this->output( "Migrating ipblocks with ID > $id...\n" );
56 [ $numBlocks, $id ] = $this->handleBatch( $id );
57 $migratedCount += $numBlocks;
58 }
59
60 $this->output( "Completed migration of $migratedCount ipblocks to block and block_target.\n" );
61
62 return true;
63 }
64
72 private function handleBatch( int $lowId ): array {
73 $migratedCount = 0;
74 $res = $this->dbw->newSelectQueryBuilder()
75 ->select( '*' )
76 ->from( 'ipblocks' )
77 ->leftJoin( 'block', null, 'bl_id=ipb_id' )
78 ->where( [
79 $this->dbw->expr( 'ipb_id', '>', $lowId ),
80 'bl_id' => null
81 ] )
82 ->orderBy( 'ipb_id' )
83 ->limit( $this->getBatchSize() )
84 ->caller( __METHOD__ )
85 ->fetchResultSet();
86
87 if ( !$res->numRows() ) {
88 return [ $migratedCount, null ];
89 }
90
91 $highestId = $lowId;
92 foreach ( $res as $row ) {
93 $highestId = $row->ipb_id;
94 $isIP = IPUtils::isValid( $row->ipb_address );
95 $isRange = IPUtils::isValidRange( $row->ipb_address );
96 $isIPOrRange = $isIP || $isRange;
97 $ipHex = null;
98 if ( $isIP ) {
99 $ipHex = IPUtils::toHex( $row->ipb_address );
100 } elseif ( $isRange ) {
101 $ipHex = $row->ipb_range_start;
102 } elseif ( (int)$row->ipb_user === 0 ) {
103 // There was data corruption circa 2006 and 2011 where some accounts were
104 // blocked as if they were logged out users. Here we'll prune the erroneous
105 // data by simply not copying it to the new schema.
106 $this->output( "ipblock with ID $row->ipb_id: account block with ipb_user=0, skipping…\n" );
107 continue;
108 }
109
110 // Insert into block_target
111 $blockTarget = [
112 'bt_address' => $isIPOrRange ? $row->ipb_address : null,
113 'bt_user' => $isIPOrRange ? null : $row->ipb_user,
114 'bt_user_text' => $isIPOrRange ? null : $row->ipb_address,
115 'bt_auto' => $row->ipb_auto,
116 'bt_range_start' => $isRange ? $row->ipb_range_start : null,
117 'bt_range_end' => $isRange ? $row->ipb_range_end : null,
118 'bt_ip_hex' => $ipHex,
119 'bt_count' => 1
120 ];
121 $this->dbw->newInsertQueryBuilder()
122 ->insertInto( 'block_target' )
123 ->row( $blockTarget )
124 ->caller( __METHOD__ )
125 ->execute();
126 $insertId = $this->dbw->insertId();
127 if ( !$insertId ) {
128 $this->fatalError(
129 "ipblock with ID $row->ipb_id: Failed to create block_target. Insert ID is falsy!"
130 );
131 }
132
133 // Insert into block
134 $block = [
135 'bl_id' => $row->ipb_id,
136 'bl_target' => $insertId,
137 'bl_by_actor' => $row->ipb_by_actor,
138 'bl_reason_id' => $row->ipb_reason_id,
139 'bl_timestamp' => $row->ipb_timestamp,
140 'bl_anon_only' => $row->ipb_anon_only,
141 'bl_create_account' => $row->ipb_create_account,
142 'bl_enable_autoblock' => $row->ipb_enable_autoblock,
143 'bl_expiry' => $row->ipb_expiry,
144 'bl_deleted' => $row->ipb_deleted,
145 'bl_block_email' => $row->ipb_block_email,
146 'bl_allow_usertalk' => $row->ipb_allow_usertalk,
147 // See T282890
148 'bl_parent_block_id' => (int)$row->ipb_parent_block_id === 0 ? null : $row->ipb_parent_block_id,
149 'bl_sitewide' => $row->ipb_sitewide,
150 ];
151 $this->dbw->newInsertQueryBuilder()
152 ->insertInto( 'block' )
153 ->ignore()
154 ->row( $block )
155 ->caller( __METHOD__ )
156 ->execute();
157 if ( $this->dbw->affectedRows() ) {
158 $migratedCount++;
159 }
160 }
161
162 $this->output( "Migrated $migratedCount blocks\n" );
163
164 // Sleep between batches for replication to catch up
165 $this->waitForReplication();
166 $sleep = (int)$this->getOption( 'sleep', 0 );
167 if ( $sleep > 0 ) {
168 sleep( $sleep );
169 }
170
171 return [ $migratedCount, $highestId ];
172 }
173}
174
175$maintClass = MigrateBlocks::class;
176require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Maintenance script that migrates rows from ipblocks to block and block_target.
doDBUpdates()
Do the actual work.
__construct()
Default constructor.
getUpdateKey()
Get the update key name to go in the update log table.
Advanced database interface for IDatabase handles that include maintenance methods.
$maintClass
const DB_PRIMARY
Definition defines.php:28