| | 3 | |
|---|
| | 4 | // First let's set up the tables: |
|---|
| | 5 | |
|---|
| | 6 | $wp_queries="CREATE TABLE $wpdb->categories ( |
|---|
| | 7 | cat_ID int(4) NOT NULL auto_increment, |
|---|
| | 8 | cat_name varchar(55) NOT NULL default '', |
|---|
| | 9 | category_nicename varchar(200) NOT NULL default '', |
|---|
| | 10 | category_description text NOT NULL, |
|---|
| | 11 | category_parent int(4) NOT NULL default '0', |
|---|
| | 12 | PRIMARY KEY (cat_ID), |
|---|
| | 13 | UNIQUE KEY cat_name (cat_name), |
|---|
| | 14 | KEY category_nicename (category_nicename) |
|---|
| | 15 | ); |
|---|
| | 16 | CREATE TABLE $wpdb->comments ( |
|---|
| | 17 | comment_ID int(11) unsigned NOT NULL auto_increment, |
|---|
| | 18 | comment_post_ID int(11) NOT NULL default '0', |
|---|
| | 19 | comment_author tinytext NOT NULL, |
|---|
| | 20 | comment_author_email varchar(100) NOT NULL default '', |
|---|
| | 21 | comment_author_url varchar(200) NOT NULL default '', |
|---|
| | 22 | comment_author_IP varchar(100) NOT NULL default '', |
|---|
| | 23 | comment_date datetime NOT NULL default '0000-00-00 00:00:00', |
|---|
| | 24 | comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00', |
|---|
| | 25 | comment_content text NOT NULL, |
|---|
| | 26 | comment_karma int(11) NOT NULL default '0', |
|---|
| | 27 | comment_approved enum('0','1') NOT NULL default '1', |
|---|
| | 28 | user_id int(11) NOT NULL default '0', |
|---|
| | 29 | PRIMARY KEY (comment_ID), |
|---|
| | 30 | KEY comment_approved (comment_approved), |
|---|
| | 31 | KEY comment_post_ID (comment_post_ID) |
|---|
| | 32 | ); |
|---|
| | 33 | CREATE TABLE $wpdb->linkcategories ( |
|---|
| | 34 | cat_id int(11) NOT NULL auto_increment, |
|---|
| | 35 | cat_name tinytext NOT NULL, |
|---|
| | 36 | auto_toggle enum('Y','N') NOT NULL default 'N', |
|---|
| | 37 | show_images enum('Y','N') NOT NULL default 'Y', |
|---|
| | 38 | show_description enum('Y','N') NOT NULL default 'N', |
|---|
| | 39 | show_rating enum('Y','N') NOT NULL default 'Y', |
|---|
| | 40 | show_updated enum('Y','N') NOT NULL default 'Y', |
|---|
| | 41 | sort_order varchar(64) NOT NULL default 'name', |
|---|
| | 42 | sort_desc enum('Y','N') NOT NULL default 'N', |
|---|
| | 43 | text_before_link varchar(128) NOT NULL default '<li>', |
|---|
| | 44 | text_after_link varchar(128) NOT NULL default '<br />', |
|---|
| | 45 | text_after_all varchar(128) NOT NULL default '</li>', |
|---|
| | 46 | list_limit int(11) NOT NULL default '-1', |
|---|
| | 47 | PRIMARY KEY (cat_id) |
|---|
| | 48 | ); |
|---|
| | 49 | CREATE TABLE $wpdb->links ( |
|---|
| | 50 | link_id int(11) NOT NULL auto_increment, |
|---|
| | 51 | link_url varchar(255) NOT NULL default '', |
|---|
| | 52 | link_name varchar(255) NOT NULL default '', |
|---|
| | 53 | link_image varchar(255) NOT NULL default '', |
|---|
| | 54 | link_target varchar(25) NOT NULL default '', |
|---|
| | 55 | link_category int(11) NOT NULL default '0', |
|---|
| | 56 | link_description varchar(255) NOT NULL default '', |
|---|
| | 57 | link_visible enum('Y','N') NOT NULL default 'Y', |
|---|
| | 58 | link_owner int(11) NOT NULL default '1', |
|---|
| | 59 | link_rating int(11) NOT NULL default '0', |
|---|
| | 60 | link_updated datetime NOT NULL default '0000-00-00 00:00:00', |
|---|
| | 61 | link_rel varchar(255) NOT NULL default '', |
|---|
| | 62 | link_notes mediumtext NOT NULL, |
|---|
| | 63 | link_rss varchar(255) NOT NULL default '', |
|---|
| | 64 | PRIMARY KEY (link_id), |
|---|
| | 65 | KEY link_category (link_category), |
|---|
| | 66 | KEY link_visible (link_visible) |
|---|
| | 67 | ); |
|---|
| | 68 | CREATE TABLE $wpdb->optiongroup_options ( |
|---|
| | 69 | group_id int(11) NOT NULL default '0', |
|---|
| | 70 | option_id int(11) NOT NULL default '0', |
|---|
| | 71 | seq int(11) NOT NULL default '0', |
|---|
| | 72 | PRIMARY KEY (group_id,option_id) |
|---|
| | 73 | ); |
|---|
| | 74 | CREATE TABLE $wpdb->optiongroups ( |
|---|
| | 75 | group_id int(11) NOT NULL auto_increment, |
|---|
| | 76 | group_name varchar(64) NOT NULL default '', |
|---|
| | 77 | group_desc varchar(255) default NULL, |
|---|
| | 78 | group_longdesc tinytext, |
|---|
| | 79 | PRIMARY KEY (group_id) |
|---|
| | 80 | ); |
|---|
| | 81 | CREATE TABLE $wpdb->options ( |
|---|
| | 82 | option_id int(11) NOT NULL auto_increment, |
|---|
| | 83 | blog_id int(11) NOT NULL default '0', |
|---|
| | 84 | option_name varchar(64) NOT NULL default '', |
|---|
| | 85 | option_can_override enum('Y','N') NOT NULL default 'Y', |
|---|
| | 86 | option_type int(11) NOT NULL default '1', |
|---|
| | 87 | option_value text NOT NULL, |
|---|
| | 88 | option_width int(11) NOT NULL default '20', |
|---|
| | 89 | option_height int(11) NOT NULL default '8', |
|---|
| | 90 | option_description tinytext NOT NULL, |
|---|
| | 91 | option_admin_level int(11) NOT NULL default '1', |
|---|
| | 92 | autoload enum('yes','no') NOT NULL default 'yes', |
|---|
| | 93 | PRIMARY KEY (option_id,blog_id,option_name) |
|---|
| | 94 | ); |
|---|
| | 95 | CREATE TABLE $wpdb->optiontypes ( |
|---|
| | 96 | optiontype_id int(11) NOT NULL auto_increment, |
|---|
| | 97 | optiontype_name varchar(64) NOT NULL default '', |
|---|
| | 98 | PRIMARY KEY (optiontype_id) |
|---|
| | 99 | ); |
|---|
| | 100 | CREATE TABLE $wpdb->optionvalues ( |
|---|
| | 101 | option_id int(11) NOT NULL default '0', |
|---|
| | 102 | optionvalue tinytext, |
|---|
| | 103 | optionvalue_desc varchar(255) default NULL, |
|---|
| | 104 | optionvalue_max int(11) default NULL, |
|---|
| | 105 | optionvalue_min int(11) default NULL, |
|---|
| | 106 | optionvalue_seq int(11) default NULL, |
|---|
| | 107 | UNIQUE KEY option_id (option_id,optionvalue(255)), |
|---|
| | 108 | KEY option_id_2 (option_id,optionvalue_seq) |
|---|
| | 109 | ); |
|---|
| | 110 | CREATE TABLE $wpdb->post2cat ( |
|---|
| | 111 | rel_id int(11) NOT NULL auto_increment, |
|---|
| | 112 | post_id int(11) NOT NULL default '0', |
|---|
| | 113 | category_id int(11) NOT NULL default '0', |
|---|
| | 114 | PRIMARY KEY (rel_id), |
|---|
| | 115 | KEY post_id (post_id,category_id) |
|---|
| | 116 | ); |
|---|
| | 117 | CREATE TABLE $wpdb->postmeta ( |
|---|
| | 118 | meta_id int(11) NOT NULL auto_increment, |
|---|
| | 119 | post_id int(11) NOT NULL default '0', |
|---|
| | 120 | meta_key varchar(255) default NULL, |
|---|
| | 121 | meta_value text, |
|---|
| | 122 | PRIMARY KEY (meta_id), |
|---|
| | 123 | KEY post_id (post_id), |
|---|
| | 124 | KEY meta_key (meta_key) |
|---|
| | 125 | ); |
|---|
| | 126 | CREATE TABLE $wpdb->posts ( |
|---|
| | 127 | ID int(10) unsigned NOT NULL auto_increment, |
|---|
| | 128 | post_author int(4) NOT NULL default '0', |
|---|
| | 129 | post_date datetime NOT NULL default '0000-00-00 00:00:00', |
|---|
| | 130 | post_date_gmt datetime NOT NULL default '0000-00-00 00:00:00', |
|---|
| | 131 | post_content text NOT NULL, |
|---|
| | 132 | post_title text NOT NULL, |
|---|
| | 133 | post_category int(4) NOT NULL default '0', |
|---|
| | 134 | post_excerpt text NOT NULL, |
|---|
| | 135 | post_lat float default NULL, |
|---|
| | 136 | post_lon float default NULL, |
|---|
| | 137 | post_status enum('publish','draft','private','static') NOT NULL default 'publish', |
|---|
| | 138 | comment_status enum('open','closed','registered_only') NOT NULL default 'open', |
|---|
| | 139 | ping_status enum('open','closed') NOT NULL default 'open', |
|---|
| | 140 | post_password varchar(20) NOT NULL default '', |
|---|
| | 141 | post_name varchar(200) NOT NULL default '', |
|---|
| | 142 | to_ping text NOT NULL, |
|---|
| | 143 | pinged text NOT NULL, |
|---|
| | 144 | post_modified datetime NOT NULL default '0000-00-00 00:00:00', |
|---|
| | 145 | post_modified_gmt datetime NOT NULL default '0000-00-00 00:00:00', |
|---|
| | 146 | post_content_filtered text NOT NULL, |
|---|
| | 147 | post_parent int(11) NOT NULL default '0', |
|---|
| | 148 | PRIMARY KEY (ID), |
|---|
| | 149 | KEY post_date (post_date), |
|---|
| | 150 | KEY post_date_gmt (post_date_gmt), |
|---|
| | 151 | KEY post_name (post_name), |
|---|
| | 152 | KEY post_status (post_status) |
|---|
| | 153 | ); |
|---|
| | 154 | CREATE TABLE $wpdb->users ( |
|---|
| | 155 | ID int(10) unsigned NOT NULL auto_increment, |
|---|
| | 156 | user_login varchar(20) NOT NULL default '', |
|---|
| | 157 | user_pass varchar(64) NOT NULL default '', |
|---|
| | 158 | user_firstname varchar(50) NOT NULL default '', |
|---|
| | 159 | user_lastname varchar(50) NOT NULL default '', |
|---|
| | 160 | user_nickname varchar(50) NOT NULL default '', |
|---|
| | 161 | user_nicename varchar(50) NOT NULL default '', |
|---|
| | 162 | user_icq int(10) unsigned NOT NULL default '0', |
|---|
| | 163 | user_email varchar(100) NOT NULL default '', |
|---|
| | 164 | user_url varchar(100) NOT NULL default '', |
|---|
| | 165 | user_ip varchar(15) NOT NULL default '', |
|---|
| | 166 | user_domain varchar(200) NOT NULL default '', |
|---|
| | 167 | user_browser varchar(200) NOT NULL default '', |
|---|
| | 168 | dateYMDhour datetime NOT NULL default '0000-00-00 00:00:00', |
|---|
| | 169 | user_level int(2) unsigned NOT NULL default '0', |
|---|
| | 170 | user_aim varchar(50) NOT NULL default '', |
|---|
| | 171 | user_msn varchar(100) NOT NULL default '', |
|---|
| | 172 | user_yim varchar(50) NOT NULL default '', |
|---|
| | 173 | user_idmode varchar(20) NOT NULL default '', |
|---|
| | 174 | user_activation_key varchar(60) NOT NULL default '', |
|---|
| | 175 | user_status int(11) NOT NULL default '0', |
|---|
| | 176 | PRIMARY KEY (ID), |
|---|
| | 177 | UNIQUE KEY user_login (user_login) |
|---|
| | 178 | );"; |
|---|
| | 179 | |
|---|
| | 180 | |
|---|
| 11 | | } |
|---|
| 12 | | |
|---|
| 13 | | // General |
|---|
| 14 | | function maybe_create_table($table_name, $create_ddl) { |
|---|
| 15 | | global $wpdb; |
|---|
| 16 | | foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
|---|
| 17 | | if ($table == $table_name) { |
|---|
| 18 | | return true; |
|---|
| 19 | | } |
|---|
| 20 | | } |
|---|
| 21 | | //didn't find it try to create it. |
|---|
| 22 | | $q = $wpdb->query($create_ddl); |
|---|
| 23 | | // we cannot directly tell that whether this succeeded! |
|---|
| 24 | | foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
|---|
| 25 | | if ($table == $table_name) { |
|---|
| 26 | | return true; |
|---|
| 27 | | } |
|---|
| 28 | | } |
|---|
| 29 | | return false; |
|---|
| 30 | | } |
|---|
| 31 | | |
|---|
| 32 | | function drop_index($table, $index) { |
|---|
| 33 | | global $wpdb; |
|---|
| 34 | | $wpdb->hide_errors(); |
|---|
| 35 | | $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`"); |
|---|
| 36 | | // Now we need to take out all the extra ones we may have created |
|---|
| 37 | | for ($i = 0; $i < 25; $i++) { |
|---|
| 38 | | $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`"); |
|---|
| 39 | | } |
|---|
| 40 | | $wpdb->show_errors(); |
|---|
| 41 | | return true; |
|---|
| 42 | | } |
|---|
| 43 | | |
|---|
| 44 | | function add_clean_index($table, $index) { |
|---|
| 45 | | global $wpdb; |
|---|
| 46 | | drop_index($table, $index); |
|---|
| 47 | | $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )"); |
|---|
| 48 | | return true; |
|---|
| 49 | | } |
|---|
| 50 | | |
|---|
| 51 | | /** |
|---|
| 52 | | ** maybe_add_column() |
|---|
| 53 | | ** Add column to db table if it doesn't exist. |
|---|
| 54 | | ** Returns: true if already exists or on successful completion |
|---|
| 55 | | ** false on error |
|---|
| 56 | | */ |
|---|
| 57 | | function maybe_add_column($table_name, $column_name, $create_ddl) { |
|---|
| 58 | | global $wpdb, $debug; |
|---|
| 59 | | foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) { |
|---|
| 60 | | if ($debug) echo("checking $column == $column_name<br />"); |
|---|
| 61 | | if ($column == $column_name) { |
|---|
| 62 | | return true; |
|---|
| 63 | | } |
|---|
| 64 | | } |
|---|
| 65 | | //didn't find it try to create it. |
|---|
| 66 | | $q = $wpdb->query($create_ddl); |
|---|
| 67 | | // we cannot directly tell that whether this succeeded! |
|---|
| 68 | | foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) { |
|---|
| 69 | | if ($column == $column_name) { |
|---|
| 70 | | return true; |
|---|
| 71 | | } |
|---|
| 72 | | } |
|---|
| 73 | | return false; |
|---|
| 74 | | } |
|---|
| 75 | | |
|---|
| 76 | | |
|---|
| 77 | | // get_alloptions as it was for 1.2. |
|---|
| 78 | | function get_alloptions_110() { |
|---|
| 79 | | global $wpdb; |
|---|
| 80 | | if ($options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options")) { |
|---|
| 81 | | foreach ($options as $option) { |
|---|
| 82 | | // "When trying to design a foolproof system, |
|---|
| 83 | | // never underestimate the ingenuity of the fools :)" -- Dougal |
|---|
| 84 | | if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value); |
|---|
| 85 | | if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value); |
|---|
| 86 | | if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value); |
|---|
| 87 | | $all_options->{$option->option_name} = stripslashes($option->option_value); |
|---|
| 88 | | } |
|---|
| 89 | | } |
|---|
| 90 | | return $all_options; |
|---|
| 91 | | } |
|---|
| 92 | | |
|---|
| 93 | | function deslash($content) { |
|---|
| 94 | | // Note: \\\ inside a regex denotes a single backslash. |
|---|
| 95 | | |
|---|
| 96 | | // Replace one or more backslashes followed by a single quote with |
|---|
| 97 | | // a single quote. |
|---|
| 98 | | $content = preg_replace("/\\\+'/", "'", $content); |
|---|
| 99 | | |
|---|
| 100 | | // Replace one or more backslashes followed by a double quote with |
|---|
| 101 | | // a double quote. |
|---|
| 102 | | $content = preg_replace('/\\\+"/', '"', $content); |
|---|
| 103 | | |
|---|
| 104 | | // Replace one or more backslashes with one backslash. |
|---|
| 105 | | $content = preg_replace("/\\\+/", "\\", $content); |
|---|
| 106 | | |
|---|
| 107 | | return $content; |
|---|
| 108 | | } |
|---|
| | 189 | |
|---|
| | 190 | // Options that should not exist |
|---|
| | 191 | $obs_options = array(''); |
|---|
| | 192 | } |
|---|
| | 193 | |
|---|
| | 1016 | |
|---|
| | 1017 | |
|---|
| | 1018 | } |
|---|
| | 1019 | |
|---|
| | 1020 | // The functions we use to actually do stuff |
|---|
| | 1021 | |
|---|
| | 1022 | // General |
|---|
| | 1023 | function maybe_create_table($table_name, $create_ddl) { |
|---|
| | 1024 | global $wpdb; |
|---|
| | 1025 | foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
|---|
| | 1026 | if ($table == $table_name) { |
|---|
| | 1027 | return true; |
|---|
| | 1028 | } |
|---|
| | 1029 | } |
|---|
| | 1030 | //didn't find it try to create it. |
|---|
| | 1031 | $q = $wpdb->query($create_ddl); |
|---|
| | 1032 | // we cannot directly tell that whether this succeeded! |
|---|
| | 1033 | foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
|---|
| | 1034 | if ($table == $table_name) { |
|---|
| | 1035 | return true; |
|---|
| | 1036 | } |
|---|
| | 1037 | } |
|---|
| | 1038 | return false; |
|---|
| | 1039 | } |
|---|
| | 1040 | |
|---|
| | 1041 | function drop_index($table, $index) { |
|---|
| | 1042 | global $wpdb; |
|---|
| | 1043 | $wpdb->hide_errors(); |
|---|
| | 1044 | $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`"); |
|---|
| | 1045 | // Now we need to take out all the extra ones we may have created |
|---|
| | 1046 | for ($i = 0; $i < 25; $i++) { |
|---|
| | 1047 | $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`"); |
|---|
| | 1048 | } |
|---|
| | 1049 | $wpdb->show_errors(); |
|---|
| | 1050 | return true; |
|---|
| | 1051 | } |
|---|
| | 1052 | |
|---|
| | 1053 | function add_clean_index($table, $index) { |
|---|
| | 1054 | global $wpdb; |
|---|
| | 1055 | drop_index($table, $index); |
|---|
| | 1056 | $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )"); |
|---|
| | 1057 | return true; |
|---|
| | 1058 | } |
|---|
| | 1059 | |
|---|
| | 1060 | /** |
|---|
| | 1061 | ** maybe_add_column() |
|---|
| | 1062 | ** Add column to db table if it doesn't exist. |
|---|
| | 1063 | ** Returns: true if already exists or on successful completion |
|---|
| | 1064 | ** false on error |
|---|
| | 1065 | */ |
|---|
| | 1066 | function maybe_add_column($table_name, $column_name, $create_ddl) { |
|---|
| | 1067 | global $wpdb, $debug; |
|---|
| | 1068 | foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) { |
|---|
| | 1069 | if ($debug) echo("checking $column == $column_name<br />"); |
|---|
| | 1070 | if ($column == $column_name) { |
|---|
| | 1071 | return true; |
|---|
| | 1072 | } |
|---|
| | 1073 | } |
|---|
| | 1074 | //didn't find it try to create it. |
|---|
| | 1075 | $q = $wpdb->query($create_ddl); |
|---|
| | 1076 | // we cannot directly tell that whether this succeeded! |
|---|
| | 1077 | foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) { |
|---|
| | 1078 | if ($column == $column_name) { |
|---|
| | 1079 | return true; |
|---|
| | 1080 | } |
|---|
| | 1081 | } |
|---|
| | 1082 | return false; |
|---|
| | 1083 | } |
|---|
| | 1084 | |
|---|
| | 1085 | |
|---|
| | 1086 | // get_alloptions as it was for 1.2. |
|---|
| | 1087 | function get_alloptions_110() { |
|---|
| | 1088 | global $wpdb; |
|---|
| | 1089 | if ($options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options")) { |
|---|
| | 1090 | foreach ($options as $option) { |
|---|
| | 1091 | // "When trying to design a foolproof system, |
|---|
| | 1092 | // never underestimate the ingenuity of the fools :)" -- Dougal |
|---|
| | 1093 | if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value); |
|---|
| | 1094 | if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value); |
|---|
| | 1095 | if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value); |
|---|
| | 1096 | $all_options->{$option->option_name} = stripslashes($option->option_value); |
|---|
| | 1097 | } |
|---|
| | 1098 | } |
|---|
| | 1099 | return $all_options; |
|---|
| | 1100 | } |
|---|
| | 1101 | |
|---|
| | 1102 | function deslash($content) { |
|---|
| | 1103 | // Note: \\\ inside a regex denotes a single backslash. |
|---|
| | 1104 | |
|---|
| | 1105 | // Replace one or more backslashes followed by a single quote with |
|---|
| | 1106 | // a single quote. |
|---|
| | 1107 | $content = preg_replace("/\\\+'/", "'", $content); |
|---|
| | 1108 | |
|---|
| | 1109 | // Replace one or more backslashes followed by a double quote with |
|---|
| | 1110 | // a double quote. |
|---|
| | 1111 | $content = preg_replace('/\\\+"/', '"', $content); |
|---|
| | 1112 | |
|---|
| | 1113 | // Replace one or more backslashes with one backslash. |
|---|
| | 1114 | $content = preg_replace("/\\\+/", "\\", $content); |
|---|
| | 1115 | |
|---|
| | 1116 | return $content; |
|---|
| | 1117 | } |
|---|
| | 1118 | |
|---|
| | 1119 | function dbDelta($queries, $execute = true) { |
|---|
| | 1120 | global $wpdb; |
|---|
| | 1121 | |
|---|
| | 1122 | // Seperate individual queries into an array |
|---|
| | 1123 | if( !is_array($queries) ) { |
|---|
| | 1124 | $queries = explode( ';', $queries ); |
|---|
| | 1125 | if('' == $queries[count($queries) - 1]) array_pop($queries); |
|---|
| | 1126 | } |
|---|
| | 1127 | |
|---|
| | 1128 | $cqueries = array(); // Creation Queries |
|---|
| | 1129 | $iqueries = array(); // Insertion Queries |
|---|
| | 1130 | $for_update = array(); |
|---|
| | 1131 | |
|---|
| | 1132 | // Create a tablename index for an array ($cqueries) of queries |
|---|
| | 1133 | foreach($queries as $qry) { |
|---|
| | 1134 | if(preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) { |
|---|
| | 1135 | $cqueries[strtolower($matches[1])] = $qry; |
|---|
| | 1136 | $for_update[$matches[1]] = 'Created table '.$matches[1]; |
|---|
| | 1137 | } |
|---|
| | 1138 | else if(preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) { |
|---|
| | 1139 | array_unshift($cqueries, $qry); |
|---|
| | 1140 | } |
|---|
| | 1141 | else if(preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) { |
|---|
| | 1142 | $iqueries[] = $qry; |
|---|
| | 1143 | } |
|---|
| | 1144 | else if(preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) { |
|---|
| | 1145 | $iqueries[] = $qry; |
|---|
| | 1146 | } |
|---|
| | 1147 | else { |
|---|
| | 1148 | // Unrecognized query type |
|---|
| | 1149 | } |
|---|
| | 1150 | } |
|---|
| | 1151 | |
|---|
| | 1152 | // Check to see which tables and fields exist |
|---|
| | 1153 | if($tables = $wpdb->get_col('SHOW TABLES;')) { |
|---|
| | 1154 | // For every table in the database |
|---|
| | 1155 | foreach($tables as $table) { |
|---|
| | 1156 | // If a table query exists for the database table... |
|---|
| | 1157 | if( array_key_exists(strtolower($table), $cqueries) ) { |
|---|
| | 1158 | // Clear the field and index arrays |
|---|
| | 1159 | unset($cfields); |
|---|
| | 1160 | unset($indices); |
|---|
| | 1161 | // Get all of the field names in the query from between the parens |
|---|
| | 1162 | preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2); |
|---|
| | 1163 | $qryline = trim($match2[1]); |
|---|
| | 1164 | |
|---|
| | 1165 | // Separate field lines into an array |
|---|
| | 1166 | $flds = explode("\n", $qryline); |
|---|
| | 1167 | |
|---|
| | 1168 | //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>"; |
|---|
| | 1169 | |
|---|
| | 1170 | // For every field line specified in the query |
|---|
| | 1171 | foreach($flds as $fld) { |
|---|
| | 1172 | // Extract the field name |
|---|
| | 1173 | preg_match("|^([^ ]*)|", trim($fld), $fvals); |
|---|
| | 1174 | $fieldname = $fvals[1]; |
|---|
| | 1175 | |
|---|
| | 1176 | // Verify the found field name |
|---|
| | 1177 | $validfield = true; |
|---|
| | 1178 | switch(strtolower($fieldname)) |
|---|
| | 1179 | { |
|---|
| | 1180 | case '': |
|---|
| | 1181 | case 'primary': |
|---|
| | 1182 | case 'index': |
|---|
| | 1183 | case 'fulltext': |
|---|
| | 1184 | case 'unique': |
|---|
| | 1185 | case 'key': |
|---|
| | 1186 | $validfield = false; |
|---|
| | 1187 | $indices[] = trim(trim($fld), ", \n"); |
|---|
| | 1188 | break; |
|---|
| | 1189 | } |
|---|
| | 1190 | $fld = trim($fld); |
|---|
| | 1191 | |
|---|
| | 1192 | // If it's a valid field, add it to the field array |
|---|
| | 1193 | if($validfield) { |
|---|
| | 1194 | $cfields[strtolower($fieldname)] = trim($fld, ", \n"); |
|---|
| | 1195 | } |
|---|
| | 1196 | } |
|---|
| | 1197 | |
|---|
| | 1198 | // Fetch the table column structure from the database |
|---|
| | 1199 | $tablefields = $wpdb->get_results("DESCRIBE {$table};"); |
|---|
| | 1200 | |
|---|
| | 1201 | // For every field in the table |
|---|
| | 1202 | foreach($tablefields as $tablefield) { |
|---|
| | 1203 | // If the table field exists in the field array... |
|---|
| | 1204 | if(array_key_exists(strtolower($tablefield->Field), $cfields)) { |
|---|
| | 1205 | // Get the field type from the query |
|---|
| | 1206 | preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches); |
|---|
| | 1207 | $fieldtype = $matches[1]; |
|---|
| | 1208 | |
|---|
| | 1209 | // Is actual field type different from the field type in query? |
|---|
| | 1210 | if($tablefield->Type != $fieldtype) { |
|---|
| | 1211 | // Add a query to change the column type |
|---|
| | 1212 | $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; |
|---|
| | 1213 | $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; |
|---|
| | 1214 | } |
|---|
| | 1215 | |
|---|
| | 1216 | // Get the default value from the array |
|---|
| | 1217 | //echo "{$cfields[strtolower($tablefield->Field)]}<br>"; |
|---|
| | 1218 | if(preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) { |
|---|
| | 1219 | $default_value = $matches[1]; |
|---|
| | 1220 | if($tablefield->Default != $default_value) |
|---|
| | 1221 | { |
|---|
| | 1222 | // Add a query to change the column's default value |
|---|
| | 1223 | $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'"; |
|---|
| | 1224 | $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}"; |
|---|
| | 1225 | } |
|---|
| | 1226 | } |
|---|
| | 1227 | |
|---|
| | 1228 | // Remove the field from the array (so it's not added) |
|---|
| | 1229 | unset($cfields[strtolower($tablefield->Field)]); |
|---|
| | 1230 | } |
|---|
| | 1231 | else { |
|---|
| | 1232 | // This field exists in the table, but not in the creation queries? |
|---|
| | 1233 | } |
|---|
| | 1234 | } |
|---|
| | 1235 | |
|---|
| | 1236 | // For every remaining field specified for the table |
|---|
| | 1237 | foreach($cfields as $fieldname => $fielddef) { |
|---|
| | 1238 | // Push a query line into $cqueries that adds the field to that table |
|---|
| | 1239 | $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef"; |
|---|
| | 1240 | $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname; |
|---|
| | 1241 | } |
|---|
| | 1242 | |
|---|
| | 1243 | // Index stuff goes here |
|---|
| | 1244 | // Fetch the table index structure from the database |
|---|
| | 1245 | $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};"); |
|---|
| | 1246 | |
|---|
| | 1247 | if($tableindices) { |
|---|
| | 1248 | // Clear the index array |
|---|
| | 1249 | unset($index_ary); |
|---|
| | 1250 | |
|---|
| | 1251 | // For every index in the table |
|---|
| | 1252 | foreach($tableindices as $tableindex) { |
|---|
| | 1253 | // Add the index to the index data array |
|---|
| | 1254 | $keyname = $tableindex->Key_name; |
|---|
| | 1255 | $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part); |
|---|
| | 1256 | $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false; |
|---|
| | 1257 | } |
|---|
| | 1258 | |
|---|
| | 1259 | // For each actual index in the index array |
|---|
| | 1260 | foreach($index_ary as $index_name => $index_data) { |
|---|
| | 1261 | // Build a create string to compare to the query |
|---|
| | 1262 | $index_string = ''; |
|---|
| | 1263 | if($index_name == 'PRIMARY') { |
|---|
| | 1264 | $index_string .= 'PRIMARY '; |
|---|
| | 1265 | } |
|---|
| | 1266 | else if($index_data['unique']) { |
|---|
| | 1267 | $index_string .= 'UNIQUE '; |
|---|
| | 1268 | } |
|---|
| | 1269 | $index_string .= 'KEY '; |
|---|
| | 1270 | if($index_name != 'PRIMARY') { |
|---|
| | 1271 | $index_string .= $index_name; |
|---|
| | 1272 | } |
|---|
| | 1273 | $index_columns = ''; |
|---|
| | 1274 | // For each column in the index |
|---|
| | 1275 | foreach($index_data['columns'] as $column_data) { |
|---|
| | 1276 | if($index_columns != '') $index_columns .= ','; |
|---|
| | 1277 | // Add the field to the column list string |
|---|
| | 1278 | $index_columns .= $column_data['fieldname']; |
|---|
| | 1279 | if($column_data['subpart'] != '') { |
|---|
| | 1280 | $index_columns .= '('.$column_data['subpart'].')'; |
|---|
| | 1281 | } |
|---|
| | 1282 | } |
|---|
| | 1283 | // Add the column list to the index create string |
|---|
| | 1284 | $index_string .= ' ('.$index_columns.')'; |
|---|
| | 1285 | |
|---|
| | 1286 | if(!(($aindex = array_search($index_string, $indices)) === false)) { |
|---|
| | 1287 | unset($indices[$aindex]); |
|---|
| | 1288 | //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br/>Found index:".$index_string."</pre>\n"; |
|---|
| | 1289 | } |
|---|
| | 1290 | //else echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br/><b>Did not find index:</b>".$index_string."<br/>".print_r($indices, true)."</pre>\n"; |
|---|
| | 1291 | } |
|---|
| | 1292 | } |
|---|
| | 1293 | |
|---|
| | 1294 | // For every remaining index specified for the table |
|---|
| | 1295 | foreach($indices as $index) { |
|---|
| | 1296 | // Push a query line into $cqueries that adds the index to that table |
|---|
| | 1297 | $cqueries[] = "ALTER TABLE {$table} ADD $index"; |
|---|
| | 1298 | $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index; |
|---|
| | 1299 | } |
|---|
| | 1300 | |
|---|
| | 1301 | // Remove the original table creation query from processing |
|---|
| | 1302 | unset($cqueries[strtolower($table)]); |
|---|
| | 1303 | unset($for_update[strtolower($table)]); |
|---|
| | 1304 | } else { |
|---|
| | 1305 | // This table exists in the database, but not in the creation queries? |
|---|
| | 1306 | } |
|---|
| | 1307 | } |
|---|
| | 1308 | } |
|---|
| | 1309 | |
|---|
| | 1310 | $allqueries = array_merge($cqueries, $iqueries); |
|---|
| | 1311 | if($execute) { |
|---|
| | 1312 | foreach($allqueries as $query) { |
|---|
| | 1313 | //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n"; |
|---|
| | 1314 | $wpdb->query($query); |
|---|
| | 1315 | } |
|---|
| | 1316 | } |
|---|
| | 1317 | |
|---|
| | 1318 | return $for_update; |
|---|
| | 1319 | } |
|---|
| | 1320 | |
|---|
| | 1321 | function make_db_current() { |
|---|
| | 1322 | global $wp_queries; |
|---|
| | 1323 | |
|---|
| | 1324 | $alterations = dbDelta($wp_queries); |
|---|
| | 1325 | echo "<ol>\n"; |
|---|
| | 1326 | foreach($alterations as $alteration) echo "<li>$alteration</li>\n"; |
|---|
| | 1327 | echo "</ol>\n"; |
|---|
| | 1328 | } |
|---|
| | 1329 | |
|---|
| | 1330 | function rename_field($table, $field, $new) { |
|---|
| | 1331 | // ALTER TABLE `wp_users` CHANGE `ID` `user_id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT |
|---|
| | 1332 | } |
|---|
| | 1333 | |
|---|
| | 1334 | function remove_field($table, $field) { |
|---|
| | 1335 | global $wpdb; |
|---|
| | 1336 | // ALTER TABLE `wp_users` DROP `user_domain` |
|---|