--- C:\Documents and Settings\malkanlar\Desktop\community_tags\community_tags.module	Sat Jul 19 14:10:18 2008
+++ C:\Documents and Settings\malkanlar\Desktop\community_tags\community_tags.modulefixed	Wed Jan 14 17:47:26 2009
@@ -12,6 +12,7 @@
 define('COMMUNITY_TAGS_MODE_BLOCK', 0);
 define('COMMUNITY_TAGS_MODE_TAB', 1);
 define('COMMUNITY_TAGS_MODE_INLINE', 2);
+define('COMMUNITY_TAGS_MAX_PROCESS_ROW', 100);
 
 /**
  * Implementation of hook_help().
@@ -88,7 +89,6 @@
 function community_tags_rehash($silent = FALSE) {
   // Get the community tagged vocabularies.
   $community_tagged = variable_get('community_tags_vocabularies', array());
-
   $changed = FALSE;
 
   // If community tagging is in use, resync.
@@ -106,11 +106,28 @@
     }
 
     // Insert new community tag relations for new tags based on the original node author.
-    $result = db_query('SELECT t.tid, t.nid, n.uid, n.created FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid LEFT JOIN {community_tags} c ON c.tid = t.tid AND c.nid = t.nid INNER JOIN {term_data} d ON t.tid = d.tid WHERE c.tid IS NULL AND d.vid IN ('. $placeholders .')', $community_tagged);
-    while ($term = db_fetch_object($result)) {
-      db_query('INSERT INTO {community_tags} (tid, nid, uid, date) VALUES (%d, %d, %d, %d)', $term->tid, $term->nid, $term->uid, $term->created);
-      $changed = TRUE;
+    $result = db_query('SELECT distinct t.tid, t.nid, n.uid, n.created FROM 
+    {term_node} t 
+    INNER JOIN {node} n ON t.nid = n.nid 
+    LEFT JOIN {community_tags} c ON c.tid = t.tid AND c.nid = t.nid 
+    INNER JOIN {term_data} d ON t.tid = d.tid 
+    WHERE c.tid IS NULL AND d.vid IN ('. $placeholders .')', $community_tagged);
+    
+    for ($i = 0; $term = db_fetch_object($result); $i++) {
+        $values .= ",(". $term->tid . "," . $term->nid . "," . $term->uid . "," . $term->created . ")";
+        if ($i == COMMUNITY_TAGS_MAX_PROCESS_ROW) {
+            // Remove the first comma
+            $values = substr($values, 1);
+            db_query('INSERT INTO {community_tags} (tid, nid, uid, date) VALUES '. $values .';'); 
+            $values = "";
+            $i = 0;
+        }
     }
+    // If there are any rows left, process them 
+    if ($values) {
+        $values = substr($values, 1);
+        db_query('INSERT INTO {community_tags} (tid, nid, uid, date) VALUES '. $values .';');     
+    }    
   }
   else {
     // Delete all community tag relations.
