From b068bd644775e0c070869a3be2332dee9e12c48b Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 26 May 2007 17:47:20 +0000 Subject: [PATCH] During upgrade, make slugs unique and put like slugs in a term group. see #4189 git-svn-id: http://svn.automattic.com/wordpress/trunk@5552 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/upgrade.php | 17 ++++++++++++++++- wp-includes/taxonomy.php | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/wp-admin/includes/upgrade.php b/wp-admin/includes/upgrade.php index 915520652d..8b573ff7e5 100644 --- a/wp-admin/includes/upgrade.php +++ b/wp-admin/includes/upgrade.php @@ -578,13 +578,28 @@ function upgrade_230() { // Convert categories to terms. $tt_ids = array(); - $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories"); + $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_ID"); foreach ($categories as $category) { $term_id = (int) $category->cat_ID; $name = $wpdb->escape($category->cat_name); $description = $wpdb->escape($category->category_description); $slug = $wpdb->escape($category->category_nicename); $parent = $wpdb->escape($category->category_parent); + $term_group = 0; + + // Associate terms with the same slug in a term group and make slugs unique. + if ( $exists = $wpdb->get_results("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = '$slug'") ) { + $num = count($exists); + $num++; + $slug = $slug . "-$num"; + $term_group = $exists[0]->term_group; + $id = $exists[0]->term_id; + if ( empty( $term_group ) ) { + $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1; + $wpdb->query("UPDATE $wpdb->terms SET term_group = '$term_group' WHERE term_id = '$id'"); + } + } + $wpdb->query("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES ('$term_id', '$name', '$slug', '$term_group')"); if ( !empty($category->category_count) ) { diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 8aac37e46a..d0178622c5 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -67,7 +67,7 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { $term_group = $alias->term_group; } else { // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. - $term_group = $wpdb->get_var("SELECT MAX() term_group FROM $wpdb->terms GROUP BY term_group") + 1; + $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1; $wpdb->query("UPDATE $wpdb->terms SET term_group = $term_group WHERE term_id = $alias->term_id"); } }