From c19c1eaab041f6a21015900cca5cef6773a157dd Mon Sep 17 00:00:00 2001
From: Geoff Appleby <gapple@490940.no-reply.drupal.org>
Date: Tue, 29 Mar 2011 12:06:24 -0700
Subject: [PATCH] Optimized and reduced number of DB queries

Simplified queries within taxonomy_blocks_config()
Merged queries in theme functions
Refactored taxonomy term retrieval functions
---
 taxonomyblocks.module |  172 ++++++++++++++++++++++++++-----------------------
 1 files changed, 91 insertions(+), 81 deletions(-)

diff --git a/taxonomyblocks.module b/taxonomyblocks.module
index ef091b5..af82adc 100644
--- a/taxonomyblocks.module
+++ b/taxonomyblocks.module
@@ -184,50 +184,44 @@ function taxonomy_blocks_config() {
       $path_set       = '';
       $custom_title_set = '';
       $weight_set       = '';
-      $custom_title_set = db_result(db_query("SELECT custom_title FROM {tb_customized} WHERE tb_id = %d and vid = %d", $tb_id, $vocab->vid));
-      $enabled_set    = db_result(db_query("SELECT enabled FROM {tb_customized} WHERE tb_id = %d and vid = %d", $tb_id, $vocab->vid));
-      $counter_set    = db_result(db_query("SELECT counter FROM {tb_customized} WHERE tb_id = %d and vid = %d", $tb_id, $vocab->vid));
-      $path_set       = db_result(db_query("SELECT path_pattern FROM {tb_customized} WHERE tb_id = %d and vid = %d", $tb_id, $vocab->vid));
-      $link_title_set = db_result(db_query("SELECT title_pattern FROM {tb_customized} WHERE tb_id = %d and vid = %d", $tb_id, $vocab->vid));
-      $weight_set     = db_result(db_query("SELECT weight FROM {tb_customized} WHERE tb_id = %d and vid = %d", $tb_id, $vocab->vid));
-      if (!empty($custom_title_set)) {
-        $custom_title_def = $custom_title_set;
+      $customresult = db_query("SELECT enabled, custom_title, counter, path_pattern, title_pattern, weight FROM {tb_customized} WHERE tb_id = %d and vid = %d", $tb_id, $vocab->vid);
+      $customdata = db_fetch_object($customresult);
+
+      $enabled_def = ($customdata->enabled == 1);
+
+      if (!empty($customdata->custom_title)) {
+        $custom_title_def = $customdata->custom_title;
       }
       else {
         $custom_title_def = check_plain($vocab->name);
       }
-      if (!empty($counter_set)) {
-        $node_count_def = $counter_set;
+      if (!empty($customdata->counter)) {
+        $node_count_def = $customdata->counter;
       }
       else {
         $node_count_def = 'all';
       }
-      if (!empty($path_set)) {
-        $path_def = $path_set;
+      if (!empty($customdata->path_pattern)) {
+        $path_def = $customdata->path_pattern;
       }
       else {
         $path_def = 'taxonomy/term/%tid';
       }
-      if (!empty($link_title_set)) {
-        $link_title_def = $link_title_set;
+      if (!empty($customdata->title_pattern)) {
+        $link_title_def = $customdata->title_pattern;
       }
       else {
         $link_title_def = 'Content tagged with @termname';
       }
-      if ($enabled_set == 1) {
-        $enabled_def = TRUE;
-      }
-      else {
-        $enabled_def = FALSE;
-      }
-      if (!empty($weight_set)) {
-        $weight_def = $weight_set;
+
+      if (!empty($customdata->weight)) {
+        $weight_def = $customdata->weight;
       }
       else {
         $weight_def = 0;
       }
     }
-    if ($ismodified == 0) {
+    else if ($ismodified == 0) {
 
       if ($tb_id != 1) {
         $link_title_def = 'Content tagged with @termname';
@@ -243,11 +237,12 @@ function taxonomy_blocks_config() {
       $custom_title_def = check_plain($vocab->name);
       $weight_def = 0;
     }
-    $tb_id_omit_def = db_result(db_query("SELECT omit_tids FROM {tb_added_list} WHERE tb_id = %d", $tb_id));
-    $omit_tids = explode(',', $tb_id_omit_def);
-    $tb_id_open_def = db_result(db_query("SELECT open_tids FROM {tb_added_list} WHERE tb_id = %d", $tb_id));
-    $open_tids = explode(',', $tb_id_open_def);
-    $maintitle_def = db_result(db_query("SELECT block_title FROM {tb_added_list} WHERE tb_id = %d", $tb_id));
+    $tb_added_result = db_result(db_query("SELECT omit_tids, open_tids, block_title FROM {tb_added_list} WHERE tb_id = %d", $tb_id));
+    $tb_added_data = db_fetch_object($tb_added_result);
+    $omit_tids = explode(',', $tb_added_result->omit_tids);
+    $open_tids = explode(',', $tb_added_result->open_tids);
+    $maintitle_def = $tb_added_result->block_title;
+
     $content_types = array();
     $content_types['all'] = t('All Content Types');
 
@@ -261,10 +256,9 @@ function taxonomy_blocks_config() {
       '#title' => 'Main Title for block',
       '#default_value' => $maintitle_def,
     );
-    $optionsresult = db_query("SELECT type FROM {vocabulary_node_types} WHERE vid = %d", $vocab->vid);
+    $optionsresult = db_query("SELECT vnt.type, nt.name FROM {vocabulary_node_types} vnt LEFT JOIN {node_type} nt ON vnt.type = nt.type WHERE vid = %d", $vocab->vid);
     while ($enabled = db_fetch_object($optionsresult)) {
-      $type_readable = db_result(db_query("SELECT name from {node_type} WHERE type = '%s'", $enabled->type));
-      $content_types[$enabled->type] = $type_readable;
+      $content_types[$enabled->type] = $enabled->name;
     }
     $content_types['none'] = t("Don't show node count");
 
@@ -567,14 +561,16 @@ function taxonomyblocks_block($op = 'list', $delta = 0, $edit = array()) {
 function theme_multi_taxonomyblock($tb_id = 0) {
   if (arg(0) == 'node' && is_numeric(arg(1))) {
     $nid = arg(1);
-    $nodeuid = db_result(db_query("SELECT uid FROM {node} WHERE nid = %d", $nid));
-    $nodetype = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $nid));
+    $node = node_load($nid);
+    $nodeuid = $node->uid;
+    $nodetype = $node->type;
   }
   global $user;
   if ($user->uid) {
     $useruid = $user->uid;
   }
-  $ismodified = db_result(db_query("SELECT custom FROM {tb_added_list} WHERE tb_id = %d", $tb_id));
+  $tb_added_result = db_fetch_object(db_query("SELECT custom, omit_tids, open_tids FROM {tb_added_list} WHERE tb_id = %d", $tb_id));
+  $ismodified = $tb_added_result->custom;
   if ($ismodified == 0) {
     if ($tb_id != 1) {
       $result = db_query("SELECT vid FROM {vocabulary} WHERE name <> 'Forums' ");
@@ -586,9 +582,9 @@ function theme_multi_taxonomyblock($tb_id = 0) {
     }
 
   }
-  if ($ismodified == 1) {
+  else if ($ismodified == 1) {
     $result = db_query("SELECT vid FROM {tb_customized} WHERE tb_id = %d AND enabled = 1 ORDER BY weight ASC", $tb_id);
-    $omit = db_result(db_query("SELECT omit_tids FROM {tb_added_list} WHERE tb_id = %d", $tb_id));
+    $omit = $tb_added_result->omit_tids;
 
     if (!empty($omit)) {
       $omit = explode(',', $omit);
@@ -596,7 +592,7 @@ function theme_multi_taxonomyblock($tb_id = 0) {
     else {
       $omit = array();
     }
-    $open = db_result(db_query("SELECT open_tids FROM {tb_added_list} WHERE tb_id = %d", $tb_id));
+    $open = $tb_added_result->open_tids;
     $open = explode(',', $open);
   }
   while ($vocabs = db_fetch_object($result)) {
@@ -604,7 +600,7 @@ function theme_multi_taxonomyblock($tb_id = 0) {
   }
   if (!empty($pre_vocab_list)) {
     foreach ($pre_vocab_list as $vid) {
-      $hasterms = db_result(db_query("SELECT tid FROM {term_data} WHERE vid = %d", $vid));
+      $hasterms = db_result(db_query("SELECT tid FROM {term_data} WHERE vid = %d LIMIT 1", $vid));
       if (!empty($hasterms)) {
         $toplevel = taxonomy_get_toplevel($vid);
         $nodecounter = db_result(db_query("SELECT counter FROM {tb_customized} WHERE tb_id = %d AND vid = %d", $tb_id, $vid));
@@ -638,15 +634,17 @@ function theme_multi_taxonomyblock($tb_id = 0) {
           $nodecounter  = 'all';
           $titlepattern = 'Content tagged with @termname';
         }
-        if ($ismodified == 0 && $tb_id == 1) {
+        else if ($ismodified == 0 && $tb_id == 1) {
           $pathpattern  = 'forum/%tid';
           $nodecounter  = 'forum';
           $titlepattern = 'Forums categorized as %tid';
         }
-        if ($ismodified == 1) {
-          $pathpattern = db_result(db_query("SELECT path_pattern FROM {tb_customized} WHERE tb_id = %d AND vid = %d", $tb_id, $vid));
-          $nodecounter = db_result(db_query("SELECT counter FROM {tb_customized} WHERE tb_id = %d AND vid = %d", $tb_id, $vid));
-          $titlepattern = db_result(db_query("SELECT title_pattern FROM {tb_customized} WHERE tb_id = %d AND vid = %d", $tb_id, $vid));
+        else if ($ismodified == 1) {
+          $customized_result = db_fetch_object(db_query("SELECT path_pattern, counter, title_pattern, custom_title FROM {tb_customized} WHERE tb_id = %d AND vid = %d", $tb_id, $vid));
+          $pathpattern = $customized_result->path_pattern;
+          $nodecounter = $customized_result->counter;
+          $titlepattern = $customized_result->title_pattern;
+          $vocabname = $customized_result->custom_title;
         }
 
         $additional = variable_get('tb_additional', 0);
@@ -681,9 +679,6 @@ function theme_multi_taxonomyblock($tb_id = 0) {
           if ($ismodified == 0) {
             $vocabname = db_result(db_query("SELECT name FROM {vocabulary} WHERE vid = %d", $vid));
           }
-          if ($ismodified != 0) {
-            $vocabname = db_result(db_query("SELECT custom_title FROM {tb_customized} WHERE tb_id = %d AND vid = %d", $tb_id, $vid));
-          }
 
           if (($vocabname != '<none>') && !empty($menu)) {
             if (function_exists('tt')) {
@@ -706,14 +701,16 @@ function theme_multi_taxonomyblock($tb_id = 0) {
 function theme_forum_taxonomyblock() {
   if (arg(0) == 'node' && is_numeric(arg(1))) {
     $nid = arg(1);
-    $nodeuid = db_result(db_query("SELECT uid FROM {node} WHERE nid = %d", $nid));
-    $nodetype = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $nid));
+    $node = node_load($nid);
+    $nodeuid = $node->uid;
+    $nodetype = $node->type;
   }
   global $user;
   if ($user->uid) {
     $useruid = $user->uid;
   }
-  $ismodified = db_result(db_query("SELECT custom FROM {tb_added_list} WHERE tb_id = 1"));
+  $tb_added_result = db_fetch_object(db_query("SELECT custom, omit_tids, open_tids FROM {tb_added_list} WHERE tb_id = 1"));
+  $ismodified = $tb_added_list->custom;
   if ($ismodified == 0) {
     $pathpattern = 'forum/%tid';
     $nodecounter = 'forum';
@@ -726,14 +723,15 @@ function theme_forum_taxonomyblock() {
       $titlepattern = $forum->title_pattern;
       $nodecounter  = $forum->counter;
     }
-    $omit = db_result(db_query("SELECT omit_tids FROM {tb_added_list} WHERE tb_id = 1"));
+
+    $omit = $tb_added_result->omit_tids;
     if (!empty($omit)) {
       $omit = explode(',', $omit);
     }
     else {
       $omit = array();
     }
-    $open = db_result(db_query("SELECT open_tids FROM {tb_added_list} WHERE tb_id = 1"));
+    $open = $tb_added_result->open_tids;
     $open = explode(',', $open);
   }
 
@@ -783,52 +781,64 @@ function term_has_parent($tid) {
   return $hasparent;
 }
 
- function taxonomy_get_toplevel($vid) {
-  //Check to insure that the vid has terms.
-  $result = db_query("SELECT tid FROM {term_data} WHERE vid = %d ORDER BY weight,name ASC", $vid);
+/**
+ * Retrieve a list of the top level terms in a vocabulary.
+ *
+ * @param $vid
+ * @return
+ *   An array of term ids
+ */
+function taxonomy_get_toplevel($vid) {
+  $result = db_query("SELECT tid FROM {term_data} td JOIN {term_hierarchy} th ON td.tid = th.tid WHERE vid = %d AND parent = 0 ORDER BY weight,name ASC", $vid);
 
   while ($term = db_fetch_object($result)) {
-    $hasparent = term_has_parent($term->tid);
-    if ($hasparent == 0) {
-    //Instead of counting here we will put this off till the creation of final array.
       $toplevel_array[] = $term->tid;
-    }
   }
   return $toplevel_array;
 }
+/**
+ * Retrieve a list of a term's children, ordered by weight
+ *
+ * @param $parent_tid
+ * @return
+ *   An array of term ids
+ */
 function get_one_level_children($parent_tid) {
   $result = db_query("SELECT th.tid FROM {term_hierarchy} th, {term_data} td WHERE th.parent = %d AND td.tid = th.tid ORDER BY td.weight,td.name ASC", $parent_tid);
   while ($term = db_fetch_object($result)) {
-    $children[]   = $term->tid;
+    $children[] = $term->tid;
   }
   return $children;
 }
-function get_all_children($parent_tid, $prev = NULL) {
-  $level = '';
-  $level = get_one_level_children($parent_tid);
+/**
+ * Retrieve a list of a term's descendents
+ *
+ * @param $parent_tid
+ * @return
+ *   An array of term ids
+ */
+function get_all_children($parent_tid) {
 
-  if (!empty($level) && $prev != NULL) {
-    $children = array_merge($level, $prev);
-  }
-  if (!empty($level) && $prev == NULL) {
-      $children = $level;
-  }
-  if (empty($level) && $prev != NULL) {
-    $children = $prev;
-  }
+  $children = array();
+  $unprocessed = array($parent_tid);
 
-  if (!empty($level)) {
-    foreach ($level as $ctid) {
-      $next = array();
-      $next = get_all_children($ctid, $children);
-      if (!empty($next)) {
-        $children = array_merge($next, $children);
-      }
+  while ($unprocessed) {
+    $result = db_query(
+      "SELECT th.tid FROM {term_hierarchy} th WHERE th.parent IN(%s)",
+      implode(',',$unprocessed)
+    );
+
+    // Build the unprocessed list for the next iteration.
+    $unprocessed = array();
+    while ($term = db_fetch_object($result)) {
+      $unprocessed[] = $term->tid;
     }
+    // Don't re-process terms.
+    $unprocessed = array_diff($unprocessed, $children);
+
+    $children = array_merge($children, $unprocessed);
   }
-  if (is_array($children)) {
-    $children = array_unique($children);
-  }
+
   return $children;
 }
 /**
-- 
1.7.4.msysgit.0

