52 |
54 |
53 // If not a public blog, don't ping. |
55 // If not a public blog, don't ping. |
54 if ( ! $public ) |
56 if ( ! $public ) |
55 update_option('default_pingback_flag', 0); |
57 update_option('default_pingback_flag', 0); |
56 |
58 |
57 // Create default user. If the user already exists, the user tables are |
59 // Create default user. If the user already exists, the user tables are |
58 // being shared among blogs. Just set the role in that case. |
60 // being shared among blogs. Just set the role in that case. |
59 $user_id = username_exists($user_name); |
61 $user_id = username_exists($user_name); |
60 if ( !$user_id ) { |
62 $user_password = trim($user_password); |
61 $random_password = wp_generate_password(); |
63 $email_password = false; |
|
64 if ( !$user_id && empty($user_password) ) { |
|
65 $user_password = wp_generate_password( 12, false ); |
62 $message = __('<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.'); |
66 $message = __('<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.'); |
63 $user_id = wp_create_user($user_name, $random_password, $user_email); |
67 $user_id = wp_create_user($user_name, $user_password, $user_email); |
64 update_usermeta($user_id, 'default_password_nag', true); |
68 update_user_option($user_id, 'default_password_nag', true, true); |
|
69 $email_password = true; |
|
70 } else if ( !$user_id ) { |
|
71 // Password has been provided |
|
72 $message = '<em>'.__('Your chosen password.').'</em>'; |
|
73 $user_id = wp_create_user($user_name, $user_password, $user_email); |
65 } else { |
74 } else { |
66 $random_password = ''; |
75 $message = __('User already exists. Password inherited.'); |
67 $message = __('User already exists. Password inherited.'); |
|
68 } |
76 } |
69 |
77 |
70 $user = new WP_User($user_id); |
78 $user = new WP_User($user_id); |
71 $user->set_role('administrator'); |
79 $user->set_role('administrator'); |
72 |
80 |
73 wp_install_defaults($user_id); |
81 wp_install_defaults($user_id); |
74 |
82 |
75 $wp_rewrite->flush_rules(); |
83 flush_rewrite_rules(); |
76 |
84 |
77 wp_new_blog_notification($blog_title, $guessurl, $user_id, $random_password); |
85 wp_new_blog_notification($blog_title, $guessurl, $user_id, ($email_password ? $user_password : __('The password you chose during the install.') ) ); |
78 |
86 |
79 wp_cache_flush(); |
87 wp_cache_flush(); |
80 |
88 |
81 return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $random_password, 'password_message' => $message); |
89 return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $user_password, 'password_message' => $message); |
82 } |
90 } |
83 endif; |
91 endif; |
84 |
92 |
85 if ( !function_exists('wp_install_defaults') ) : |
93 if ( !function_exists('wp_install_defaults') ) : |
86 /** |
94 /** |
87 * {@internal Missing Short Description}} |
95 * {@internal Missing Short Description}} |
88 * |
96 * |
89 * {@internal Missing Long Description}} |
97 * {@internal Missing Long Description}} |
90 * |
98 * |
91 * @since unknown |
99 * @since 2.1.0 |
92 * |
100 * |
93 * @param int $user_id User ID. |
101 * @param int $user_id User ID. |
94 */ |
102 */ |
95 function wp_install_defaults($user_id) { |
103 function wp_install_defaults($user_id) { |
96 global $wpdb; |
104 global $wpdb, $wp_rewrite, $current_site, $table_prefix; |
97 |
105 |
98 // Default category |
106 // Default category |
99 $cat_name = __('Uncategorized'); |
107 $cat_name = __('Uncategorized'); |
100 /* translators: Default category slug */ |
108 /* translators: Default category slug */ |
101 $cat_slug = sanitize_title(_x('Uncategorized', 'Default category slug')); |
109 $cat_slug = sanitize_title(_x('Uncategorized', 'Default category slug')); |
102 |
110 |
103 $wpdb->insert( $wpdb->terms, array('name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) ); |
111 if ( global_terms_enabled() ) { |
104 $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => '1', 'taxonomy' => 'category', 'description' => '', 'parent' => 0, 'count' => 1)); |
112 $cat_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) ); |
|
113 if ( $cat_id == null ) { |
|
114 $wpdb->insert( $wpdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) ); |
|
115 $cat_id = $wpdb->insert_id; |
|
116 } |
|
117 update_option('default_category', $cat_id); |
|
118 } else { |
|
119 $cat_id = 1; |
|
120 } |
|
121 |
|
122 $wpdb->insert( $wpdb->terms, array('term_id' => $cat_id, 'name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) ); |
|
123 $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $cat_id, 'taxonomy' => 'category', 'description' => '', 'parent' => 0, 'count' => 1)); |
|
124 $cat_tt_id = $wpdb->insert_id; |
105 |
125 |
106 // Default link category |
126 // Default link category |
107 $cat_name = __('Blogroll'); |
127 $cat_name = __('Blogroll'); |
108 /* translators: Default link category slug */ |
128 /* translators: Default link category slug */ |
109 $cat_slug = sanitize_title(_x('Blogroll', 'Default link category slug')); |
129 $cat_slug = sanitize_title(_x('Blogroll', 'Default link category slug')); |
110 |
130 |
111 $wpdb->insert( $wpdb->terms, array('name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) ); |
131 if ( global_terms_enabled() ) { |
112 $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => '2', 'taxonomy' => 'link_category', 'description' => '', 'parent' => 0, 'count' => 7)); |
132 $blogroll_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) ); |
|
133 if ( $blogroll_id == null ) { |
|
134 $wpdb->insert( $wpdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) ); |
|
135 $blogroll_id = $wpdb->insert_id; |
|
136 } |
|
137 update_option('default_link_category', $blogroll_id); |
|
138 } else { |
|
139 $blogroll_id = 2; |
|
140 } |
|
141 |
|
142 $wpdb->insert( $wpdb->terms, array('term_id' => $blogroll_id, 'name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) ); |
|
143 $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $blogroll_id, 'taxonomy' => 'link_category', 'description' => '', 'parent' => 0, 'count' => 7)); |
|
144 $blogroll_tt_id = $wpdb->insert_id; |
113 |
145 |
114 // Now drop in some default links |
146 // Now drop in some default links |
115 $default_links = array(); |
147 $default_links = array(); |
116 $default_links[] = array( 'link_url' => 'http://codex.wordpress.org/', |
148 $default_links[] = array( 'link_url' => __( 'http://codex.wordpress.org/' ), |
117 'link_name' => 'Documentation', |
149 'link_name' => __( 'Documentation' ), |
118 'link_rss' => '', |
150 'link_rss' => '', |
119 'link_notes' => ''); |
151 'link_notes' => ''); |
120 |
152 |
121 $default_links[] = array( 'link_url' => 'http://wordpress.org/development/', |
153 $default_links[] = array( 'link_url' => __( 'http://wordpress.org/news/' ), |
122 'link_name' => 'Development Blog', |
154 'link_name' => __( 'WordPress Blog' ), |
123 'link_rss' => 'http://wordpress.org/development/feed/', |
155 'link_rss' => __( 'http://wordpress.org/news/feed/' ), |
124 'link_notes' => ''); |
156 'link_notes' => ''); |
125 |
157 |
126 $default_links[] = array( 'link_url' => 'http://wordpress.org/extend/ideas/', |
158 $default_links[] = array( 'link_url' => __( 'http://wordpress.org/support/' ), |
127 'link_name' => 'Suggest Ideas', |
159 'link_name' => _x( 'Support Forums', 'default link' ), |
128 'link_rss' => '', |
160 'link_rss' => '', |
129 'link_notes' =>''); |
161 'link_notes' =>''); |
130 |
162 |
131 $default_links[] = array( 'link_url' => 'http://wordpress.org/support/', |
163 $default_links[] = array( 'link_url' => 'http://wordpress.org/extend/plugins/', |
132 'link_name' => 'Support Forum', |
164 'link_name' => _x( 'Plugins', 'Default link to wordpress.org/extend/plugins/' ), |
133 'link_rss' => '', |
165 'link_rss' => '', |
134 'link_notes' =>''); |
166 'link_notes' =>''); |
135 |
167 |
136 $default_links[] = array( 'link_url' => 'http://wordpress.org/extend/plugins/', |
168 $default_links[] = array( 'link_url' => 'http://wordpress.org/extend/themes/', |
137 'link_name' => 'Plugins', |
169 'link_name' => _x( 'Themes', 'Default link to wordpress.org/extend/themes/' ), |
138 'link_rss' => '', |
170 'link_rss' => '', |
139 'link_notes' =>''); |
171 'link_notes' =>''); |
140 |
172 |
141 $default_links[] = array( 'link_url' => 'http://wordpress.org/extend/themes/', |
173 $default_links[] = array( 'link_url' => __( 'http://wordpress.org/support/forum/requests-and-feedback' ), |
142 'link_name' => 'Themes', |
174 'link_name' => __( 'Feedback' ), |
143 'link_rss' => '', |
175 'link_rss' => '', |
144 'link_notes' =>''); |
176 'link_notes' =>''); |
145 |
177 |
146 $default_links[] = array( 'link_url' => 'http://planet.wordpress.org/', |
178 $default_links[] = array( 'link_url' => __( 'http://planet.wordpress.org/' ), |
147 'link_name' => 'WordPress Planet', |
179 'link_name' => __( 'WordPress Planet' ), |
148 'link_rss' => '', |
180 'link_rss' => '', |
149 'link_notes' =>''); |
181 'link_notes' =>''); |
150 |
182 |
151 foreach ( $default_links as $link ) { |
183 foreach ( $default_links as $link ) { |
152 $wpdb->insert( $wpdb->links, $link); |
184 $wpdb->insert( $wpdb->links, $link); |
153 $wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => 2, 'object_id' => $wpdb->insert_id) ); |
185 $wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => $blogroll_tt_id, 'object_id' => $wpdb->insert_id) ); |
154 } |
186 } |
155 |
187 |
156 // First post |
188 // First post |
157 $now = date('Y-m-d H:i:s'); |
189 $now = date('Y-m-d H:i:s'); |
158 $now_gmt = gmdate('Y-m-d H:i:s'); |
190 $now_gmt = gmdate('Y-m-d H:i:s'); |
159 $first_post_guid = get_option('home') . '/?p=1'; |
191 $first_post_guid = get_option('home') . '/?p=1'; |
160 |
192 |
|
193 if ( is_multisite() ) { |
|
194 $first_post = get_site_option( 'first_post' ); |
|
195 |
|
196 if ( empty($first_post) ) |
|
197 $first_post = stripslashes( __( 'Welcome to <a href="SITE_URL">SITE_NAME</a>. This is your first post. Edit or delete it, then start blogging!' ) ); |
|
198 |
|
199 $first_post = str_replace( "SITE_URL", esc_url( network_home_url() ), $first_post ); |
|
200 $first_post = str_replace( "SITE_NAME", $current_site->site_name, $first_post ); |
|
201 } else { |
|
202 $first_post = __('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!'); |
|
203 } |
|
204 |
161 $wpdb->insert( $wpdb->posts, array( |
205 $wpdb->insert( $wpdb->posts, array( |
162 'post_author' => $user_id, |
206 'post_author' => $user_id, |
163 'post_date' => $now, |
207 'post_date' => $now, |
164 'post_date_gmt' => $now_gmt, |
208 'post_date_gmt' => $now_gmt, |
165 'post_content' => __('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!'), |
209 'post_content' => $first_post, |
166 'post_excerpt' => '', |
210 'post_excerpt' => '', |
167 'post_title' => __('Hello world!'), |
211 'post_title' => __('Hello world!'), |
168 /* translators: Default post slug */ |
212 /* translators: Default post slug */ |
169 'post_name' => _x('hello-world', 'Default post slug'), |
213 'post_name' => sanitize_title( _x('hello-world', 'Default post slug') ), |
170 'post_modified' => $now, |
214 'post_modified' => $now, |
171 'post_modified_gmt' => $now_gmt, |
215 'post_modified_gmt' => $now_gmt, |
172 'guid' => $first_post_guid, |
216 'guid' => $first_post_guid, |
173 'comment_count' => 1, |
217 'comment_count' => 1, |
174 'to_ping' => '', |
218 'to_ping' => '', |
175 'pinged' => '', |
219 'pinged' => '', |
176 'post_content_filtered' => '' |
220 'post_content_filtered' => '' |
177 )); |
221 )); |
178 $wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => 1, 'object_id' => 1) ); |
222 $wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => $cat_tt_id, 'object_id' => 1) ); |
179 |
223 |
180 // Default comment |
224 // Default comment |
|
225 $first_comment_author = __('Mr WordPress'); |
|
226 $first_comment_url = 'http://wordpress.org/'; |
|
227 $first_comment = __('Hi, this is a comment.<br />To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.'); |
|
228 if ( is_multisite() ) { |
|
229 $first_comment_author = get_site_option( 'first_comment_author', $first_comment_author ); |
|
230 $first_comment_url = get_site_option( 'first_comment_url', network_home_url() ); |
|
231 $first_comment = get_site_option( 'first_comment', $first_comment ); |
|
232 } |
181 $wpdb->insert( $wpdb->comments, array( |
233 $wpdb->insert( $wpdb->comments, array( |
182 'comment_post_ID' => 1, |
234 'comment_post_ID' => 1, |
183 'comment_author' => __('Mr WordPress'), |
235 'comment_author' => $first_comment_author, |
184 'comment_author_email' => '', |
236 'comment_author_email' => '', |
185 'comment_author_url' => 'http://wordpress.org/', |
237 'comment_author_url' => $first_comment_url, |
186 'comment_date' => $now, |
238 'comment_date' => $now, |
187 'comment_date_gmt' => $now_gmt, |
239 'comment_date_gmt' => $now_gmt, |
188 'comment_content' => __('Hi, this is a comment.<br />To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.') |
240 'comment_content' => $first_comment |
189 )); |
241 )); |
|
242 |
190 // First Page |
243 // First Page |
|
244 $first_page = sprintf( __( "This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this: |
|
245 |
|
246 <blockquote>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my blog. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin' caught in the rain.)</blockquote> |
|
247 |
|
248 ...or something like this: |
|
249 |
|
250 <blockquote>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickies to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</blockquote> |
|
251 |
|
252 As a new WordPress user, you should go to <a href=\"%s\">your dashboard</a> to delete this page and create new pages for your content. Have fun!" ), admin_url() ); |
|
253 if ( is_multisite() ) |
|
254 $first_page = get_site_option( 'first_page', $first_page ); |
191 $first_post_guid = get_option('home') . '/?page_id=2'; |
255 $first_post_guid = get_option('home') . '/?page_id=2'; |
192 $wpdb->insert( $wpdb->posts, array( |
256 $wpdb->insert( $wpdb->posts, array( |
193 'post_author' => $user_id, |
257 'post_author' => $user_id, |
194 'post_date' => $now, |
258 'post_date' => $now, |
195 'post_date_gmt' => $now_gmt, |
259 'post_date_gmt' => $now_gmt, |
196 'post_content' => __('This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many pages like this one or sub-pages as you like and manage all of your content inside of WordPress.'), |
260 'post_content' => $first_page, |
197 'post_excerpt' => '', |
261 'post_excerpt' => '', |
198 'post_title' => __('About'), |
262 'post_title' => __( 'Sample Page' ), |
199 /* translators: Default page slug */ |
263 /* translators: Default page slug */ |
200 'post_name' => _x('about', 'Default page slug'), |
264 'post_name' => __( 'sample-page' ), |
201 'post_modified' => $now, |
265 'post_modified' => $now, |
202 'post_modified_gmt' => $now_gmt, |
266 'post_modified_gmt' => $now_gmt, |
203 'guid' => $first_post_guid, |
267 'guid' => $first_post_guid, |
204 'post_type' => 'page', |
268 'post_type' => 'page', |
205 'to_ping' => '', |
269 'to_ping' => '', |
206 'pinged' => '', |
270 'pinged' => '', |
207 'post_content_filtered' => '' |
271 'post_content_filtered' => '' |
208 )); |
272 )); |
|
273 $wpdb->insert( $wpdb->postmeta, array( 'post_id' => 2, 'meta_key' => '_wp_page_template', 'meta_value' => 'default' ) ); |
|
274 |
|
275 // Set up default widgets for default theme. |
|
276 update_option( 'widget_search', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) ); |
|
277 update_option( 'widget_recent-posts', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) ); |
|
278 update_option( 'widget_recent-comments', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) ); |
|
279 update_option( 'widget_archives', array ( 2 => array ( 'title' => '', 'count' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) ); |
|
280 update_option( 'widget_categories', array ( 2 => array ( 'title' => '', 'count' => 0, 'hierarchical' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) ); |
|
281 update_option( 'widget_meta', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) ); |
|
282 update_option( 'sidebars_widgets', array ( 'wp_inactive_widgets' => array ( ), 'sidebar-1' => array ( 0 => 'search-2', 1 => 'recent-posts-2', 2 => 'recent-comments-2', 3 => 'archives-2', 4 => 'categories-2', 5 => 'meta-2', ), 'sidebar-2' => array ( ), 'sidebar-3' => array ( ), 'sidebar-4' => array ( ), 'sidebar-5' => array ( ), 'array_version' => 3 ) ); |
|
283 |
|
284 if ( ! is_multisite() ) |
|
285 update_user_meta( $user_id, 'show_welcome_panel', 1 ); |
|
286 elseif ( ! is_super_admin( $user_id ) && ! metadata_exists( 'user', $user_id, 'show_welcome_panel' ) ) |
|
287 update_user_meta( $user_id, 'show_welcome_panel', 2 ); |
|
288 |
|
289 if ( is_multisite() ) { |
|
290 // Flush rules to pick up the new page. |
|
291 $wp_rewrite->init(); |
|
292 $wp_rewrite->flush_rules(); |
|
293 |
|
294 $user = new WP_User($user_id); |
|
295 $wpdb->update( $wpdb->options, array('option_value' => $user->user_email), array('option_name' => 'admin_email') ); |
|
296 |
|
297 // Remove all perms except for the login user. |
|
298 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'user_level') ); |
|
299 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'capabilities') ); |
|
300 |
|
301 // Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.) TODO: Get previous_blog_id. |
|
302 if ( !is_super_admin( $user_id ) && $user_id != 1 ) |
|
303 $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $user_id , 'meta_key' => $wpdb->base_prefix.'1_capabilities' ) ); |
|
304 } |
209 } |
305 } |
210 endif; |
306 endif; |
211 |
307 |
212 if ( !function_exists('wp_new_blog_notification') ) : |
308 if ( !function_exists('wp_new_blog_notification') ) : |
213 /** |
309 /** |
214 * {@internal Missing Short Description}} |
310 * {@internal Missing Short Description}} |
215 * |
311 * |
216 * {@internal Missing Long Description}} |
312 * {@internal Missing Long Description}} |
217 * |
313 * |
218 * @since unknown |
314 * @since 2.1.0 |
219 * |
315 * |
220 * @param string $blog_title Blog title. |
316 * @param string $blog_title Blog title. |
221 * @param string $blog_url Blog url. |
317 * @param string $blog_url Blog url. |
222 * @param int $user_id User ID. |
318 * @param int $user_id User ID. |
223 * @param string $password User's Password. |
319 * @param string $password User's Password. |
224 */ |
320 */ |
225 function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) { |
321 function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) { |
226 $user = new WP_User($user_id); |
322 $user = new WP_User($user_id); |
227 $email = $user->user_email; |
323 $email = $user->user_email; |
228 $name = $user->user_login; |
324 $name = $user->user_login; |
229 $message = sprintf(__("Your new WordPress blog has been successfully set up at: |
325 $message = sprintf(__("Your new WordPress site has been successfully set up at: |
230 |
326 |
231 %1\$s |
327 %1\$s |
232 |
328 |
233 You can log in to the administrator account with the following information: |
329 You can log in to the administrator account with the following information: |
234 |
330 |
235 Username: %2\$s |
331 Username: %2\$s |
236 Password: %3\$s |
332 Password: %3\$s |
237 |
333 |
238 We hope you enjoy your new blog. Thanks! |
334 We hope you enjoy your new site. Thanks! |
239 |
335 |
240 --The WordPress Team |
336 --The WordPress Team |
241 http://wordpress.org/ |
337 http://wordpress.org/ |
242 "), $blog_url, $name, $password); |
338 "), $blog_url, $name, $password); |
243 |
339 |
244 @wp_mail($email, __('New WordPress Blog'), $message); |
340 @wp_mail($email, __('New WordPress Site'), $message); |
245 } |
341 } |
246 endif; |
342 endif; |
247 |
343 |
248 if ( !function_exists('wp_upgrade') ) : |
344 if ( !function_exists('wp_upgrade') ) : |
249 /** |
345 /** |
250 * Run WordPress Upgrade functions. |
346 * Run WordPress Upgrade functions. |
251 * |
347 * |
252 * {@internal Missing Long Description}} |
348 * {@internal Missing Long Description}} |
253 * |
349 * |
254 * @since unknown |
350 * @since 2.1.0 |
255 * |
351 * |
256 * @return null |
352 * @return null |
257 */ |
353 */ |
258 function wp_upgrade() { |
354 function wp_upgrade() { |
259 global $wp_current_db_version, $wp_db_version; |
355 global $wp_current_db_version, $wp_db_version, $wpdb; |
260 |
356 |
261 $wp_current_db_version = __get_option('db_version'); |
357 $wp_current_db_version = __get_option('db_version'); |
262 |
358 |
263 // We are up-to-date. Nothing to do. |
359 // We are up-to-date. Nothing to do. |
264 if ( $wp_db_version == $wp_current_db_version ) |
360 if ( $wp_db_version == $wp_current_db_version ) |
265 return; |
361 return; |
266 |
362 |
267 if( ! is_blog_installed() ) |
363 if ( ! is_blog_installed() ) |
268 return; |
364 return; |
269 |
365 |
270 wp_check_mysql_version(); |
366 wp_check_mysql_version(); |
271 wp_cache_flush(); |
367 wp_cache_flush(); |
272 pre_schema_upgrade(); |
368 pre_schema_upgrade(); |
273 make_db_current_silent(); |
369 make_db_current_silent(); |
274 upgrade_all(); |
370 upgrade_all(); |
|
371 if ( is_multisite() && is_main_site() ) |
|
372 upgrade_network(); |
275 wp_cache_flush(); |
373 wp_cache_flush(); |
|
374 |
|
375 if ( is_multisite() ) { |
|
376 if ( $wpdb->get_row( "SELECT blog_id FROM {$wpdb->blog_versions} WHERE blog_id = '{$wpdb->blogid}'" ) ) |
|
377 $wpdb->query( "UPDATE {$wpdb->blog_versions} SET db_version = '{$wp_db_version}' WHERE blog_id = '{$wpdb->blogid}'" ); |
|
378 else |
|
379 $wpdb->query( "INSERT INTO {$wpdb->blog_versions} ( `blog_id` , `db_version` , `last_updated` ) VALUES ( '{$wpdb->blogid}', '{$wp_db_version}', NOW());" ); |
|
380 } |
276 } |
381 } |
277 endif; |
382 endif; |
278 |
383 |
279 /** |
384 /** |
280 * Functions to be called in install and upgrade scripts. |
385 * Functions to be called in install and upgrade scripts. |
281 * |
386 * |
282 * {@internal Missing Long Description}} |
387 * {@internal Missing Long Description}} |
283 * |
388 * |
284 * @since unknown |
389 * @since 1.0.1 |
285 */ |
390 */ |
286 function upgrade_all() { |
391 function upgrade_all() { |
287 global $wp_current_db_version, $wp_db_version, $wp_rewrite; |
392 global $wp_current_db_version, $wp_db_version; |
288 $wp_current_db_version = __get_option('db_version'); |
393 $wp_current_db_version = __get_option('db_version'); |
289 |
394 |
290 // We are up-to-date. Nothing to do. |
395 // We are up-to-date. Nothing to do. |
291 if ( $wp_db_version == $wp_current_db_version ) |
396 if ( $wp_db_version == $wp_current_db_version ) |
292 return; |
397 return; |
293 |
398 |
294 // If the version is not set in the DB, try to guess the version. |
399 // If the version is not set in the DB, try to guess the version. |
295 if ( empty($wp_current_db_version) ) { |
400 if ( empty($wp_current_db_version) ) { |
1001 update_option( 'thread_comments', 0 ); |
1115 update_option( 'thread_comments', 0 ); |
1002 } |
1116 } |
1003 } |
1117 } |
1004 } |
1118 } |
1005 |
1119 |
|
1120 /** |
|
1121 * Execute changes made in WordPress 3.0. |
|
1122 * |
|
1123 * @since 3.0.0 |
|
1124 */ |
|
1125 function upgrade_300() { |
|
1126 global $wp_current_db_version, $wpdb; |
|
1127 |
|
1128 if ( $wp_current_db_version < 15093 ) |
|
1129 populate_roles_300(); |
|
1130 |
|
1131 if ( $wp_current_db_version < 14139 && is_multisite() && is_main_site() && ! defined( 'MULTISITE' ) && get_site_option( 'siteurl' ) === false ) |
|
1132 add_site_option( 'siteurl', '' ); |
|
1133 |
|
1134 // 3.0 screen options key name changes. |
|
1135 if ( is_main_site() && !defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) { |
|
1136 $prefix = like_escape($wpdb->base_prefix); |
|
1137 $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE '{$prefix}%meta-box-hidden%' OR meta_key LIKE '{$prefix}%closedpostboxes%' OR meta_key LIKE '{$prefix}%manage-%-columns-hidden%' OR meta_key LIKE '{$prefix}%meta-box-order%' OR meta_key LIKE '{$prefix}%metaboxorder%' OR meta_key LIKE '{$prefix}%screen_layout%' |
|
1138 OR meta_key = 'manageedittagscolumnshidden' OR meta_key='managecategoriescolumnshidden' OR meta_key = 'manageedit-tagscolumnshidden' OR meta_key = 'manageeditcolumnshidden' OR meta_key = 'categories_per_page' OR meta_key = 'edit_tags_per_page'" ); |
|
1139 } |
|
1140 |
|
1141 } |
|
1142 |
|
1143 /** |
|
1144 * Execute changes made in WordPress 3.3. |
|
1145 * |
|
1146 * @since 3.3.0 |
|
1147 */ |
|
1148 function upgrade_330() { |
|
1149 global $wp_current_db_version, $wpdb, $wp_registered_widgets, $sidebars_widgets; |
|
1150 |
|
1151 if ( $wp_current_db_version < 19061 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) { |
|
1152 $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('show_admin_bar_admin', 'plugins_last_view')" ); |
|
1153 } |
|
1154 |
|
1155 if ( $wp_current_db_version >= 11548 ) |
|
1156 return; |
|
1157 |
|
1158 $sidebars_widgets = get_option( 'sidebars_widgets', array() ); |
|
1159 $_sidebars_widgets = array(); |
|
1160 |
|
1161 if ( isset($sidebars_widgets['wp_inactive_widgets']) || empty($sidebars_widgets) ) |
|
1162 $sidebars_widgets['array_version'] = 3; |
|
1163 elseif ( !isset($sidebars_widgets['array_version']) ) |
|
1164 $sidebars_widgets['array_version'] = 1; |
|
1165 |
|
1166 switch ( $sidebars_widgets['array_version'] ) { |
|
1167 case 1 : |
|
1168 foreach ( (array) $sidebars_widgets as $index => $sidebar ) |
|
1169 if ( is_array($sidebar) ) |
|
1170 foreach ( (array) $sidebar as $i => $name ) { |
|
1171 $id = strtolower($name); |
|
1172 if ( isset($wp_registered_widgets[$id]) ) { |
|
1173 $_sidebars_widgets[$index][$i] = $id; |
|
1174 continue; |
|
1175 } |
|
1176 $id = sanitize_title($name); |
|
1177 if ( isset($wp_registered_widgets[$id]) ) { |
|
1178 $_sidebars_widgets[$index][$i] = $id; |
|
1179 continue; |
|
1180 } |
|
1181 |
|
1182 $found = false; |
|
1183 |
|
1184 foreach ( $wp_registered_widgets as $widget_id => $widget ) { |
|
1185 if ( strtolower($widget['name']) == strtolower($name) ) { |
|
1186 $_sidebars_widgets[$index][$i] = $widget['id']; |
|
1187 $found = true; |
|
1188 break; |
|
1189 } elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) { |
|
1190 $_sidebars_widgets[$index][$i] = $widget['id']; |
|
1191 $found = true; |
|
1192 break; |
|
1193 } |
|
1194 } |
|
1195 |
|
1196 if ( $found ) |
|
1197 continue; |
|
1198 |
|
1199 unset($_sidebars_widgets[$index][$i]); |
|
1200 } |
|
1201 $_sidebars_widgets['array_version'] = 2; |
|
1202 $sidebars_widgets = $_sidebars_widgets; |
|
1203 unset($_sidebars_widgets); |
|
1204 |
|
1205 case 2 : |
|
1206 $sidebars_widgets = retrieve_widgets(); |
|
1207 $sidebars_widgets['array_version'] = 3; |
|
1208 update_option( 'sidebars_widgets', $sidebars_widgets ); |
|
1209 } |
|
1210 } |
|
1211 |
|
1212 /** |
|
1213 * Execute changes made in WordPress 3.4. |
|
1214 * |
|
1215 * @since 3.4.0 |
|
1216 */ |
|
1217 function upgrade_340() { |
|
1218 global $wp_current_db_version, $wpdb; |
|
1219 |
|
1220 if ( $wp_current_db_version < 19798 ) { |
|
1221 $wpdb->hide_errors(); |
|
1222 $wpdb->query( "ALTER TABLE $wpdb->options DROP COLUMN blog_id" ); |
|
1223 $wpdb->show_errors(); |
|
1224 } |
|
1225 |
|
1226 if ( $wp_current_db_version < 19799 ) { |
|
1227 $wpdb->hide_errors(); |
|
1228 $wpdb->query("ALTER TABLE $wpdb->comments DROP INDEX comment_approved"); |
|
1229 $wpdb->show_errors(); |
|
1230 } |
|
1231 |
|
1232 if ( $wp_current_db_version < 20022 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) { |
|
1233 $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 'themes_last_view'" ); |
|
1234 } |
|
1235 |
|
1236 if ( $wp_current_db_version < 20080 ) { |
|
1237 if ( 'yes' == $wpdb->get_var( "SELECT autoload FROM $wpdb->options WHERE option_name = 'uninstall_plugins'" ) ) { |
|
1238 $uninstall_plugins = get_option( 'uninstall_plugins' ); |
|
1239 delete_option( 'uninstall_plugins' ); |
|
1240 add_option( 'uninstall_plugins', $uninstall_plugins, null, 'no' ); |
|
1241 } |
|
1242 } |
|
1243 } |
|
1244 |
|
1245 /** |
|
1246 * Execute network level changes |
|
1247 * |
|
1248 * @since 3.0.0 |
|
1249 */ |
|
1250 function upgrade_network() { |
|
1251 global $wp_current_db_version, $wpdb; |
|
1252 // 2.8 |
|
1253 if ( $wp_current_db_version < 11549 ) { |
|
1254 $wpmu_sitewide_plugins = get_site_option( 'wpmu_sitewide_plugins' ); |
|
1255 $active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' ); |
|
1256 if ( $wpmu_sitewide_plugins ) { |
|
1257 if ( !$active_sitewide_plugins ) |
|
1258 $sitewide_plugins = (array) $wpmu_sitewide_plugins; |
|
1259 else |
|
1260 $sitewide_plugins = array_merge( (array) $active_sitewide_plugins, (array) $wpmu_sitewide_plugins ); |
|
1261 |
|
1262 update_site_option( 'active_sitewide_plugins', $sitewide_plugins ); |
|
1263 } |
|
1264 delete_site_option( 'wpmu_sitewide_plugins' ); |
|
1265 delete_site_option( 'deactivated_sitewide_plugins' ); |
|
1266 |
|
1267 $start = 0; |
|
1268 while( $rows = $wpdb->get_results( "SELECT meta_key, meta_value FROM {$wpdb->sitemeta} ORDER BY meta_id LIMIT $start, 20" ) ) { |
|
1269 foreach( $rows as $row ) { |
|
1270 $value = $row->meta_value; |
|
1271 if ( !@unserialize( $value ) ) |
|
1272 $value = stripslashes( $value ); |
|
1273 if ( $value !== $row->meta_value ) { |
|
1274 update_site_option( $row->meta_key, $value ); |
|
1275 } |
|
1276 } |
|
1277 $start += 20; |
|
1278 } |
|
1279 } |
|
1280 |
|
1281 // 3.0 |
|
1282 if ( $wp_current_db_version < 13576 ) |
|
1283 update_site_option( 'global_terms_enabled', '1' ); |
|
1284 |
|
1285 // 3.3 |
|
1286 if ( $wp_current_db_version < 19390 ) |
|
1287 update_site_option( 'initial_db_version', $wp_current_db_version ); |
|
1288 |
|
1289 if ( $wp_current_db_version < 19470 ) { |
|
1290 if ( false === get_site_option( 'active_sitewide_plugins' ) ) |
|
1291 update_site_option( 'active_sitewide_plugins', array() ); |
|
1292 } |
|
1293 |
|
1294 // 3.4 |
|
1295 if ( $wp_current_db_version < 20148 ) { |
|
1296 // 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name. |
|
1297 $allowedthemes = get_site_option( 'allowedthemes' ); |
|
1298 $allowed_themes = get_site_option( 'allowed_themes' ); |
|
1299 if ( false === $allowedthemes && is_array( $allowed_themes ) && $allowed_themes ) { |
|
1300 $converted = array(); |
|
1301 $themes = wp_get_themes(); |
|
1302 foreach ( $themes as $stylesheet => $theme_data ) { |
|
1303 if ( isset( $allowed_themes[ $theme_data->get('Name') ] ) ) |
|
1304 $converted[ $stylesheet ] = true; |
|
1305 } |
|
1306 update_site_option( 'allowedthemes', $converted ); |
|
1307 delete_site_option( 'allowed_themes' ); |
|
1308 } |
|
1309 } |
|
1310 } |
1006 |
1311 |
1007 // The functions we use to actually do stuff |
1312 // The functions we use to actually do stuff |
1008 |
1313 |
1009 // General |
1314 // General |
1010 |
1315 |
1011 /** |
1316 /** |
1012 * {@internal Missing Short Description}} |
1317 * {@internal Missing Short Description}} |
1013 * |
1318 * |
1014 * {@internal Missing Long Description}} |
1319 * {@internal Missing Long Description}} |
1015 * |
1320 * |
1016 * @since unknown |
1321 * @since 1.0.0 |
1017 * |
1322 * |
1018 * @param string $table_name Database table name to create. |
1323 * @param string $table_name Database table name to create. |
1019 * @param string $create_ddl SQL statement to create table. |
1324 * @param string $create_ddl SQL statement to create table. |
1020 * @return bool If table already exists or was created by function. |
1325 * @return bool If table already exists or was created by function. |
1021 */ |
1326 */ |
1184 /** |
1484 /** |
1185 * {@internal Missing Short Description}} |
1485 * {@internal Missing Short Description}} |
1186 * |
1486 * |
1187 * {@internal Missing Long Description}} |
1487 * {@internal Missing Long Description}} |
1188 * |
1488 * |
1189 * @since unknown |
1489 * @since 1.5.0 |
1190 * |
1490 * |
1191 * @param unknown_type $queries |
1491 * @param unknown_type $queries |
1192 * @param unknown_type $execute |
1492 * @param unknown_type $execute |
1193 * @return unknown |
1493 * @return unknown |
1194 */ |
1494 */ |
1195 function dbDelta($queries, $execute = true) { |
1495 function dbDelta( $queries = '', $execute = true ) { |
1196 global $wpdb; |
1496 global $wpdb; |
1197 |
1497 |
|
1498 if ( in_array( $queries, array( '', 'all', 'blog', 'global', 'ms_global' ), true ) ) |
|
1499 $queries = wp_get_db_schema( $queries ); |
|
1500 |
1198 // Separate individual queries into an array |
1501 // Separate individual queries into an array |
1199 if( !is_array($queries) ) { |
1502 if ( !is_array($queries) ) { |
1200 $queries = explode( ';', $queries ); |
1503 $queries = explode( ';', $queries ); |
1201 if('' == $queries[count($queries) - 1]) array_pop($queries); |
1504 $queries = array_filter( $queries ); |
1202 } |
1505 } |
|
1506 $queries = apply_filters( 'dbdelta_queries', $queries ); |
1203 |
1507 |
1204 $cqueries = array(); // Creation Queries |
1508 $cqueries = array(); // Creation Queries |
1205 $iqueries = array(); // Insertion Queries |
1509 $iqueries = array(); // Insertion Queries |
1206 $for_update = array(); |
1510 $for_update = array(); |
1207 |
1511 |
1208 // Create a tablename index for an array ($cqueries) of queries |
1512 // Create a tablename index for an array ($cqueries) of queries |
1209 foreach($queries as $qry) { |
1513 foreach($queries as $qry) { |
1210 if(preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) { |
1514 if (preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) { |
1211 $cqueries[trim( strtolower($matches[1]), '`' )] = $qry; |
1515 $cqueries[ trim( $matches[1], '`' ) ] = $qry; |
1212 $for_update[$matches[1]] = 'Created table '.$matches[1]; |
1516 $for_update[$matches[1]] = 'Created table '.$matches[1]; |
1213 } |
1517 } else if (preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) { |
1214 else if(preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) { |
|
1215 array_unshift($cqueries, $qry); |
1518 array_unshift($cqueries, $qry); |
1216 } |
1519 } else if (preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) { |
1217 else if(preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) { |
|
1218 $iqueries[] = $qry; |
1520 $iqueries[] = $qry; |
1219 } |
1521 } else if (preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) { |
1220 else if(preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) { |
|
1221 $iqueries[] = $qry; |
1522 $iqueries[] = $qry; |
1222 } |
1523 } else { |
1223 else { |
|
1224 // Unrecognized query type |
1524 // Unrecognized query type |
1225 } |
1525 } |
1226 } |
1526 } |
1227 |
1527 $cqueries = apply_filters( 'dbdelta_create_queries', $cqueries ); |
1228 // Check to see which tables and fields exist |
1528 $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries ); |
1229 if($tables = $wpdb->get_col('SHOW TABLES;')) { |
1529 |
1230 // For every table in the database |
1530 $global_tables = $wpdb->tables( 'global' ); |
1231 foreach($tables as $table) { |
1531 foreach ( $cqueries as $table => $qry ) { |
1232 // If a table query exists for the database table... |
1532 // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined. |
1233 if( array_key_exists(strtolower($table), $cqueries) ) { |
1533 if ( in_array( $table, $global_tables ) && ( !is_main_site() || defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) ) |
1234 // Clear the field and index arrays |
1534 continue; |
1235 unset($cfields); |
1535 |
1236 unset($indices); |
1536 // Fetch the table column structure from the database |
1237 // Get all of the field names in the query from between the parens |
1537 $wpdb->suppress_errors(); |
1238 preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2); |
1538 $tablefields = $wpdb->get_results("DESCRIBE {$table};"); |
1239 $qryline = trim($match2[1]); |
1539 $wpdb->suppress_errors( false ); |
1240 |
1540 |
1241 // Separate field lines into an array |
1541 if ( ! $tablefields ) |
1242 $flds = explode("\n", $qryline); |
1542 continue; |
1243 |
1543 |
1244 //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>"; |
1544 // Clear the field and index arrays |
1245 |
1545 $cfields = $indices = array(); |
1246 // For every field line specified in the query |
1546 // Get all of the field names in the query from between the parens |
1247 foreach($flds as $fld) { |
1547 preg_match("|\((.*)\)|ms", $qry, $match2); |
1248 // Extract the field name |
1548 $qryline = trim($match2[1]); |
1249 preg_match("|^([^ ]*)|", trim($fld), $fvals); |
1549 |
1250 $fieldname = trim( $fvals[1], '`' ); |
1550 // Separate field lines into an array |
1251 |
1551 $flds = explode("\n", $qryline); |
1252 // Verify the found field name |
1552 |
1253 $validfield = true; |
1553 //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>"; |
1254 switch(strtolower($fieldname)) |
1554 |
1255 { |
1555 // For every field line specified in the query |
1256 case '': |
1556 foreach ($flds as $fld) { |
1257 case 'primary': |
1557 // Extract the field name |
1258 case 'index': |
1558 preg_match("|^([^ ]*)|", trim($fld), $fvals); |
1259 case 'fulltext': |
1559 $fieldname = trim( $fvals[1], '`' ); |
1260 case 'unique': |
1560 |
1261 case 'key': |
1561 // Verify the found field name |
1262 $validfield = false; |
1562 $validfield = true; |
1263 $indices[] = trim(trim($fld), ", \n"); |
1563 switch (strtolower($fieldname)) { |
1264 break; |
1564 case '': |
1265 } |
1565 case 'primary': |
1266 $fld = trim($fld); |
1566 case 'index': |
1267 |
1567 case 'fulltext': |
1268 // If it's a valid field, add it to the field array |
1568 case 'unique': |
1269 if($validfield) { |
1569 case 'key': |
1270 $cfields[strtolower($fieldname)] = trim($fld, ", \n"); |
1570 $validfield = false; |
|
1571 $indices[] = trim(trim($fld), ", \n"); |
|
1572 break; |
|
1573 } |
|
1574 $fld = trim($fld); |
|
1575 |
|
1576 // If it's a valid field, add it to the field array |
|
1577 if ($validfield) { |
|
1578 $cfields[strtolower($fieldname)] = trim($fld, ", \n"); |
|
1579 } |
|
1580 } |
|
1581 |
|
1582 // For every field in the table |
|
1583 foreach ($tablefields as $tablefield) { |
|
1584 // If the table field exists in the field array... |
|
1585 if (array_key_exists(strtolower($tablefield->Field), $cfields)) { |
|
1586 // Get the field type from the query |
|
1587 preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches); |
|
1588 $fieldtype = $matches[1]; |
|
1589 |
|
1590 // Is actual field type different from the field type in query? |
|
1591 if ($tablefield->Type != $fieldtype) { |
|
1592 // Add a query to change the column type |
|
1593 $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; |
|
1594 $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; |
|
1595 } |
|
1596 |
|
1597 // Get the default value from the array |
|
1598 //echo "{$cfields[strtolower($tablefield->Field)]}<br>"; |
|
1599 if (preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) { |
|
1600 $default_value = $matches[1]; |
|
1601 if ($tablefield->Default != $default_value) { |
|
1602 // Add a query to change the column's default value |
|
1603 $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'"; |
|
1604 $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}"; |
1271 } |
1605 } |
1272 } |
1606 } |
1273 |
1607 |
1274 // Fetch the table column structure from the database |
1608 // Remove the field from the array (so it's not added) |
1275 $tablefields = $wpdb->get_results("DESCRIBE {$table};"); |
1609 unset($cfields[strtolower($tablefield->Field)]); |
1276 |
1610 } else { |
1277 // For every field in the table |
1611 // This field exists in the table, but not in the creation queries? |
1278 foreach($tablefields as $tablefield) { |
1612 } |
1279 // If the table field exists in the field array... |
1613 } |
1280 if(array_key_exists(strtolower($tablefield->Field), $cfields)) { |
1614 |
1281 // Get the field type from the query |
1615 // For every remaining field specified for the table |
1282 preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches); |
1616 foreach ($cfields as $fieldname => $fielddef) { |
1283 $fieldtype = $matches[1]; |
1617 // Push a query line into $cqueries that adds the field to that table |
1284 |
1618 $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef"; |
1285 // Is actual field type different from the field type in query? |
1619 $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname; |
1286 if($tablefield->Type != $fieldtype) { |
1620 } |
1287 // Add a query to change the column type |
1621 |
1288 $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; |
1622 // Index stuff goes here |
1289 $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; |
1623 // Fetch the table index structure from the database |
1290 } |
1624 $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};"); |
1291 |
1625 |
1292 // Get the default value from the array |
1626 if ($tableindices) { |
1293 //echo "{$cfields[strtolower($tablefield->Field)]}<br>"; |
1627 // Clear the index array |
1294 if(preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) { |
1628 unset($index_ary); |
1295 $default_value = $matches[1]; |
1629 |
1296 if($tablefield->Default != $default_value) |
1630 // For every index in the table |
1297 { |
1631 foreach ($tableindices as $tableindex) { |
1298 // Add a query to change the column's default value |
1632 // Add the index to the index data array |
1299 $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'"; |
1633 $keyname = $tableindex->Key_name; |
1300 $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}"; |
1634 $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part); |
1301 } |
1635 $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false; |
1302 } |
1636 } |
1303 |
1637 |
1304 // Remove the field from the array (so it's not added) |
1638 // For each actual index in the index array |
1305 unset($cfields[strtolower($tablefield->Field)]); |
1639 foreach ($index_ary as $index_name => $index_data) { |
1306 } |
1640 // Build a create string to compare to the query |
1307 else { |
1641 $index_string = ''; |
1308 // This field exists in the table, but not in the creation queries? |
1642 if ($index_name == 'PRIMARY') { |
|
1643 $index_string .= 'PRIMARY '; |
|
1644 } else if($index_data['unique']) { |
|
1645 $index_string .= 'UNIQUE '; |
|
1646 } |
|
1647 $index_string .= 'KEY '; |
|
1648 if ($index_name != 'PRIMARY') { |
|
1649 $index_string .= $index_name; |
|
1650 } |
|
1651 $index_columns = ''; |
|
1652 // For each column in the index |
|
1653 foreach ($index_data['columns'] as $column_data) { |
|
1654 if ($index_columns != '') $index_columns .= ','; |
|
1655 // Add the field to the column list string |
|
1656 $index_columns .= $column_data['fieldname']; |
|
1657 if ($column_data['subpart'] != '') { |
|
1658 $index_columns .= '('.$column_data['subpart'].')'; |
1309 } |
1659 } |
1310 } |
1660 } |
1311 |
1661 // Add the column list to the index create string |
1312 // For every remaining field specified for the table |
1662 $index_string .= ' ('.$index_columns.')'; |
1313 foreach($cfields as $fieldname => $fielddef) { |
1663 if (!(($aindex = array_search($index_string, $indices)) === false)) { |
1314 // Push a query line into $cqueries that adds the field to that table |
1664 unset($indices[$aindex]); |
1315 $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef"; |
1665 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n"; |
1316 $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname; |
|
1317 } |
1666 } |
1318 |
1667 //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"; |
1319 // Index stuff goes here |
|
1320 // Fetch the table index structure from the database |
|
1321 $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};"); |
|
1322 |
|
1323 if($tableindices) { |
|
1324 // Clear the index array |
|
1325 unset($index_ary); |
|
1326 |
|
1327 // For every index in the table |
|
1328 foreach($tableindices as $tableindex) { |
|
1329 // Add the index to the index data array |
|
1330 $keyname = $tableindex->Key_name; |
|
1331 $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part); |
|
1332 $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false; |
|
1333 } |
|
1334 |
|
1335 // For each actual index in the index array |
|
1336 foreach($index_ary as $index_name => $index_data) { |
|
1337 // Build a create string to compare to the query |
|
1338 $index_string = ''; |
|
1339 if($index_name == 'PRIMARY') { |
|
1340 $index_string .= 'PRIMARY '; |
|
1341 } |
|
1342 else if($index_data['unique']) { |
|
1343 $index_string .= 'UNIQUE '; |
|
1344 } |
|
1345 $index_string .= 'KEY '; |
|
1346 if($index_name != 'PRIMARY') { |
|
1347 $index_string .= $index_name; |
|
1348 } |
|
1349 $index_columns = ''; |
|
1350 // For each column in the index |
|
1351 foreach($index_data['columns'] as $column_data) { |
|
1352 if($index_columns != '') $index_columns .= ','; |
|
1353 // Add the field to the column list string |
|
1354 $index_columns .= $column_data['fieldname']; |
|
1355 if($column_data['subpart'] != '') { |
|
1356 $index_columns .= '('.$column_data['subpart'].')'; |
|
1357 } |
|
1358 } |
|
1359 // Add the column list to the index create string |
|
1360 $index_string .= ' ('.$index_columns.')'; |
|
1361 if(!(($aindex = array_search($index_string, $indices)) === false)) { |
|
1362 unset($indices[$aindex]); |
|
1363 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n"; |
|
1364 } |
|
1365 //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"; |
|
1366 } |
|
1367 } |
|
1368 |
|
1369 // For every remaining index specified for the table |
|
1370 foreach ( (array) $indices as $index ) { |
|
1371 // Push a query line into $cqueries that adds the index to that table |
|
1372 $cqueries[] = "ALTER TABLE {$table} ADD $index"; |
|
1373 $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index; |
|
1374 } |
|
1375 |
|
1376 // Remove the original table creation query from processing |
|
1377 unset($cqueries[strtolower($table)]); |
|
1378 unset($for_update[strtolower($table)]); |
|
1379 } else { |
|
1380 // This table exists in the database, but not in the creation queries? |
|
1381 } |
1668 } |
1382 } |
1669 } |
|
1670 |
|
1671 // For every remaining index specified for the table |
|
1672 foreach ( (array) $indices as $index ) { |
|
1673 // Push a query line into $cqueries that adds the index to that table |
|
1674 $cqueries[] = "ALTER TABLE {$table} ADD $index"; |
|
1675 $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index; |
|
1676 } |
|
1677 |
|
1678 // Remove the original table creation query from processing |
|
1679 unset( $cqueries[ $table ], $for_update[ $table ] ); |
1383 } |
1680 } |
1384 |
1681 |
1385 $allqueries = array_merge($cqueries, $iqueries); |
1682 $allqueries = array_merge($cqueries, $iqueries); |
1386 if($execute) { |
1683 if ($execute) { |
1387 foreach($allqueries as $query) { |
1684 foreach ($allqueries as $query) { |
1388 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n"; |
1685 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n"; |
1389 $wpdb->query($query); |
1686 $wpdb->query($query); |
1390 } |
1687 } |
1391 } |
1688 } |
1392 |
1689 |