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

252 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 ) {
131 $defaults = [
132 [
133 'name' => __( 'Redirections', 'redirection' ),
134 'module_id' => 1,
135 'position' => 0,
136 ],
137 [
138 'name' => __( 'Modified Posts', 'redirection' ),
139 'module_id' => 1,
140 'position' => 1,
141 ],
142 ];
143
144 $existing_groups = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" );
145
146 // Default groups
147 if ( intval( $existing_groups, 10 ) === 0 ) {
148 $wpdb->insert( $wpdb->prefix . 'redirection_groups', $defaults[0] );
149 $wpdb->insert( $wpdb->prefix . 'redirection_groups', $defaults[1] );
150 }
151
152 $group = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}redirection_groups LIMIT 1" );
153 if ( $group ) {
154 red_set_options( array( 'last_group_id' => $group->id ) );
155 }
156
157 return true;
158 }
159
160 /**
161 * Creates all the tables
162 */
163 public function create_tables( $wpdb ) {
164 global $wpdb;
165
166 foreach ( $this->get_all_tables() as $table => $sql ) {
167 $sql = preg_replace( '/[ \t]{2,}/', '', $sql );
168 $this->do_query( $wpdb, $sql );
169 }
170
171 return true;
172 }
173
174 private function create_items_sql( $prefix, $charset_collate ) {
175 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_items` (
176 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
177 `url` mediumtext NOT NULL,
178 `match_url` varchar(2000) DEFAULT NULL,
179 `match_data` text,
180 `regex` int(11) unsigned NOT NULL DEFAULT '0',
181 `position` int(11) unsigned NOT NULL DEFAULT '0',
182 `last_count` int(10) unsigned NOT NULL DEFAULT '0',
183 `last_access` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
184 `group_id` int(11) NOT NULL DEFAULT '0',
185 `status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
186 `action_type` varchar(20) NOT NULL,
187 `action_code` int(11) unsigned NOT NULL,
188 `action_data` mediumtext,
189 `match_type` varchar(20) NOT NULL,
190 `title` text,
191 PRIMARY KEY (`id`),
192 KEY `url` (`url`(191)),
193 KEY `status` (`status`),
194 KEY `regex` (`regex`),
195 KEY `group_idpos` (`group_id`,`position`),
196 KEY `group` (`group_id`),
197 KEY `match_url` (`match_url`(191))
198 ) $charset_collate";
199 }
200
201 private function create_groups_sql( $prefix, $charset_collate ) {
202 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_groups` (
203 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
204 `name` varchar(50) NOT NULL,
205 `tracking` int(11) NOT NULL DEFAULT '1',
206 `module_id` int(11) unsigned NOT NULL DEFAULT '0',
207 `status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
208 `position` int(11) unsigned NOT NULL DEFAULT '0',
209 PRIMARY KEY (`id`),
210 KEY `module_id` (`module_id`),
211 KEY `status` (`status`)
212 ) $charset_collate";
213 }
214
215 private function create_log_sql( $prefix, $charset_collate ) {
216 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_logs` (
217 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
218 `created` datetime NOT NULL,
219 `url` mediumtext NOT NULL,
220 `sent_to` mediumtext,
221 `agent` mediumtext NOT NULL,
222 `referrer` mediumtext,
223 `redirection_id` int(11) unsigned DEFAULT NULL,
224 `ip` varchar(45) DEFAULT NULL,
225 `module_id` int(11) unsigned NOT NULL,
226 `group_id` int(11) unsigned DEFAULT NULL,
227 PRIMARY KEY (`id`),
228 KEY `created` (`created`),
229 KEY `redirection_id` (`redirection_id`),
230 KEY `ip` (`ip`),
231 KEY `group_id` (`group_id`),
232 KEY `module_id` (`module_id`)
233 ) $charset_collate";
234 }
235
236 private function create_404_sql( $prefix, $charset_collate ) {
237 return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_404` (
238 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
239 `created` datetime NOT NULL,
240 `url` varchar(255) NOT NULL DEFAULT '',
241 `agent` varchar(255) DEFAULT NULL,
242 `referrer` varchar(255) DEFAULT NULL,
243 `ip` varchar(45) DEFAULT NULL,
244 PRIMARY KEY (`id`),
245 KEY `created` (`created`),
246 KEY `url` (`url`(191)),
247 KEY `referrer` (`referrer`(191)),
248 KEY `ip` (`ip`)
249 ) $charset_collate";
250 }
251 }
252