-
+ EA54040FBC85077BD4141C30DC129DBC1560D14DCCAF15936D8D27A1887AB728233C8A2D541CB125DC63007CD5F4BE2108B9F3EC2DE27C19EC7A2CE9BECA9505
mp-wp/wp-admin/includes/upgrade.php
(0 . 0)(1 . 1593)
45242 <?php
45243 /**
45244 * WordPress Upgrade API
45245 *
45246 * Most of the functions are pluggable and can be overwritten
45247 *
45248 * @package WordPress
45249 * @subpackage Administration
45250 */
45251
45252 /** Include user install customize script. */
45253 if ( file_exists(WP_CONTENT_DIR . '/install.php') )
45254 require (WP_CONTENT_DIR . '/install.php');
45255
45256 /** WordPress Administration API */
45257 require_once(ABSPATH . 'wp-admin/includes/admin.php');
45258
45259 /** WordPress Schema API */
45260 require_once(ABSPATH . 'wp-admin/includes/schema.php');
45261
45262 if ( !function_exists('wp_install') ) :
45263 /**
45264 * {@internal Missing Short Description}}
45265 *
45266 * {@internal Missing Long Description}}
45267 *
45268 * @since unknown
45269 *
45270 * @param string $blog_title Blog title.
45271 * @param string $user_name User's username.
45272 * @param string $user_email User's email.
45273 * @param bool $public Whether blog is public.
45274 * @param null $deprecated Optional. Not used.
45275 * @return array Array keys 'url', 'user_id', 'password'.
45276 */
45277 function wp_install($blog_title, $user_name, $user_email, $public, $deprecated='') {
45278 global $wp_rewrite;
45279
45280 wp_check_mysql_version();
45281 wp_cache_flush();
45282 make_db_current_silent();
45283 populate_options();
45284 populate_roles();
45285
45286 update_option('blogname', $blog_title);
45287 update_option('admin_email', $user_email);
45288 update_option('blog_public', $public);
45289
45290 $guessurl = wp_guess_url();
45291
45292 update_option('siteurl', $guessurl);
45293
45294 // If not a public blog, don't ping.
45295 if ( ! $public )
45296 update_option('default_pingback_flag', 0);
45297
45298 // Create default user. If the user already exists, the user tables are
45299 // being shared among blogs. Just set the role in that case.
45300 $user_id = username_exists($user_name);
45301 if ( !$user_id ) {
45302 $random_password = wp_generate_password();
45303 $user_id = wp_create_user($user_name, $random_password, $user_email);
45304 } else {
45305 $random_password = __('User already exists. Password inherited.');
45306 }
45307
45308 $user = new WP_User($user_id);
45309 $user->set_role('administrator');
45310
45311 wp_install_defaults($user_id);
45312
45313 $wp_rewrite->flush_rules();
45314
45315 wp_new_blog_notification($blog_title, $guessurl, $user_id, $random_password);
45316
45317 wp_cache_flush();
45318
45319 return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $random_password);
45320 }
45321 endif;
45322
45323 if ( !function_exists('wp_install_defaults') ) :
45324 /**
45325 * {@internal Missing Short Description}}
45326 *
45327 * {@internal Missing Long Description}}
45328 *
45329 * @since unknown
45330 *
45331 * @param int $user_id User ID.
45332 */
45333 function wp_install_defaults($user_id) {
45334 global $wpdb;
45335
45336 // Default category
45337 $cat_name = $wpdb->escape(__('Uncategorized'));
45338 $cat_slug = sanitize_title(_c('Uncategorized|Default category slug'));
45339 $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$cat_name', '$cat_slug', '0')");
45340 $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('1', 'category', '', '0', '1')");
45341
45342 // Default link category
45343 $cat_name = $wpdb->escape(__('Blogroll'));
45344 $cat_slug = sanitize_title(_c('Blogroll|Default link category slug'));
45345 $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$cat_name', '$cat_slug', '0')");
45346 $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('2', 'link_category', '', '0', '7')");
45347
45348 // Now drop in some default links
45349 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://codex.wordpress.org/', 'Documentation', 0, '', '');");
45350 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (1, 2)" );
45351
45352 $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/', '');");
45353 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (2, 2)" );
45354
45355 $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, '', '');");
45356 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (3, 2)" );
45357
45358 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/support/', 'Support Forum', 0, '', '');");
45359 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (4, 2)" );
45360
45361 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/extend/plugins/', 'Plugins', 0, '', '');");
45362 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (5, 2)" );
45363
45364 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/extend/themes/', 'Themes', 0, '', '');");
45365 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (6, 2)" );
45366
45367 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://planet.wordpress.org/', 'WordPress Planet', 0, '', '');");
45368 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (7, 2)" );
45369
45370 // First post
45371 $now = date('Y-m-d H:i:s');
45372 $now_gmt = gmdate('Y-m-d H:i:s');
45373 $first_post_guid = get_option('home') . '/?p=1';
45374 $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', '', '', '')");
45375 $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (1, 1)" );
45376
45377 // Default comment
45378 $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.'))."')");
45379
45380 // First Page
45381 $first_post_guid = get_option('home') . '/?page_id=2';
45382 $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', '', '', '')");
45383 }
45384 endif;
45385
45386 if ( !function_exists('wp_new_blog_notification') ) :
45387 /**
45388 * {@internal Missing Short Description}}
45389 *
45390 * {@internal Missing Long Description}}
45391 *
45392 * @since unknown
45393 *
45394 * @param string $blog_title Blog title.
45395 * @param string $blog_url Blog url.
45396 * @param int $user_id User ID.
45397 * @param string $password User's Password.
45398 */
45399 function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) {
45400 $user = new WP_User($user_id);
45401 $email = $user->user_email;
45402 $name = $user->user_login;
45403 $message_headers = 'From: "' . $blog_title . '" <wordpress@' . $_SERVER['SERVER_NAME'] . '>';
45404 $message = sprintf(__("Your new WordPress blog has been successfully set up at:
45405
45406 %1\$s
45407
45408 You can log in to the administrator account with the following information:
45409
45410 Username: %2\$s
45411 Password: %3\$s
45412
45413 We hope you enjoy your new blog. Thanks!
45414
45415 --The WordPress Team
45416 http://wordpress.org/
45417 "), $blog_url, $name, $password);
45418
45419 @wp_mail($email, __('New WordPress Blog'), $message, $message_headers);
45420 }
45421 endif;
45422
45423 if ( !function_exists('wp_upgrade') ) :
45424 /**
45425 * Run WordPress Upgrade functions.
45426 *
45427 * {@internal Missing Long Description}}
45428 *
45429 * @since unknown
45430 *
45431 * @return null
45432 */
45433 function wp_upgrade() {
45434 global $wp_current_db_version, $wp_db_version;
45435
45436 $wp_current_db_version = __get_option('db_version');
45437
45438 // We are up-to-date. Nothing to do.
45439 if ( $wp_db_version == $wp_current_db_version )
45440 return;
45441
45442 if( ! is_blog_installed() )
45443 return;
45444
45445 wp_check_mysql_version();
45446 wp_cache_flush();
45447 make_db_current_silent();
45448 upgrade_all();
45449 wp_cache_flush();
45450 }
45451 endif;
45452
45453 /**
45454 * Functions to be called in install and upgrade scripts.
45455 *
45456 * {@internal Missing Long Description}}
45457 *
45458 * @since unknown
45459 */
45460 function upgrade_all() {
45461 global $wp_current_db_version, $wp_db_version, $wp_rewrite;
45462 $wp_current_db_version = __get_option('db_version');
45463
45464 // We are up-to-date. Nothing to do.
45465 if ( $wp_db_version == $wp_current_db_version )
45466 return;
45467
45468 // If the version is not set in the DB, try to guess the version.
45469 if ( empty($wp_current_db_version) ) {
45470 $wp_current_db_version = 0;
45471
45472 // If the template option exists, we have 1.5.
45473 $template = __get_option('template');
45474 if ( !empty($template) )
45475 $wp_current_db_version = 2541;
45476 }
45477
45478 if ( $wp_current_db_version < 6039 )
45479 upgrade_230_options_table();
45480
45481 populate_options();
45482
45483 if ( $wp_current_db_version < 2541 ) {
45484 upgrade_100();
45485 upgrade_101();
45486 upgrade_110();
45487 upgrade_130();
45488 }
45489
45490 if ( $wp_current_db_version < 3308 )
45491 upgrade_160();
45492
45493 if ( $wp_current_db_version < 4772 )
45494 upgrade_210();
45495
45496 if ( $wp_current_db_version < 4351 )
45497 upgrade_old_slugs();
45498
45499 if ( $wp_current_db_version < 5539 )
45500 upgrade_230();
45501
45502 if ( $wp_current_db_version < 6124 )
45503 upgrade_230_old_tables();
45504
45505 if ( $wp_current_db_version < 7499 )
45506 upgrade_250();
45507
45508 if ( $wp_current_db_version < 7796 )
45509 upgrade_251();
45510
45511 if ( $wp_current_db_version < 7935 )
45512 upgrade_252();
45513
45514 if ( $wp_current_db_version < 8201 )
45515 upgrade_260();
45516
45517 if ( $wp_current_db_version < 8989 )
45518 upgrade_270();
45519
45520 maybe_disable_automattic_widgets();
45521
45522 $wp_rewrite->flush_rules();
45523
45524 update_option('db_version', $wp_db_version);
45525 }
45526
45527 /**
45528 * Execute changes made in WordPress 1.0.
45529 *
45530 * @since 1.0.0
45531 */
45532 function upgrade_100() {
45533 global $wpdb;
45534
45535 // Get the title and ID of every post, post_name to check if it already has a value
45536 $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
45537 if ($posts) {
45538 foreach($posts as $post) {
45539 if ('' == $post->post_name) {
45540 $newtitle = sanitize_title($post->post_title);
45541 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) );
45542 }
45543 }
45544 }
45545
45546 $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories");
45547 foreach ($categories as $category) {
45548 if ('' == $category->category_nicename) {
45549 $newtitle = sanitize_title($category->cat_name);
45550 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->categories SET category_nicename = %s WHERE cat_ID = %d", $newtitle, $category->cat_ID) );
45551 }
45552 }
45553
45554 $wpdb->query("UPDATE $wpdb->options SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/')
45555 WHERE option_name LIKE 'links_rating_image%'
45556 AND option_value LIKE 'wp-links/links-images/%'");
45557
45558 $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat");
45559 if ($done_ids) :
45560 foreach ($done_ids as $done_id) :
45561 $done_posts[] = $done_id->post_id;
45562 endforeach;
45563 $catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')';
45564 else:
45565 $catwhere = '';
45566 endif;
45567
45568 $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere");
45569 if ($allposts) :
45570 foreach ($allposts as $post) {
45571 // Check to see if it's already been imported
45572 $cat = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->post2cat WHERE post_id = %d AND category_id = %d", $post->ID, $post->post_category) );
45573 if (!$cat && 0 != $post->post_category) { // If there's no result
45574 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->post2cat
45575 (post_id, category_id)
45576 VALUES (%s, %s)
45577 ", $post->ID, $post->post_category) );
45578 }
45579 }
45580 endif;
45581 }
45582
45583 /**
45584 * Execute changes made in WordPress 1.0.1.
45585 *
45586 * @since 1.0.1
45587 */
45588 function upgrade_101() {
45589 global $wpdb;
45590
45591 // Clean up indices, add a few
45592 add_clean_index($wpdb->posts, 'post_name');
45593 add_clean_index($wpdb->posts, 'post_status');
45594 add_clean_index($wpdb->categories, 'category_nicename');
45595 add_clean_index($wpdb->comments, 'comment_approved');
45596 add_clean_index($wpdb->comments, 'comment_post_ID');
45597 add_clean_index($wpdb->links , 'link_category');
45598 add_clean_index($wpdb->links , 'link_visible');
45599 }
45600
45601 /**
45602 * Execute changes made in WordPress 1.2.
45603 *
45604 * @since 1.2.0
45605 */
45606 function upgrade_110() {
45607 global $wpdb;
45608
45609 // Set user_nicename.
45610 $users = $wpdb->get_results("SELECT ID, user_nickname, user_nicename FROM $wpdb->users");
45611 foreach ($users as $user) {
45612 if ('' == $user->user_nicename) {
45613 $newname = sanitize_title($user->user_nickname);
45614 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->users SET user_nicename = %s WHERE ID = %d", $newname, $user->ID) );
45615 }
45616 }
45617
45618 $users = $wpdb->get_results("SELECT ID, user_pass from $wpdb->users");
45619 foreach ($users as $row) {
45620 if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) {
45621 $wpdb->query('UPDATE '.$wpdb->users.' SET user_pass = MD5(\''.$row->user_pass.'\') WHERE ID = \''.$row->ID.'\'');
45622 }
45623 }
45624
45625 // Get the GMT offset, we'll use that later on
45626 $all_options = get_alloptions_110();
45627
45628 $time_difference = $all_options->time_difference;
45629
45630 $server_time = time()+date('Z');
45631 $weblogger_time = $server_time + $time_difference*3600;
45632 $gmt_time = time();
45633
45634 $diff_gmt_server = ($gmt_time - $server_time) / 3600;
45635 $diff_weblogger_server = ($weblogger_time - $server_time) / 3600;
45636 $diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server;
45637 $gmt_offset = -$diff_gmt_weblogger;
45638
45639 // Add a gmt_offset option, with value $gmt_offset
45640 add_option('gmt_offset', $gmt_offset);
45641
45642 // Check if we already set the GMT fields (if we did, then
45643 // MAX(post_date_gmt) can't be '0000-00-00 00:00:00'
45644 // <michel_v> I just slapped myself silly for not thinking about it earlier
45645 $got_gmt_fields = ($wpdb->get_var("SELECT MAX(post_date_gmt) FROM $wpdb->posts") == '0000-00-00 00:00:00') ? false : true;
45646
45647 if (!$got_gmt_fields) {
45648
45649 // Add or substract time to all dates, to get GMT dates
45650 $add_hours = intval($diff_gmt_weblogger);
45651 $add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours));
45652 $wpdb->query("UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
45653 $wpdb->query("UPDATE $wpdb->posts SET post_modified = post_date");
45654 $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'");
45655 $wpdb->query("UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
45656 $wpdb->query("UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
45657 }
45658
45659 }
45660
45661 /**
45662 * Execute changes made in WordPress 1.5.
45663 *
45664 * @since 1.5.0
45665 */
45666 function upgrade_130() {
45667 global $wpdb;
45668
45669 // Remove extraneous backslashes.
45670 $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
45671 if ($posts) {
45672 foreach($posts as $post) {
45673 $post_content = addslashes(deslash($post->post_content));
45674 $post_title = addslashes(deslash($post->post_title));
45675 $post_excerpt = addslashes(deslash($post->post_excerpt));
45676 if ( empty($post->guid) )
45677 $guid = get_permalink($post->ID);
45678 else
45679 $guid = $post->guid;
45680
45681 $wpdb->query("UPDATE $wpdb->posts SET post_title = '$post_title', post_content = '$post_content', post_excerpt = '$post_excerpt', guid = '$guid' WHERE ID = '$post->ID'");
45682 }
45683 }
45684
45685 // Remove extraneous backslashes.
45686 $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
45687 if ($comments) {
45688 foreach($comments as $comment) {
45689 $comment_content = addslashes(deslash($comment->comment_content));
45690 $comment_author = addslashes(deslash($comment->comment_author));
45691 $wpdb->query("UPDATE $wpdb->comments SET comment_content = '$comment_content', comment_author = '$comment_author' WHERE comment_ID = '$comment->comment_ID'");
45692 }
45693 }
45694
45695 // Remove extraneous backslashes.
45696 $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
45697 if ($links) {
45698 foreach($links as $link) {
45699 $link_name = addslashes(deslash($link->link_name));
45700 $link_description = addslashes(deslash($link->link_description));
45701 $wpdb->query("UPDATE $wpdb->links SET link_name = '$link_name', link_description = '$link_description' WHERE link_id = '$link->link_id'");
45702 }
45703 }
45704
45705 // The "paged" option for what_to_show is no more.
45706 if ($wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'what_to_show'") == 'paged') {
45707 $wpdb->query("UPDATE $wpdb->options SET option_value = 'posts' WHERE option_name = 'what_to_show'");
45708 }
45709
45710 $active_plugins = __get_option('active_plugins');
45711
45712 // If plugins are not stored in an array, they're stored in the old
45713 // newline separated format. Convert to new format.
45714 if ( !is_array( $active_plugins ) ) {
45715 $active_plugins = explode("\n", trim($active_plugins));
45716 update_option('active_plugins', $active_plugins);
45717 }
45718
45719 // Obsolete tables
45720 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues');
45721 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes');
45722 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups');
45723 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options');
45724
45725 // Update comments table to use comment_type
45726 $wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'");
45727 $wpdb->query("UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'");
45728
45729 // Some versions have multiple duplicate option_name rows with the same values
45730 $options = $wpdb->get_results("SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name");
45731 foreach ( $options as $option ) {
45732 if ( 1 != $option->dupes ) { // Could this be done in the query?
45733 $limit = $option->dupes - 1;
45734 $dupe_ids = $wpdb->get_col( $wpdb->prepare("SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit) );
45735 $dupe_ids = join($dupe_ids, ',');
45736 $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)");
45737 }
45738 }
45739
45740 make_site_theme();
45741 }
45742
45743 /**
45744 * Execute changes made in WordPress 2.0.
45745 *
45746 * @since 2.0.0
45747 */
45748 function upgrade_160() {
45749 global $wpdb, $wp_current_db_version;
45750
45751 populate_roles_160();
45752
45753 $users = $wpdb->get_results("SELECT * FROM $wpdb->users");
45754 foreach ( $users as $user ) :
45755 if ( !empty( $user->user_firstname ) )
45756 update_usermeta( $user->ID, 'first_name', $wpdb->escape($user->user_firstname) );
45757 if ( !empty( $user->user_lastname ) )
45758 update_usermeta( $user->ID, 'last_name', $wpdb->escape($user->user_lastname) );
45759 if ( !empty( $user->user_nickname ) )
45760 update_usermeta( $user->ID, 'nickname', $wpdb->escape($user->user_nickname) );
45761 if ( !empty( $user->user_level ) )
45762 update_usermeta( $user->ID, $wpdb->prefix . 'user_level', $user->user_level );
45763 if ( !empty( $user->user_icq ) )
45764 update_usermeta( $user->ID, 'icq', $wpdb->escape($user->user_icq) );
45765 if ( !empty( $user->user_aim ) )
45766 update_usermeta( $user->ID, 'aim', $wpdb->escape($user->user_aim) );
45767 if ( !empty( $user->user_msn ) )
45768 update_usermeta( $user->ID, 'msn', $wpdb->escape($user->user_msn) );
45769 if ( !empty( $user->user_yim ) )
45770 update_usermeta( $user->ID, 'yim', $wpdb->escape($user->user_icq) );
45771 if ( !empty( $user->user_description ) )
45772 update_usermeta( $user->ID, 'description', $wpdb->escape($user->user_description) );
45773
45774 if ( isset( $user->user_idmode ) ):
45775 $idmode = $user->user_idmode;
45776 if ($idmode == 'nickname') $id = $user->user_nickname;
45777 if ($idmode == 'login') $id = $user->user_login;
45778 if ($idmode == 'firstname') $id = $user->user_firstname;
45779 if ($idmode == 'lastname') $id = $user->user_lastname;
45780 if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname;
45781 if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname;
45782 if (!$idmode) $id = $user->user_nickname;
45783 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->users SET display_name = %s WHERE ID = %d", $id, $user->ID) );
45784 endif;
45785
45786 // FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set.
45787 $caps = get_usermeta( $user->ID, $wpdb->prefix . 'capabilities');
45788 if ( empty($caps) || defined('RESET_CAPS') ) {
45789 $level = get_usermeta($user->ID, $wpdb->prefix . 'user_level');
45790 $role = translate_level_to_role($level);
45791 update_usermeta( $user->ID, $wpdb->prefix . 'capabilities', array($role => true) );
45792 }
45793
45794 endforeach;
45795 $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' );
45796 $wpdb->hide_errors();
45797 foreach ( $old_user_fields as $old )
45798 $wpdb->query("ALTER TABLE $wpdb->users DROP $old");
45799 $wpdb->show_errors();
45800
45801 // populate comment_count field of posts table
45802 $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" );
45803 if( is_array( $comments ) ) {
45804 foreach ($comments as $comment) {
45805 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET comment_count = %d WHERE ID = %d", $comment->c, $comment->comment_post_ID) );
45806 }
45807 }
45808
45809 // Some alpha versions used a post status of object instead of attachment and put
45810 // the mime type in post_type instead of post_mime_type.
45811 if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) {
45812 $objects = $wpdb->get_results("SELECT ID, post_type FROM $wpdb->posts WHERE post_status = 'object'");
45813 foreach ($objects as $object) {
45814 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'attachment',
45815 post_mime_type = %s,
45816 post_type = ''
45817 WHERE ID = %d", $object->post_type, $object->ID) );
45818
45819 $meta = get_post_meta($object->ID, 'imagedata', true);
45820 if ( ! empty($meta['file']) )
45821 update_attached_file( $object->ID, $meta['file'] );
45822 }
45823 }
45824 }
45825
45826 /**
45827 * Execute changes made in WordPress 2.1.
45828 *
45829 * @since 2.1.0
45830 */
45831 function upgrade_210() {
45832 global $wpdb, $wp_current_db_version;
45833
45834 if ( $wp_current_db_version < 3506 ) {
45835 // Update status and type.
45836 $posts = $wpdb->get_results("SELECT ID, post_status FROM $wpdb->posts");
45837
45838 if ( ! empty($posts) ) foreach ($posts as $post) {
45839 $status = $post->post_status;
45840 $type = 'post';
45841
45842 if ( 'static' == $status ) {
45843 $status = 'publish';
45844 $type = 'page';
45845 } else if ( 'attachment' == $status ) {
45846 $status = 'inherit';
45847 $type = 'attachment';
45848 }
45849
45850 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID) );
45851 }
45852 }
45853
45854 if ( $wp_current_db_version < 3845 ) {
45855 populate_roles_210();
45856 }
45857
45858 if ( $wp_current_db_version < 3531 ) {
45859 // Give future posts a post_status of future.
45860 $now = gmdate('Y-m-d H:i:59');
45861 $wpdb->query ("UPDATE $wpdb->posts SET post_status = 'future' WHERE post_status = 'publish' AND post_date_gmt > '$now'");
45862
45863 $posts = $wpdb->get_results("SELECT ID, post_date FROM $wpdb->posts WHERE post_status ='future'");
45864 if ( !empty($posts) )
45865 foreach ( $posts as $post )
45866 wp_schedule_single_event(mysql2date('U', $post->post_date), 'publish_future_post', array($post->ID));
45867 }
45868 }
45869
45870 /**
45871 * Execute changes made in WordPress 2.3.
45872 *
45873 * @since 2.3.0
45874 */
45875 function upgrade_230() {
45876 global $wp_current_db_version, $wpdb;
45877
45878 if ( $wp_current_db_version < 5200 ) {
45879 populate_roles_230();
45880 }
45881
45882 // Convert categories to terms.
45883 $tt_ids = array();
45884 $have_tags = false;
45885 $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_ID");
45886 foreach ($categories as $category) {
45887 $term_id = (int) $category->cat_ID;
45888 $name = $category->cat_name;
45889 $description = $category->category_description;
45890 $slug = $category->category_nicename;
45891 $parent = $category->category_parent;
45892 $term_group = 0;
45893
45894 // Associate terms with the same slug in a term group and make slugs unique.
45895 if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
45896 $term_group = $exists[0]->term_group;
45897 $id = $exists[0]->term_id;
45898 $num = 2;
45899 do {
45900 $alt_slug = $slug . "-$num";
45901 $num++;
45902 $slug_check = $wpdb->get_var( $wpdb->prepare("SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug) );
45903 } while ( $slug_check );
45904
45905 $slug = $alt_slug;
45906
45907 if ( empty( $term_group ) ) {
45908 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1;
45909 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $id) );
45910 }
45911 }
45912
45913 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES
45914 (%d, %s, %s, %d)", $term_id, $name, $slug, $term_group) );
45915
45916 $count = 0;
45917 if ( !empty($category->category_count) ) {
45918 $count = (int) $category->category_count;
45919 $taxonomy = 'category';
45920 $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) );
45921 $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
45922 }
45923
45924 if ( !empty($category->link_count) ) {
45925 $count = (int) $category->link_count;
45926 $taxonomy = 'link_category';
45927 $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) );
45928 $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
45929 }
45930
45931 if ( !empty($category->tag_count) ) {
45932 $have_tags = true;
45933 $count = (int) $category->tag_count;
45934 $taxonomy = 'post_tag';
45935 $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) );
45936 $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
45937 }
45938
45939 if ( empty($count) ) {
45940 $count = 0;
45941 $taxonomy = 'category';
45942 $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) );
45943 $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
45944 }
45945 }
45946
45947 $select = 'post_id, category_id';
45948 if ( $have_tags )
45949 $select .= ', rel_type';
45950
45951 $posts = $wpdb->get_results("SELECT $select FROM $wpdb->post2cat GROUP BY post_id, category_id");
45952 foreach ( $posts as $post ) {
45953 $post_id = (int) $post->post_id;
45954 $term_id = (int) $post->category_id;
45955 $taxonomy = 'category';
45956 if ( !empty($post->rel_type) && 'tag' == $post->rel_type)
45957 $taxonomy = 'tag';
45958 $tt_id = $tt_ids[$term_id][$taxonomy];
45959 if ( empty($tt_id) )
45960 continue;
45961
45962 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $post_id, $tt_id) );
45963 }
45964
45965 // < 3570 we used linkcategories. >= 3570 we used categories and link2cat.
45966 if ( $wp_current_db_version < 3570 ) {
45967 // Create link_category terms for link categories. Create a map of link cat IDs
45968 // to link_category terms.
45969 $link_cat_id_map = array();
45970 $default_link_cat = 0;
45971 $tt_ids = array();
45972 $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->prefix . 'linkcategories');
45973 foreach ( $link_cats as $category) {
45974 $cat_id = (int) $category->cat_id;
45975 $term_id = 0;
45976 $name = $wpdb->escape($category->cat_name);
45977 $slug = sanitize_title($name);
45978 $term_group = 0;
45979
45980 // Associate terms with the same slug in a term group and make slugs unique.
45981 if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
45982 $term_group = $exists[0]->term_group;
45983 $term_id = $exists[0]->term_id;
45984 }
45985
45986 if ( empty($term_id) ) {
45987 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES (%s, %s, %d)", $name, $slug, $term_group) );
45988 $term_id = (int) $wpdb->insert_id;
45989 }
45990
45991 $link_cat_id_map[$cat_id] = $term_id;
45992 $default_link_cat = $term_id;
45993
45994 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES (%d, 'link_category', '', '0', '0')", $term_id) );
45995 $tt_ids[$term_id] = (int) $wpdb->insert_id;
45996 }
45997
45998 // Associate links to cats.
45999 $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links");
46000 if ( !empty($links) ) foreach ( $links as $link ) {
46001 if ( 0 == $link->link_category )
46002 continue;
46003 if ( ! isset($link_cat_id_map[$link->link_category]) )
46004 continue;
46005 $term_id = $link_cat_id_map[$link->link_category];
46006 $tt_id = $tt_ids[$term_id];
46007 if ( empty($tt_id) )
46008 continue;
46009
46010 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $link->link_id, $tt_id) );
46011 }
46012
46013 // Set default to the last category we grabbed during the upgrade loop.
46014 update_option('default_link_category', $default_link_cat);
46015 } else {
46016 $links = $wpdb->get_results("SELECT link_id, category_id FROM $wpdb->link2cat GROUP BY link_id, category_id");
46017 foreach ( $links as $link ) {
46018 $link_id = (int) $link->link_id;
46019 $term_id = (int) $link->category_id;
46020 $taxonomy = 'link_category';
46021 $tt_id = $tt_ids[$term_id][$taxonomy];
46022 if ( empty($tt_id) )
46023 continue;
46024
46025 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $link_id, $tt_id) );
46026 }
46027 }
46028
46029 if ( $wp_current_db_version < 4772 ) {
46030 // Obsolete linkcategories table
46031 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories');
46032 }
46033
46034 // Recalculate all counts
46035 $terms = $wpdb->get_results("SELECT term_taxonomy_id, taxonomy FROM $wpdb->term_taxonomy");
46036 foreach ( (array) $terms as $term ) {
46037 if ( ('post_tag' == $term->taxonomy) || ('category' == $term->taxonomy) )
46038 $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) );
46039 else
46040 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term->term_taxonomy_id) );
46041 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_taxonomy_id = %d", $count, $term->term_taxonomy_id) );
46042 }
46043 }
46044
46045 /**
46046 * Remove old options from the database.
46047 *
46048 * @since 2.3.0
46049 */
46050 function upgrade_230_options_table() {
46051 global $wpdb;
46052 $old_options_fields = array( 'option_can_override', 'option_type', 'option_width', 'option_height', 'option_description', 'option_admin_level' );
46053 $wpdb->hide_errors();
46054 foreach ( $old_options_fields as $old )
46055 $wpdb->query("ALTER TABLE $wpdb->options DROP $old");
46056 $wpdb->show_errors();
46057 }
46058
46059 /**
46060 * Remove old categories, link2cat, and post2cat database tables.
46061 *
46062 * @since 2.3.0
46063 */
46064 function upgrade_230_old_tables() {
46065 global $wpdb;
46066 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'categories');
46067 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'link2cat');
46068 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'post2cat');
46069 }
46070
46071 /**
46072 * Upgrade old slugs made in version 2.2.
46073 *
46074 * @since 2.2.0
46075 */
46076 function upgrade_old_slugs() {
46077 // upgrade people who were using the Redirect Old Slugs plugin
46078 global $wpdb;
46079 $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'");
46080 }
46081
46082 /**
46083 * Execute changes made in WordPress 2.5.0.
46084 *
46085 * @since 2.5.0
46086 */
46087 function upgrade_250() {
46088 global $wp_current_db_version;
46089
46090 if ( $wp_current_db_version < 6689 ) {
46091 populate_roles_250();
46092 }
46093
46094 }
46095
46096 /**
46097 * Execute changes made in WordPress 2.5.1.
46098 *
46099 * @since 2.5.1
46100 */
46101 function upgrade_251() {
46102 global $wp_current_db_version;
46103
46104 // Make the secret longer
46105 update_option('secret', wp_generate_password(64));
46106 }
46107
46108 /**
46109 * Execute changes made in WordPress 2.5.2.
46110 *
46111 * @since 2.5.2
46112 */
46113 function upgrade_252() {
46114 global $wpdb;
46115
46116 $wpdb->query("UPDATE $wpdb->users SET user_activation_key = ''");
46117 }
46118
46119 /**
46120 * Execute changes made in WordPress 2.6.
46121 *
46122 * @since 2.6.0
46123 */
46124 function upgrade_260() {
46125 global $wp_current_db_version;
46126
46127 if ( $wp_current_db_version < 8000 )
46128 populate_roles_260();
46129
46130 if ( $wp_current_db_version < 8201 ) {
46131 update_option('enable_app', 1);
46132 update_option('enable_xmlrpc', 1);
46133 }
46134 }
46135
46136 /**
46137 * Execute changes made in WordPress 2.7.
46138 *
46139 * @since 2.7.0
46140 */
46141 function upgrade_270() {
46142 global $wpdb, $wp_current_db_version;
46143
46144 if ( $wp_current_db_version < 8980 )
46145 populate_roles_270();
46146
46147 // Update post_date for unpublished posts with empty timestamp
46148 if ( $wp_current_db_version < 8921 )
46149 $wpdb->query( "UPDATE $wpdb->posts SET post_date = post_modified WHERE post_date = '0000-00-00 00:00:00'" );
46150 }
46151
46152
46153 // The functions we use to actually do stuff
46154
46155 // General
46156
46157 /**
46158 * {@internal Missing Short Description}}
46159 *
46160 * {@internal Missing Long Description}}
46161 *
46162 * @since unknown
46163 *
46164 * @param string $table_name Database table name to create.
46165 * @param string $create_ddl SQL statement to create table.
46166 * @return bool If table already exists or was created by function.
46167 */
46168 function maybe_create_table($table_name, $create_ddl) {
46169 global $wpdb;
46170 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
46171 if ($table == $table_name) {
46172 return true;
46173 }
46174 }
46175 //didn't find it try to create it.
46176 $q = $wpdb->query($create_ddl);
46177 // we cannot directly tell that whether this succeeded!
46178 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
46179 if ($table == $table_name) {
46180 return true;
46181 }
46182 }
46183 return false;
46184 }
46185
46186 /**
46187 * {@internal Missing Short Description}}
46188 *
46189 * {@internal Missing Long Description}}
46190 *
46191 * @since unknown
46192 *
46193 * @param string $table Database table name.
46194 * @param string $index Index name to drop.
46195 * @return bool True, when finished.
46196 */
46197 function drop_index($table, $index) {
46198 global $wpdb;
46199 $wpdb->hide_errors();
46200 $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`");
46201 // Now we need to take out all the extra ones we may have created
46202 for ($i = 0; $i < 25; $i++) {
46203 $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`");
46204 }
46205 $wpdb->show_errors();
46206 return true;
46207 }
46208
46209 /**
46210 * {@internal Missing Short Description}}
46211 *
46212 * {@internal Missing Long Description}}
46213 *
46214 * @since unknown
46215 *
46216 * @param string $table Database table name.
46217 * @param string $index Database table index column.
46218 * @return bool True, when done with execution.
46219 */
46220 function add_clean_index($table, $index) {
46221 global $wpdb;
46222 drop_index($table, $index);
46223 $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )");
46224 return true;
46225 }
46226
46227 /**
46228 ** maybe_add_column()
46229 ** Add column to db table if it doesn't exist.
46230 ** Returns: true if already exists or on successful completion
46231 ** false on error
46232 */
46233 function maybe_add_column($table_name, $column_name, $create_ddl) {
46234 global $wpdb, $debug;
46235 foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
46236 if ($debug) echo("checking $column == $column_name<br />");
46237 if ($column == $column_name) {
46238 return true;
46239 }
46240 }
46241 //didn't find it try to create it.
46242 $q = $wpdb->query($create_ddl);
46243 // we cannot directly tell that whether this succeeded!
46244 foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
46245 if ($column == $column_name) {
46246 return true;
46247 }
46248 }
46249 return false;
46250 }
46251
46252 /**
46253 * Retrieve all options as it was for 1.2.
46254 *
46255 * @since 1.2.0
46256 *
46257 * @return array List of options.
46258 */
46259 function get_alloptions_110() {
46260 global $wpdb;
46261 if ($options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options")) {
46262 foreach ($options as $option) {
46263 // "When trying to design a foolproof system,
46264 // never underestimate the ingenuity of the fools :)" -- Dougal
46265 if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
46266 if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
46267 if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
46268 $all_options->{$option->option_name} = stripslashes($option->option_value);
46269 }
46270 }
46271 return $all_options;
46272 }
46273
46274 /**
46275 * Version of get_option that is private to install/upgrade.
46276 *
46277 * @since unknown
46278 * @access private
46279 *
46280 * @param string $setting Option name.
46281 * @return mixed
46282 */
46283 function __get_option($setting) {
46284 global $wpdb;
46285
46286 if ( $setting == 'home' && defined( 'WP_HOME' ) ) {
46287 return preg_replace( '|/+$|', '', constant( 'WP_HOME' ) );
46288 }
46289
46290 if ( $setting == 'siteurl' && defined( 'WP_SITEURL' ) ) {
46291 return preg_replace( '|/+$|', '', constant( 'WP_SITEURL' ) );
46292 }
46293
46294 $option = $wpdb->get_var( $wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting) );
46295
46296 if ( 'home' == $setting && '' == $option )
46297 return __get_option('siteurl');
46298
46299 if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
46300 $option = preg_replace('|/+$|', '', $option);
46301
46302 @ $kellogs = unserialize($option);
46303 if ($kellogs !== FALSE)
46304 return $kellogs;
46305 else
46306 return $option;
46307 }
46308
46309 /**
46310 * {@internal Missing Short Description}}
46311 *
46312 * {@internal Missing Long Description}}
46313 *
46314 * @since unknown
46315 *
46316 * @param string $content
46317 * @return string
46318 */
46319 function deslash($content) {
46320 // Note: \\\ inside a regex denotes a single backslash.
46321
46322 // Replace one or more backslashes followed by a single quote with
46323 // a single quote.
46324 $content = preg_replace("/\\\+'/", "'", $content);
46325
46326 // Replace one or more backslashes followed by a double quote with
46327 // a double quote.
46328 $content = preg_replace('/\\\+"/', '"', $content);
46329
46330 // Replace one or more backslashes with one backslash.
46331 $content = preg_replace("/\\\+/", "\\", $content);
46332
46333 return $content;
46334 }
46335
46336 /**
46337 * {@internal Missing Short Description}}
46338 *
46339 * {@internal Missing Long Description}}
46340 *
46341 * @since unknown
46342 *
46343 * @param unknown_type $queries
46344 * @param unknown_type $execute
46345 * @return unknown
46346 */
46347 function dbDelta($queries, $execute = true) {
46348 global $wpdb;
46349
46350 // Separate individual queries into an array
46351 if( !is_array($queries) ) {
46352 $queries = explode( ';', $queries );
46353 if('' == $queries[count($queries) - 1]) array_pop($queries);
46354 }
46355
46356 $cqueries = array(); // Creation Queries
46357 $iqueries = array(); // Insertion Queries
46358 $for_update = array();
46359
46360 // Create a tablename index for an array ($cqueries) of queries
46361 foreach($queries as $qry) {
46362 if(preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) {
46363 $cqueries[strtolower($matches[1])] = $qry;
46364 $for_update[$matches[1]] = 'Created table '.$matches[1];
46365 }
46366 else if(preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) {
46367 array_unshift($cqueries, $qry);
46368 }
46369 else if(preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) {
46370 $iqueries[] = $qry;
46371 }
46372 else if(preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) {
46373 $iqueries[] = $qry;
46374 }
46375 else {
46376 // Unrecognized query type
46377 }
46378 }
46379
46380 // Check to see which tables and fields exist
46381 if($tables = $wpdb->get_col('SHOW TABLES;')) {
46382 // For every table in the database
46383 foreach($tables as $table) {
46384 // If a table query exists for the database table...
46385 if( array_key_exists(strtolower($table), $cqueries) ) {
46386 // Clear the field and index arrays
46387 unset($cfields);
46388 unset($indices);
46389 // Get all of the field names in the query from between the parens
46390 preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2);
46391 $qryline = trim($match2[1]);
46392
46393 // Separate field lines into an array
46394 $flds = explode("\n", $qryline);
46395
46396 //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>";
46397
46398 // For every field line specified in the query
46399 foreach($flds as $fld) {
46400 // Extract the field name
46401 preg_match("|^([^ ]*)|", trim($fld), $fvals);
46402 $fieldname = $fvals[1];
46403
46404 // Verify the found field name
46405 $validfield = true;
46406 switch(strtolower($fieldname))
46407 {
46408 case '':
46409 case 'primary':
46410 case 'index':
46411 case 'fulltext':
46412 case 'unique':
46413 case 'key':
46414 $validfield = false;
46415 $indices[] = trim(trim($fld), ", \n");
46416 break;
46417 }
46418 $fld = trim($fld);
46419
46420 // If it's a valid field, add it to the field array
46421 if($validfield) {
46422 $cfields[strtolower($fieldname)] = trim($fld, ", \n");
46423 }
46424 }
46425
46426 // Fetch the table column structure from the database
46427 $tablefields = $wpdb->get_results("DESCRIBE {$table};");
46428
46429 // For every field in the table
46430 foreach($tablefields as $tablefield) {
46431 // If the table field exists in the field array...
46432 if(array_key_exists(strtolower($tablefield->Field), $cfields)) {
46433 // Get the field type from the query
46434 preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches);
46435 $fieldtype = $matches[1];
46436
46437 // Is actual field type different from the field type in query?
46438 if($tablefield->Type != $fieldtype) {
46439 // Add a query to change the column type
46440 $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
46441 $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
46442 }
46443
46444 // Get the default value from the array
46445 //echo "{$cfields[strtolower($tablefield->Field)]}<br>";
46446 if(preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) {
46447 $default_value = $matches[1];
46448 if($tablefield->Default != $default_value)
46449 {
46450 // Add a query to change the column's default value
46451 $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
46452 $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
46453 }
46454 }
46455
46456 // Remove the field from the array (so it's not added)
46457 unset($cfields[strtolower($tablefield->Field)]);
46458 }
46459 else {
46460 // This field exists in the table, but not in the creation queries?
46461 }
46462 }
46463
46464 // For every remaining field specified for the table
46465 foreach($cfields as $fieldname => $fielddef) {
46466 // Push a query line into $cqueries that adds the field to that table
46467 $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef";
46468 $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname;
46469 }
46470
46471 // Index stuff goes here
46472 // Fetch the table index structure from the database
46473 $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
46474
46475 if($tableindices) {
46476 // Clear the index array
46477 unset($index_ary);
46478
46479 // For every index in the table
46480 foreach($tableindices as $tableindex) {
46481 // Add the index to the index data array
46482 $keyname = $tableindex->Key_name;
46483 $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
46484 $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
46485 }
46486
46487 // For each actual index in the index array
46488 foreach($index_ary as $index_name => $index_data) {
46489 // Build a create string to compare to the query
46490 $index_string = '';
46491 if($index_name == 'PRIMARY') {
46492 $index_string .= 'PRIMARY ';
46493 }
46494 else if($index_data['unique']) {
46495 $index_string .= 'UNIQUE ';
46496 }
46497 $index_string .= 'KEY ';
46498 if($index_name != 'PRIMARY') {
46499 $index_string .= $index_name;
46500 }
46501 $index_columns = '';
46502 // For each column in the index
46503 foreach($index_data['columns'] as $column_data) {
46504 if($index_columns != '') $index_columns .= ',';
46505 // Add the field to the column list string
46506 $index_columns .= $column_data['fieldname'];
46507 if($column_data['subpart'] != '') {
46508 $index_columns .= '('.$column_data['subpart'].')';
46509 }
46510 }
46511 // Add the column list to the index create string
46512 $index_string .= ' ('.$index_columns.')';
46513 if(!(($aindex = array_search($index_string, $indices)) === false)) {
46514 unset($indices[$aindex]);
46515 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";
46516 }
46517 //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";
46518 }
46519 }
46520
46521 // For every remaining index specified for the table
46522 foreach ( (array) $indices as $index ) {
46523 // Push a query line into $cqueries that adds the index to that table
46524 $cqueries[] = "ALTER TABLE {$table} ADD $index";
46525 $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index;
46526 }
46527
46528 // Remove the original table creation query from processing
46529 unset($cqueries[strtolower($table)]);
46530 unset($for_update[strtolower($table)]);
46531 } else {
46532 // This table exists in the database, but not in the creation queries?
46533 }
46534 }
46535 }
46536
46537 $allqueries = array_merge($cqueries, $iqueries);
46538 if($execute) {
46539 foreach($allqueries as $query) {
46540 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n";
46541 $wpdb->query($query);
46542 }
46543 }
46544
46545 return $for_update;
46546 }
46547
46548 /**
46549 * {@internal Missing Short Description}}
46550 *
46551 * {@internal Missing Long Description}}
46552 *
46553 * @since unknown
46554 */
46555 function make_db_current() {
46556 global $wp_queries;
46557
46558 $alterations = dbDelta($wp_queries);
46559 echo "<ol>\n";
46560 foreach($alterations as $alteration) echo "<li>$alteration</li>\n";
46561 echo "</ol>\n";
46562 }
46563
46564 /**
46565 * {@internal Missing Short Description}}
46566 *
46567 * {@internal Missing Long Description}}
46568 *
46569 * @since unknown
46570 */
46571 function make_db_current_silent() {
46572 global $wp_queries;
46573
46574 $alterations = dbDelta($wp_queries);
46575 }
46576
46577 /**
46578 * {@internal Missing Short Description}}
46579 *
46580 * {@internal Missing Long Description}}
46581 *
46582 * @since unknown
46583 *
46584 * @param unknown_type $theme_name
46585 * @param unknown_type $template
46586 * @return unknown
46587 */
46588 function make_site_theme_from_oldschool($theme_name, $template) {
46589 $home_path = get_home_path();
46590 $site_dir = WP_CONTENT_DIR . "/themes/$template";
46591
46592 if (! file_exists("$home_path/index.php"))
46593 return false;
46594
46595 // Copy files from the old locations to the site theme.
46596 // TODO: This does not copy arbitarary include dependencies. Only the
46597 // standard WP files are copied.
46598 $files = array('index.php' => 'index.php', 'wp-layout.css' => 'style.css', 'wp-comments.php' => 'comments.php', 'wp-comments-popup.php' => 'comments-popup.php');
46599
46600 foreach ($files as $oldfile => $newfile) {
46601 if ($oldfile == 'index.php')
46602 $oldpath = $home_path;
46603 else
46604 $oldpath = ABSPATH;
46605
46606 if ($oldfile == 'index.php') { // Check to make sure it's not a new index
46607 $index = implode('', file("$oldpath/$oldfile"));
46608 if (strpos($index, 'WP_USE_THEMES') !== false) {
46609 if (! @copy(WP_CONTENT_DIR . '/themes/default/index.php', "$site_dir/$newfile"))
46610 return false;
46611 continue; // Don't copy anything
46612 }
46613 }
46614
46615 if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile"))
46616 return false;
46617
46618 chmod("$site_dir/$newfile", 0777);
46619
46620 // Update the blog header include in each file.
46621 $lines = explode("\n", implode('', file("$site_dir/$newfile")));
46622 if ($lines) {
46623 $f = fopen("$site_dir/$newfile", 'w');
46624
46625 foreach ($lines as $line) {
46626 if (preg_match('/require.*wp-blog-header/', $line))
46627 $line = '//' . $line;
46628
46629 // Update stylesheet references.
46630 $line = str_replace("<?php echo __get_option('siteurl'); ?>/wp-layout.css", "<?php bloginfo('stylesheet_url'); ?>", $line);
46631
46632 // Update comments template inclusion.
46633 $line = str_replace("<?php include(ABSPATH . 'wp-comments.php'); ?>", "<?php comments_template(); ?>", $line);
46634
46635 fwrite($f, "{$line}\n");
46636 }
46637 fclose($f);
46638 }
46639 }
46640
46641 // Add a theme header.
46642 $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";
46643
46644 $stylelines = file_get_contents("$site_dir/style.css");
46645 if ($stylelines) {
46646 $f = fopen("$site_dir/style.css", 'w');
46647
46648 fwrite($f, $header);
46649 fwrite($f, $stylelines);
46650 fclose($f);
46651 }
46652
46653 return true;
46654 }
46655
46656 /**
46657 * {@internal Missing Short Description}}
46658 *
46659 * {@internal Missing Long Description}}
46660 *
46661 * @since unknown
46662 *
46663 * @param unknown_type $theme_name
46664 * @param unknown_type $template
46665 * @return unknown
46666 */
46667 function make_site_theme_from_default($theme_name, $template) {
46668 $site_dir = WP_CONTENT_DIR . "/themes/$template";
46669 $default_dir = WP_CONTENT_DIR . '/themes/default';
46670
46671 // Copy files from the default theme to the site theme.
46672 //$files = array('index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css');
46673
46674 $theme_dir = @ opendir("$default_dir");
46675 if ($theme_dir) {
46676 while(($theme_file = readdir( $theme_dir )) !== false) {
46677 if (is_dir("$default_dir/$theme_file"))
46678 continue;
46679 if (! @copy("$default_dir/$theme_file", "$site_dir/$theme_file"))
46680 return;
46681 chmod("$site_dir/$theme_file", 0777);
46682 }
46683 }
46684 @closedir($theme_dir);
46685
46686 // Rewrite the theme header.
46687 $stylelines = explode("\n", implode('', file("$site_dir/style.css")));
46688 if ($stylelines) {
46689 $f = fopen("$site_dir/style.css", 'w');
46690
46691 foreach ($stylelines as $line) {
46692 if (strpos($line, 'Theme Name:') !== false) $line = 'Theme Name: ' . $theme_name;
46693 elseif (strpos($line, 'Theme URI:') !== false) $line = 'Theme URI: ' . __get_option('url');
46694 elseif (strpos($line, 'Description:') !== false) $line = 'Description: Your theme.';
46695 elseif (strpos($line, 'Version:') !== false) $line = 'Version: 1';
46696 elseif (strpos($line, 'Author:') !== false) $line = 'Author: You';
46697 fwrite($f, $line . "\n");
46698 }
46699 fclose($f);
46700 }
46701
46702 // Copy the images.
46703 umask(0);
46704 if (! mkdir("$site_dir/images", 0777)) {
46705 return false;
46706 }
46707
46708 $images_dir = @ opendir("$default_dir/images");
46709 if ($images_dir) {
46710 while(($image = readdir($images_dir)) !== false) {
46711 if (is_dir("$default_dir/images/$image"))
46712 continue;
46713 if (! @copy("$default_dir/images/$image", "$site_dir/images/$image"))
46714 return;
46715 chmod("$site_dir/images/$image", 0777);
46716 }
46717 }
46718 @closedir($images_dir);
46719 }
46720
46721 // Create a site theme from the default theme.
46722 /**
46723 * {@internal Missing Short Description}}
46724 *
46725 * {@internal Missing Long Description}}
46726 *
46727 * @since unknown
46728 *
46729 * @return unknown
46730 */
46731 function make_site_theme() {
46732 // Name the theme after the blog.
46733 $theme_name = __get_option('blogname');
46734 $template = sanitize_title($theme_name);
46735 $site_dir = WP_CONTENT_DIR . "/themes/$template";
46736
46737 // If the theme already exists, nothing to do.
46738 if ( is_dir($site_dir)) {
46739 return false;
46740 }
46741
46742 // We must be able to write to the themes dir.
46743 if (! is_writable(WP_CONTENT_DIR . "/themes")) {
46744 return false;
46745 }
46746
46747 umask(0);
46748 if (! mkdir($site_dir, 0777)) {
46749 return false;
46750 }
46751
46752 if (file_exists(ABSPATH . 'wp-layout.css')) {
46753 if (! make_site_theme_from_oldschool($theme_name, $template)) {
46754 // TODO: rm -rf the site theme directory.
46755 return false;
46756 }
46757 } else {
46758 if (! make_site_theme_from_default($theme_name, $template))
46759 // TODO: rm -rf the site theme directory.
46760 return false;
46761 }
46762
46763 // Make the new site theme active.
46764 $current_template = __get_option('template');
46765 if ($current_template == 'default') {
46766 update_option('template', $template);
46767 update_option('stylesheet', $template);
46768 }
46769 return $template;
46770 }
46771
46772 /**
46773 * Translate user level to user role name.
46774 *
46775 * @since unknown
46776 *
46777 * @param int $level User level.
46778 * @return string User role name.
46779 */
46780 function translate_level_to_role($level) {
46781 switch ($level) {
46782 case 10:
46783 case 9:
46784 case 8:
46785 return 'administrator';
46786 case 7:
46787 case 6:
46788 case 5:
46789 return 'editor';
46790 case 4:
46791 case 3:
46792 case 2:
46793 return 'author';
46794 case 1:
46795 return 'contributor';
46796 case 0:
46797 return 'subscriber';
46798 }
46799 }
46800
46801 /**
46802 * {@internal Missing Short Description}}
46803 *
46804 * {@internal Missing Long Description}}
46805 *
46806 * @since unknown
46807 */
46808 function wp_check_mysql_version() {
46809 global $wpdb;
46810 $result = $wpdb->check_database_version();
46811 if ( is_wp_error( $result ) )
46812 die( $result->get_error_message() );
46813 }
46814
46815 /**
46816 * {@internal Missing Short Description}}
46817 *
46818 * {@internal Missing Long Description}}
46819 *
46820 * @since unknown
46821 */
46822 function maybe_disable_automattic_widgets() {
46823 $plugins = __get_option( 'active_plugins' );
46824
46825 foreach ( (array) $plugins as $plugin ) {
46826 if ( basename( $plugin ) == 'widgets.php' ) {
46827 array_splice( $plugins, array_search( $plugin, $plugins ), 1 );
46828 update_option( 'active_plugins', $plugins );
46829 break;
46830 }
46831 }
46832 }
46833
46834 ?>