PluginProbe
Redirection / 4.3
Redirection v4.3
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / database / schema / 240.php

240.php in Redirection 4.3, at database/schema/240.php

89 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $wpdb->hide_errors();
20 $existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N );
21 $wpdb->show_errors();
22
23 if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), 'key `ip` (' ) !== false ) {
24 return true;
25 }
26
27 return false;
28 }
29
30 protected function has_varchar_ip( $wpdb ) {
31 $wpdb->hide_errors();
32 $existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N );
33 $wpdb->show_errors();
34
35 if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), '`ip` varchar(45)' ) !== false ) {
36 return true;
37 }
38
39 return false;
40 }
41
42 protected function has_int_ip( $wpdb ) {
43 $wpdb->hide_errors();
44 $existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N );
45 $wpdb->show_errors();
46
47 if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), '`ip` int' ) !== false ) {
48 return true;
49 }
50
51 return false;
52 }
53
54 protected function convert_int_ip_to_varchar_240( $wpdb ) {
55 if ( $this->has_int_ip( $wpdb ) ) {
56 $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `ipaddress` VARCHAR(45) DEFAULT NULL AFTER `ip`" );
57 $this->do_query( $wpdb, "UPDATE {$wpdb->prefix}redirection_404 SET ipaddress=INET_NTOA(ip)" );
58 $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` DROP `ip`" );
59 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` CHANGE `ipaddress` `ip` VARCHAR(45) DEFAULT NULL" );
60 }
61
62 return true;
63 }
64
65 protected function expand_log_ip_column_240( $wpdb ) {
66 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` CHANGE `ip` `ip` VARCHAR(45) DEFAULT NULL" );
67 }
68
69 protected function add_missing_index_240( $wpdb ) {
70 if ( $this->has_ip_index( $wpdb ) ) {
71 // Remove index
72 $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` DROP INDEX ip" );
73 }
74
75 // Ensure we have an IP column
76 $this->convert_int_ip_to_varchar_240( $wpdb );
77 if ( ! $this->has_varchar_ip( $wpdb ) ) {
78 $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `ip` VARCHAR(45) DEFAULT NULL" );
79 }
80
81 // Finally add the index
82 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD INDEX `ip` (`ip`)" );
83 }
84
85 protected function convert_title_to_text_240( $wpdb ) {
86 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` CHANGE `title` `title` text" );
87 }
88 }
89