Defer comment counting. Props tellyworth. fixes #5557

git-svn-id: http://svn.automattic.com/wordpress/trunk@6532 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-01-01 17:03:52 +00:00
parent 6aafd77e36
commit 8b64f70945
2 changed files with 71 additions and 25 deletions

View File

@ -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 '<li>';
printf(__('Post <i>%s</i> 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;
}

View File

@ -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 )