// Get the GMT offset, we'll use that later on.
$all_options = get_alloptions_110();
$time_difference = $all_options->time_difference;
$server_time = time() + gmdate( 'Z' );
$weblogger_time = $server_time + $time_difference * HOUR_IN_SECONDS;
$diff_gmt_server = ( $gmt_time - $server_time ) / HOUR_IN_SECONDS;
$diff_weblogger_server = ( $weblogger_time - $server_time ) / HOUR_IN_SECONDS;
$diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server;
$gmt_offset = -$diff_gmt_weblogger;
// Add a gmt_offset option, with value $gmt_offset.
add_option( 'gmt_offset', $gmt_offset );
* Check if we already set the GMT fields. If we did, then
* MAX(post_date_gmt) can't be '0000-00-00 00:00:00'.
* <michel_v> I just slapped myself silly for not thinking about it earlier.
$got_gmt_fields = ( '0000-00-00 00:00:00' !== $wpdb->get_var( "SELECT MAX(post_date_gmt) FROM $wpdb->posts" ) );
if ( ! $got_gmt_fields ) {
// Add or subtract time to all dates, to get GMT dates.
$add_hours = (int) $diff_gmt_weblogger;
$add_minutes = (int) ( 60 * ( $diff_gmt_weblogger - $add_hours ) );
$wpdb->query( "UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)" );
$wpdb->query( "UPDATE $wpdb->posts SET post_modified = post_date" );
$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'" );
$wpdb->query( "UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)" );
$wpdb->query( "UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)" );
* Execute changes made in WordPress 1.5.
* @global wpdb $wpdb WordPress database abstraction object.
// Remove extraneous backslashes.
$posts = $wpdb->get_results( "SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts" );
foreach ( $posts as $post ) {
$post_content = addslashes( deslash( $post->post_content ) );
$post_title = addslashes( deslash( $post->post_title ) );
$post_excerpt = addslashes( deslash( $post->post_excerpt ) );
if ( empty( $post->guid ) ) {
$guid = get_permalink( $post->ID );
$wpdb->update( $wpdb->posts, compact( 'post_title', 'post_content', 'post_excerpt', 'guid' ), array( 'ID' => $post->ID ) );
// Remove extraneous backslashes.
$comments = $wpdb->get_results( "SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments" );
foreach ( $comments as $comment ) {
$comment_content = deslash( $comment->comment_content );
$comment_author = deslash( $comment->comment_author );
$wpdb->update( $wpdb->comments, compact( 'comment_content', 'comment_author' ), array( 'comment_ID' => $comment->comment_ID ) );
// Remove extraneous backslashes.
$links = $wpdb->get_results( "SELECT link_id, link_name, link_description FROM $wpdb->links" );
foreach ( $links as $link ) {
$link_name = deslash( $link->link_name );
$link_description = deslash( $link->link_description );
$wpdb->update( $wpdb->links, compact( 'link_name', 'link_description' ), array( 'link_id' => $link->link_id ) );
$active_plugins = __get_option( 'active_plugins' );
* If plugins are not stored in an array, they're stored in the old
* newline separated format. Convert to new format.
if ( ! is_array( $active_plugins ) ) {
$active_plugins = explode( "\n", trim( $active_plugins ) );
update_option( 'active_plugins', $active_plugins );
$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues' );
$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes' );
$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups' );
$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options' );
// Update comments table to use comment_type.
$wpdb->query( "UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'" );
$wpdb->query( "UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'" );
// Some versions have multiple duplicate option_name rows with the same values.
$options = $wpdb->get_results( "SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name" );
foreach ( $options as $option ) {
if ( 1 != $option->dupes ) { // Could this be done in the query?
$limit = $option->dupes - 1;
$dupe_ids = $wpdb->get_col( $wpdb->prepare( "SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit ) );
$dupe_ids = implode( ',', $dupe_ids );
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)" );
* Execute changes made in WordPress 2.0.
* @global wpdb $wpdb WordPress database abstraction object.
* @global int $wp_current_db_version The old (current) database version.
global $wpdb, $wp_current_db_version;
$users = $wpdb->get_results( "SELECT * FROM $wpdb->users" );
foreach ( $users as $user ) :
if ( ! empty( $user->user_firstname ) ) {
update_user_meta( $user->ID, 'first_name', wp_slash( $user->user_firstname ) );
if ( ! empty( $user->user_lastname ) ) {
update_user_meta( $user->ID, 'last_name', wp_slash( $user->user_lastname ) );
if ( ! empty( $user->user_nickname ) ) {
update_user_meta( $user->ID, 'nickname', wp_slash( $user->user_nickname ) );
if ( ! empty( $user->user_level ) ) {
update_user_meta( $user->ID, $wpdb->prefix . 'user_level', $user->user_level );
if ( ! empty( $user->user_icq ) ) {
update_user_meta( $user->ID, 'icq', wp_slash( $user->user_icq ) );
if ( ! empty( $user->user_aim ) ) {
update_user_meta( $user->ID, 'aim', wp_slash( $user->user_aim ) );
if ( ! empty( $user->user_msn ) ) {
update_user_meta( $user->ID, 'msn', wp_slash( $user->user_msn ) );
if ( ! empty( $user->user_yim ) ) {
update_user_meta( $user->ID, 'yim', wp_slash( $user->user_icq ) );
if ( ! empty( $user->user_description ) ) {
update_user_meta( $user->ID, 'description', wp_slash( $user->user_description ) );
if ( isset( $user->user_idmode ) ) :
$idmode = $user->user_idmode;
if ( 'nickname' === $idmode ) {
$id = $user->user_nickname;
if ( 'login' === $idmode ) {
if ( 'firstname' === $idmode ) {
$id = $user->user_firstname;
if ( 'lastname' === $idmode ) {
$id = $user->user_lastname;
if ( 'namefl' === $idmode ) {
$id = $user->user_firstname . ' ' . $user->user_lastname;
if ( 'namelf' === $idmode ) {
$id = $user->user_lastname . ' ' . $user->user_firstname;
$id = $user->user_nickname;
$wpdb->update( $wpdb->users, array( 'display_name' => $id ), array( 'ID' => $user->ID ) );
// FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set.
$caps = get_user_meta( $user->ID, $wpdb->prefix . 'capabilities' );
if ( empty( $caps ) || defined( 'RESET_CAPS' ) ) {
$level = get_user_meta( $user->ID, $wpdb->prefix . 'user_level', true );
$role = translate_level_to_role( $level );
update_user_meta( $user->ID, $wpdb->prefix . 'capabilities', array( $role => true ) );
$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' );
foreach ( $old_user_fields as $old ) {
$wpdb->query( "ALTER TABLE $wpdb->users DROP $old" );
// Populate comment_count field of posts table.
$comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" );
if ( is_array( $comments ) ) {
foreach ( $comments as $comment ) {
$wpdb->update( $wpdb->posts, array( 'comment_count' => $comment->c ), array( 'ID' => $comment->comment_post_ID ) );
* Some alpha versions used a post status of object instead of attachment
* and put the mime type in post_type instead of post_mime_type.
if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) {
$objects = $wpdb->get_results( "SELECT ID, post_type FROM $wpdb->posts WHERE post_status = 'object'" );
foreach ( $objects as $object ) {
'post_status' => 'attachment',
'post_mime_type' => $object->post_type,
array( 'ID' => $object->ID )
$meta = get_post_meta( $object->ID, 'imagedata', true );
if ( ! empty( $meta['file'] ) ) {
update_attached_file( $object->ID, $meta['file'] );
* Execute changes made in WordPress 2.1.
* @global int $wp_current_db_version The old (current) database version.
* @global wpdb $wpdb WordPress database abstraction object.
global $wp_current_db_version, $wpdb;
if ( $wp_current_db_version < 3506 ) {
// Update status and type.
$posts = $wpdb->get_results( "SELECT ID, post_status FROM $wpdb->posts" );
if ( ! empty( $posts ) ) {
foreach ( $posts as $post ) {
$status = $post->post_status;
if ( 'static' === $status ) {
} elseif ( 'attachment' === $status ) {
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID ) );
if ( $wp_current_db_version < 3845 ) {
if ( $wp_current_db_version < 3531 ) {
// Give future posts a post_status of future.
$now = gmdate( 'Y-m-d H:i:59' );
$wpdb->query( "UPDATE $wpdb->posts SET post_status = 'future' WHERE post_status = 'publish' AND post_date_gmt > '$now'" );
$posts = $wpdb->get_results( "SELECT ID, post_date FROM $wpdb->posts WHERE post_status ='future'" );
if ( ! empty( $posts ) ) {
foreach ( $posts as $post ) {
wp_schedule_single_event( mysql2date( 'U', $post->post_date, false ), 'publish_future_post', array( $post->ID ) );
* Execute changes made in WordPress 2.3.
* @global int $wp_current_db_version The old (current) database version.
* @global wpdb $wpdb WordPress database abstraction object.
global $wp_current_db_version, $wpdb;
if ( $wp_current_db_version < 5200 ) {
// Convert categories to terms.
$categories = $wpdb->get_results( "SELECT * FROM $wpdb->categories ORDER BY cat_ID" );
foreach ( $categories as $category ) {
$term_id = (int) $category->cat_ID;
$name = $category->cat_name;
$description = $category->category_description;
$slug = $category->category_nicename;
$parent = $category->category_parent;
// Associate terms with the same slug in a term group and make slugs unique.
$exists = $wpdb->get_results( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug ) );
$term_group = $exists[0]->term_group;
$id = $exists[0]->term_id;
$alt_slug = $slug . "-$num";
$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
if ( empty( $term_group ) ) {
$term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group" ) + 1;
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $id ) );
"INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES
if ( ! empty( $category->category_count ) ) {
$count = (int) $category->category_count;
$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 ) );
$tt_ids[ $term_id ][ $taxonomy ] = (int) $wpdb->insert_id;
if ( ! empty( $category->link_count ) ) {
$count = (int) $category->link_count;
$taxonomy = 'link_category';
$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 ) );
$tt_ids[ $term_id ][ $taxonomy ] = (int) $wpdb->insert_id;
if ( ! empty( $category->tag_count ) ) {
$count = (int) $category->tag_count;
$wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent', 'count' ) );
$tt_ids[ $term_id ][ $taxonomy ] = (int) $wpdb->insert_id;
$wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent', 'count' ) );
$tt_ids[ $term_id ][ $taxonomy ] = (int) $wpdb->insert_id;
$select = 'post_id, category_id';
$posts = $wpdb->get_results( "SELECT $select FROM $wpdb->post2cat GROUP BY post_id, category_id" );
foreach ( $posts as $post ) {
$post_id = (int) $post->post_id;
$term_id = (int) $post->category_id;
if ( ! empty( $post->rel_type ) && 'tag' === $post->rel_type ) {
$tt_id = $tt_ids[ $term_id ][ $taxonomy ];
$wpdb->term_relationships,
'term_taxonomy_id' => $tt_id,
// < 3570 we used linkcategories. >= 3570 we used categories and link2cat.
if ( $wp_current_db_version < 3570 ) {
* Create link_category terms for link categories. Create a map of link
* category IDs to link_category terms.
$link_cat_id_map = array();
$link_cats = $wpdb->get_results( 'SELECT cat_id, cat_name FROM ' . $wpdb->prefix . 'linkcategories' );
foreach ( $link_cats as $category ) {
$cat_id = (int) $category->cat_id;
$name = wp_slash( $category->cat_name );
$slug = sanitize_title( $name );
// Associate terms with the same slug in a term group and make slugs unique.
$exists = $wpdb->get_results( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug ) );
$term_group = $exists[0]->term_group;
$term_id = $exists[0]->term_id;
if ( empty( $term_id ) ) {
$wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) );
$term_id = (int) $wpdb->insert_id;
$link_cat_id_map[ $cat_id ] = $term_id;
$default_link_cat = $term_id;
'taxonomy' => 'link_category',
$tt_ids[ $term_id ] = (int) $wpdb->insert_id;
// Associate links to categories.
$links = $wpdb->get_results( "SELECT link_id, link_category FROM $wpdb->links" );
if ( ! empty( $links ) ) {
foreach ( $links as $link ) {
if ( 0 == $link->link_category ) {
if ( ! isset( $link_cat_id_map[ $link->link_category ] ) ) {
$term_id = $link_cat_id_map[ $link->link_category ];
$tt_id = $tt_ids[ $term_id ];
$wpdb->term_relationships,
'object_id' => $link->link_id,
'term_taxonomy_id' => $tt_id,
// Set default to the last category we grabbed during the upgrade loop.
update_option( 'default_link_category', $default_link_cat );
$links = $wpdb->get_results( "SELECT link_id, category_id FROM $wpdb->link2cat GROUP BY link_id, category_id" );
foreach ( $links as $link ) {
$link_id = (int) $link->link_id;
$term_id = (int) $link->category_id;
$taxonomy = 'link_category';
$tt_id = $tt_ids[ $term_id ][ $taxonomy ];
$wpdb->term_relationships,