3 * WordPress Upgrade API
5 * Most of the functions are pluggable and can be overwritten
8 * @subpackage Administration
11 /** Include user install customize script. */
12 if ( file_exists(WP_CONTENT_DIR . '/install.php') )
13 require (WP_CONTENT_DIR . '/install.php');
15 /** WordPress Administration API */
16 require_once(ABSPATH . 'wp-admin/includes/admin.php');
18 /** WordPress Schema API */
19 require_once(ABSPATH . 'wp-admin/includes/schema.php');
21 if ( !function_exists('wp_install') ) :
23 * {@internal Missing Short Description}}
25 * {@internal Missing Long Description}}
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'.
36 function wp_install($blog_title, $user_name, $user_email, $public, $deprecated='') {
39 wp_check_mysql_version();
41 make_db_current_silent();
45 update_option('blogname', $blog_title);
46 update_option('admin_email', $user_email);
47 update_option('blog_public', $public);
49 $guessurl = wp_guess_url();
51 update_option('siteurl', $guessurl);
53 // If not a public blog, don't ping.
55 update_option('default_pingback_flag', 0);
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);
61 $random_password = wp_generate_password();
62 $user_id = wp_create_user($user_name, $random_password, $user_email);
64 $random_password = __('User already exists. Password inherited.');
67 $user = new WP_User($user_id);
68 $user->set_role('administrator');
70 wp_install_defaults($user_id);
72 $wp_rewrite->flush_rules();
74 wp_new_blog_notification($blog_title, $guessurl, $user_id, $random_password);
78 return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $random_password);
82 if ( !function_exists('wp_install_defaults') ) :
84 * {@internal Missing Short Description}}
86 * {@internal Missing Long Description}}
90 * @param int $user_id User ID.
92 function wp_install_defaults($user_id) {
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')");
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')");
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)" );
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)" );
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)" );
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)" );
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)" );
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)" );
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)" );
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)" );
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's comments. There you will have the option to edit or delete them.'))."')");
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', '', '', '')");
145 if ( !function_exists('wp_new_blog_notification') ) :
147 * {@internal Missing Short Description}}
149 * {@internal Missing Long Description}}
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.
158 function 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:
167 You can log in to the administrator account with the following information:
172 We hope you enjoy your new blog. Thanks!
175 http://wordpress.org/
176 "), $blog_url, $name, $password);
178 @wp_mail($email, __('New WordPress Blog'), $message, $message_headers);
182 if ( !function_exists('wp_upgrade') ) :
184 * Run WordPress Upgrade functions.
186 * {@internal Missing Long Description}}
192 function wp_upgrade() {
193 global $wp_current_db_version, $wp_db_version;
195 $wp_current_db_version = __get_option('db_version');
197 // We are up-to-date. Nothing to do.
198 if ( $wp_db_version == $wp_current_db_version )
201 if( ! is_blog_installed() )
204 wp_check_mysql_version();
206 make_db_current_silent();
213 * Functions to be called in install and upgrade scripts.
215 * {@internal Missing Long Description}}
219 function upgrade_all() {
220 global $wp_current_db_version, $wp_db_version, $wp_rewrite;
221 $wp_current_db_version = __get_option('db_version');
223 // We are up-to-date. Nothing to do.
224 if ( $wp_db_version == $wp_current_db_version )
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;
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;
237 if ( $wp_current_db_version < 6039 )
238 upgrade_230_options_table();
242 if ( $wp_current_db_version < 2541 ) {
249 if ( $wp_current_db_version < 3308 )
252 if ( $wp_current_db_version < 4772 )
255 if ( $wp_current_db_version < 4351 )
258 if ( $wp_current_db_version < 5539 )
261 if ( $wp_current_db_version < 6124 )
262 upgrade_230_old_tables();
264 if ( $wp_current_db_version < 7499 )
267 if ( $wp_current_db_version < 7796 )
270 if ( $wp_current_db_version < 7935 )
273 if ( $wp_current_db_version < 8201 )
276 if ( $wp_current_db_version < 8989 )
279 maybe_disable_automattic_widgets();
281 $wp_rewrite->flush_rules();
283 update_option('db_version', $wp_db_version);
287 * Execute changes made in WordPress 1.0.
291 function upgrade_100() {
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 = ''");
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) );
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) );
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/%'");
317 $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat");
319 foreach ($done_ids as $done_id) :
320 $done_posts[] = $done_id->post_id;
322 $catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')';
327 $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere");
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)
336 ", $post->ID, $post->post_category) );
343 * Execute changes made in WordPress 1.0.1.
347 function upgrade_101() {
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');
361 * Execute changes made in WordPress 1.2.
365 function upgrade_110() {
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) );
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.'\'');
384 // Get the GMT offset, we'll use that later on
385 $all_options = get_alloptions_110();
387 $time_difference = $all_options->time_difference;
389 $server_time = time()+date('Z');
390 $weblogger_time = $server_time + $time_difference*3600;
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;
398 // Add a gmt_offset option, with value $gmt_offset
399 add_option('gmt_offset', $gmt_offset);
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;
406 if (!$got_gmt_fields) {
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)");
421 * Execute changes made in WordPress 1.5.
425 function upgrade_130() {
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");
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);
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'");
444 // Remove extraneous backslashes.
445 $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->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'");
454 // Remove extraneous backslashes.
455 $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->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'");
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'");
469 $active_plugins = __get_option('active_plugins');
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);
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');
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 />%'");
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)");
503 * Execute changes made in WordPress 2.0.
507 function upgrade_160() {
508 global $wpdb, $wp_current_db_version;
510 populate_roles_160();
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) );
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) );
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) );
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();
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) );
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',
576 WHERE ID = %d", $object->post_type, $object->ID) );
578 $meta = get_post_meta($object->ID, 'imagedata', true);
579 if ( ! empty($meta['file']) )
580 update_attached_file( $object->ID, $meta['file'] );
586 * Execute changes made in WordPress 2.1.
590 function upgrade_210() {
591 global $wpdb, $wp_current_db_version;
593 if ( $wp_current_db_version < 3506 ) {
594 // Update status and type.
595 $posts = $wpdb->get_results("SELECT ID, post_status FROM $wpdb->posts");
597 if ( ! empty($posts) ) foreach ($posts as $post) {
598 $status = $post->post_status;
601 if ( 'static' == $status ) {
604 } else if ( 'attachment' == $status ) {
606 $type = 'attachment';
609 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID) );
613 if ( $wp_current_db_version < 3845 ) {
614 populate_roles_210();
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'");
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));
630 * Execute changes made in WordPress 2.3.
634 function upgrade_230() {
635 global $wp_current_db_version, $wpdb;
637 if ( $wp_current_db_version < 5200 ) {
638 populate_roles_230();
641 // Convert categories to terms.
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;
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;
659 $alt_slug = $slug . "-$num";
661 $slug_check = $wpdb->get_var( $wpdb->prepare("SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug) );
662 } while ( $slug_check );
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) );
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) );
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;
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;
690 if ( !empty($category->tag_count) ) {
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;
698 if ( empty($count) ) {
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;
706 $select = 'post_id, category_id';
708 $select .= ', rel_type';
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)
717 $tt_id = $tt_ids[$term_id][$taxonomy];
721 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $post_id, $tt_id) );
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;
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;
735 $name = $wpdb->escape($category->cat_name);
736 $slug = sanitize_title($name);
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;
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;
750 $link_cat_id_map[$cat_id] = $term_id;
751 $default_link_cat = $term_id;
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;
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 )
762 if ( ! isset($link_cat_id_map[$link->link_category]) )
764 $term_id = $link_cat_id_map[$link->link_category];
765 $tt_id = $tt_ids[$term_id];
769 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $link->link_id, $tt_id) );
772 // Set default to the last category we grabbed during the upgrade loop.
773 update_option('default_link_category', $default_link_cat);
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];
784 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $link_id, $tt_id) );
788 if ( $wp_current_db_version < 4772 ) {
789 // Obsolete linkcategories table
790 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories');
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) );
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) );
805 * Remove old options from the database.
809 function upgrade_230_options_table() {
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();
819 * Remove old categories, link2cat, and post2cat database tables.
823 function upgrade_230_old_tables() {
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');
831 * Upgrade old slugs made in version 2.2.
835 function upgrade_old_slugs() {
836 // upgrade people who were using the Redirect Old Slugs plugin
838 $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'");
842 * Execute changes made in WordPress 2.5.0.
846 function upgrade_250() {
847 global $wp_current_db_version;
849 if ( $wp_current_db_version < 6689 ) {
850 populate_roles_250();
856 * Execute changes made in WordPress 2.5.1.
860 function upgrade_251() {
861 global $wp_current_db_version;
863 // Make the secret longer
864 update_option('secret', wp_generate_password(64));
868 * Execute changes made in WordPress 2.5.2.
872 function upgrade_252() {
875 $wpdb->query("UPDATE $wpdb->users SET user_activation_key = ''");
879 * Execute changes made in WordPress 2.6.
883 function upgrade_260() {
884 global $wp_current_db_version;
886 if ( $wp_current_db_version < 8000 )
887 populate_roles_260();
889 if ( $wp_current_db_version < 8201 ) {
890 update_option('enable_app', 1);
891 update_option('enable_xmlrpc', 1);
896 * Execute changes made in WordPress 2.7.
900 function upgrade_270() {
901 global $wpdb, $wp_current_db_version;
903 if ( $wp_current_db_version < 8980 )
904 populate_roles_270();
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'" );
912 // The functions we use to actually do stuff
917 * {@internal Missing Short Description}}
919 * {@internal Missing Long Description}}
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.
927 function maybe_create_table($table_name, $create_ddl) {
929 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
930 if ($table == $table_name) {
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) {
946 * {@internal Missing Short Description}}
948 * {@internal Missing Long Description}}
952 * @param string $table Database table name.
953 * @param string $index Index name to drop.
954 * @return bool True, when finished.
956 function drop_index($table, $index) {
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`");
964 $wpdb->show_errors();
969 * {@internal Missing Short Description}}
971 * {@internal Missing Long Description}}
975 * @param string $table Database table name.
976 * @param string $index Database table index column.
977 * @return bool True, when done with execution.
979 function add_clean_index($table, $index) {
981 drop_index($table, $index);
982 $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )");
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
992 function 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) {
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) {
1012 * Retrieve all options as it was for 1.2.
1016 * @return array List of options.
1018 function get_alloptions_110() {
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);
1030 return $all_options;
1034 * Version of get_option that is private to install/upgrade.
1039 * @param string $setting Option name.
1042 function __get_option($setting) {
1045 if ( $setting == 'home' && defined( 'WP_HOME' ) ) {
1046 return preg_replace( '|/+$|', '', constant( 'WP_HOME' ) );
1049 if ( $setting == 'siteurl' && defined( 'WP_SITEURL' ) ) {
1050 return preg_replace( '|/+$|', '', constant( 'WP_SITEURL' ) );
1053 $option = $wpdb->get_var( $wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting) );
1055 if ( 'home' == $setting && '' == $option )
1056 return __get_option('siteurl');
1058 if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
1059 $option = preg_replace('|/+$|', '', $option);
1061 @ $kellogs = unserialize($option);
1062 if ($kellogs !== FALSE)
1069 * {@internal Missing Short Description}}
1071 * {@internal Missing Long Description}}
1075 * @param string $content
1078 function deslash($content) {
1079 // Note: \\\ inside a regex denotes a single backslash.
1081 // Replace one or more backslashes followed by a single quote with
1083 $content = preg_replace("/\\\+'/", "'", $content);
1085 // Replace one or more backslashes followed by a double quote with
1087 $content = preg_replace('/\\\+"/', '"', $content);
1089 // Replace one or more backslashes with one backslash.
1090 $content = preg_replace("/\\\+/", "\\", $content);
1096 * {@internal Missing Short Description}}
1098 * {@internal Missing Long Description}}
1102 * @param unknown_type $queries
1103 * @param unknown_type $execute
1106 function dbDelta($queries, $execute = true) {
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);
1115 $cqueries = array(); // Creation Queries
1116 $iqueries = array(); // Insertion Queries
1117 $for_update = array();
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];
1125 else if(preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) {
1126 array_unshift($cqueries, $qry);
1128 else if(preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) {
1131 else if(preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) {
1135 // Unrecognized query type
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
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]);
1152 // Separate field lines into an array
1153 $flds = explode("\n", $qryline);
1155 //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>";
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];
1163 // Verify the found field name
1165 switch(strtolower($fieldname))
1173 $validfield = false;
1174 $indices[] = trim(trim($fld), ", \n");
1179 // If it's a valid field, add it to the field array
1181 $cfields[strtolower($fieldname)] = trim($fld, ", \n");
1185 // Fetch the table column structure from the database
1186 $tablefields = $wpdb->get_results("DESCRIBE {$table};");
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];
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}";
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)
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}";
1215 // Remove the field from the array (so it's not added)
1216 unset($cfields[strtolower($tablefield->Field)]);
1219 // This field exists in the table, but not in the creation queries?
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;
1230 // Index stuff goes here
1231 // Fetch the table index structure from the database
1232 $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
1235 // Clear the index array
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;
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
1250 if($index_name == 'PRIMARY') {
1251 $index_string .= 'PRIMARY ';
1253 else if($index_data['unique']) {
1254 $index_string .= 'UNIQUE ';
1256 $index_string .= 'KEY ';
1257 if($index_name != 'PRIMARY') {
1258 $index_string .= $index_name;
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'].')';
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";
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";
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;
1287 // Remove the original table creation query from processing
1288 unset($cqueries[strtolower($table)]);
1289 unset($for_update[strtolower($table)]);
1291 // This table exists in the database, but not in the creation queries?
1296 $allqueries = array_merge($cqueries, $iqueries);
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);
1308 * {@internal Missing Short Description}}
1310 * {@internal Missing Long Description}}
1314 function make_db_current() {
1317 $alterations = dbDelta($wp_queries);
1319 foreach($alterations as $alteration) echo "<li>$alteration</li>\n";
1324 * {@internal Missing Short Description}}
1326 * {@internal Missing Long Description}}
1330 function make_db_current_silent() {
1333 $alterations = dbDelta($wp_queries);
1337 * {@internal Missing Short Description}}
1339 * {@internal Missing Long Description}}
1343 * @param unknown_type $theme_name
1344 * @param unknown_type $template
1347 function make_site_theme_from_oldschool($theme_name, $template) {
1348 $home_path = get_home_path();
1349 $site_dir = WP_CONTENT_DIR . "/themes/$template";
1351 if (! file_exists("$home_path/index.php"))
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');
1359 foreach ($files as $oldfile => $newfile) {
1360 if ($oldfile == 'index.php')
1361 $oldpath = $home_path;
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"))
1370 continue; // Don't copy anything
1374 if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile"))
1377 chmod("$site_dir/$newfile", 0777);
1379 // Update the blog header include in each file.
1380 $lines = explode("\n", implode('', file("$site_dir/$newfile")));
1382 $f = fopen("$site_dir/$newfile", 'w');
1384 foreach ($lines as $line) {
1385 if (preg_match('/require.*wp-blog-header/', $line))
1386 $line = '//' . $line;
1388 // Update stylesheet references.
1389 $line = str_replace("<?php echo __get_option('siteurl'); ?>/wp-layout.css", "<?php bloginfo('stylesheet_url'); ?>", $line);
1391 // Update comments template inclusion.
1392 $line = str_replace("<?php include(ABSPATH . 'wp-comments.php'); ?>", "<?php comments_template(); ?>", $line);
1394 fwrite($f, "{$line}\n");
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";
1403 $stylelines = file_get_contents("$site_dir/style.css");
1405 $f = fopen("$site_dir/style.css", 'w');
1407 fwrite($f, $header);
1408 fwrite($f, $stylelines);
1416 * {@internal Missing Short Description}}
1418 * {@internal Missing Long Description}}
1422 * @param unknown_type $theme_name
1423 * @param unknown_type $template
1426 function make_site_theme_from_default($theme_name, $template) {
1427 $site_dir = WP_CONTENT_DIR . "/themes/$template";
1428 $default_dir = WP_CONTENT_DIR . '/themes/default';
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');
1433 $theme_dir = @ opendir("$default_dir");
1435 while(($theme_file = readdir( $theme_dir )) !== false) {
1436 if (is_dir("$default_dir/$theme_file"))
1438 if (! @copy("$default_dir/$theme_file", "$site_dir/$theme_file"))
1440 chmod("$site_dir/$theme_file", 0777);
1443 @closedir($theme_dir);
1445 // Rewrite the theme header.
1446 $stylelines = explode("\n", implode('', file("$site_dir/style.css")));
1448 $f = fopen("$site_dir/style.css", 'w');
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");
1463 if (! mkdir("$site_dir/images", 0777)) {
1467 $images_dir = @ opendir("$default_dir/images");
1469 while(($image = readdir($images_dir)) !== false) {
1470 if (is_dir("$default_dir/images/$image"))
1472 if (! @copy("$default_dir/images/$image", "$site_dir/images/$image"))
1474 chmod("$site_dir/images/$image", 0777);
1477 @closedir($images_dir);
1480 // Create a site theme from the default theme.
1482 * {@internal Missing Short Description}}
1484 * {@internal Missing Long Description}}
1490 function 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";
1496 // If the theme already exists, nothing to do.
1497 if ( is_dir($site_dir)) {
1501 // We must be able to write to the themes dir.
1502 if (! is_writable(WP_CONTENT_DIR . "/themes")) {
1507 if (! mkdir($site_dir, 0777)) {
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.
1517 if (! make_site_theme_from_default($theme_name, $template))
1518 // TODO: rm -rf the site theme directory.
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);
1532 * Translate user level to user role name.
1536 * @param int $level User level.
1537 * @return string User role name.
1539 function translate_level_to_role($level) {
1544 return 'administrator';
1554 return 'contributor';
1556 return 'subscriber';
1561 * {@internal Missing Short Description}}
1563 * {@internal Missing Long Description}}
1567 function wp_check_mysql_version() {
1569 $result = $wpdb->check_database_version();
1570 if ( is_wp_error( $result ) )
1571 die( $result->get_error_message() );
1575 * {@internal Missing Short Description}}
1577 * {@internal Missing Long Description}}
1581 function maybe_disable_automattic_widgets() {
1582 $plugins = __get_option( 'active_plugins' );
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 );