Index: contributions/modules/refine_by_taxo/refine_by_taxo.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/refine_by_taxo/refine_by_taxo.module,v
retrieving revision 1.5
diff -u -p -r1.5 refine_by_taxo.module
--- contributions/modules/refine_by_taxo/refine_by_taxo.module	12 Jan 2007 22:22:57 -0000	1.5
+++ contributions/modules/refine_by_taxo/refine_by_taxo.module	18 Feb 2007 14:09:19 -0000
@@ -198,7 +198,7 @@ function refine_by_taxo_find_related_ter
   }
 
   if ($conditions) {
-    $sql = db_escape_string('SELECT tn.tid FROM {term_node} tn INNER JOIN {term_data} td ON td.tid = tn.tid WHERE' . $conditions);
+    $sql = db_escape_string('SELECT DISTINCT tn.tid FROM {term_node} tn INNER JOIN {term_data} td ON td.tid = tn.tid WHERE' . $conditions);
     $result = db_query(db_rewrite_sql($sql));
     while ($i <= variable_get('refine_by_taxo_block_tags_'. $delta, 12) && ($tid = db_fetch_object($result))) {
       $terms[$tid->tid] = taxonomy_get_term($tid->tid);
@@ -247,6 +247,50 @@ function refine_by_taxo_find_related_ter
   }
 }
 
+/**
+ * Finds all nodes that match selected taxonomy conditions.
+ * Based on taxonomy_select_nodes of taxonomy.module and modified a bit
+ * to always return all the nodes (no just the first 10 or so) and to
+ * not sort the nodes list (for performance reasons).
+ * Also removed some default parameters ($depth) and some superfluous 
+ * code ($sql_count).
+ *
+ * @param $tids
+ *   An array of term IDs to match.
+ * @param $operator
+ *   How to interpret multiple IDs in the array. Can be "or" or "and".
+ * @return
+ *   A resource identifier pointing to the query results.
+ */
+function refine_by_taxo_select_nodes($tids = array(), $operator = 'or') {
+  if (count($tids) > 0) {
+    // For each term ID, generate an array of descendant term IDs to the right depth.
+    $descendant_tids = array();
+    foreach ($tids as $index => $tid) {
+      $term = taxonomy_get_term($tid);
+      $tree = taxonomy_get_tree($term->vid, $tid, -1, 0);
+      $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
+    }
+
+    if ($operator == 'or') {
+      $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
+      $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 AND n.moderate = 0';
+    }
+    else {
+      $joins = '';
+      $wheres = '';
+      foreach ($descendant_tids as $index => $tids) {
+        $joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
+        $wheres .= ' AND tn'. $index .'.tid IN ('. implode(',', $tids) .')';
+      }
+      $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '. $joins .' WHERE n.status = 1 AND n.moderate = 0 '. $wheres;
+    }
+    $sql = db_rewrite_sql($sql);
+    $result = db_query($sql);
+  }
+
+  return $result;
+}
 
 /**
  * Returns a list of nids that appear on a term page. Not paged!
@@ -256,7 +300,7 @@ function refine_by_taxo_current_nids() {
 
   $tids = refine_by_taxo_current_terms(FALSE);
   $op = refine_by_taxo_get_leading_op();
-  $result = taxonomy_select_nodes($tids, $op, 0, FALSE);
+  $result = refine_by_taxo_select_nodes($tids, $op);
 
   while ($row = db_fetch_object($result)) {
     $nids[] = $row->nid;
