PluginProbe
Redirection / 5.6.0
Redirection v5.6.0
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 5.6.0, at database/schema/240.php

120 lines 3.4 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 /**
10 * @return array<string, string>
11 */
12 public function get_stages() {
13 return [
14 'convert_int_ip_to_varchar_240' => 'Convert integer IP values to support IPv6',
15 'expand_log_ip_column_240' => 'Expand IP size in logs to support IPv6',
16 'convert_title_to_text_240' => 'Expand size of redirect titles',
17 'add_missing_index_240' => 'Add missing IP index to 404 logs',
18 ];
19 }
20
21 /**
22 * @param \wpdb $wpdb
23 * @return bool
24 */
25 private function has_ip_index( $wpdb ) {
26 $wpdb->hide_errors();
27 $existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N );
28 $wpdb->show_errors();
29
30 if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), 'key `ip` (' ) !== false ) {
31 return true;
32 }
33
34 return false;
35 }
36
37 /**
38 * @param \wpdb $wpdb
39 * @return bool
40 */
41 protected function has_varchar_ip( $wpdb ) {
42 $wpdb->hide_errors();
43 $existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N );
44 $wpdb->show_errors();
45
46 if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), '`ip` varchar(45)' ) !== false ) {
47 return true;
48 }
49
50 return false;
51 }
52
53 /**
54 * @param \wpdb $wpdb
55 * @return bool
56 */
57 protected function has_int_ip( $wpdb ) {
58 $wpdb->hide_errors();
59 $existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N );
60 $wpdb->show_errors();
61
62 if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), '`ip` int' ) !== false ) {
63 return true;
64 }
65
66 return false;
67 }
68
69 /**
70 * @param \wpdb $wpdb
71 * @return bool
72 */
73 protected function convert_int_ip_to_varchar_240( $wpdb ) {
74 if ( $this->has_int_ip( $wpdb ) ) {
75 $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `ipaddress` VARCHAR(45) DEFAULT NULL AFTER `ip`" );
76 $this->do_query( $wpdb, "UPDATE {$wpdb->prefix}redirection_404 SET ipaddress=INET_NTOA(ip)" );
77 $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` DROP `ip`" );
78 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` CHANGE `ipaddress` `ip` VARCHAR(45) DEFAULT NULL" );
79 }
80
81 return true;
82 }
83
84 /**
85 * @param \wpdb $wpdb
86 * @return bool
87 */
88 protected function expand_log_ip_column_240( $wpdb ) {
89 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` CHANGE `ip` `ip` VARCHAR(45) DEFAULT NULL" );
90 }
91
92 /**
93 * @param \wpdb $wpdb
94 * @return bool
95 */
96 protected function add_missing_index_240( $wpdb ) {
97 if ( $this->has_ip_index( $wpdb ) ) {
98 // Remove index
99 $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` DROP INDEX ip" );
100 }
101
102 // Ensure we have an IP column
103 $this->convert_int_ip_to_varchar_240( $wpdb );
104 if ( ! $this->has_varchar_ip( $wpdb ) ) {
105 $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `ip` VARCHAR(45) DEFAULT NULL" );
106 }
107
108 // Finally add the index
109 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD INDEX `ip` (`ip`)" );
110 }
111
112 /**
113 * @param \wpdb $wpdb
114 * @return bool
115 */
116 protected function convert_title_to_text_240( $wpdb ) {
117 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` CHANGE `title` `title` text" );
118 }
119 }
120