Add comment_count to the posts table. Props donncha. fixes #1860

git-svn-id: http://svn.automattic.com/wordpress/trunk@3104 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2005-11-16 06:29:36 +00:00
parent 005dce9352
commit 25b644e8ce
6 changed files with 50 additions and 14 deletions

View File

@ -56,8 +56,7 @@ case 'delete-comment' :
if ( !current_user_can('edit_post', $comment->comment_post_ID) )
die('-1');
if ( wp_set_comment_status($comment->comment_ID, "delete") ) {
do_action('delete_comment', $comment->comment_ID);
if ( wp_delete_comment($comment->comment_ID) ) {
die('1');
} else {
die('0');

View File

@ -30,7 +30,7 @@ function upgrade_all() {
upgrade_130();
}
if ( $wp_current_db_version < 3092 )
if ( $wp_current_db_version < 3104 )
upgrade_160();
save_mod_rewrite_rules();
@ -300,7 +300,15 @@ function upgrade_160() {
$wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'");
}
}
// populate comment_count field of posts table
$comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments GROUP BY comment_post_ID" );
if( is_array( $comments ) ) {
foreach ($comments as $comment) {
$wpdb->query( "UPDATE $wpdb->posts SET comment_count = $comment->c WHERE 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 ) {

View File

@ -121,6 +121,7 @@ CREATE TABLE $wpdb->posts (
menu_order int(11) NOT NULL default '0',
post_type varchar(100) NOT NULL,
post_mime_type varchar(100) NOT NULL,
comment_count bigint(20) NOT NULL default '0',
PRIMARY KEY (ID),
KEY post_name (post_name)
);

View File

@ -81,7 +81,12 @@ function wp_insert_comment($commentdata) {
('$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id')
");
return $wpdb->insert_id;
$id = $wpdb->insert_id;
if ( $comment_approved == 1)
$wpdb->query( "UPDATE $wpdb->posts SET comment_count = comment_count + 1 WHERE ID = '$comment_post_ID'" );
return $id;
}
function wp_filter_comment($commentdata) {
@ -176,9 +181,30 @@ function wp_update_comment($commentarr) {
$rval = $wpdb->rows_affected;
$c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" );
if( is_object( $c ) )
$wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" );
do_action('edit_comment', $comment_ID);
return $rval;
return $rval;
}
function wp_delete_comment($comment_id) {
global $wpdb;
do_action('delete_comment', $comment_id);
$comment = get_comment($comment_id);
if ( ! $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1") )
return false;
$post_id = $comment->comment_post_ID;
if ( $post_id )
$wpdb->query( "UPDATE $wpdb->posts SET comment_count = comment_count - 1 WHERE ID = '$post_id'" );
do_action('wp_set_comment_status', $comment_id, 'delete');
return true;
}
function clean_url( $url ) {
@ -198,7 +224,7 @@ function get_comments_number( $post_id = 0 ) {
$post_id = $id;
if ( !isset($comment_count_cache[$post_id]) )
$comment_count_cache[$post_id] = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'");
$comment_count_cache[$id] = $wpdb->get_var("SELECT comment_count FROM $wpdb->posts WHERE ID = '$post_id'");
return apply_filters('get_comments_number', $comment_count_cache[$post_id]);
}
@ -742,7 +768,7 @@ function wp_set_comment_status($comment_id, $comment_status) {
$query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1";
break;
case 'delete':
$query = "DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1";
return wp_delete_comment($comment_id);
break;
default:
return false;
@ -750,6 +776,12 @@ function wp_set_comment_status($comment_id, $comment_status) {
if ($wpdb->query($query)) {
do_action('wp_set_comment_status', $comment_id, $comment_status);
$comment = get_comment($comment_id);
$comment_post_ID = $comment->comment_post_ID;
$c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" );
if( is_object( $c ) )
$wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" );
return true;
} else {
return false;

View File

@ -1306,11 +1306,7 @@ function update_post_caches(&$posts) {
update_post_category_cache($post_id_list);
// Do the same for comment numbers
$comment_counts = $wpdb->get_results("SELECT comment_post_ID, COUNT( comment_ID ) AS ccount
FROM $wpdb->comments
WHERE comment_post_ID IN ($post_id_list)
AND comment_approved = '1'
GROUP BY comment_post_ID");
$comment_counts = $wpdb->get_results( "SELECT ID as comment_post_ID, comment_count as ccount FROM $wpdb->posts WHERE ID in ($post_id_list)" );
if ( $comment_counts ) {
foreach ($comment_counts as $comment_count) {

View File

@ -3,6 +3,6 @@
// This just holds the version number, in a separate file so we can bump it without cluttering the SVN
$wp_version = '1.6-ALPHA-2-still-dont-use';
$wp_db_version = 3092;
$wp_db_version = 3104;
?>