24 parent::__construct();
25 $this->addDescription(
26 'Copy data from the ipblocks table into the new block and block_target tables'
30 'Sleep time (in seconds) between every batch. Default: 0',
35 $this->setBatchSize( 500 );
45 !$this->dbw->tableExists(
'block', __METHOD__ ) ||
46 !$this->dbw->tableExists(
'block_target', __METHOD__ )
48 $this->fatalError(
"Run update.php to create the block and block_target tables." );
50 if ( !$this->dbw->tableExists(
'ipblocks', __METHOD__ ) ) {
51 $this->output(
"No ipblocks table, skipping migration to block_target.\n" );
55 $this->output(
"Populating the block and block_target tables\n" );
59 while ( $id !==
null ) {
60 $this->output(
"Migrating ipblocks with ID > $id...\n" );
61 [ $numBlocks, $id ] = $this->handleBatch( $id );
62 $migratedCount += $numBlocks;
65 $this->output(
"Completed migration of $migratedCount ipblocks to block and block_target.\n" );
77 private function handleBatch(
int $lowId ): array {
79 $res = $this->dbw->newSelectQueryBuilder()
82 ->leftJoin(
'block',
null,
'bl_id=ipb_id' )
84 $this->dbw->expr(
'ipb_id',
'>', $lowId ),
88 ->limit( $this->getBatchSize() )
89 ->caller( __METHOD__ )
92 if ( !$res->numRows() ) {
93 return [ $migratedCount, null ];
97 foreach ( $res as $row ) {
98 $highestId = $row->ipb_id;
99 $isIP = IPUtils::isValid( $row->ipb_address );
100 $isRange = IPUtils::isValidRange( $row->ipb_address );
101 $isIPOrRange = $isIP || $isRange;
104 $ipHex = IPUtils::toHex( $row->ipb_address );
105 } elseif ( $isRange ) {
106 $ipHex = $row->ipb_range_start;
107 } elseif ( (
int)$row->ipb_user === 0 ) {
111 $this->output(
"ipblock with ID $row->ipb_id: account block with ipb_user=0, skipping…\n" );
117 'bt_address' => $isIPOrRange ? $row->ipb_address :
null,
118 'bt_user' => $isIPOrRange ? null : $row->ipb_user,
119 'bt_user_text' => $isIPOrRange ? null : $row->ipb_address,
120 'bt_auto' => $row->ipb_auto,
121 'bt_range_start' => $isRange ? $row->ipb_range_start :
null,
122 'bt_range_end' => $isRange ? $row->ipb_range_end :
null,
123 'bt_ip_hex' => $ipHex,
126 $this->dbw->newInsertQueryBuilder()
127 ->insertInto(
'block_target' )
128 ->row( $blockTarget )
129 ->caller( __METHOD__ )
131 $insertId = $this->dbw->insertId();
134 "ipblock with ID $row->ipb_id: Failed to create block_target. Insert ID is falsy!"
140 'bl_id' => $row->ipb_id,
141 'bl_target' => $insertId,
142 'bl_by_actor' => $row->ipb_by_actor,
143 'bl_reason_id' => $row->ipb_reason_id,
144 'bl_timestamp' => $row->ipb_timestamp,
145 'bl_anon_only' => $row->ipb_anon_only,
146 'bl_create_account' => $row->ipb_create_account,
147 'bl_enable_autoblock' => $row->ipb_enable_autoblock,
148 'bl_expiry' => $row->ipb_expiry,
149 'bl_deleted' => $row->ipb_deleted,
150 'bl_block_email' => $row->ipb_block_email,
151 'bl_allow_usertalk' => $row->ipb_allow_usertalk,
153 'bl_parent_block_id' => (int)$row->ipb_parent_block_id === 0 ?
null : $row->ipb_parent_block_id,
154 'bl_sitewide' => $row->ipb_sitewide,
156 $this->dbw->newInsertQueryBuilder()
157 ->insertInto(
'block' )
160 ->caller( __METHOD__ )
162 if ( $this->dbw->affectedRows() ) {
167 $this->output(
"Migrated $migratedCount blocks\n" );
170 $this->waitForReplication();
171 $sleep = (int)$this->getOption(
'sleep', 0 );
176 return [ $migratedCount, $highestId ];