Update cat2tag converter. Some term API tweaks. see #4189

git-svn-id: http://svn.automattic.com/wordpress/trunk@5553 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-05-26 18:54:16 +00:00
parent b068bd6447
commit 80ce3031d1
5 changed files with 43 additions and 56 deletions

View File

@ -16,7 +16,7 @@ class WP_Categories_to_Tags {
function populate_all_categories() { function populate_all_categories() {
global $wpdb; global $wpdb;
$this->all_categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE (type & ~ " . TAXONOMY_TAG . ") != 0 AND category_count > 0 ORDER BY cat_name ASC"); $this->all_categories = get_categories('get=all');
} }
function welcome() { function welcome() {
@ -40,13 +40,13 @@ class WP_Categories_to_Tags {
print '<form action="admin.php?import=wp-cat2tag&amp;step=2" method="post">'; print '<form action="admin.php?import=wp-cat2tag&amp;step=2" method="post">';
print '<ul style="list-style:none">'; print '<ul style="list-style:none">';
$hier = _get_category_hierarchy(); $hier = _get_term_hierarchy('category');
foreach ($this->all_categories as $category) { foreach ($this->all_categories as $category) {
if ((int) $category->category_parent == 0) { if ((int) $category->parent == 0) {
print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($category->cat_ID) . '" /> ' . $category->cat_name . ' (' . $category->category_count . ')</label>'; print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($category->term_id) . '" /> ' . $category->name . ' (' . $category->count . ')</label>';
if (isset($hier[$category->cat_ID])) { if (isset($hier[$category->term_id])) {
$this->_category_children($category, $hier); $this->_category_children($category, $hier);
} }
@ -63,12 +63,12 @@ class WP_Categories_to_Tags {
function _category_children($parent, $hier) { function _category_children($parent, $hier) {
print '<ul style="list-style:none">'; print '<ul style="list-style:none">';
foreach ($hier[$parent->cat_ID] as $child_id) { foreach ($hier[$parent->term_id] as $child_id) {
$child =& get_category($child_id); $child =& get_category($child_id);
print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($child->cat_ID) . '" /> ' . $child->cat_name . ' (' . $child->category_count . ')</label>'; print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($child->term_id) . '" /> ' . $child->name . ' (' . $child->count . ')</label>';
if (isset($hier[$child->cat_ID])) { if (isset($hier[$child->term_id])) {
$this->_category_children($child, $hier); $this->_category_children($child, $hier);
} }
@ -83,9 +83,9 @@ class WP_Categories_to_Tags {
$cat_id = (int) $cat_id; $cat_id = (int) $cat_id;
$maybe_exists = $wpdb->get_results("SELECT cat_ID from $wpdb->categories WHERE cat_ID = '$cat_id'"); $maybe_exists = category_exists($cat_id);
if (count($maybe_exists) > 0) { if ( $maybe_exists ) {
return true; return true;
} else { } else {
return false; return false;
@ -101,8 +101,10 @@ class WP_Categories_to_Tags {
print '</div>'; print '</div>';
} }
$this->categories_to_convert = $_POST['cats_to_convert'];
$hier = _get_category_hierarchy(); if ( empty($this->categories_to_convert) )
$this->categories_to_convert = $_POST['cats_to_convert'];
$hier = _get_term_hierarchy('category');
print '<ul>'; print '<ul>';
@ -116,24 +118,14 @@ class WP_Categories_to_Tags {
} else { } else {
$category =& get_category($cat_id); $category =& get_category($cat_id);
if ($category->link_count > 0) {
$type = $category->type | TAXONOMY_TAG;
} else {
$type = TAXONOMY_TAG;
}
// Set the category itself to $type from above // Set the category itself to $type from above
$wpdb->query("UPDATE $wpdb->categories SET type = '$type' WHERE cat_ID = '{$category->cat_ID}'"); $wpdb->query("UPDATE $wpdb->term_taxonomy SET taxonomy = '$type' WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
// Set relationships in post2cat to 'tag', category_count becomes tag_count
$wpdb->query("UPDATE $wpdb->post2cat SET rel_type = 'tag' WHERE category_ID = '{$category->cat_ID}'");
$wpdb->query("UPDATE $wpdb->categories SET tag_count = '{$category->category_count}', category_count = '0' WHERE cat_ID = '{$category->cat_ID}'");
// Set all parents to 0 (root-level) if their parent was the converted tag // Set all parents to 0 (root-level) if their parent was the converted tag
$wpdb->query("UPDATE $wpdb->categories SET category_parent = 0 WHERE category_parent = '{$category->cat_ID}'"); $wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = '{$category->term_id}' AND taxonomy = 'category'");
// Clean the cache // Clean the cache
clean_category_cache($category->cat_ID); clean_category_cache($category->term_id);
_e('Converted successfully.'); _e('Converted successfully.');
} }
@ -160,16 +152,9 @@ class WP_Categories_to_Tags {
function convert_all() { function convert_all() {
global $wpdb; global $wpdb;
$cats = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE (type & ~ " . TAXONOMY_TAG . ") != 0 AND category_count > 0"); $wpdb->query("UPDATE $wpdb->term_taxonomy SET taxonomy = '$type', parent = 0 WHERE taxonomy = 'category'");
clean_category_cache($category->term_id);
$_POST['cats_to_convert'] = array();
foreach ($cats as $cat) {
$_POST['cats_to_convert'][] = $cat->cat_ID;
}
$this->convert_them();
} }
function init() { function init() {

View File

@ -98,7 +98,7 @@ function get_nested_categories( $default = 0, $parent = 0 ) {
$checked_categories[] = $default; $checked_categories[] = $default;
} }
$cats = get_categories("child_of=$parent&hide_empty=0&get=ids"); $cats = get_categories("child_of=$parent&hide_empty=0&fields=ids");
$result = array (); $result = array ();
if ( is_array( $cats ) ) { if ( is_array( $cats ) ) {

View File

@ -4,7 +4,7 @@ function get_all_category_ids() {
global $wpdb; global $wpdb;
if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) { if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) {
$cat_ids = get_terms('category', 'get=ids&hierarchical=0&hide_empty=0'); $cat_ids = get_terms('category', 'fields=ids&get=all');
wp_cache_add('all_category_ids', $cat_ids, 'category'); wp_cache_add('all_category_ids', $cat_ids, 'category');
} }

View File

@ -447,7 +447,7 @@ function wp_delete_post($postid = 0) {
function wp_get_post_categories($post_id = 0) { function wp_get_post_categories($post_id = 0) {
$post_id = (int) $post_id; $post_id = (int) $post_id;
$cats = get_object_terms($post_id, 'category', 'get=ids'); $cats = get_object_terms($post_id, 'category', 'get=fields');
return $cats; return $cats;
} }

View File

@ -136,7 +136,7 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) {
$objects = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$tt_id'"); $objects = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$tt_id'");
foreach ( (array) $objects as $object ) { foreach ( (array) $objects as $object ) {
$terms = get_object_terms($object, $taxonomy, 'get=ids'); $terms = get_object_terms($object, $taxonomy, 'fields=ids');
if ( 1 == count($terms) && isset($default) ) if ( 1 == count($terms) && isset($default) )
$terms = array($default); $terms = array($default);
else else
@ -319,7 +319,7 @@ function get_object_terms($object_id, $taxonomy, $args = array()) {
// TODO cast to int // TODO cast to int
$object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id; $object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id;
$defaults = array('orderby' => 'name', 'order' => 'ASC', 'get' => 'everything'); $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
$args = wp_parse_args( $args, $defaults ); $args = wp_parse_args( $args, $defaults );
extract($args); extract($args);
@ -331,16 +331,16 @@ function get_object_terms($object_id, $taxonomy, $args = array()) {
$taxonomies = "'" . implode("', '", $taxonomies) . "'"; $taxonomies = "'" . implode("', '", $taxonomies) . "'";
$object_ids = implode(', ', $object_ids); $object_ids = implode(', ', $object_ids);
if ( 'everything' == $get ) if ( 'all' == $fields )
$select_this = 't.*'; $select_this = 't.*';
else if ( 'ids' == $get ) else if ( 'ids' == $fields )
$select_this = 't.term_id'; $select_this = 't.term_id';
$query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) ORDER BY $orderby $order"; $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) ORDER BY $orderby $order";
if ( 'everything' == $get ) if ( 'all' == $fields )
$taxonomy_data = $wpdb->get_results($query); $taxonomy_data = $wpdb->get_results($query);
else if ( 'ids' == $get ) else if ( 'ids' == $fields )
$taxonomy_data = $wpdb->get_col($query); $taxonomy_data = $wpdb->get_col($query);
if ( ! $taxonomy_data ) if ( ! $taxonomy_data )
@ -379,19 +379,21 @@ function &get_terms($taxonomies, $args = '') {
$defaults = array('orderby' => 'name', 'order' => 'ASC', $defaults = array('orderby' => 'name', 'order' => 'ASC',
'hide_empty' => true, 'exclude' => '', 'include' => '', 'hide_empty' => true, 'exclude' => '', 'include' => '',
'number' => '', 'get' => 'everything', 'slug' => '', 'parent' => '', 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
'hierarchical' => true, 'child_of' => 0); 'hierarchical' => true, 'child_of' => 0, 'get' => '');
$args = wp_parse_args( $args, $defaults ); $args = wp_parse_args( $args, $defaults );
$args['number'] = (int) $args['number']; $args['number'] = (int) $args['number'];
if ( ! $single_taxonomy ) { if ( ! $single_taxonomy ) {
$args['child_of'] = 0; $args['child_of'] = 0;
$args['hierarchical'] = false; $args['hierarchical'] = false;
} else { } else if ( !is_taxonomy_hierarchical($taxonomies[0]) ) {
$tax = get_taxonomy($taxonomy); $args['child_of'] = 0;
if ( !$tax['hierarchical'] ) { $args['hierarchical'] = false;
$args['child_of'] = 0; }
$args['hierarchical'] = false; if ( 'all' == $args['get'] ) {
} $args['child_of'] = 0;
$args['hide_empty'] = 0;
$args['hierarchical'] = false;
} }
extract($args); extract($args);
@ -463,16 +465,16 @@ function &get_terms($taxonomies, $args = '') {
else else
$number = ''; $number = '';
if ( 'everything' == $get ) if ( 'all' == $fields )
$select_this = 't.*, tt.*'; $select_this = 't.*, tt.*';
else if ( 'ids' == $get ) else if ( 'ids' == $fields )
$select_this = 't.term_id'; $select_this = 't.term_id';
$query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $number"; $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $number";
if ( 'everything' == $get ) if ( 'all' == $fields )
$terms = $wpdb->get_results($query); $terms = $wpdb->get_results($query);
else if ( 'ids' == $get ) else if ( 'ids' == $fields )
$terms = $wpdb->get_col($query); $terms = $wpdb->get_col($query);
if ( empty($terms) ) if ( empty($terms) )