22 parent::__construct();
24 'Copy data from the ipblocks table into the new block and block_target tables'
28 'Sleep time (in seconds) between every batch. Default: 0',
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." );
45 if ( !$this->dbw->tableExists(
'ipblocks' ) ) {
46 $this->
output(
"No ipblocks table, skipping migration to block_target.\n" );
50 $this->
output(
"Populating the block and block_target tables\n" );
54 while ( $id !==
null ) {
55 $this->
output(
"Migrating ipblocks with ID > $id...\n" );
56 [ $numBlocks, $id ] = $this->handleBatch( $id );
57 $migratedCount += $numBlocks;
60 $this->
output(
"Completed migration of $migratedCount ipblocks to block and block_target.\n" );
72 private function handleBatch(
int $lowId ): array {
74 $res = $this->dbw->newSelectQueryBuilder()
77 ->leftJoin(
'block',
null,
'bl_id=ipb_id' )
79 $this->dbw->expr(
'ipb_id',
'>', $lowId ),
84 ->caller( __METHOD__ )
87 if ( !$res->numRows() ) {
88 return [ $migratedCount, null ];
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;
99 $ipHex = IPUtils::toHex( $row->ipb_address );
100 } elseif ( $isRange ) {
101 $ipHex = $row->ipb_range_start;
102 } elseif ( (
int)$row->ipb_user === 0 ) {
106 $this->
output(
"ipblock with ID $row->ipb_id: account block with ipb_user=0, skipping…\n" );
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,
121 $this->dbw->newInsertQueryBuilder()
122 ->insertInto(
'block_target' )
123 ->row( $blockTarget )
124 ->caller( __METHOD__ )
126 $insertId = $this->dbw->insertId();
129 "ipblock with ID $row->ipb_id: Failed to create block_target. Insert ID is falsy!"
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,
148 'bl_parent_block_id' => (int)$row->ipb_parent_block_id === 0 ?
null : $row->ipb_parent_block_id,
149 'bl_sitewide' => $row->ipb_sitewide,
151 $this->dbw->newInsertQueryBuilder()
152 ->insertInto(
'block' )
155 ->caller( __METHOD__ )
157 if ( $this->dbw->affectedRows() ) {
162 $this->
output(
"Migrated $migratedCount blocks\n" );
166 $sleep = (int)$this->
getOption(
'sleep', 0 );
171 return [ $migratedCount, $highestId ];
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.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.