--- refine_by_taxo.module	2007-10-10 16:01:50.000000000 +0200
+++ refine_by_taxo.module.new	2007-10-11 12:00:53.000000000 +0200
@@ -251,7 +251,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;
@@ -317,3 +317,104 @@ function _refine_by_taxo_options() {
    'and_hierarchy' => t('Refine tags with AND, interconnected through hierarchy'),
   );
 }
+
+/**
+ * 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);
+    $limit = variable_get('refine_by_taxo_q_limit', 10);
+    if ($limit > 0) {
+      $result = db_query_range($sql, 0, $limit);
+    }
+    else {
+      $result = db_query($sql);
+    }
+  }
+
+  return $result;
+}
+
+/**
+ * Implementation of hook_menu
+ */
+function refine_by_taxo_menu($may_cache) {
+  global $user;
+  $items = array();
+
+  if ($may_cache) {
+    $items[] = array(
+      'path' => 'admin/settings/refine_by_taxo',
+      'title' => t('Refine by taxonomy'),
+      'description' => t('Control some functionalities within this module.'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('refine_by_taxo_admin_settings'),
+      'access' => user_access('administer site configuration'),
+      'type' => MENU_NORMAL_ITEM, // optional
+   );
+  }
+  return $items;
+}
+
+/**
+ * settings form
+ */
+function refine_by_taxo_admin_settings() {
+
+  $form['query_limit'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Node query limit'),
+  );
+  $form['query_limit']['refine_by_taxo_q_limit'] = array(
+    '#type' => 'select',
+    '#title' => t('Number of nodes'),
+    '#default_value' => variable_get('refine_by_taxo_q_limit', 10),
+    '#options' => array(
+      0 => t('no limit'),
+      10 => 10,
+      50 => 50,
+      100 => 100,
+      500 => 500,
+      1000 => 1000,
+      5000 => 5000,
+      10000 => 10000,
+    ),
+    '#description' => t('Set a number of nodes to limit the queries used by this module. Setting no limit on a site with thousands of nodes for each taxonomy term might be very CPU consuming, so consider to limit the query, which would gives you, obviously, less accurate results.'),
+  );
+
+  return system_settings_form($form);
+}
\ No newline at end of file
