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',
48 !$this->dbw->tableExists(
'block', __METHOD__ ) ||
49 !$this->dbw->tableExists(
'block_target', __METHOD__ )
51 $this->
fatalError(
"Run update.php to create the block and block_target tables." );
53 if ( !$this->dbw->tableExists(
'ipblocks', __METHOD__ ) ) {
54 $this->
output(
"No ipblocks table, skipping migration to block_target.\n" );
58 $this->
output(
"Populating the block and block_target tables\n" );
62 while ( $id !==
null ) {
63 $this->
output(
"Migrating ipblocks with ID > $id...\n" );
64 [ $numBlocks, $id ] = $this->handleBatch( $id );
65 $migratedCount += $numBlocks;
68 $this->
output(
"Completed migration of $migratedCount ipblocks to block and block_target.\n" );
80 private function handleBatch(
int $lowId ): array {
82 $res = $this->dbw->newSelectQueryBuilder()
85 ->leftJoin(
'block',
null,
'bl_id=ipb_id' )
87 $this->dbw->expr(
'ipb_id',
'>', $lowId ),
92 ->caller( __METHOD__ )
95 if ( !$res->numRows() ) {
96 return [ $migratedCount, null ];
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;
107 $ipHex = IPUtils::toHex( $row->ipb_address );
108 } elseif ( $isRange ) {
109 $ipHex = $row->ipb_range_start;
110 } elseif ( (
int)$row->ipb_user === 0 ) {
114 $this->
output(
"ipblock with ID $row->ipb_id: account block with ipb_user=0, skipping…\n" );
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,
129 $this->dbw->newInsertQueryBuilder()
130 ->insertInto(
'block_target' )
131 ->row( $blockTarget )
132 ->caller( __METHOD__ )
134 $insertId = $this->dbw->insertId();
137 "ipblock with ID $row->ipb_id: Failed to create block_target. Insert ID is falsy!"
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,
156 'bl_parent_block_id' => (int)$row->ipb_parent_block_id === 0 ?
null : $row->ipb_parent_block_id,
157 'bl_sitewide' => $row->ipb_sitewide,
159 $this->dbw->newInsertQueryBuilder()
160 ->insertInto(
'block' )
163 ->caller( __METHOD__ )
165 if ( $this->dbw->affectedRows() ) {
170 $this->
output(
"Migrated $migratedCount blocks\n" );
174 $sleep = (int)$this->
getOption(
'sleep', 0 );
179 return [ $migratedCount, $highestId ];
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.