From 805e41198cd865fc83deaea107e76458e5a66684 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 12 Dec 2007 05:14:00 +0000 Subject: [PATCH] Defer term counting during import. Props tellyworth. fixes #5377 git-svn-id: http://svn.automattic.com/wordpress/trunk@6376 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/import/wordpress.php | 2 ++ wp-includes/taxonomy.php | 39 +++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/wp-admin/import/wordpress.php b/wp-admin/import/wordpress.php index ac7f6cfc05..64fb9d8838 100644 --- a/wp-admin/import/wordpress.php +++ b/wp-admin/import/wordpress.php @@ -446,11 +446,13 @@ class WP_Import { $this->file = $file; $this->get_authors_from_post(); + wp_defer_term_counting(true); $this->get_entries(); $this->process_categories(); $this->process_tags(); $result = $this->process_posts(); $this->backfill_parents(); + wp_defer_term_counting(false); if ( is_wp_error( $result ) ) return $result; } diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index fdaa15abe2..4a9dcadc2f 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -1317,6 +1317,21 @@ function wp_update_term( $term, $taxonomy, $args = array() ) { return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); } +// enable or disable term count deferring +// if no value is supplied, the current value of the defer setting is returned +function wp_defer_term_counting($defer=NULL) { + static $_defer = false; + + if ( is_bool($defer) ) { + $_defer = $defer; + // flush any deferred counts + if ( !$defer ) + wp_update_term_count( NULL, NULL, true ); + } + + return $_defer; +} + /** * wp_update_term_count() - Updates the amount of terms in taxonomy * @@ -1334,8 +1349,15 @@ function wp_update_term( $term, $taxonomy, $args = array() ) { * @param string $taxonomy The context of the term. * @return bool If no terms will return false, and if successful will return true. */ -function wp_update_term_count( $terms, $taxonomy ) { - global $wpdb; +function wp_update_term_count( $terms, $taxonomy, $do_deferred=false ) { + static $_deferred = array(); + + if ( $do_deferred ) { + foreach ( array_keys($_deferred) as $tax ) { + wp_update_term_count_now( $_deferred[$tax], $tax ); + unset( $_deferred[$tax] ); + } + } if ( empty($terms) ) return false; @@ -1343,6 +1365,19 @@ function wp_update_term_count( $terms, $taxonomy ) { if ( !is_array($terms) ) $terms = array($terms); + if ( wp_defer_term_counting() ) { + if ( !isset($_deferred[$taxonomy]) ) + $_deferred[$taxonomy] = array(); + $_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) ); + return true; + } + + return wp_update_term_count_now( $terms, $taxonomy ); +} + +function wp_update_term_count_now( $terms, $taxonomy ) { + global $wpdb; + $terms = array_map('intval', $terms); $taxonomy = get_taxonomy($taxonomy);