Projects : mp-wp : mp-wp_svg-screenshots-and-errorreporting-r2

mp-wp/wp-admin/includes/upgrade.php

Dir - Raw

1<?php
2/**
3 * WordPress Upgrade API
4 *
5 * Most of the functions are pluggable and can be overwritten
6 *
7 * @package WordPress
8 * @subpackage Administration
9 */
10
11/** Include user install customize script. */
12if ( file_exists(WP_CONTENT_DIR . '/install.php') )
13 require (WP_CONTENT_DIR . '/install.php');
14
15/** WordPress Administration API */
16require_once(ABSPATH . 'wp-admin/includes/admin.php');
17
18/** WordPress Schema API */
19require_once(ABSPATH . 'wp-admin/includes/schema.php');
20
21if ( !function_exists('wp_install') ) :
22/**
23 * {@internal Missing Short Description}}
24 *
25 * {@internal Missing Long Description}}
26 *
27 * @since unknown
28 *
29 * @param string $blog_title Blog title.
30 * @param string $user_name User's username.
31 * @param string $user_email User's email.
32 * @param bool $public Whether blog is public.
33 * @param null $deprecated Optional. Not used.
34 * @return array Array keys 'url', 'user_id', 'password'.
35 */
36function wp_install($blog_title, $user_name, $user_email, $public, $deprecated='') {
37 global $wp_rewrite;
38
39 wp_check_mysql_version();
40 wp_cache_flush();
41 make_db_current_silent();
42 populate_options();
43 populate_roles();
44
45 update_option('blogname', $blog_title);
46 update_option('admin_email', $user_email);
47 update_option('blog_public', $public);
48
49 $guessurl = wp_guess_url();
50
51 update_option('siteurl', $guessurl);
52
53 // If not a public blog, don't ping.
54 if ( ! $public )
55 update_option('default_pingback_flag', 0);
56
57 // Create default user. If the user already exists, the user tables are
58 // being shared among blogs. Just set the role in that case.
59 $user_id = username_exists($user_name);
60 if ( !$user_id ) {
61 $random_password = wp_generate_password();
62 $user_id = wp_create_user($user_name, $random_password, $user_email);
63 } else {
64 $random_password = __('User already exists. Password inherited.');
65 }
66
67 $user = new WP_User($user_id);
68 $user->set_role('administrator');
69
70 wp_install_defaults($user_id);
71
72 $wp_rewrite->flush_rules();
73
74 wp_new_blog_notification($blog_title, $guessurl, $user_id, $random_password);
75
76 wp_cache_flush();
77
78 return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $random_password);
79}
80endif;
81
82if ( !function_exists('wp_install_defaults') ) :
83/**
84 * {@internal Missing Short Description}}
85 *
86 * {@internal Missing Long Description}}
87 *
88 * @since unknown
89 *
90 * @param int $user_id User ID.
91 */
92function wp_install_defaults($user_id) {
93 global $wpdb;
94
95 // Default category
96 $cat_name = $wpdb->escape(__('Uncategorized'));
97 $cat_slug = sanitize_title(_c('Uncategorized|Default category slug'));
98 $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$cat_name', '$cat_slug', '0')");
99 $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('1', 'category', '', '0', '1')");
100
101 // Default link category
102 $cat_name = $wpdb->escape(__('Blogroll'));
103 $cat_slug = sanitize_title(_c('Blogroll|Default link category slug'));
104 $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$cat_name', '$cat_slug', '0')");
105 $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('2', 'link_category', '', '0', '7')");
106
107 // Now drop in some default links
108 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://codex.wordpress.org/', 'Documentation', 0, '', '');");
109 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (1, 2)" );
110
111 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/development/', 'Development Blog', 0, 'http://wordpress.org/development/feed/', '');");
112 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (2, 2)" );
113
114 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/extend/ideas/', 'Suggest Ideas', 0, '', '');");
115 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (3, 2)" );
116
117 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/support/', 'Support Forum', 0, '', '');");
118 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (4, 2)" );
119
120 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/extend/plugins/', 'Plugins', 0, '', '');");
121 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (5, 2)" );
122
123 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/extend/themes/', 'Themes', 0, '', '');");
124 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (6, 2)" );
125
126 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://planet.wordpress.org/', 'WordPress Planet', 0, '', '');");
127 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (7, 2)" );
128
129 // First post
130 $now = date('Y-m-d H:i:s');
131 $now_gmt = gmdate('Y-m-d H:i:s');
132 $first_post_guid = get_option('home') . '/?p=1';
133 $wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_excerpt, post_title, post_category, post_name, post_modified, post_modified_gmt, guid, comment_count, to_ping, pinged, post_content_filtered) VALUES ($user_id, '$now', '$now_gmt', '".$wpdb->escape(__('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!'))."', '', '".$wpdb->escape(__('Hello world!'))."', '0', '".$wpdb->escape(_c('hello-world|Default post slug'))."', '$now', '$now_gmt', '$first_post_guid', '1', '', '', '')");
134 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (1, 1)" );
135
136 // Default comment
137 $wpdb->query("INSERT INTO $wpdb->comments (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_date, comment_date_gmt, comment_content) VALUES ('1', '".$wpdb->escape(__('Mr WordPress'))."', '', 'http://wordpress.org/', '$now', '$now_gmt', '".$wpdb->escape(__('Hi, this is a comment.<br />To delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.'))."')");
138
139 // First Page
140 $first_post_guid = get_option('home') . '/?page_id=2';
141 $wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_excerpt, post_title, post_category, post_name, post_modified, post_modified_gmt, guid, post_status, post_type, to_ping, pinged, post_content_filtered) VALUES ($user_id, '$now', '$now_gmt', '".$wpdb->escape(__('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.'))."', '', '".$wpdb->escape(__('About'))."', '0', '".$wpdb->escape(_c('about|Default page slug'))."', '$now', '$now_gmt','$first_post_guid', 'publish', 'page', '', '', '')");
142}
143endif;
144
145if ( !function_exists('wp_new_blog_notification') ) :
146/**
147 * {@internal Missing Short Description}}
148 *
149 * {@internal Missing Long Description}}
150 *
151 * @since unknown
152 *
153 * @param string $blog_title Blog title.
154 * @param string $blog_url Blog url.
155 * @param int $user_id User ID.
156 * @param string $password User's Password.
157 */
158function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) {
159 $user = new WP_User($user_id);
160 $email = $user->user_email;
161 $name = $user->user_login;
162 $message_headers = 'From: "' . $blog_title . '" <wordpress@' . $_SERVER['SERVER_NAME'] . '>';
163 $message = sprintf(__("Your new WordPress blog has been successfully set up at:
164
165%1\$s
166
167You can log in to the administrator account with the following information:
168
169Username: %2\$s
170Password: %3\$s
171
172We hope you enjoy your new blog. Thanks!
173
174--The WordPress Team
175http://wordpress.org/
176"), $blog_url, $name, $password);
177
178 @wp_mail($email, __('New WordPress Blog'), $message, $message_headers);
179}
180endif;
181
182if ( !function_exists('wp_upgrade') ) :
183/**
184 * Run WordPress Upgrade functions.
185 *
186 * {@internal Missing Long Description}}
187 *
188 * @since unknown
189 *
190 * @return null
191 */
192function wp_upgrade() {
193 global $wp_current_db_version, $wp_db_version;
194
195 $wp_current_db_version = __get_option('db_version');
196
197 // We are up-to-date. Nothing to do.
198 if ( $wp_db_version == $wp_current_db_version )
199 return;
200
201 if( ! is_blog_installed() )
202 return;
203
204 wp_check_mysql_version();
205 wp_cache_flush();
206 make_db_current_silent();
207 upgrade_all();
208 wp_cache_flush();
209}
210endif;
211
212/**
213 * Functions to be called in install and upgrade scripts.
214 *
215 * {@internal Missing Long Description}}
216 *
217 * @since unknown
218 */
219function upgrade_all() {
220 global $wp_current_db_version, $wp_db_version, $wp_rewrite;
221 $wp_current_db_version = __get_option('db_version');
222
223 // We are up-to-date. Nothing to do.
224 if ( $wp_db_version == $wp_current_db_version )
225 return;
226
227 // If the version is not set in the DB, try to guess the version.
228 if ( empty($wp_current_db_version) ) {
229 $wp_current_db_version = 0;
230
231 // If the template option exists, we have 1.5.
232 $template = __get_option('template');
233 if ( !empty($template) )
234 $wp_current_db_version = 2541;
235 }
236
237 if ( $wp_current_db_version < 6039 )
238 upgrade_230_options_table();
239
240 populate_options();
241
242 if ( $wp_current_db_version < 2541 ) {
243 upgrade_100();
244 upgrade_101();
245 upgrade_110();
246 upgrade_130();
247 }
248
249 if ( $wp_current_db_version < 3308 )
250 upgrade_160();
251
252 if ( $wp_current_db_version < 4772 )
253 upgrade_210();
254
255 if ( $wp_current_db_version < 4351 )
256 upgrade_old_slugs();
257
258 if ( $wp_current_db_version < 5539 )
259 upgrade_230();
260
261 if ( $wp_current_db_version < 6124 )
262 upgrade_230_old_tables();
263
264 if ( $wp_current_db_version < 7499 )
265 upgrade_250();
266
267 if ( $wp_current_db_version < 7796 )
268 upgrade_251();
269
270 if ( $wp_current_db_version < 7935 )
271 upgrade_252();
272
273 if ( $wp_current_db_version < 8201 )
274 upgrade_260();
275
276 if ( $wp_current_db_version < 8989 )
277 upgrade_270();
278
279 maybe_disable_automattic_widgets();
280
281 $wp_rewrite->flush_rules();
282
283 update_option('db_version', $wp_db_version);
284}
285
286/**
287 * Execute changes made in WordPress 1.0.
288 *
289 * @since 1.0.0
290 */
291function upgrade_100() {
292 global $wpdb;
293
294 // Get the title and ID of every post, post_name to check if it already has a value
295 $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
296 if ($posts) {
297 foreach($posts as $post) {
298 if ('' == $post->post_name) {
299 $newtitle = sanitize_title($post->post_title);
300 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) );
301 }
302 }
303 }
304
305 $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories");
306 foreach ($categories as $category) {
307 if ('' == $category->category_nicename) {
308 $newtitle = sanitize_title($category->cat_name);
309 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->categories SET category_nicename = %s WHERE cat_ID = %d", $newtitle, $category->cat_ID) );
310 }
311 }
312
313 $wpdb->query("UPDATE $wpdb->options SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/')
314 WHERE option_name LIKE 'links_rating_image%'
315 AND option_value LIKE 'wp-links/links-images/%'");
316
317 $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat");
318 if ($done_ids) :
319 foreach ($done_ids as $done_id) :
320 $done_posts[] = $done_id->post_id;
321 endforeach;
322 $catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')';
323 else:
324 $catwhere = '';
325 endif;
326
327 $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere");
328 if ($allposts) :
329 foreach ($allposts as $post) {
330 // Check to see if it's already been imported
331 $cat = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->post2cat WHERE post_id = %d AND category_id = %d", $post->ID, $post->post_category) );
332 if (!$cat && 0 != $post->post_category) { // If there's no result
333 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->post2cat
334 (post_id, category_id)
335 VALUES (%s, %s)
336 ", $post->ID, $post->post_category) );
337 }
338 }
339 endif;
340}
341
342/**
343 * Execute changes made in WordPress 1.0.1.
344 *
345 * @since 1.0.1
346 */
347function upgrade_101() {
348 global $wpdb;
349
350 // Clean up indices, add a few
351 add_clean_index($wpdb->posts, 'post_name');
352 add_clean_index($wpdb->posts, 'post_status');
353 add_clean_index($wpdb->categories, 'category_nicename');
354 add_clean_index($wpdb->comments, 'comment_approved');
355 add_clean_index($wpdb->comments, 'comment_post_ID');
356 add_clean_index($wpdb->links , 'link_category');
357 add_clean_index($wpdb->links , 'link_visible');
358}
359
360/**
361 * Execute changes made in WordPress 1.2.
362 *
363 * @since 1.2.0
364 */
365function upgrade_110() {
366 global $wpdb;
367
368 // Set user_nicename.
369 $users = $wpdb->get_results("SELECT ID, user_nickname, user_nicename FROM $wpdb->users");
370 foreach ($users as $user) {
371 if ('' == $user->user_nicename) {
372 $newname = sanitize_title($user->user_nickname);
373 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->users SET user_nicename = %s WHERE ID = %d", $newname, $user->ID) );
374 }
375 }
376
377 $users = $wpdb->get_results("SELECT ID, user_pass from $wpdb->users");
378 foreach ($users as $row) {
379 if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) {
380 $wpdb->query('UPDATE '.$wpdb->users.' SET user_pass = MD5(\''.$row->user_pass.'\') WHERE ID = \''.$row->ID.'\'');
381 }
382 }
383
384 // Get the GMT offset, we'll use that later on
385 $all_options = get_alloptions_110();
386
387 $time_difference = $all_options->time_difference;
388
389 $server_time = time()+date('Z');
390 $weblogger_time = $server_time + $time_difference*3600;
391 $gmt_time = time();
392
393 $diff_gmt_server = ($gmt_time - $server_time) / 3600;
394 $diff_weblogger_server = ($weblogger_time - $server_time) / 3600;
395 $diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server;
396 $gmt_offset = -$diff_gmt_weblogger;
397
398 // Add a gmt_offset option, with value $gmt_offset
399 add_option('gmt_offset', $gmt_offset);
400
401 // Check if we already set the GMT fields (if we did, then
402 // MAX(post_date_gmt) can't be '0000-00-00 00:00:00'
403 // <michel_v> I just slapped myself silly for not thinking about it earlier
404 $got_gmt_fields = ($wpdb->get_var("SELECT MAX(post_date_gmt) FROM $wpdb->posts") == '0000-00-00 00:00:00') ? false : true;
405
406 if (!$got_gmt_fields) {
407
408 // Add or substract time to all dates, to get GMT dates
409 $add_hours = intval($diff_gmt_weblogger);
410 $add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours));
411 $wpdb->query("UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
412 $wpdb->query("UPDATE $wpdb->posts SET post_modified = post_date");
413 $wpdb->query("UPDATE $wpdb->posts SET post_modified_gmt = DATE_ADD(post_modified, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE) WHERE post_modified != '0000-00-00 00:00:00'");
414 $wpdb->query("UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
415 $wpdb->query("UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
416 }
417
418}
419
420/**
421 * Execute changes made in WordPress 1.5.
422 *
423 * @since 1.5.0
424 */
425function upgrade_130() {
426 global $wpdb;
427
428 // Remove extraneous backslashes.
429 $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
430 if ($posts) {
431 foreach($posts as $post) {
432 $post_content = addslashes(deslash($post->post_content));
433 $post_title = addslashes(deslash($post->post_title));
434 $post_excerpt = addslashes(deslash($post->post_excerpt));
435 if ( empty($post->guid) )
436 $guid = get_permalink($post->ID);
437 else
438 $guid = $post->guid;
439
440 $wpdb->query("UPDATE $wpdb->posts SET post_title = '$post_title', post_content = '$post_content', post_excerpt = '$post_excerpt', guid = '$guid' WHERE ID = '$post->ID'");
441 }
442 }
443
444 // Remove extraneous backslashes.
445 $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
446 if ($comments) {
447 foreach($comments as $comment) {
448 $comment_content = addslashes(deslash($comment->comment_content));
449 $comment_author = addslashes(deslash($comment->comment_author));
450 $wpdb->query("UPDATE $wpdb->comments SET comment_content = '$comment_content', comment_author = '$comment_author' WHERE comment_ID = '$comment->comment_ID'");
451 }
452 }
453
454 // Remove extraneous backslashes.
455 $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
456 if ($links) {
457 foreach($links as $link) {
458 $link_name = addslashes(deslash($link->link_name));
459 $link_description = addslashes(deslash($link->link_description));
460 $wpdb->query("UPDATE $wpdb->links SET link_name = '$link_name', link_description = '$link_description' WHERE link_id = '$link->link_id'");
461 }
462 }
463
464 // The "paged" option for what_to_show is no more.
465 if ($wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'what_to_show'") == 'paged') {
466 $wpdb->query("UPDATE $wpdb->options SET option_value = 'posts' WHERE option_name = 'what_to_show'");
467 }
468
469 $active_plugins = __get_option('active_plugins');
470
471 // If plugins are not stored in an array, they're stored in the old
472 // newline separated format. Convert to new format.
473 if ( !is_array( $active_plugins ) ) {
474 $active_plugins = explode("\n", trim($active_plugins));
475 update_option('active_plugins', $active_plugins);
476 }
477
478 // Obsolete tables
479 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues');
480 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes');
481 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups');
482 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options');
483
484 // Update comments table to use comment_type
485 $wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'");
486 $wpdb->query("UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'");
487
488 // Some versions have multiple duplicate option_name rows with the same values
489 $options = $wpdb->get_results("SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name");
490 foreach ( $options as $option ) {
491 if ( 1 != $option->dupes ) { // Could this be done in the query?
492 $limit = $option->dupes - 1;
493 $dupe_ids = $wpdb->get_col( $wpdb->prepare("SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit) );
494 $dupe_ids = join($dupe_ids, ',');
495 $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)");
496 }
497 }
498
499 make_site_theme();
500}
501
502/**
503 * Execute changes made in WordPress 2.0.
504 *
505 * @since 2.0.0
506 */
507function upgrade_160() {
508 global $wpdb, $wp_current_db_version;
509
510 populate_roles_160();
511
512 $users = $wpdb->get_results("SELECT * FROM $wpdb->users");
513 foreach ( $users as $user ) :
514 if ( !empty( $user->user_firstname ) )
515 update_usermeta( $user->ID, 'first_name', $wpdb->escape($user->user_firstname) );
516 if ( !empty( $user->user_lastname ) )
517 update_usermeta( $user->ID, 'last_name', $wpdb->escape($user->user_lastname) );
518 if ( !empty( $user->user_nickname ) )
519 update_usermeta( $user->ID, 'nickname', $wpdb->escape($user->user_nickname) );
520 if ( !empty( $user->user_level ) )
521 update_usermeta( $user->ID, $wpdb->prefix . 'user_level', $user->user_level );
522 if ( !empty( $user->user_icq ) )
523 update_usermeta( $user->ID, 'icq', $wpdb->escape($user->user_icq) );
524 if ( !empty( $user->user_aim ) )
525 update_usermeta( $user->ID, 'aim', $wpdb->escape($user->user_aim) );
526 if ( !empty( $user->user_msn ) )
527 update_usermeta( $user->ID, 'msn', $wpdb->escape($user->user_msn) );
528 if ( !empty( $user->user_yim ) )
529 update_usermeta( $user->ID, 'yim', $wpdb->escape($user->user_icq) );
530 if ( !empty( $user->user_description ) )
531 update_usermeta( $user->ID, 'description', $wpdb->escape($user->user_description) );
532
533 if ( isset( $user->user_idmode ) ):
534 $idmode = $user->user_idmode;
535 if ($idmode == 'nickname') $id = $user->user_nickname;
536 if ($idmode == 'login') $id = $user->user_login;
537 if ($idmode == 'firstname') $id = $user->user_firstname;
538 if ($idmode == 'lastname') $id = $user->user_lastname;
539 if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname;
540 if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname;
541 if (!$idmode) $id = $user->user_nickname;
542 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->users SET display_name = %s WHERE ID = %d", $id, $user->ID) );
543 endif;
544
545 // FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set.
546 $caps = get_usermeta( $user->ID, $wpdb->prefix . 'capabilities');
547 if ( empty($caps) || defined('RESET_CAPS') ) {
548 $level = get_usermeta($user->ID, $wpdb->prefix . 'user_level');
549 $role = translate_level_to_role($level);
550 update_usermeta( $user->ID, $wpdb->prefix . 'capabilities', array($role => true) );
551 }
552
553 endforeach;
554 $old_user_fields = array( 'user_firstname', 'user_lastname', 'user_icq', 'user_aim', 'user_msn', 'user_yim', 'user_idmode', 'user_ip', 'user_domain', 'user_browser', 'user_description', 'user_nickname', 'user_level' );
555 $wpdb->hide_errors();
556 foreach ( $old_user_fields as $old )
557 $wpdb->query("ALTER TABLE $wpdb->users DROP $old");
558 $wpdb->show_errors();
559
560 // populate comment_count field of posts table
561 $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" );
562 if( is_array( $comments ) ) {
563 foreach ($comments as $comment) {
564 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET comment_count = %d WHERE ID = %d", $comment->c, $comment->comment_post_ID) );
565 }
566 }
567
568 // Some alpha versions used a post status of object instead of attachment and put
569 // the mime type in post_type instead of post_mime_type.
570 if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) {
571 $objects = $wpdb->get_results("SELECT ID, post_type FROM $wpdb->posts WHERE post_status = 'object'");
572 foreach ($objects as $object) {
573 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'attachment',
574 post_mime_type = %s,
575 post_type = ''
576 WHERE ID = %d", $object->post_type, $object->ID) );
577
578 $meta = get_post_meta($object->ID, 'imagedata', true);
579 if ( ! empty($meta['file']) )
580 update_attached_file( $object->ID, $meta['file'] );
581 }
582 }
583}
584
585/**
586 * Execute changes made in WordPress 2.1.
587 *
588 * @since 2.1.0
589 */
590function upgrade_210() {
591 global $wpdb, $wp_current_db_version;
592
593 if ( $wp_current_db_version < 3506 ) {
594 // Update status and type.
595 $posts = $wpdb->get_results("SELECT ID, post_status FROM $wpdb->posts");
596
597 if ( ! empty($posts) ) foreach ($posts as $post) {
598 $status = $post->post_status;
599 $type = 'post';
600
601 if ( 'static' == $status ) {
602 $status = 'publish';
603 $type = 'page';
604 } else if ( 'attachment' == $status ) {
605 $status = 'inherit';
606 $type = 'attachment';
607 }
608
609 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID) );
610 }
611 }
612
613 if ( $wp_current_db_version < 3845 ) {
614 populate_roles_210();
615 }
616
617 if ( $wp_current_db_version < 3531 ) {
618 // Give future posts a post_status of future.
619 $now = gmdate('Y-m-d H:i:59');
620 $wpdb->query ("UPDATE $wpdb->posts SET post_status = 'future' WHERE post_status = 'publish' AND post_date_gmt > '$now'");
621
622 $posts = $wpdb->get_results("SELECT ID, post_date FROM $wpdb->posts WHERE post_status ='future'");
623 if ( !empty($posts) )
624 foreach ( $posts as $post )
625 wp_schedule_single_event(mysql2date('U', $post->post_date), 'publish_future_post', array($post->ID));
626 }
627}
628
629/**
630 * Execute changes made in WordPress 2.3.
631 *
632 * @since 2.3.0
633 */
634function upgrade_230() {
635 global $wp_current_db_version, $wpdb;
636
637 if ( $wp_current_db_version < 5200 ) {
638 populate_roles_230();
639 }
640
641 // Convert categories to terms.
642 $tt_ids = array();
643 $have_tags = false;
644 $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_ID");
645 foreach ($categories as $category) {
646 $term_id = (int) $category->cat_ID;
647 $name = $category->cat_name;
648 $description = $category->category_description;
649 $slug = $category->category_nicename;
650 $parent = $category->category_parent;
651 $term_group = 0;
652
653 // Associate terms with the same slug in a term group and make slugs unique.
654 if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
655 $term_group = $exists[0]->term_group;
656 $id = $exists[0]->term_id;
657 $num = 2;
658 do {
659 $alt_slug = $slug . "-$num";
660 $num++;
661 $slug_check = $wpdb->get_var( $wpdb->prepare("SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug) );
662 } while ( $slug_check );
663
664 $slug = $alt_slug;
665
666 if ( empty( $term_group ) ) {
667 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1;
668 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $id) );
669 }
670 }
671
672 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES
673 (%d, %s, %s, %d)", $term_id, $name, $slug, $term_group) );
674
675 $count = 0;
676 if ( !empty($category->category_count) ) {
677 $count = (int) $category->category_count;
678 $taxonomy = 'category';
679 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
680 $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
681 }
682
683 if ( !empty($category->link_count) ) {
684 $count = (int) $category->link_count;
685 $taxonomy = 'link_category';
686 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
687 $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
688 }
689
690 if ( !empty($category->tag_count) ) {
691 $have_tags = true;
692 $count = (int) $category->tag_count;
693 $taxonomy = 'post_tag';
694 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
695 $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
696 }
697
698 if ( empty($count) ) {
699 $count = 0;
700 $taxonomy = 'category';
701 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
702 $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
703 }
704 }
705
706 $select = 'post_id, category_id';
707 if ( $have_tags )
708 $select .= ', rel_type';
709
710 $posts = $wpdb->get_results("SELECT $select FROM $wpdb->post2cat GROUP BY post_id, category_id");
711 foreach ( $posts as $post ) {
712 $post_id = (int) $post->post_id;
713 $term_id = (int) $post->category_id;
714 $taxonomy = 'category';
715 if ( !empty($post->rel_type) && 'tag' == $post->rel_type)
716 $taxonomy = 'tag';
717 $tt_id = $tt_ids[$term_id][$taxonomy];
718 if ( empty($tt_id) )
719 continue;
720
721 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $post_id, $tt_id) );
722 }
723
724 // < 3570 we used linkcategories. >= 3570 we used categories and link2cat.
725 if ( $wp_current_db_version < 3570 ) {
726 // Create link_category terms for link categories. Create a map of link cat IDs
727 // to link_category terms.
728 $link_cat_id_map = array();
729 $default_link_cat = 0;
730 $tt_ids = array();
731 $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->prefix . 'linkcategories');
732 foreach ( $link_cats as $category) {
733 $cat_id = (int) $category->cat_id;
734 $term_id = 0;
735 $name = $wpdb->escape($category->cat_name);
736 $slug = sanitize_title($name);
737 $term_group = 0;
738
739 // Associate terms with the same slug in a term group and make slugs unique.
740 if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
741 $term_group = $exists[0]->term_group;
742 $term_id = $exists[0]->term_id;
743 }
744
745 if ( empty($term_id) ) {
746 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES (%s, %s, %d)", $name, $slug, $term_group) );
747 $term_id = (int) $wpdb->insert_id;
748 }
749
750 $link_cat_id_map[$cat_id] = $term_id;
751 $default_link_cat = $term_id;
752
753 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES (%d, 'link_category', '', '0', '0')", $term_id) );
754 $tt_ids[$term_id] = (int) $wpdb->insert_id;
755 }
756
757 // Associate links to cats.
758 $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links");
759 if ( !empty($links) ) foreach ( $links as $link ) {
760 if ( 0 == $link->link_category )
761 continue;
762 if ( ! isset($link_cat_id_map[$link->link_category]) )
763 continue;
764 $term_id = $link_cat_id_map[$link->link_category];
765 $tt_id = $tt_ids[$term_id];
766 if ( empty($tt_id) )
767 continue;
768
769 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $link->link_id, $tt_id) );
770 }
771
772 // Set default to the last category we grabbed during the upgrade loop.
773 update_option('default_link_category', $default_link_cat);
774 } else {
775 $links = $wpdb->get_results("SELECT link_id, category_id FROM $wpdb->link2cat GROUP BY link_id, category_id");
776 foreach ( $links as $link ) {
777 $link_id = (int) $link->link_id;
778 $term_id = (int) $link->category_id;
779 $taxonomy = 'link_category';
780 $tt_id = $tt_ids[$term_id][$taxonomy];
781 if ( empty($tt_id) )
782 continue;
783
784 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $link_id, $tt_id) );
785 }
786 }
787
788 if ( $wp_current_db_version < 4772 ) {
789 // Obsolete linkcategories table
790 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories');
791 }
792
793 // Recalculate all counts
794 $terms = $wpdb->get_results("SELECT term_taxonomy_id, taxonomy FROM $wpdb->term_taxonomy");
795 foreach ( (array) $terms as $term ) {
796 if ( ('post_tag' == $term->taxonomy) || ('category' == $term->taxonomy) )
797 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = %d", $term->term_taxonomy_id) );
798 else
799 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term->term_taxonomy_id) );
800 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_taxonomy_id = %d", $count, $term->term_taxonomy_id) );
801 }
802}
803
804/**
805 * Remove old options from the database.
806 *
807 * @since 2.3.0
808 */
809function upgrade_230_options_table() {
810 global $wpdb;
811 $old_options_fields = array( 'option_can_override', 'option_type', 'option_width', 'option_height', 'option_description', 'option_admin_level' );
812 $wpdb->hide_errors();
813 foreach ( $old_options_fields as $old )
814 $wpdb->query("ALTER TABLE $wpdb->options DROP $old");
815 $wpdb->show_errors();
816}
817
818/**
819 * Remove old categories, link2cat, and post2cat database tables.
820 *
821 * @since 2.3.0
822 */
823function upgrade_230_old_tables() {
824 global $wpdb;
825 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'categories');
826 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'link2cat');
827 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'post2cat');
828}
829
830/**
831 * Upgrade old slugs made in version 2.2.
832 *
833 * @since 2.2.0
834 */
835function upgrade_old_slugs() {
836 // upgrade people who were using the Redirect Old Slugs plugin
837 global $wpdb;
838 $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'");
839}
840
841/**
842 * Execute changes made in WordPress 2.5.0.
843 *
844 * @since 2.5.0
845 */
846function upgrade_250() {
847 global $wp_current_db_version;
848
849 if ( $wp_current_db_version < 6689 ) {
850 populate_roles_250();
851 }
852
853}
854
855/**
856 * Execute changes made in WordPress 2.5.1.
857 *
858 * @since 2.5.1
859 */
860function upgrade_251() {
861 global $wp_current_db_version;
862
863 // Make the secret longer
864 update_option('secret', wp_generate_password(64));
865}
866
867/**
868 * Execute changes made in WordPress 2.5.2.
869 *
870 * @since 2.5.2
871 */
872function upgrade_252() {
873 global $wpdb;
874
875 $wpdb->query("UPDATE $wpdb->users SET user_activation_key = ''");
876}
877
878/**
879 * Execute changes made in WordPress 2.6.
880 *
881 * @since 2.6.0
882 */
883function upgrade_260() {
884 global $wp_current_db_version;
885
886 if ( $wp_current_db_version < 8000 )
887 populate_roles_260();
888
889 if ( $wp_current_db_version < 8201 ) {
890 update_option('enable_app', 1);
891 update_option('enable_xmlrpc', 1);
892 }
893}
894
895/**
896 * Execute changes made in WordPress 2.7.
897 *
898 * @since 2.7.0
899 */
900function upgrade_270() {
901 global $wpdb, $wp_current_db_version;
902
903 if ( $wp_current_db_version < 8980 )
904 populate_roles_270();
905
906 // Update post_date for unpublished posts with empty timestamp
907 if ( $wp_current_db_version < 8921 )
908 $wpdb->query( "UPDATE $wpdb->posts SET post_date = post_modified WHERE post_date = '0000-00-00 00:00:00'" );
909}
910
911
912// The functions we use to actually do stuff
913
914// General
915
916/**
917 * {@internal Missing Short Description}}
918 *
919 * {@internal Missing Long Description}}
920 *
921 * @since unknown
922 *
923 * @param string $table_name Database table name to create.
924 * @param string $create_ddl SQL statement to create table.
925 * @return bool If table already exists or was created by function.
926 */
927function maybe_create_table($table_name, $create_ddl) {
928 global $wpdb;
929 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
930 if ($table == $table_name) {
931 return true;
932 }
933 }
934 //didn't find it try to create it.
935 $q = $wpdb->query($create_ddl);
936 // we cannot directly tell that whether this succeeded!
937 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
938 if ($table == $table_name) {
939 return true;
940 }
941 }
942 return false;
943}
944
945/**
946 * {@internal Missing Short Description}}
947 *
948 * {@internal Missing Long Description}}
949 *
950 * @since unknown
951 *
952 * @param string $table Database table name.
953 * @param string $index Index name to drop.
954 * @return bool True, when finished.
955 */
956function drop_index($table, $index) {
957 global $wpdb;
958 $wpdb->hide_errors();
959 $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`");
960 // Now we need to take out all the extra ones we may have created
961 for ($i = 0; $i < 25; $i++) {
962 $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`");
963 }
964 $wpdb->show_errors();
965 return true;
966}
967
968/**
969 * {@internal Missing Short Description}}
970 *
971 * {@internal Missing Long Description}}
972 *
973 * @since unknown
974 *
975 * @param string $table Database table name.
976 * @param string $index Database table index column.
977 * @return bool True, when done with execution.
978 */
979function add_clean_index($table, $index) {
980 global $wpdb;
981 drop_index($table, $index);
982 $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )");
983 return true;
984}
985
986/**
987 ** maybe_add_column()
988 ** Add column to db table if it doesn't exist.
989 ** Returns: true if already exists or on successful completion
990 ** false on error
991 */
992function maybe_add_column($table_name, $column_name, $create_ddl) {
993 global $wpdb, $debug;
994 foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
995 if ($debug) echo("checking $column == $column_name<br />");
996 if ($column == $column_name) {
997 return true;
998 }
999 }
1000 //didn't find it try to create it.
1001 $q = $wpdb->query($create_ddl);
1002 // we cannot directly tell that whether this succeeded!
1003 foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
1004 if ($column == $column_name) {
1005 return true;
1006 }
1007 }
1008 return false;
1009}
1010
1011/**
1012 * Retrieve all options as it was for 1.2.
1013 *
1014 * @since 1.2.0
1015 *
1016 * @return array List of options.
1017 */
1018function get_alloptions_110() {
1019 global $wpdb;
1020 if ($options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options")) {
1021 foreach ($options as $option) {
1022 // "When trying to design a foolproof system,
1023 // never underestimate the ingenuity of the fools :)" -- Dougal
1024 if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
1025 if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
1026 if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
1027 $all_options->{$option->option_name} = stripslashes($option->option_value);
1028 }
1029 }
1030 return $all_options;
1031}
1032
1033/**
1034 * Version of get_option that is private to install/upgrade.
1035 *
1036 * @since unknown
1037 * @access private
1038 *
1039 * @param string $setting Option name.
1040 * @return mixed
1041 */
1042function __get_option($setting) {
1043 global $wpdb;
1044
1045 if ( $setting == 'home' && defined( 'WP_HOME' ) ) {
1046 return preg_replace( '|/+$|', '', constant( 'WP_HOME' ) );
1047 }
1048
1049 if ( $setting == 'siteurl' && defined( 'WP_SITEURL' ) ) {
1050 return preg_replace( '|/+$|', '', constant( 'WP_SITEURL' ) );
1051 }
1052
1053 $option = $wpdb->get_var( $wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting) );
1054
1055 if ( 'home' == $setting && '' == $option )
1056 return __get_option('siteurl');
1057
1058 if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
1059 $option = preg_replace('|/+$|', '', $option);
1060
1061 @ $kellogs = unserialize($option);
1062 if ($kellogs !== FALSE)
1063 return $kellogs;
1064 else
1065 return $option;
1066}
1067
1068/**
1069 * {@internal Missing Short Description}}
1070 *
1071 * {@internal Missing Long Description}}
1072 *
1073 * @since unknown
1074 *
1075 * @param string $content
1076 * @return string
1077 */
1078function deslash($content) {
1079 // Note: \\\ inside a regex denotes a single backslash.
1080
1081 // Replace one or more backslashes followed by a single quote with
1082 // a single quote.
1083 $content = preg_replace("/\\\+'/", "'", $content);
1084
1085 // Replace one or more backslashes followed by a double quote with
1086 // a double quote.
1087 $content = preg_replace('/\\\+"/', '"', $content);
1088
1089 // Replace one or more backslashes with one backslash.
1090 $content = preg_replace("/\\\+/", "\\", $content);
1091
1092 return $content;
1093}
1094
1095/**
1096 * {@internal Missing Short Description}}
1097 *
1098 * {@internal Missing Long Description}}
1099 *
1100 * @since unknown
1101 *
1102 * @param unknown_type $queries
1103 * @param unknown_type $execute
1104 * @return unknown
1105 */
1106function dbDelta($queries, $execute = true) {
1107 global $wpdb;
1108
1109 // Separate individual queries into an array
1110 if( !is_array($queries) ) {
1111 $queries = explode( ';', $queries );
1112 if('' == $queries[count($queries) - 1]) array_pop($queries);
1113 }
1114
1115 $cqueries = array(); // Creation Queries
1116 $iqueries = array(); // Insertion Queries
1117 $for_update = array();
1118
1119 // Create a tablename index for an array ($cqueries) of queries
1120 foreach($queries as $qry) {
1121 if(preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) {
1122 $cqueries[strtolower($matches[1])] = $qry;
1123 $for_update[$matches[1]] = 'Created table '.$matches[1];
1124 }
1125 else if(preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) {
1126 array_unshift($cqueries, $qry);
1127 }
1128 else if(preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) {
1129 $iqueries[] = $qry;
1130 }
1131 else if(preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) {
1132 $iqueries[] = $qry;
1133 }
1134 else {
1135 // Unrecognized query type
1136 }
1137 }
1138
1139 // Check to see which tables and fields exist
1140 if($tables = $wpdb->get_col('SHOW TABLES;')) {
1141 // For every table in the database
1142 foreach($tables as $table) {
1143 // If a table query exists for the database table...
1144 if( array_key_exists(strtolower($table), $cqueries) ) {
1145 // Clear the field and index arrays
1146 unset($cfields);
1147 unset($indices);
1148 // Get all of the field names in the query from between the parens
1149 preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2);
1150 $qryline = trim($match2[1]);
1151
1152 // Separate field lines into an array
1153 $flds = explode("\n", $qryline);
1154
1155 //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>";
1156
1157 // For every field line specified in the query
1158 foreach($flds as $fld) {
1159 // Extract the field name
1160 preg_match("|^([^ ]*)|", trim($fld), $fvals);
1161 $fieldname = $fvals[1];
1162
1163 // Verify the found field name
1164 $validfield = true;
1165 switch(strtolower($fieldname))
1166 {
1167 case '':
1168 case 'primary':
1169 case 'index':
1170 case 'fulltext':
1171 case 'unique':
1172 case 'key':
1173 $validfield = false;
1174 $indices[] = trim(trim($fld), ", \n");
1175 break;
1176 }
1177 $fld = trim($fld);
1178
1179 // If it's a valid field, add it to the field array
1180 if($validfield) {
1181 $cfields[strtolower($fieldname)] = trim($fld, ", \n");
1182 }
1183 }
1184
1185 // Fetch the table column structure from the database
1186 $tablefields = $wpdb->get_results("DESCRIBE {$table};");
1187
1188 // For every field in the table
1189 foreach($tablefields as $tablefield) {
1190 // If the table field exists in the field array...
1191 if(array_key_exists(strtolower($tablefield->Field), $cfields)) {
1192 // Get the field type from the query
1193 preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches);
1194 $fieldtype = $matches[1];
1195
1196 // Is actual field type different from the field type in query?
1197 if($tablefield->Type != $fieldtype) {
1198 // Add a query to change the column type
1199 $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
1200 $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
1201 }
1202
1203 // Get the default value from the array
1204 //echo "{$cfields[strtolower($tablefield->Field)]}<br>";
1205 if(preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) {
1206 $default_value = $matches[1];
1207 if($tablefield->Default != $default_value)
1208 {
1209 // Add a query to change the column's default value
1210 $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
1211 $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
1212 }
1213 }
1214
1215 // Remove the field from the array (so it's not added)
1216 unset($cfields[strtolower($tablefield->Field)]);
1217 }
1218 else {
1219 // This field exists in the table, but not in the creation queries?
1220 }
1221 }
1222
1223 // For every remaining field specified for the table
1224 foreach($cfields as $fieldname => $fielddef) {
1225 // Push a query line into $cqueries that adds the field to that table
1226 $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef";
1227 $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname;
1228 }
1229
1230 // Index stuff goes here
1231 // Fetch the table index structure from the database
1232 $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
1233
1234 if($tableindices) {
1235 // Clear the index array
1236 unset($index_ary);
1237
1238 // For every index in the table
1239 foreach($tableindices as $tableindex) {
1240 // Add the index to the index data array
1241 $keyname = $tableindex->Key_name;
1242 $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
1243 $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
1244 }
1245
1246 // For each actual index in the index array
1247 foreach($index_ary as $index_name => $index_data) {
1248 // Build a create string to compare to the query
1249 $index_string = '';
1250 if($index_name == 'PRIMARY') {
1251 $index_string .= 'PRIMARY ';
1252 }
1253 else if($index_data['unique']) {
1254 $index_string .= 'UNIQUE ';
1255 }
1256 $index_string .= 'KEY ';
1257 if($index_name != 'PRIMARY') {
1258 $index_string .= $index_name;
1259 }
1260 $index_columns = '';
1261 // For each column in the index
1262 foreach($index_data['columns'] as $column_data) {
1263 if($index_columns != '') $index_columns .= ',';
1264 // Add the field to the column list string
1265 $index_columns .= $column_data['fieldname'];
1266 if($column_data['subpart'] != '') {
1267 $index_columns .= '('.$column_data['subpart'].')';
1268 }
1269 }
1270 // Add the column list to the index create string
1271 $index_string .= ' ('.$index_columns.')';
1272 if(!(($aindex = array_search($index_string, $indices)) === false)) {
1273 unset($indices[$aindex]);
1274 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";
1275 }
1276 //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";
1277 }
1278 }
1279
1280 // For every remaining index specified for the table
1281 foreach ( (array) $indices as $index ) {
1282 // Push a query line into $cqueries that adds the index to that table
1283 $cqueries[] = "ALTER TABLE {$table} ADD $index";
1284 $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index;
1285 }
1286
1287 // Remove the original table creation query from processing
1288 unset($cqueries[strtolower($table)]);
1289 unset($for_update[strtolower($table)]);
1290 } else {
1291 // This table exists in the database, but not in the creation queries?
1292 }
1293 }
1294 }
1295
1296 $allqueries = array_merge($cqueries, $iqueries);
1297 if($execute) {
1298 foreach($allqueries as $query) {
1299 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n";
1300 $wpdb->query($query);
1301 }
1302 }
1303
1304 return $for_update;
1305}
1306
1307/**
1308 * {@internal Missing Short Description}}
1309 *
1310 * {@internal Missing Long Description}}
1311 *
1312 * @since unknown
1313 */
1314function make_db_current() {
1315 global $wp_queries;
1316
1317 $alterations = dbDelta($wp_queries);
1318 echo "<ol>\n";
1319 foreach($alterations as $alteration) echo "<li>$alteration</li>\n";
1320 echo "</ol>\n";
1321}
1322
1323/**
1324 * {@internal Missing Short Description}}
1325 *
1326 * {@internal Missing Long Description}}
1327 *
1328 * @since unknown
1329 */
1330function make_db_current_silent() {
1331 global $wp_queries;
1332
1333 $alterations = dbDelta($wp_queries);
1334}
1335
1336/**
1337 * {@internal Missing Short Description}}
1338 *
1339 * {@internal Missing Long Description}}
1340 *
1341 * @since unknown
1342 *
1343 * @param unknown_type $theme_name
1344 * @param unknown_type $template
1345 * @return unknown
1346 */
1347function make_site_theme_from_oldschool($theme_name, $template) {
1348 $home_path = get_home_path();
1349 $site_dir = WP_CONTENT_DIR . "/themes/$template";
1350
1351 if (! file_exists("$home_path/index.php"))
1352 return false;
1353
1354 // Copy files from the old locations to the site theme.
1355 // TODO: This does not copy arbitarary include dependencies. Only the
1356 // standard WP files are copied.
1357 $files = array('index.php' => 'index.php', 'wp-layout.css' => 'style.css', 'wp-comments.php' => 'comments.php', 'wp-comments-popup.php' => 'comments-popup.php');
1358
1359 foreach ($files as $oldfile => $newfile) {
1360 if ($oldfile == 'index.php')
1361 $oldpath = $home_path;
1362 else
1363 $oldpath = ABSPATH;
1364
1365 if ($oldfile == 'index.php') { // Check to make sure it's not a new index
1366 $index = implode('', file("$oldpath/$oldfile"));
1367 if (strpos($index, 'WP_USE_THEMES') !== false) {
1368 if (! @copy(WP_CONTENT_DIR . '/themes/default/index.php', "$site_dir/$newfile"))
1369 return false;
1370 continue; // Don't copy anything
1371 }
1372 }
1373
1374 if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile"))
1375 return false;
1376
1377 chmod("$site_dir/$newfile", 0777);
1378
1379 // Update the blog header include in each file.
1380 $lines = explode("\n", implode('', file("$site_dir/$newfile")));
1381 if ($lines) {
1382 $f = fopen("$site_dir/$newfile", 'w');
1383
1384 foreach ($lines as $line) {
1385 if (preg_match('/require.*wp-blog-header/', $line))
1386 $line = '//' . $line;
1387
1388 // Update stylesheet references.
1389 $line = str_replace("<?php echo __get_option('siteurl'); ?>/wp-layout.css", "<?php bloginfo('stylesheet_url'); ?>", $line);
1390
1391 // Update comments template inclusion.
1392 $line = str_replace("<?php include(ABSPATH . 'wp-comments.php'); ?>", "<?php comments_template(); ?>", $line);
1393
1394 fwrite($f, "{$line}\n");
1395 }
1396 fclose($f);
1397 }
1398 }
1399
1400 // Add a theme header.
1401 $header = "/*\nTheme Name: $theme_name\nTheme URI: " . __get_option('siteurl') . "\nDescription: A theme automatically created by the upgrade.\nVersion: 1.0\nAuthor: Moi\n*/\n";
1402
1403 $stylelines = file_get_contents("$site_dir/style.css");
1404 if ($stylelines) {
1405 $f = fopen("$site_dir/style.css", 'w');
1406
1407 fwrite($f, $header);
1408 fwrite($f, $stylelines);
1409 fclose($f);
1410 }
1411
1412 return true;
1413}
1414
1415/**
1416 * {@internal Missing Short Description}}
1417 *
1418 * {@internal Missing Long Description}}
1419 *
1420 * @since unknown
1421 *
1422 * @param unknown_type $theme_name
1423 * @param unknown_type $template
1424 * @return unknown
1425 */
1426function make_site_theme_from_default($theme_name, $template) {
1427 $site_dir = WP_CONTENT_DIR . "/themes/$template";
1428 $default_dir = WP_CONTENT_DIR . '/themes/default';
1429
1430 // Copy files from the default theme to the site theme.
1431 //$files = array('index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css');
1432
1433 $theme_dir = @ opendir("$default_dir");
1434 if ($theme_dir) {
1435 while(($theme_file = readdir( $theme_dir )) !== false) {
1436 if (is_dir("$default_dir/$theme_file"))
1437 continue;
1438 if (! @copy("$default_dir/$theme_file", "$site_dir/$theme_file"))
1439 return;
1440 chmod("$site_dir/$theme_file", 0777);
1441 }
1442 }
1443 @closedir($theme_dir);
1444
1445 // Rewrite the theme header.
1446 $stylelines = explode("\n", implode('', file("$site_dir/style.css")));
1447 if ($stylelines) {
1448 $f = fopen("$site_dir/style.css", 'w');
1449
1450 foreach ($stylelines as $line) {
1451 if (strpos($line, 'Theme Name:') !== false) $line = 'Theme Name: ' . $theme_name;
1452 elseif (strpos($line, 'Theme URI:') !== false) $line = 'Theme URI: ' . __get_option('url');
1453 elseif (strpos($line, 'Description:') !== false) $line = 'Description: Your theme.';
1454 elseif (strpos($line, 'Version:') !== false) $line = 'Version: 1';
1455 elseif (strpos($line, 'Author:') !== false) $line = 'Author: You';
1456 fwrite($f, $line . "\n");
1457 }
1458 fclose($f);
1459 }
1460
1461 // Copy the images.
1462 umask(0);
1463 if (! mkdir("$site_dir/images", 0777)) {
1464 return false;
1465 }
1466
1467 $images_dir = @ opendir("$default_dir/images");
1468 if ($images_dir) {
1469 while(($image = readdir($images_dir)) !== false) {
1470 if (is_dir("$default_dir/images/$image"))
1471 continue;
1472 if (! @copy("$default_dir/images/$image", "$site_dir/images/$image"))
1473 return;
1474 chmod("$site_dir/images/$image", 0777);
1475 }
1476 }
1477 @closedir($images_dir);
1478}
1479
1480// Create a site theme from the default theme.
1481/**
1482 * {@internal Missing Short Description}}
1483 *
1484 * {@internal Missing Long Description}}
1485 *
1486 * @since unknown
1487 *
1488 * @return unknown
1489 */
1490function make_site_theme() {
1491 // Name the theme after the blog.
1492 $theme_name = __get_option('blogname');
1493 $template = sanitize_title($theme_name);
1494 $site_dir = WP_CONTENT_DIR . "/themes/$template";
1495
1496 // If the theme already exists, nothing to do.
1497 if ( is_dir($site_dir)) {
1498 return false;
1499 }
1500
1501 // We must be able to write to the themes dir.
1502 if (! is_writable(WP_CONTENT_DIR . "/themes")) {
1503 return false;
1504 }
1505
1506 umask(0);
1507 if (! mkdir($site_dir, 0777)) {
1508 return false;
1509 }
1510
1511 if (file_exists(ABSPATH . 'wp-layout.css')) {
1512 if (! make_site_theme_from_oldschool($theme_name, $template)) {
1513 // TODO: rm -rf the site theme directory.
1514 return false;
1515 }
1516 } else {
1517 if (! make_site_theme_from_default($theme_name, $template))
1518 // TODO: rm -rf the site theme directory.
1519 return false;
1520 }
1521
1522 // Make the new site theme active.
1523 $current_template = __get_option('template');
1524 if ($current_template == 'default') {
1525 update_option('template', $template);
1526 update_option('stylesheet', $template);
1527 }
1528 return $template;
1529}
1530
1531/**
1532 * Translate user level to user role name.
1533 *
1534 * @since unknown
1535 *
1536 * @param int $level User level.
1537 * @return string User role name.
1538 */
1539function translate_level_to_role($level) {
1540 switch ($level) {
1541 case 10:
1542 case 9:
1543 case 8:
1544 return 'administrator';
1545 case 7:
1546 case 6:
1547 case 5:
1548 return 'editor';
1549 case 4:
1550 case 3:
1551 case 2:
1552 return 'author';
1553 case 1:
1554 return 'contributor';
1555 case 0:
1556 return 'subscriber';
1557 }
1558}
1559
1560/**
1561 * {@internal Missing Short Description}}
1562 *
1563 * {@internal Missing Long Description}}
1564 *
1565 * @since unknown
1566 */
1567function wp_check_mysql_version() {
1568 global $wpdb;
1569 $result = $wpdb->check_database_version();
1570 if ( is_wp_error( $result ) )
1571 die( $result->get_error_message() );
1572}
1573
1574/**
1575 * {@internal Missing Short Description}}
1576 *
1577 * {@internal Missing Long Description}}
1578 *
1579 * @since unknown
1580 */
1581function maybe_disable_automattic_widgets() {
1582 $plugins = __get_option( 'active_plugins' );
1583
1584 foreach ( (array) $plugins as $plugin ) {
1585 if ( basename( $plugin ) == 'widgets.php' ) {
1586 array_splice( $plugins, array_search( $plugin, $plugins ), 1 );
1587 update_option( 'active_plugins', $plugins );
1588 break;
1589 }
1590 }
1591}
1592
1593?>