From d6332401135c7aa05263eab91afcecbd6a14e1fb Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 1 Jan 2008 17:03:52 +0000 Subject: [PATCH] Defer comment counting. Props tellyworth. fixes #5557 git-svn-id: https://develop.svn.wordpress.org/trunk@6532 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/import/wordpress.php | 61 +++++++++++++++++++++-------------- wp-includes/comment.php | 35 +++++++++++++++++++- 2 files changed, 71 insertions(+), 25 deletions(-) diff --git a/wp-admin/import/wordpress.php b/wp-admin/import/wordpress.php index 1e5a37f264..9898bc3df4 100644 --- a/wp-admin/import/wordpress.php +++ b/wp-admin/import/wordpress.php @@ -376,7 +376,9 @@ class WP_Import { $cat_index++; } - if ($post_id = post_exists($post_title, '', $post_date)) { + $post_exists = post_exists($post_title, '', $post_date); + + if ( $post_exists ) { echo '
  • '; printf(__('Post %s already exists.'), stripslashes($post_title)); } else { @@ -475,7 +477,8 @@ class WP_Import { $comment_type = $this->get_tag( $comment, 'wp:comment_type'); $comment_parent = $this->get_tag( $comment, 'wp:comment_parent'); - if ( !comment_exists($comment_author, $comment_date) ) { + // if this is a new post we can skip the comment_exists() check + if ( !$post_exists || !comment_exists($comment_author, $comment_date) ) { $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_approved', 'comment_type', 'comment_parent'); wp_insert_comment($commentdata); $num_comments++; @@ -648,35 +651,45 @@ class WP_Import { return apply_filters('import_attachment_size_limit', 0); } - - function import($id, $fetch_attachments = false) { - $this->id = (int) $id; - $this->fetch_attachments = ($this->allow_fetch_attachments() && (bool) $fetch_attachments); - - add_filter('import_post_meta_key', array($this, 'is_valid_meta_key')); - do_action('import_start'); - $file = get_attached_file($this->id); - $this->import_file($file); - do_action('import_end'); - } - - function import_file($file) { - $this->file = $file; - - $this->get_authors_from_post(); + function import_start() { wp_defer_term_counting(true); - $this->get_entries(); - $this->process_categories(); - $this->process_tags(); - $result = $this->process_posts(); - $this->backfill_parents(); - $this->backfill_attachment_urls(); + wp_defer_comment_counting(true); + do_action('import_start'); + } + + function import_end() { + do_action('import_end'); // clear the caches after backfilling foreach ($this->post_ids_processed as $post_id) clean_post_cache($post_id); wp_defer_term_counting(false); + wp_defer_comment_counting(false); + } + + function import($id, $fetch_attachments = false) { + $this->id = (int) $id; + $this->fetch_attachments = ($this->allow_fetch_attachments() && (bool) $fetch_attachments); + + add_filter('import_post_meta_key', array($this, 'is_valid_meta_key')); + $file = get_attached_file($this->id); + $this->import_file($file); + } + + function import_file($file) { + $this->file = $file; + + $this->import_start(); + $this->get_authors_from_post(); + $this->get_entries(); + $this->process_categories(); + $this->process_tags(); + $result = $this->process_posts(); + $this->backfill_parents(); + $this->backfill_attachment_urls(); + $this->import_end(); + if ( is_wp_error( $result ) ) return $result; } diff --git a/wp-includes/comment.php b/wp-includes/comment.php index 683b1df944..6f370a5fae 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -491,8 +491,41 @@ function wp_update_comment($commentarr) { return $rval; } +function wp_defer_comment_counting($defer=NULL) { + static $_defer = false; + + if ( is_bool($defer) ) { + $_defer = $defer; + // flush any deferred counts + if ( !$defer ) + wp_update_comment_count( NULL, true ); + } + + return $_defer; +} -function wp_update_comment_count($post_id) { +function wp_update_comment_count($post_id, $do_deferred=false) { + static $_deferred = array(); + + if ( $do_deferred ) { + $_deferred = array_unique($_deferred); + foreach ( $_deferred as $i => $_post_id ) { + wp_update_comment_count_now($_post_id); + unset( $_deferred[$i] ); + } + } + + if ( wp_defer_comment_counting() ) { + $_deferred[] = $post_id; + return true; + } + elseif ( $post_id ) { + return wp_update_comment_count_now($post_id); + } + +} + +function wp_update_comment_count_now($post_id) { global $wpdb; $post_id = (int) $post_id; if ( !$post_id )