WordPress.org

Make WordPress Core

Changeset 5814


Ignore:
Timestamp:
07/21/2007 08:53:19 PM (13 years ago)
Author:
ryan
Message:

First attempt at upgrading old releases that used the linkcategories table to taxonomy. Needs testing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/upgrade.php

    r5700 r5814  
    455455    $wpdb->show_errors();
    456456
    457     if ( 0 == $wpdb->get_var("SELECT SUM(category_count) FROM $wpdb->categories") ) { // Create counts
    458         $categories = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
    459         foreach ( $categories as $cat_id ) {
    460             $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status='publish' AND category_id = '$cat_id'");
    461             $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'");
    462         }
    463     }
    464 
    465457    // populate comment_count field of posts table
    466458    $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" );
     
    524516            foreach ( $posts as $post )
    525517                wp_schedule_single_event(mysql2date('U', $post->post_date), 'publish_future_post', array($post->ID));
    526     }
    527 
    528     if ( $wp_current_db_version < 3570 ) {
    529         // Create categories for link categories if a category with the same
    530         // name doesn't exist.  Create a map of link cat IDs to cat IDs.
    531         $link_cat_id_map = array();
    532         $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->prefix . 'linkcategories');
    533         foreach ( $link_cats as $link_cat) {
    534             if ( $cat_id = category_exists($link_cat->cat_name) ) {
    535                 $link_cat_id_map[$link_cat->cat_id] = $cat_id;
    536                 $default_link_cat = $cat_id;
    537             } else {
    538                 $link_cat_id_map[$link_cat->cat_id] = wp_create_category($link_cat->cat_name);
    539                 $default_link_cat = $link_cat_id_map[$link_cat->cat_id];
    540             }
    541         }
    542 
    543         // Associate links to cats.
    544         $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links");
    545         if ( !empty($links) ) foreach ( $links as $link ) {
    546             if ( 0 == $link->link_category )
    547                 continue;
    548             if ( ! isset($link_cat_id_map[$link->link_category]) )
    549                 continue;
    550             $link_cat = $link_cat_id_map[$link->link_category];
    551             $cat = $wpdb->get_row("SELECT * FROM $wpdb->link2cat WHERE link_id = '$link->link_id' AND category_id = '$link_cat'");
    552             if ( !$cat ) {
    553                 $wpdb->query("INSERT INTO $wpdb->link2cat (link_id, category_id)
    554                     VALUES ('$link->link_id', '$link_cat')");
    555             }
    556         }
    557 
    558         // Set default to the last category we grabbed during the upgrade loop.
    559         update_option('default_link_category', $default_link_cat);
    560 
    561         // Count links per category.
    562         if ( 0 == $wpdb->get_var("SELECT SUM(link_count) FROM $wpdb->categories") ) {
    563             $categories = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
    564             foreach ( $categories as $cat_id ) {
    565                 $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->link2cat, $wpdb->links WHERE $wpdb->links.link_id = $wpdb->link2cat.link_id AND category_id = '$cat_id'");
    566                 $wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'");
    567             }
    568         }
    569     }
    570 
    571     if ( $wp_current_db_version < 4772 ) {
    572         // Obsolete linkcategories table
    573         $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories');
    574518    }
    575519}
     
    652596    }
    653597
    654     $links = $wpdb->get_results("SELECT * FROM $wpdb->link2cat");
    655     foreach ( $links as $link ) {
    656         $link_id = (int) $link->link_id;
    657         $term_id = (int) $link->category_id;
    658         $taxonomy = 'link_category';
    659         $tt_id = $tt_ids[$term_id][$taxonomy];
    660         if ( empty($tt_id) )
    661             continue;
    662 
    663         $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$link_id', '$tt_id')");
    664     }
     598    // < 3570 we used linkcategories.  >= 3570 we used categories and link2cat.
     599    if ( $wp_current_db_version < 3570 ) {
     600        // Create link_category terms for link categories.  Create a map of link cat IDs
     601        // to link_category terms.
     602        $link_cat_id_map = array();
     603        $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->prefix . 'linkcategories');
     604        foreach ( $link_cats as $category) {
     605            $cat_id = (int) $category->cat_id;
     606            $term_id = 0;
     607            $name = $wpdb->escape($category->cat_name);
     608            $slug = sanitize_title($name);
     609            $term_group = 0;
     610   
     611            // Associate terms with the same slug in a term group and make slugs unique.
     612            if ( $exists = $wpdb->get_results("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = '$slug'") ) {
     613                $num = count($exists);
     614                $num++;
     615                $slug = $slug . "-$num";
     616                $term_group = $exists[0]->term_group;
     617                $term_id = $exists[0]->term_id;
     618                if ( empty( $term_group ) ) {
     619                    $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1;
     620                    $wpdb->query("UPDATE $wpdb->terms SET term_group = '$term_group' WHERE term_id = '$term_id'");
     621                }
     622            }
     623
     624            if ( !empty($term_id) ) {
     625                $wpdb->query("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES ('$term_id', '$name', '$slug', '$term_group')");
     626            } else {
     627                $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$name', '$slug', '$term_group')");
     628                $term_id = (int) $wpdb->insert_id; 
     629            }
     630
     631            $link_cat_id_map[$cat_id] = $term_id;
     632            $default_link_cat = $term_id;
     633
     634            $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', 'link_category', '', '0', '0')");
     635            $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
     636        }
     637
     638        // Associate links to cats.
     639        $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links");
     640        if ( !empty($links) ) foreach ( $links as $link ) {
     641            if ( 0 == $link->link_category )
     642                continue;
     643            if ( ! isset($link_cat_id_map[$link->link_category]) )
     644                continue;
     645            $tt_id = $tt_ids[$term_id]['link_category'];
     646            if ( empty($tt_id) )
     647                continue;
     648
     649            $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$link->link_id', '$tt_id')");
     650        }
     651
     652        // Set default to the last category we grabbed during the upgrade loop.
     653        update_option('default_link_category', $default_link_cat);
     654    } else {
     655        $links = $wpdb->get_results("SELECT * FROM $wpdb->link2cat");
     656        foreach ( $links as $link ) {
     657            $link_id = (int) $link->link_id;
     658            $term_id = (int) $link->category_id;
     659            $taxonomy = 'link_category';
     660            $tt_id = $tt_ids[$term_id][$taxonomy];
     661            if ( empty($tt_id) )
     662                continue;
     663
     664            $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$link_id', '$tt_id')");
     665        }
     666    }
     667
     668    if ( $wp_current_db_version < 4772 ) {
     669        // Obsolete linkcategories table
     670        $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories');
     671    }
     672
     673    //  TODO: Recalculate all counts
    665674}
    666675
Note: See TracChangeset for help on using the changeset viewer.