25 parent::__construct();
27 'Copy data from the ipblocks table into the new block and block_target tables'
31 'Sleep time (in seconds) between every batch. Default: 0',
46 !$this->dbw->tableExists(
'block', __METHOD__ ) ||
47 !$this->dbw->tableExists(
'block_target', __METHOD__ )
49 $this->
fatalError(
"Run update.php to create the block and block_target tables." );
51 if ( !$this->dbw->tableExists(
'ipblocks', __METHOD__ ) ) {
52 $this->
output(
"No ipblocks table, skipping migration to block_target.\n" );
56 $this->
output(
"Populating the block and block_target tables\n" );
60 while ( $id !==
null ) {
61 $this->
output(
"Migrating ipblocks with ID > $id...\n" );
62 [ $numBlocks, $id ] = $this->handleBatch( $id );
63 $migratedCount += $numBlocks;
66 $this->
output(
"Completed migration of $migratedCount ipblocks to block and block_target.\n" );
78 private function handleBatch(
int $lowId ): array {
80 $res = $this->dbw->newSelectQueryBuilder()
83 ->leftJoin(
'block',
null,
'bl_id=ipb_id' )
85 $this->dbw->expr(
'ipb_id',
'>', $lowId ),
90 ->caller( __METHOD__ )
93 if ( !$res->numRows() ) {
94 return [ $migratedCount, null ];
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;
105 $ipHex = IPUtils::toHex( $row->ipb_address );
106 } elseif ( $isRange ) {
107 $ipHex = $row->ipb_range_start;
108 } elseif ( (
int)$row->ipb_user === 0 ) {
112 $this->
output(
"ipblock with ID $row->ipb_id: account block with ipb_user=0, skipping…\n" );
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,
127 $this->dbw->newInsertQueryBuilder()
128 ->insertInto(
'block_target' )
129 ->row( $blockTarget )
130 ->caller( __METHOD__ )
132 $insertId = $this->dbw->insertId();
135 "ipblock with ID $row->ipb_id: Failed to create block_target. Insert ID is falsy!"
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,
154 'bl_parent_block_id' => (int)$row->ipb_parent_block_id === 0 ?
null : $row->ipb_parent_block_id,
155 'bl_sitewide' => $row->ipb_sitewide,
157 $this->dbw->newInsertQueryBuilder()
158 ->insertInto(
'block' )
161 ->caller( __METHOD__ )
163 if ( $this->dbw->affectedRows() ) {
168 $this->
output(
"Migrated $migratedCount blocks\n" );
172 $sleep = (int)$this->
getOption(
'sleep', 0 );
177 return [ $migratedCount, $highestId ];
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.