PluginProbe
Redirection / 4.8
Redirection v4.8
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 4.8, at database/schema/latest.php

258 lines 7.2 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 delete_option( Red_Database_Status::DB_UPGRADE_STAGE );
59 }
60
61 /**
62 * Return any tables that are missing from the database
63 *
64 * @return array Array of missing table names
65 */
66 public function get_missing_tables() {
67 global $wpdb;
68
69 $tables = array_keys( $this->get_all_tables() );
70 $missing = [];
71
72 foreach ( $tables as $table ) {
73 $result = $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) );
74
75 if ( intval( $result, 10 ) !== 1 ) {
76 $missing[] = $table;
77 }
78 }
79
80 return $missing;
81 }
82
83 /**
84 * Get table schema for latest database tables
85 *
86 * @return array Database schema array
87 */
88 public function get_table_schema() {
89 global $wpdb;
90
91 $tables = array_keys( $this->get_all_tables() );
92 $show = array();
93
94 foreach ( $tables as $table ) {
95 // These are known queries without user input
96 // phpcs:ignore
97 $row = $wpdb->get_row( 'SHOW CREATE TABLE ' . $table, ARRAY_N );
98
99 if ( $row ) {
100 $show = array_merge( $show, explode( "\n", $row[1] ) );
101 $show[] = '';
102 } else {
103 /* translators: 1: table name */
104 $show[] = sprintf( __( 'Table "%s" is missing', 'redirection' ), $table );
105 }
106 }
107
108 return $show;
109 }
110
111 /**
112 * Return array of table names and table schema
113 *
114 * @return array
115 */
116 public function get_all_tables() {
117 global $wpdb;
118
119 $charset_collate = $this->get_charset();
120
121 return array(
122 "{$wpdb->prefix}redirection_items" => $this->create_items_sql( $wpdb->prefix, $charset_collate ),
123 "{$wpdb->prefix}redirection_groups" => $this->create_groups_sql( $wpdb->prefix, $charset_collate ),
124 "{$wpdb->prefix}redirection_logs" => $this->create_log_sql( $wpdb->prefix, $charset_collate ),
125 "{$wpdb->prefix}redirection_404" => $this->create_404_sql( $wpdb->prefix, $charset_collate ),
126 );
127 }
128
129 /**
130 * Creates default group information
131 */
132 public function create_groups( $wpdb, $is_live = true ) {
133 if ( ! $is_live ) {
134 return true;
135 }
136
137 $defaults = [
138 [
139 'name' => __( 'Redirections', 'redirection' ),
140 'module_id' => 1,
141 'position' => 0,
142 ],
143 [
144 'name' => __( 'Modified Posts', 'redirection' ),
145 'module_id' => 1,
146 'position' => 1,
147 ],
148 ];
149
150 $existing_groups = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" );
151
152 // Default groups
153 if ( intval( $existing_groups, 10 ) === 0 ) {
154 $wpdb->insert( $wpdb->prefix . 'redirection_groups', $defaults[0] );
155 $wpdb->insert( $wpdb->prefix . 'redirection_groups', $defaults[1] );
156 }
157
158 $group = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}redirection_groups LIMIT 1" );
159 if ( $group ) {
160 red_set_options( array( 'last_group_id' => $group->id ) );
161 }
162
163 return true;
164 }
165
166 /**
167 * Creates all the tables
168 */
169 public function create_tables( $wpdb ) {
170 global $wpdb;
171
172 foreach ( $this->get_all_tables() as $table => $sql ) {
173 $sql = preg_replace( '/[ \t]{2,}/', '', $sql );
174 $this->do_query( $wpdb, $sql );
175 }
176
177 return true;
178 }
179
180 private function create_items_sql( $prefix, $charset_collate ) {
181 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_items` (
182 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
183 `url` mediumtext NOT NULL,
184 `match_url` varchar(2000) DEFAULT NULL,
185 `match_data` text,
186 `regex` int(11) unsigned NOT NULL DEFAULT '0',
187 `position` int(11) unsigned NOT NULL DEFAULT '0',
188 `last_count` int(10) unsigned NOT NULL DEFAULT '0',
189 `last_access` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
190 `group_id` int(11) NOT NULL DEFAULT '0',
191 `status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
192 `action_type` varchar(20) NOT NULL,
193 `action_code` int(11) unsigned NOT NULL,
194 `action_data` mediumtext,
195 `match_type` varchar(20) NOT NULL,
196 `title` text,
197 PRIMARY KEY (`id`),
198 KEY `url` (`url`(191)),
199 KEY `status` (`status`),
200 KEY `regex` (`regex`),
201 KEY `group_idpos` (`group_id`,`position`),
202 KEY `group` (`group_id`),
203 KEY `match_url` (`match_url`(191))
204 ) $charset_collate";
205 }
206
207 private function create_groups_sql( $prefix, $charset_collate ) {
208 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_groups` (
209 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
210 `name` varchar(50) NOT NULL,
211 `tracking` int(11) NOT NULL DEFAULT '1',
212 `module_id` int(11) unsigned NOT NULL DEFAULT '0',
213 `status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
214 `position` int(11) unsigned NOT NULL DEFAULT '0',
215 PRIMARY KEY (`id`),
216 KEY `module_id` (`module_id`),
217 KEY `status` (`status`)
218 ) $charset_collate";
219 }
220
221 private function create_log_sql( $prefix, $charset_collate ) {
222 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_logs` (
223 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
224 `created` datetime NOT NULL,
225 `url` mediumtext NOT NULL,
226 `sent_to` mediumtext,
227 `agent` mediumtext NOT NULL,
228 `referrer` mediumtext,
229 `redirection_id` int(11) unsigned DEFAULT NULL,
230 `ip` varchar(45) DEFAULT NULL,
231 `module_id` int(11) unsigned NOT NULL,
232 `group_id` int(11) unsigned DEFAULT NULL,
233 PRIMARY KEY (`id`),
234 KEY `created` (`created`),
235 KEY `redirection_id` (`redirection_id`),
236 KEY `ip` (`ip`),
237 KEY `group_id` (`group_id`),
238 KEY `module_id` (`module_id`)
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` varchar(255) NOT NULL DEFAULT '',
247 `agent` varchar(255) DEFAULT NULL,
248 `referrer` varchar(255) DEFAULT NULL,
249 `ip` varchar(45) DEFAULT NULL,
250 PRIMARY KEY (`id`),
251 KEY `created` (`created`),
252 KEY `url` (`url`(191)),
253 KEY `referrer` (`referrer`(191)),
254 KEY `ip` (`ip`)
255 ) $charset_collate";
256 }
257 }
258