| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* There are several problems with 2.3.3 => 2.4 that this attempts to cope with: |
| 5 |
* - some sites have a misconfigured IP column |
| 6 |
* - some sites don't have any IP column |
| 7 |
*/ |
| 8 |
class Red_Database_240 extends Red_Database_Upgrader { |
| 9 |
public function get_stages() { |
| 10 |
return [ |
| 11 |
'convert_int_ip_to_varchar_240' => 'Convert integer IP values to support IPv6', |
| 12 |
'expand_log_ip_column_240' => 'Expand IP size in logs to support IPv6', |
| 13 |
'convert_title_to_text_240' => 'Expand size of redirect titles', |
| 14 |
'add_missing_index_240' => 'Add missing IP index to 404 logs', |
| 15 |
]; |
| 16 |
} |
| 17 |
|
| 18 |
private function has_ip_index( $wpdb ) { |
| 19 |
$existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N ); |
| 20 |
|
| 21 |
if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), 'key `ip` (' ) !== false ) { |
| 22 |
return true; |
| 23 |
} |
| 24 |
|
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
protected function has_varchar_ip( $wpdb ) { |
| 29 |
$existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N ); |
| 30 |
|
| 31 |
if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), '`ip` varchar(45)' ) !== false ) { |
| 32 |
return true; |
| 33 |
} |
| 34 |
|
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
protected function has_int_ip( $wpdb ) { |
| 39 |
$existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N ); |
| 40 |
|
| 41 |
if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), '`ip` int' ) !== false ) { |
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
protected function convert_int_ip_to_varchar_240( $wpdb ) { |
| 49 |
if ( $this->has_int_ip( $wpdb ) ) { |
| 50 |
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `ipaddress` VARCHAR(45) DEFAULT NULL AFTER `ip`" ); |
| 51 |
$this->do_query( $wpdb, "UPDATE {$wpdb->prefix}redirection_404 SET ipaddress=INET_NTOA(ip)" ); |
| 52 |
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` DROP `ip`" ); |
| 53 |
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` CHANGE `ipaddress` `ip` VARCHAR(45) DEFAULT NULL" ); |
| 54 |
} |
| 55 |
|
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
protected function expand_log_ip_column_240( $wpdb ) { |
| 60 |
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` CHANGE `ip` `ip` VARCHAR(45) DEFAULT NULL" ); |
| 61 |
} |
| 62 |
|
| 63 |
protected function add_missing_index_240( $wpdb ) { |
| 64 |
if ( $this->has_ip_index( $wpdb ) ) { |
| 65 |
// Remove index |
| 66 |
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` DROP INDEX ip" ); |
| 67 |
} |
| 68 |
|
| 69 |
// Ensure we have an IP column |
| 70 |
$this->convert_int_ip_to_varchar_240( $wpdb ); |
| 71 |
if ( ! $this->has_varchar_ip( $wpdb ) ) { |
| 72 |
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `ip` VARCHAR(45) DEFAULT NULL" ); |
| 73 |
} |
| 74 |
|
| 75 |
// Finally add the index |
| 76 |
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD INDEX `ip` (`ip`)" ); |
| 77 |
} |
| 78 |
|
| 79 |
protected function convert_title_to_text_240( $wpdb ) { |
| 80 |
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` CHANGE `title` `title` text" ); |
| 81 |
} |
| 82 |
} |
| 83 |
|