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

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