Index: taxonomy.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.module,v
retrieving revision 1.367
diff -u -p -r1.367 taxonomy.module
--- taxonomy.module	2 Jul 2007 17:08:36 -0000	1.367
+++ taxonomy.module	6 Jul 2007 11:30:16 -0000
@@ -347,7 +347,7 @@ function taxonomy_save_vocabulary(&$edit
     module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit);
     $status = SAVED_NEW;
   }
-
+  cache_clear_all('taxonomy:'. $edit['vid'] .':', 'cache', TRUE);
   cache_clear_all();
 
   return $status;
@@ -373,6 +373,7 @@ function taxonomy_del_vocabulary($vid) {
 
   module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
 
+  cache_clear_all('taxonomy:'. $vid .':', 'cache', TRUE);
   cache_clear_all();
 
   return SAVED_DELETED;
@@ -566,6 +567,8 @@ function taxonomy_save_term(&$form_value
     module_invoke_all('taxonomy', $hook, 'term', $form_values);
   }
 
+  $term = taxonomy_get_term($form_values['tid']);
+  cache_clear_all('taxonomy:'. $term->vid .':', 'cache', TRUE);
   cache_clear_all();
 
   return $status;
@@ -581,6 +584,8 @@ function taxonomy_save_term(&$form_value
  */
 function taxonomy_del_term($tid) {
   $tids = array($tid);
+  $vids = array();
+
   while ($tids) {
     $children_tids = $orphans = array();
     foreach ($tids as $tid) {
@@ -595,7 +600,10 @@ function taxonomy_del_term($tid) {
         }
       }
 
-      $term = (array) taxonomy_get_term($tid);
+      $term = taxonomy_get_term($tid);
+
+      // Record vocabulary IDs for clearing cache.
+      $vids[$term->vid] = $term->vid;
 
       db_query('DELETE FROM {term_data} WHERE tid = %d', $tid);
       db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $tid);
@@ -609,6 +617,12 @@ function taxonomy_del_term($tid) {
     $tids = $orphans;
   }
 
+  // Clear cache for all affected vocabularies just in case a term has a child
+  // in another vocabulary.
+  foreach($vids as $vid) {
+    cache_clear_all('taxonomy:'. $vid .':', 'cache', TRUE);
+  }
+
   cache_clear_all();
 
   return SAVED_DELETED;
@@ -981,13 +995,31 @@ function taxonomy_get_children($tid, $vi
  * @param $max_depth
  *   The number of levels of the tree to return. Leave NULL to return all levels.
  *
+ * @param $cache
+ *   Whether this tree should be cached in the database. This is used for
+ *   internal purposes and should almost always be left set to TRUE.
+ *
  * @return
  *   An array of all term objects in the tree. Each term object is extended
  *   to have "depth" and "parents" attributes in addition to its normal ones.
- *   Results are statically cached.
+ *   Results are cached in the database.
  */
-function taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL) {
-  static $children, $parents, $terms;
+function taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL, $cache = TRUE) {
+  // Check for a cached version of this vocabulary's tree with the given params.
+  // Some functions pass in NULL for $parent and $max_depth so we need placeholders
+  // for those.
+  $id = 'taxonomy:'. $vid .':';
+  $id .= isset($parent) ? $parent : 'NULL';
+  $id .= ':'. $depth .':';
+  $id .= isset($max_depth) ? $max_depth : 'NULL';
+
+  if ($cache && ($cached = cache_get($id))) {
+    return $cached->data;
+  }
+
+  $children = array();
+  $parents = array();
+  $terms = array();
 
   $depth++;
 
@@ -1017,12 +1049,18 @@ function taxonomy_get_tree($vid, $parent
         $tree[] = $term;
 
         if (!empty($children[$vid][$child])) {
-          $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth));
+          // Recurse down the tree, but do not cache recursive calls.
+          $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth, FALSE));
         }
       }
     }
   }
 
+  // Don't cache tree if empty, may be able to remove this after more testing.
+  if ($tree && $cache) {
+    cache_set($id, $tree);
+  }
+
   return $tree;
 }
 
@@ -1032,10 +1070,11 @@ function taxonomy_get_tree($vid, $parent
 function taxonomy_get_synonyms($tid) {
   if ($tid) {
     $result = db_query('SELECT name FROM {term_synonym} WHERE tid = %d', $tid);
+    $synonyms = array();
     while ($synonym = db_fetch_array($result)) {
       $synonyms[] = $synonym['name'];
     }
-    return $synonyms ? $synonyms : array();
+    return $synonyms;
   }
   else {
     return array();
@@ -1443,7 +1482,7 @@ function taxonomy_term_page($str_tids = 
  * Page to edit a vocabulary.
  */
 function taxonomy_admin_vocabulary_edit($vocabulary) {
-  if ($_POST['op'] == t('Delete') || $_POST['confirm']) {
+  if ((isset($_POST['op']) && $_POST['op'] == t('Delete')) || !empty($_POST['confirm'])) {
     return drupal_get_form('taxonomy_vocabulary_confirm_delete', $vocabulary->vid);
   }
   return drupal_get_form('taxonomy_form_vocabulary', (array)$vocabulary);
@@ -1453,11 +1492,12 @@ function taxonomy_admin_vocabulary_edit(
  * Page to edit a vocabulary term.
  */
 function taxonomy_admin_term_edit($tid) {
-  if ($_POST['op'] == t('Delete') || $_POST['confirm']) {
+  if ((isset($_POST['op']) && $_POST['op'] == t('Delete')) || !empty($_POST['confirm'])) {
     return drupal_get_form('taxonomy_term_confirm_delete', $tid);
   }
   if ($term = (array)taxonomy_get_term($tid)) {
-    return drupal_get_form('taxonomy_form_term', $term['vid'], $term);
+    $vocabulary = taxonomy_vocabulary_load($term['vid']);
+    return drupal_get_form('taxonomy_form_term', $vocabulary, $term);
   }
   return drupal_not_found();
 }
