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 / latest.php

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

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