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." );
46 $this->
output(
"Populating the block and block_target tables\n" );
50 while ( $id !==
null ) {
51 $this->
output(
"Migrating ipblocks with ID > $id...\n" );
52 [ $numBlocks, $id ] = $this->handleBatch( $id );
53 $migratedCount += $numBlocks;
56 $this->
output(
"Completed migration of $migratedCount ipblocks to block and block_target.\n" );
68 private function handleBatch(
int $lowId ): array {
70 $res = $this->dbw->newSelectQueryBuilder()
73 ->leftJoin(
'block',
null,
'bl_id=ipb_id' )
75 $this->dbw->expr(
'ipb_id',
'>', $lowId ),
80 ->caller( __METHOD__ )
83 if ( !$res->numRows() ) {
84 return [ $migratedCount, null ];
88 foreach ( $res as $row ) {
89 $highestId = $row->ipb_id;
90 $isIP = IPUtils::isValid( $row->ipb_address );
91 $isRange = IPUtils::isValidRange( $row->ipb_address );
92 $isIPOrRange = $isIP || $isRange;
95 $ipHex = IPUtils::toHex( $row->ipb_address );
96 } elseif ( $isRange ) {
97 $ipHex = $row->ipb_range_start;
98 } elseif ( (
int)$row->ipb_user === 0 ) {
102 $this->
output(
"ipblock with ID $row->ipb_id: account block with ipb_user=0, skipping…\n" );
108 'bt_address' => $isIPOrRange ? $row->ipb_address :
null,
109 'bt_user' => $isIPOrRange ? null : $row->ipb_user,
110 'bt_user_text' => $isIPOrRange ? null : $row->ipb_address,
111 'bt_auto' => $row->ipb_auto,
112 'bt_range_start' => $isRange ? $row->ipb_range_start :
null,
113 'bt_range_end' => $isRange ? $row->ipb_range_end :
null,
114 'bt_ip_hex' => $ipHex,
117 $this->dbw->newInsertQueryBuilder()
118 ->insertInto(
'block_target' )
119 ->row( $blockTarget )
120 ->caller( __METHOD__ )
122 $insertId = $this->dbw->insertId();
125 "ipblock with ID $row->ipb_id: Failed to create block_target. Insert ID is falsy!"
131 'bl_id' => $row->ipb_id,
132 'bl_target' => $insertId,
133 'bl_by_actor' => $row->ipb_by_actor,
134 'bl_reason_id' => $row->ipb_reason_id,
135 'bl_timestamp' => $row->ipb_timestamp,
136 'bl_anon_only' => $row->ipb_anon_only,
137 'bl_create_account' => $row->ipb_create_account,
138 'bl_enable_autoblock' => $row->ipb_enable_autoblock,
139 'bl_expiry' => $row->ipb_expiry,
140 'bl_deleted' => $row->ipb_deleted,
141 'bl_block_email' => $row->ipb_block_email,
142 'bl_allow_usertalk' => $row->ipb_allow_usertalk,
144 'bl_parent_block_id' => (int)$row->ipb_parent_block_id === 0 ?
null : $row->ipb_parent_block_id,
145 'bl_sitewide' => $row->ipb_sitewide,
147 $this->dbw->newInsertQueryBuilder()
148 ->insertInto(
'block' )
151 ->caller( __METHOD__ )
153 if ( $this->dbw->affectedRows() ) {
158 $this->
output(
"Migrated $migratedCount blocks\n" );
162 $sleep = (int)$this->
getOption(
'sleep', 0 );
167 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.