? taxonomy_count_nodes-1.414.2.16.patch Index: modules/taxonomy/taxonomy.module =================================================================== RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.module,v retrieving revision 1.414.2.16 diff -u -p -r1.414.2.16 taxonomy.module --- modules/taxonomy/taxonomy.module 6 Aug 2010 11:10:57 -0000 1.414.2.16 +++ modules/taxonomy/taxonomy.module 7 Sep 2010 16:45:26 -0000 @@ -912,48 +912,23 @@ function taxonomy_get_synonym_root($syno function taxonomy_term_count_nodes($tid, $type = 0) { static $count; - if (!isset($count[$type])) { - // $type == 0 always evaluates TRUE if $type is a string + $term = taxonomy_get_term($tid); + $tree = taxonomy_get_tree($term->vid, $tid); + + $tids = array($tid); + foreach ($tree as $descendant) { + $tids[] = $descendant->tid; + } + + if (!isset($count[$type][$tid])) { if (is_numeric($type)) { - $result = db_query(db_rewrite_sql('SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 GROUP BY t.tid')); + $count[$type][$tid] = db_result(db_query(db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 AND t.tid IN (%s)"), implode(',', $tids))); } else { - $result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type); - } - $count[$type] = array(); - while ($term = db_fetch_object($result)) { - $count[$type][$term->tid] = $term->c; - } - } - $children_count = 0; - foreach (_taxonomy_term_children($tid) as $c) { - $children_count += taxonomy_term_count_nodes($c, $type); - } - return $children_count + (isset($count[$type][$tid]) ? $count[$type][$tid] : 0); -} - -/** - * Helper for taxonomy_term_count_nodes(). Used to find out - * which terms are children of a parent term. - * - * @param $tid - * The parent term's ID - * - * @return array - * An array of term IDs representing the children of $tid. - * Results are statically cached. - * - */ -function _taxonomy_term_children($tid) { - static $children; - - if (!isset($children)) { - $result = db_query('SELECT tid, parent FROM {term_hierarchy}'); - while ($term = db_fetch_object($result)) { - $children[$term->parent][] = $term->tid; + $count[$type][$tid] = db_result(db_query(db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 AND n.type = '%s' AND t.tid IN (%s)"), $type, implode(',', $tids))); } } - return isset($children[$tid]) ? $children[$tid] : array(); + return $count[$type][$tid]; } /**