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

251 lines 7.0 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 `match_url` varchar(2000) DEFAULT NULL,
178 `match_data` text,
179 `regex` int(11) unsigned NOT NULL DEFAULT '0',
180 `position` int(11) unsigned NOT NULL DEFAULT '0',
181 `last_count` int(10) unsigned NOT NULL DEFAULT '0',
182 `last_access` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
183 `group_id` int(11) NOT NULL DEFAULT '0',
184 `status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
185 `action_type` varchar(20) NOT NULL,
186 `action_code` int(11) unsigned NOT NULL,
187 `action_data` mediumtext,
188 `match_type` varchar(20) NOT NULL,
189 `title` text,
190 PRIMARY KEY (`id`),
191 KEY `url` (`url`(191)),
192 KEY `status` (`status`),
193 KEY `regex` (`regex`),
194 KEY `group_idpos` (`group_id`,`position`),
195 KEY `group` (`group_id`),
196 KEY `match_url` (`match_url`(191))
197 ) $charset_collate";
198 }
199
200 private function create_groups_sql( $prefix, $charset_collate ) {
201 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_groups` (
202 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
203 `name` varchar(50) NOT NULL,
204 `tracking` int(11) NOT NULL DEFAULT '1',
205 `module_id` int(11) unsigned NOT NULL DEFAULT '0',
206 `status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
207 `position` int(11) unsigned NOT NULL DEFAULT '0',
208 PRIMARY KEY (`id`),
209 KEY `module_id` (`module_id`),
210 KEY `status` (`status`)
211 ) $charset_collate";
212 }
213
214 private function create_log_sql( $prefix, $charset_collate ) {
215 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_logs` (
216 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
217 `created` datetime NOT NULL,
218 `url` mediumtext NOT NULL,
219 `sent_to` mediumtext,
220 `agent` mediumtext NOT NULL,
221 `referrer` mediumtext,
222 `redirection_id` int(11) unsigned DEFAULT NULL,
223 `ip` varchar(45) DEFAULT NULL,
224 `module_id` int(11) unsigned NOT NULL,
225 `group_id` int(11) unsigned DEFAULT NULL,
226 PRIMARY KEY (`id`),
227 KEY `created` (`created`),
228 KEY `redirection_id` (`redirection_id`),
229 KEY `ip` (`ip`),
230 KEY `group_id` (`group_id`),
231 KEY `module_id` (`module_id`)
232 ) $charset_collate";
233 }
234
235 private function create_404_sql( $prefix, $charset_collate ) {
236 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_404` (
237 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
238 `created` datetime NOT NULL,
239 `url` varchar(255) NOT NULL DEFAULT '',
240 `agent` varchar(255) DEFAULT NULL,
241 `referrer` varchar(255) DEFAULT NULL,
242 `ip` varchar(45) DEFAULT NULL,
243 PRIMARY KEY (`id`),
244 KEY `created` (`created`),
245 KEY `url` (`url`(191)),
246 KEY `referrer` (`referrer`(191)),
247 KEY `ip` (`ip`)
248 ) $charset_collate";
249 }
250 }
251