? ._comment_alter_taxonomy.module
? cat-save-improvement.patch
Index: comment_alter_taxonomy.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/comment_alter_taxonomy/comment_alter_taxonomy.module,v
retrieving revision 1.9
diff -u -p -r1.9 comment_alter_taxonomy.module
--- comment_alter_taxonomy.module	30 Sep 2008 11:18:11 -0000	1.9
+++ comment_alter_taxonomy.module	7 Oct 2008 22:53:26 -0000
@@ -239,49 +239,28 @@ function comment_alter_taxonomy_comment(
       if (isset($arg['cid']['#value'])) {
         return array();
       }
-    return comment_alter_taxonomy_comment_form($arg);
+      return comment_alter_taxonomy_comment_form($arg);
 
     case 'insert':
-      // See if we already have a record of this node in {comment_alter_taxonomy} and if
-      // not save the "original" state of the taxonomy terms of this node.
-      if (!db_result(db_query_range('SELECT nid FROM {comment_alter_taxonomy} WHERE nid = %d AND cid = 0', $arg['nid'], 0, 1))) {
-        _comment_alter_taxonomy_save_tids($arg['nid'], 0);
-      }
-
-      // Create rows in {comment_alter_taxonomy} for any comments on this node
-      // between the last comment saved to the table and this comment.  If there
-      // are any such comments, then store term data for those comments based
-      // on the data present in the last saved comment for this node in the table.
-      // This is necessary to prevent the module from thinking that terms were
-      // deleted by a comment between the last comment in the table and this comment.
-      // This code is really only necessary in the rare case when this module is disabled
-      // and one or more comments is made on a node, and then the module is re-enabled.
-      $result = db_query("SELECT cid FROM {comments} WHERE nid = %d AND cid > (SELECT MAX(cid) FROM {comment_alter_taxonomy} WHERE nid = %d) AND cid <> %d", $arg['nid'], $arg['nid'], $arg['cid']);
-      while ($row = db_fetch_object($result)) {
-        _comment_alter_taxonomy_save_tids($arg['nid'], $row->cid);
-      }
-
       if (isset($arg['taxonomy'])) {
+        // Fetch the cid of the previous comment and store the tids of the current node if necessary.
+        $previous_cid = (int)db_result(db_query_range('SELECT cid FROM {comments} WHERE nid = %d AND cid < %d ORDER BY cid DESC', $arg['nid'], $arg['cid'], 0, 1));
+        _comment_alter_taxonomy_save_tids($arg['nid'], $previous_cid);
+
         // Save the terms to the node itself.
         taxonomy_node_save($arg['nid'], $arg['taxonomy']);
+
         // Load the node again so we can reset the internal node_load cache.
         node_load($arg['nid'], NULL, TRUE);
-      }
 
-      // Save the tids assigned to the node at this point to the {comment_alter_taxonomy} table.
-      _comment_alter_taxonomy_save_tids($arg['nid'], $arg['cid']);
+        // Store the tids assigned to the node at this point to the {comment_alter_taxonomy} table.
+        _comment_alter_taxonomy_save_tids($arg['nid'], $arg['cid']);
+      }
       break;
 
     case 'delete':
       db_query("DELETE FROM {comment_alter_taxonomy} WHERE cid = %d", $arg->cid);
       break;
-
-    case 'validate':
-      // Only validate term changes on new followups.
-      if (isset($arg['cid'])) {
-        return;
-      }
-      break;
   }
 }
 
@@ -299,13 +278,14 @@ function comment_alter_taxonomy_comment(
  *   node and not an actual comment.
  */
 function _comment_alter_taxonomy_save_tids($nid, $cid) {
-  if ($cid == 0) {
+  if (!db_result(db_query_range("SELECT cid FROM {comment_alter_taxonomy} WHERE nid = %d AND cid = %d", $nid, $cid, 0, 1))) {
     // Insert a fake term to have rows in the table even if the node has no terms.
-    db_query("INSERT INTO {comment_alter_taxonomy} (nid, cid, tid) VALUES (%d, %d, %d)", $nid, $cid, 0);
-  }
-  $res = db_query("SELECT tid FROM {term_node} WHERE nid = %d", $nid);
-  while ($term = db_fetch_object($res)) {
-    db_query("INSERT INTO {comment_alter_taxonomy} (nid, cid, tid) VALUES(%d, %d, %d)", $nid, $cid, $term->tid);
+    // These queries could fail in case of concurrent access.
+    @db_query("INSERT INTO {comment_alter_taxonomy} (nid, cid, tid) VALUES (%d, %d, %d)", $nid, $cid, 0);
+    $res = db_query("SELECT tid FROM {term_node} WHERE nid = %d", $nid);
+    while ($term = db_fetch_object($res)) {
+      @db_query("INSERT INTO {comment_alter_taxonomy} (nid, cid, tid) VALUES (%d, %d, %d)", $nid, $cid, $term->tid);
+    }
   }
 }
 
