PluginProbe
Redirection / 5.2
Redirection v5.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 / latest.php

latest.php in Redirection 5.2, at database/schema/latest.php

261 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Latest database schema
5 */
6 class Red_Latest_Database extends Red_Database_Upgrader {
7 public function get_stages() {
8 return [
9 /* translators: displayed when installing the plugin */
10 'create_tables' => __( 'Install Redirection tables', 'redirection' ),
11 /* translators: displayed when installing the plugin */
12 'create_groups' => __( 'Create basic data', 'redirection' ),
13 ];
14 }
15
16 /**
17 * Install the latest database
18 *
19 * @return bool|WP_Error true if installed, WP_Error otherwise
20 */
21 public function install() {
22 global $wpdb;
23
24 foreach ( $this->get_stages() as $stage => $info ) {
25 $result = $this->$stage( $wpdb );
26
27 if ( is_wp_error( $result ) ) {
28 if ( $wpdb->last_error ) {
29 $result->add_data( $wpdb->last_error );
30 }
31
32 return $result;
33 }
34 }
35
36 red_set_options( array( 'database' => REDIRECTION_DB_VERSION ) );
37 return true;
38 }
39
40 /**
41 * Remove the database and any options (including unused ones)
42 */
43 public function remove() {
44 global $wpdb;
45
46 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_items" );
47 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_logs" );
48 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_groups" );
49 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_modules" );
50 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_404" );
51
52 delete_option( 'redirection_lookup' );
53 delete_option( 'redirection_post' );
54 delete_option( 'redirection_root' );
55 delete_option( 'redirection_index' );
56 delete_option( 'redirection_options' );
57 delete_option( Red_Database_Status::OLD_DB_VERSION );
58 }
59
60 /**
61 * Return any tables that are missing from the database
62 *
63 * @return array Array of missing table names
64 */
65 public function get_missing_tables() {
66 global $wpdb;
67
68 $tables = array_keys( $this->get_all_tables() );
69 $missing = [];
70
71 foreach ( $tables as $table ) {
72 $result = $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) );
73
74 if ( intval( $result, 10 ) !== 1 ) {
75 $missing[] = $table;
76 }
77 }
78
79 return $missing;
80 }
81
82 /**
83 * Get table schema for latest database tables
84 *
85 * @return array Database schema array
86 */
87 public function get_table_schema() {
88 global $wpdb;
89
90 $tables = array_keys( $this->get_all_tables() );
91 $show = array();
92
93 foreach ( $tables as $table ) {
94 // These are known queries without user input
95 // phpcs:ignore
96 $row = $wpdb->get_row( 'SHOW CREATE TABLE ' . $table, ARRAY_N );
97
98 if ( $row ) {
99 $show = array_merge( $show, explode( "\n", $row[1] ) );
100 $show[] = '';
101 } else {
102 /* translators: 1: table name */
103 $show[] = sprintf( __( 'Table "%s" is missing', 'redirection' ), $table );
104 }
105 }
106
107 return $show;
108 }
109
110 /**
111 * Return array of table names and table schema
112 *
113 * @return array
114 */
115 public function get_all_tables() {
116 global $wpdb;
117
118 $charset_collate = $this->get_charset();
119
120 return array(
121 "{$wpdb->prefix}redirection_items" => $this->create_items_sql( $wpdb->prefix, $charset_collate ),
122 "{$wpdb->prefix}redirection_groups" => $this->create_groups_sql( $wpdb->prefix, $charset_collate ),
123 "{$wpdb->prefix}redirection_logs" => $this->create_log_sql( $wpdb->prefix, $charset_collate ),
124 "{$wpdb->prefix}redirection_404" => $this->create_404_sql( $wpdb->prefix, $charset_collate ),
125 );
126 }
127
128 /**
129 * Creates default group information
130 */
131 public function create_groups( $wpdb, $is_live = true ) {
132 if ( ! $is_live ) {
133 return true;
134 }
135
136 $defaults = [
137 [
138 'name' => __( 'Redirections', 'redirection' ),
139 'module_id' => 1,
140 'position' => 0,
141 ],
142 [
143 'name' => __( 'Modified Posts', 'redirection' ),
144 'module_id' => 1,
145 'position' => 1,
146 ],
147 ];
148
149 $existing_groups = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" );
150
151 // Default groups
152 if ( intval( $existing_groups, 10 ) === 0 ) {
153 $wpdb->insert( $wpdb->prefix . 'redirection_groups', $defaults[0] );
154 $wpdb->insert( $wpdb->prefix . 'redirection_groups', $defaults[1] );
155 }
156
157 $group = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}redirection_groups LIMIT 1" );
158 if ( $group ) {
159 red_set_options( array( 'last_group_id' => $group->id ) );
160 }
161
162 return true;
163 }
164
165 /**
166 * Creates all the tables
167 */
168 public function create_tables( $wpdb ) {
169 global $wpdb;
170
171 foreach ( $this->get_all_tables() as $table => $sql ) {
172 $sql = preg_replace( '/[ \t]{2,}/', '', $sql );
173 $this->do_query( $wpdb, $sql );
174 }
175
176 return true;
177 }
178
179 private function create_items_sql( $prefix, $charset_collate ) {
180 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_items` (
181 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
182 `url` mediumtext NOT NULL,
183 `match_url` VARCHAR(2000) DEFAULT NULL,
184 `match_data` TEXT,
185 `regex` INT(11) unsigned NOT NULL DEFAULT '0',
186 `position` INT(11) unsigned NOT NULL DEFAULT '0',
187 `last_count` INT(10) unsigned NOT NULL DEFAULT '0',
188 `last_access` datetime NOT NULL DEFAULT '1970-01-01 00:00:00',
189 `group_id` INT(11) NOT NULL DEFAULT '0',
190 `status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
191 `action_type` VARCHAR(20) NOT NULL,
192 `action_code` INT(11) unsigned NOT NULL,
193 `action_data` MEDIUMTEXT,
194 `match_type` VARCHAR(20) NOT NULL,
195 `title` TEXT,
196 PRIMARY KEY (`id`),
197 KEY `url` (`url`(191)),
198 KEY `status` (`status`),
199 KEY `regex` (`regex`),
200 KEY `group_idpos` (`group_id`,`position`),
201 KEY `group` (`group_id`),
202 KEY `match_url` (`match_url`(191))
203 ) $charset_collate";
204 }
205
206 private function create_groups_sql( $prefix, $charset_collate ) {
207 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_groups` (
208 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
209 `name` VARCHAR(50) NOT NULL,
210 `tracking` INT(11) NOT NULL DEFAULT '1',
211 `module_id` INT(11) unsigned NOT NULL DEFAULT '0',
212 `status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
213 `position` INT(11) unsigned NOT NULL DEFAULT '0',
214 PRIMARY KEY (`id`),
215 KEY `module_id` (`module_id`),
216 KEY `status` (`status`)
217 ) $charset_collate";
218 }
219
220 private function create_log_sql( $prefix, $charset_collate ) {
221 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_logs` (
222 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
223 `created` datetime NOT NULL,
224 `url` MEDIUMTEXT NOT NULL,
225 `domain` VARCHAR(255) DEFAULT NULL,
226 `sent_to` MEDIUMTEXT,
227 `agent` MEDIUMTEXT,
228 `referrer` MEDIUMTEXT,
229 `http_code` INT(11) unsigned NOT NULL DEFAULT '0',
230 `request_method` VARCHAR(10) DEFAULT NULL,
231 `request_data` MEDIUMTEXT,
232 `redirect_by` VARCHAR(50) DEFAULT NULL,
233 `redirection_id` INT(11) unsigned DEFAULT NULL,
234 `ip` VARCHAR(45) DEFAULT NULL,
235 PRIMARY KEY (`id`),
236 KEY `created` (`created`),
237 KEY `redirection_id` (`redirection_id`),
238 KEY `ip` (`ip`)
239 ) $charset_collate";
240 }
241
242 private function create_404_sql( $prefix, $charset_collate ) {
243 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_404` (
244 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
245 `created` datetime NOT NULL,
246 `url` MEDIUMTEXT NOT NULL,
247 `domain` VARCHAR(255) DEFAULT NULL,
248 `agent` VARCHAR(255) DEFAULT NULL,
249 `referrer` VARCHAR(255) DEFAULT NULL,
250 `http_code` INT(11) unsigned NOT NULL DEFAULT '0',
251 `request_method` VARCHAR(10) DEFAULT NULL,
252 `request_data` MEDIUMTEXT,
253 `ip` VARCHAR(45) DEFAULT NULL,
254 PRIMARY KEY (`id`),
255 KEY `created` (`created`),
256 KEY `referrer` (`referrer`(191)),
257 KEY `ip` (`ip`)
258 ) $charset_collate";
259 }
260 }
261