Index: /Users/mark/WorkingCopies/PIP/sites/all/modules/taxonomy_menu/taxonomy_menu.batch.inc
===================================================================
RCS file: taxonomy_menu.batch.inc
diff -N taxonomy_menu.batch.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ taxonomy_menu.batch.inc	9 Aug 2009 13:39:54 -0000
@@ -0,0 +1,123 @@
+<?php
+
+
+/**
+* The $batch can include the following values. Only 'operations'
+* and 'finished' are required, all others will be set to default values.
+*
+* @param operations
+*   An array of callbacks and arguments for the callbacks.
+*   There can be one callback called one time, one callback
+*   called repeatedly with different arguments, different
+*   callbacks with the same arguments, one callback with no
+*   arguments, etc.
+*
+* @param finished
+*   A callback to be used when the batch finishes.
+*
+* @param title
+*   A title to be displayed to the end user when the batch starts.
+*
+* @param init_message
+*   An initial message to be displayed to the end user when the batch starts.
+*
+* @param progress_message
+*   A progress message for the end user. Placeholders are available.
+*   Placeholders note the progression by operation, i.e. if there are
+*   2 operations, the message will look like:
+*    'Processed 1 out of 2.'
+*    'Processed 2 out of 2.'
+*   Placeholders include:
+*     @current, @remaining, @total and @percentage
+*
+* @param error_message
+*   The error message that will be displayed to the end user if the batch
+*   fails.
+*
+*/
+function _taxonomy_menu_insert_link_items_batch($vid) {
+  $terms = taxonomy_get_tree($vid);
+  $menu_name = variable_get('taxonomy_menu_vocab_menu_'. $vid, FALSE);
+
+  $batch = array( 
+    'operations' => array( 
+      array('_taxonomy_menu_insert_link_items_process', array($terms, $menu_name)),
+    ), 
+    'finished' => '_taxonomy_menu_insert_link_items_success', 
+    'title' => t('Rebuilding Taxonomy Menu'), 
+    'init_message' => t('The menu items have been deleted, and are about to be regenerated.'), 
+    'progress_message' => t('Import Progress: Completed @current of @total Stages.'),
+    'redirect' => 'admin/content/taxonomy/edit/vocabulary/'. $vid,
+    'error_message' => t('The Taxonomy Menu rebuild process encountered an error.'), 
+  ); 
+  batch_set($batch);
+  batch_process();
+}
+
+
+/*
+ * Insert 10 menu link items
+ */
+function _taxonomy_menu_insert_link_items_process($terms, $menu_name, &$context) {
+  _taxonomy_menu_batch_init_context($context, $start, $end, 10);
+  
+  //Loop through $terms to process each term
+  for ($i=$start; $i<count($terms) && $i<$end; $i++) {
+    $args = array(
+      'term' => $terms[$i],
+      'menu_name' => $menu_name,
+    );
+    $mlid = taxonomy_menu_handler('insert', $args);
+  }
+  
+  _taxonomy_menu_batch_update_context($context, $end, count($terms), 'Creating Menu Items');
+}
+
+
+
+/*
+ * Set a message stating the menu has been updated 
+ */
+function _taxonomy_menu_insert_link_items_success() {
+  //TODO state menu name here
+  drupal_set_message("The Taxonomy Menu has been updated.");
+  //TODO add call to rebuild hook?
+  _pipmod_taxonomy_menu_rebuild(2);
+}
+
+/*
+ * Initialise the batch context
+ * @param array $context Batch context array.
+ * @param int $start The item to start on in this pass
+ * @param int $end The end item of this pass
+ * @param int $items The number of items to process in this pass
+ */
+function _taxonomy_menu_batch_init_context(&$context, &$start, &$end, $items) {
+  //Initialize sandbox the first time through. 
+  if(!isset($context['sandbox']['progress'])) { 
+    $context['sandbox']['progress'] = 0; 
+  }
+  
+  $start = $context['sandbox']['progress'];
+  $end = $start + $items; 
+}
+
+
+/*
+ * Update the batch context
+ *
+ * @param array $context Batch context array.
+ * @param int $end The end point of the most recent pass
+ * @param int $total The total number of items to process in this batch
+ * @param str $msg Message for the progress bar
+ */
+function _taxonomy_menu_batch_update_context(&$context, $end, $total, $msg) {
+  //Update context array
+  if ($end > $total) {
+    $context['finished'] = 1;
+    return;
+  }
+  $context['message'] = "{$msg}: {$end} of {$total}";
+  $context['sandbox']['progress'] = $end;
+  $context['finished'] = $end/$total;
+}
Index: /Users/mark/WorkingCopies/PIP/sites/all/modules/taxonomy_menu/taxonomy_menu.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_menu/taxonomy_menu.module,v
retrieving revision 1.19.2.2.2.36
diff -u -p -r1.19.2.2.2.36 taxonomy_menu.module
--- taxonomy_menu.module	31 May 2009 11:56:03 -0000	1.19.2.2.2.36
+++ taxonomy_menu.module	9 Aug 2009 13:39:57 -0000
@@ -13,6 +13,9 @@
 //include the database layer
 require_once(drupal_get_path('module', 'taxonomy_menu') .'/taxonomy_menu.database.inc');
 
+//include the batch functions
+require_once(drupal_get_path('module', 'taxonomy_menu') .'/taxonomy_menu.batch.inc');
+
 /**
  * Implementation of hook_form_alter().
  *
@@ -188,23 +191,8 @@ function _taxonomy_menu_update_link_item
   //get a list of the current menu links
   $menu_links = _taxonomy_menu_get_terms($vid);
 
-  //cycle through the
-  foreach ($menu_links as $menu_link) {
-    if ($menu_link->tid == 0) {
-      $args['vid'] = $menu_link->vid;
-    }
-    else {
-      $args['term'] = taxonomy_get_term($menu_link->tid);
-    }
-
-    //set the mneu name
-    $args['menu_name'] = $menu_name;
-
-    //update the menu link
-    taxonomy_menu_handler('update', $args);
-  }
-
-  return t("The Taxonomy Menu $menu_name has been updated.");
+  //let batch api take care of inserting the menu items
+  _taxonomy_menu_insert_link_items_batch($vid);
 }
 
 /**
