PluginProbe
Redirection / 3.7.2
Redirection v3.7.2
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 3.7.2, at database/schema/240.php

85 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Note that some installations have the 2.4 tables setup, but in a slightly different way, and with the DB version set to 2.3.3
5 * Unsure why this would happen, but it means we get an error when upgrading
6 * Try and detect this and skip the upgrade
7 */
8 class Red_Database_240 extends Red_Database_Upgrader {
9 private $existing_column = false;
10
11 public function get_stages() {
12 return [
13 'detect_existing_240' => 'Detect existing 2.4 upgrade',
14 'expand_log_ip_column_240' => 'Expand IP size in logs to support IPv6',
15 'convert_int_ip_to_varchar_240' => 'Convert integer IP values to support IPv6',
16 'swap_ip_column_240' => 'Swap IPv4 for IPv6',
17 'convert_title_to_text_240' => 'Expand size of redirect titles',
18 'add_missing_index_240' => 'Add missing IP index to 404 logs',
19 ];
20 }
21
22 private function has_weird_ip_index() {
23 global $wpdb;
24
25 $existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N );
26
27 if ( isset( $existing[1] ) ) {
28 return strpos( strtolower( $existing[1] ), 'key `ip` (' ) !== false;
29 }
30
31 return false;
32 }
33
34 protected function detect_existing_240( $wpdb ) {
35 $existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N );
36
37 if ( isset( $existing[1] ) ) {
38 if ( strpos( strtolower( $existing[1] ), '`ip` varchar(45)' ) !== false ) {
39 // IP as varchar exists - don't recreate
40 $this->existing_column = true;
41 }
42 }
43
44 return true;
45 }
46
47 protected function expand_log_ip_column_240( $wpdb ) {
48 if ( ! $this->existing_column ) {
49 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` CHANGE `ip` `ip` VARCHAR(45) DEFAULT NULL" );
50 }
51
52 return true;
53 }
54
55 protected function convert_int_ip_to_varchar_240( $wpdb ) {
56 if ( ! $this->existing_column ) {
57 $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `ipaddress` VARCHAR(45) DEFAULT NULL AFTER `ip`" );
58 return $this->do_query( $wpdb, "UPDATE {$wpdb->prefix}redirection_404 SET ipaddress=INET_NTOA(ip)" );
59 }
60
61 return true;
62 }
63
64 protected function swap_ip_column_240( $wpdb ) {
65 if ( ! $this->existing_column ) {
66 $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` DROP `ip`" );
67 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` CHANGE `ipaddress` `ip` VARCHAR(45) DEFAULT NULL" );
68 }
69
70 return true;
71 }
72
73 protected function add_missing_index_240( $wpdb ) {
74 if ( $this->has_weird_ip_index() ) {
75 $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` DROP INDEX ip" );
76 }
77
78 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD INDEX `ip` (`ip`)" );
79 }
80
81 protected function convert_title_to_text_240( $wpdb ) {
82 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` CHANGE `title` `title` text" );
83 }
84 }
85