Make WordPress Core


Ignore:
Timestamp:
02/27/2006 04:57:30 AM (19 years ago)
Author:
ryan
Message:

Bookmark/link rework. #2499

File:
1 edited

Legend:

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

    r3548 r3570  
    22
    33require_once(ABSPATH . '/wp-admin/admin-functions.php');
     4require_once(ABSPATH . '/wp-admin/admin-db.php');
    45require_once(ABSPATH . '/wp-admin/upgrade-schema.php');
    56// Functions to be called in install and upgrade scripts
     
    3435        upgrade_160();
    3536
    36     if ( $wp_current_db_version < 3548 )
     37    if ( $wp_current_db_version < 3570 )
    3738        upgrade_210();
    3839
     
    366367            foreach ( $posts as $post )
    367368                wp_schedule_event(mysql2date('U', $post->post_date), 'once', 'publish_future_post', $post->ID);
     369    }
     370    if ( $wp_current_db_version < 3570 ) {
     371        // Create categories for link categories if a category with the same
     372        // name doesn't exist.  Create a map of link cat IDs to cat IDs.
     373        $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->linkcategories"); 
     374        foreach ( $link_cats as $link_cat) {
     375            if ( $cat_id = category_exists($link_cat->cat_name) ) {
     376                $link_cat_id_map[$link_cat->cat_id] = $cat_id;
     377                $default_link_cat = $cat_id;
     378            } else {
     379                $link_cat_id_map[$link_cat->cat_id] = wp_create_category($link_cat->cat_name);
     380                $default_link_cat = $link_cat_id_map[$link_cat->cat_id];
     381            }
     382        }
     383
     384        // Associate links to cats.
     385        $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links");
     386        foreach ( $links as $link ) {
     387            $link_cat = $link_cat_id_map[$link->link_category];
     388            $cat = $wpdb->get_row("SELECT * FROM $wpdb->link2cat WHERE link_id = '$link->link_id' AND category_id = '$link_cat'");
     389            if (!$cat && 0 != $link->link_category) {
     390                $wpdb->query("INSERT INTO $wpdb->link2cat (link_id, category_id)
     391                    VALUES ('$link->link_id', '$link_cat')");
     392            }           
     393        }
     394       
     395        // Set default to the last category we grabbed during the upgrade loop.
     396        update_option('default_link_category', $default_link_cat);
     397
     398        // Count links per category.
     399        if ( 0 == $wpdb->get_var("SELECT SUM(link_count) FROM $wpdb->categories") ) {
     400            $categories = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
     401            foreach ( $categories as $cat_id ) {
     402                $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'");
     403                $wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'");
     404            }
     405        }
    368406    }
    369407}
Note: See TracChangeset for help on using the changeset viewer.