PluginProbe
Redirection / 3.7.3
Redirection v3.7.3
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 3.7.3, at database/schema/latest.php

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