Changeset 6532

Show
Ignore:
Timestamp:
01/01/08 17:03:52 (8 months ago)
Author:
ryan
Message:

Defer comment counting. Props tellyworth. fixes #5557

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/import/wordpress.php

    r6477 r6532  
    377377        } 
    378378 
    379         if ($post_id = post_exists($post_title, '', $post_date)) { 
     379        $post_exists = post_exists($post_title, '', $post_date); 
     380         
     381        if ( $post_exists ) { 
    380382            echo '<li>'; 
    381383            printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title)); 
     
    476478            $comment_parent       = $this->get_tag( $comment, 'wp:comment_parent'); 
    477479 
    478             if ( !comment_exists($comment_author, $comment_date) ) { 
     480            // if this is a new post we can skip the comment_exists() check 
     481            if ( !$post_exists || !comment_exists($comment_author, $comment_date) ) { 
    479482                $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'); 
    480483                wp_insert_comment($commentdata); 
     
    649652    } 
    650653     
     654    function import_start() { 
     655        wp_defer_term_counting(true); 
     656        wp_defer_comment_counting(true); 
     657        do_action('import_start'); 
     658    } 
     659     
     660    function import_end() { 
     661        do_action('import_end'); 
     662         
     663        // clear the caches after backfilling 
     664        foreach ($this->post_ids_processed as $post_id) 
     665            clean_post_cache($post_id); 
     666         
     667        wp_defer_term_counting(false); 
     668        wp_defer_comment_counting(false); 
     669    } 
    651670 
    652671    function import($id, $fetch_attachments = false) { 
     
    655674 
    656675        add_filter('import_post_meta_key', array($this, 'is_valid_meta_key')); 
    657         do_action('import_start'); 
    658676        $file = get_attached_file($this->id); 
    659677        $this->import_file($file); 
    660         do_action('import_end'); 
    661678    } 
    662679         
     
    664681        $this->file = $file; 
    665682         
     683        $this->import_start(); 
    666684        $this->get_authors_from_post(); 
    667         wp_defer_term_counting(true); 
    668685        $this->get_entries(); 
    669686        $this->process_categories(); 
     
    672689        $this->backfill_parents(); 
    673690        $this->backfill_attachment_urls(); 
    674          
    675         // clear the caches after backfilling 
    676         foreach ($this->post_ids_processed as $post_id) 
    677             clean_post_cache($post_id); 
    678          
    679         wp_defer_term_counting(false); 
     691        $this->import_end(); 
     692         
    680693        if ( is_wp_error( $result ) ) 
    681694            return $result; 
  • trunk/wp-includes/comment.php

    r6364 r6532  
    492492} 
    493493 
    494  
    495 function wp_update_comment_count($post_id) { 
     494function wp_defer_comment_counting($defer=NULL) { 
     495    static $_defer = false; 
     496     
     497    if ( is_bool($defer) ) { 
     498        $_defer = $defer; 
     499        // flush any deferred counts 
     500        if ( !$defer ) 
     501            wp_update_comment_count( NULL, true ); 
     502    } 
     503     
     504    return $_defer; 
     505
     506 
     507function wp_update_comment_count($post_id, $do_deferred=false) { 
     508    static $_deferred = array(); 
     509     
     510    if ( $do_deferred ) { 
     511        $_deferred = array_unique($_deferred); 
     512        foreach ( $_deferred as $i => $_post_id ) { 
     513            wp_update_comment_count_now($_post_id); 
     514            unset( $_deferred[$i] ); 
     515        } 
     516    } 
     517     
     518    if ( wp_defer_comment_counting() ) { 
     519        $_deferred[] = $post_id; 
     520        return true; 
     521    } 
     522    elseif ( $post_id ) { 
     523        return wp_update_comment_count_now($post_id); 
     524    } 
     525         
     526
     527 
     528function wp_update_comment_count_now($post_id) { 
    496529    global $wpdb; 
    497530    $post_id = (int) $post_id;