--- forum.module.orig	2007-08-29 17:53:22.000000000 -0700
+++ forum.module.new	2007-11-11 23:13:24.000000000 -0800
@@ -713,48 +713,82 @@ function _forum_format($topic) {
  */
 function forum_get_forums($tid = 0) {
 
+  global $user;
+  
   $forums = array();
-  $_forums = taxonomy_get_tree(variable_get('forum_nav_vocabulary', ''), $tid);
 
+  $_forums = taxonomy_get_tree(variable_get('forum_nav_vocabulary', ''), $tid);
+  
   if (count($_forums)) {
-
-    $counts = array();
-
-    $sql = "SELECT r.tid, COUNT(n.nid) AS topic_count, SUM(l.comment_count) AS comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {term_node} r ON n.nid = r.nid WHERE n.status = 1 AND n.type = 'forum' GROUP BY r.tid";
-    $sql = db_rewrite_sql($sql);
-    $_counts = db_query($sql, $forum->tid);
-    while ($count = db_fetch_object($_counts)) {
-      $counts[$count->tid] = $count;
+    
+    if (module_exists('advcache')) {
+      $cache_key = 'counts::' . $tid . '::' . advcache_array2int($user->roles);
+      $cache = cache_get($cache_key, 'cache_forum');
+      $counts = $cache->data;
+    }
+    
+    if (!$counts) {
+
+      $counts = array();
+  
+      $sql = "SELECT r.tid, COUNT(n.nid) AS topic_count, SUM(l.comment_count) AS comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {term_node} r ON n.nid = r.nid WHERE n.status = 1 AND n.type = 'forum' GROUP BY r.tid";
+      $sql = db_rewrite_sql($sql);
+      $_counts = db_query($sql, $forum->tid);
+  
+      while ($count = db_fetch_object($_counts)) {
+        $counts[$count->tid] = $count;
+      }
+      
+      if (module_exists('advcache')) {
+        cache_set($cache_key, 'cache_forum', $counts, time() + variable_get('advcache_forum_cache_timeout', 60 * 15));
+      }
     }
   }
 
   foreach ($_forums as $forum) {
-    if (in_array($forum->tid, variable_get('forum_containers', array()))) {
-      $forum->container = 1;
-    }
 
-    if ($counts[$forum->tid]) {
-      $forum->num_topics = $counts[$forum->tid]->topic_count;
-      $forum->num_posts = $counts[$forum->tid]->topic_count + $counts[$forum->tid]->comment_count;
+    if (module_exists('advcache')) {
+      $cached = FALSE;
+      $cache_key = 'forum::' . $forum->tid . '::' . advcache_array2int($user->roles);
+      if ($cache = cache_get($cache_key, 'cache_forum')) {
+        $forum = $cache->data;
+        $cached = TRUE;
+      }
     }
-    else {
-      $forum->num_topics = 0;
-      $forum->num_posts = 0;
+  
+    if (!$cached) {
+      if (in_array($forum->tid, variable_get('forum_containers', array()))) {
+        $forum->container = 1;
+      }
+  
+      if ($counts[$forum->tid]) {
+        $forum->num_topics = $counts[$forum->tid]->topic_count;
+        $forum->num_posts = $counts[$forum->tid]->topic_count + $counts[$forum->tid]->comment_count;
+      }
+      else {
+        $forum->num_topics = 0;
+        $forum->num_posts = 0;
+      }
+  
+      // This query does not use full ANSI syntax since MySQL 3.x does not support
+      // table1 INNER JOIN table2 INNER JOIN table3 ON table2_criteria ON table3_criteria
+      // used to join node_comment_statistics to users.
+      $sql = "SELECT ncs.last_comment_timestamp, IF (ncs.last_comment_uid != 0, u2.name, ncs.last_comment_name) AS last_comment_name, ncs.last_comment_uid FROM {node} n INNER JOIN {users} u1 ON n.uid = u1.uid INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid INNER JOIN {users} u2 ON ncs.last_comment_uid=u2.uid WHERE n.status = 1 AND n.type='forum' AND tn.tid = %d ORDER BY ncs.last_comment_timestamp DESC";
+      $sql = db_rewrite_sql($sql);
+  
+      $topic = db_fetch_object(db_query_range($sql, $forum->tid, 0, 1));
+  
+      $last_post = new stdClass();
+      $last_post->timestamp = $topic->last_comment_timestamp;
+      $last_post->name = $topic->last_comment_name;
+      $last_post->uid = $topic->last_comment_uid;
+      $forum->last_post = $last_post;
+      
+      if (module_exists('advcache')) {
+        cache_set($cache_key, 'cache_forum', $forum, time() + variable_get('advcache_forum_cache_timeout', 60 * 15));
+      }
     }
-
-    // This query does not use full ANSI syntax since MySQL 3.x does not support
-    // table1 INNER JOIN table2 INNER JOIN table3 ON table2_criteria ON table3_criteria
-    // used to join node_comment_statistics to users.
-    $sql = "SELECT ncs.last_comment_timestamp, IF (ncs.last_comment_uid != 0, u2.name, ncs.last_comment_name) AS last_comment_name, ncs.last_comment_uid FROM {node} n INNER JOIN {users} u1 ON n.uid = u1.uid INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid INNER JOIN {users} u2 ON ncs.last_comment_uid=u2.uid WHERE n.status = 1 AND n.type='forum' AND tn.tid = %d ORDER BY ncs.last_comment_timestamp DESC";
-    $sql = db_rewrite_sql($sql);
-    $topic = db_fetch_object(db_query_range($sql, $forum->tid, 0, 1));
-
-    $last_post = new stdClass();
-    $last_post->timestamp = $topic->last_comment_timestamp;
-    $last_post->name = $topic->last_comment_name;
-    $last_post->uid = $topic->last_comment_uid;
-    $forum->last_post = $last_post;
-
+    
     $forums[$forum->tid] = $forum;
   }
 
@@ -782,51 +816,81 @@ function forum_get_topics($tid, $sortby,
     array('data' => t('Last reply'), 'field' => 'l.last_comment_timestamp'),
   );
 
-  $order = _forum_get_topic_order($sortby);
-  for ($i = 0; $i < count($forum_topic_list_header); $i++) {
-    if ($forum_topic_list_header[$i]['field'] == $order['field']) {
-      $forum_topic_list_header[$i]['sort'] = $order['sort'];
+  if (module_exists('advcache')) {
+    $page = isset($_GET['page']) ? $_GET['page'] : '';
+    $cache_key = 'topics::' . $tid . '::' . $page . '::' . advcache_array2int($user->roles);
+    $pager_key = 'pager::topics::' . $tid . '::' . advcache_array2int($user->roles);
+    if ( ($cache = cache_get($cache_key, 'cache_forum')) && ($pager_cache = cache_get($pager_key, 'cache_forum')) ) {
+
+      $topics = $cache->data;
+
+      // Get the pager
+      global $pager_page_array, $pager_total, $pager_total_items;
+      $pager = $pager_cache->data;
+      $pager_page_array = $pager->pager_page_array;
+      $pager_total = $pager->pager_total;
+      $pager_total_items = $pager->pager_total_items;
     }
   }
 
-  $term = taxonomy_get_term($tid);
-
-  $sql = db_rewrite_sql("SELECT n.nid, f.tid, n.title, n.sticky, u.name, u.uid, n.created AS timestamp, n.comment AS comment_mode, l.last_comment_timestamp, IF(l.last_comment_uid != 0, cu.name, l.last_comment_name) AS last_comment_name, l.last_comment_uid, l.comment_count AS num_comments FROM {node_comment_statistics} l, {users} cu, {term_node} r, {users} u, {forum} f, {node} n WHERE n.status = 1 AND l.last_comment_uid = cu.uid AND n.nid = l.nid AND n.nid = r.nid AND r.tid = %d AND n.uid = u.uid AND n.vid = f.vid");
-  $sql .= tablesort_sql($forum_topic_list_header, 'n.sticky DESC,');
-  $sql .= ', n.created DESC';  // Always add a secondary sort order so that the news forum topics are on top.
-
-  $sql_count = db_rewrite_sql("SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.status = 1 AND n.type = 'forum'");
-
-  $result = pager_query($sql, $forum_per_page, 0, $sql_count, $tid);
-  $topics = array();
-  while ($topic = db_fetch_object($result)) {
-    if ($user->uid) {
-      // folder is new if topic is new or there are new comments since last visit
-      if ($topic->tid != $tid) {
-        $topic->new = 0;
+  if (!$topics) {
+    $order = _forum_get_topic_order($sortby);
+    for ($i = 0; $i < count($forum_topic_list_header); $i++) {
+      if ($forum_topic_list_header[$i]['field'] == $order['field']) {
+        $forum_topic_list_header[$i]['sort'] = $order['sort'];
+      }
+    }
+  
+    $term = taxonomy_get_term($tid);
+  
+    $sql = db_rewrite_sql("SELECT n.nid, f.tid, n.title, n.sticky, u.name, u.uid, n.created AS timestamp, n.comment AS comment_mode, l.last_comment_timestamp, IF(l.last_comment_uid != 0, cu.name, l.last_comment_name) AS last_comment_name, l.last_comment_uid, l.comment_count AS num_comments FROM {node_comment_statistics} l, {users} cu, {term_node} r, {users} u, {forum} f, {node} n WHERE n.status = 1 AND l.last_comment_uid = cu.uid AND n.nid = l.nid AND n.nid = r.nid AND r.tid = %d AND n.uid = u.uid AND n.vid = f.vid");
+    $sql .= tablesort_sql($forum_topic_list_header, 'n.sticky DESC,');
+    $sql .= ', n.created DESC';  // Always add a secondary sort order so that the news forum topics are on top.
+  
+    $sql_count = db_rewrite_sql("SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.status = 1 AND n.type = 'forum'");
+  
+    $result = pager_query($sql, $forum_per_page, 0, $sql_count, $tid);
+    $topics = array();
+    while ($topic = db_fetch_object($result)) {
+      if ($user->uid) {
+        // folder is new if topic is new or there are new comments since last visit
+        if ($topic->tid != $tid) {
+          $topic->new = 0;
+        }
+        else {
+          $history = _forum_user_last_visit($topic->nid);
+          $topic->new_replies = comment_num_new($topic->nid, $history);
+          $topic->new = $topic->new_replies || ($topic->timestamp > $history);
+        }
       }
       else {
-        $history = _forum_user_last_visit($topic->nid);
-        $topic->new_replies = comment_num_new($topic->nid, $history);
-        $topic->new = $topic->new_replies || ($topic->timestamp > $history);
+        // Do not track "new replies" status for topics if the user is anonymous.
+        $topic->new_replies = 0;
+        $topic->new = 0;
       }
+  
+      if ($topic->num_comments > 0) {
+        $last_reply = new stdClass();
+        $last_reply->timestamp = $topic->last_comment_timestamp;
+        $last_reply->name = $topic->last_comment_name;
+        $last_reply->uid = $topic->last_comment_uid;
+        $topic->last_reply = $last_reply;
+      }
+      $topics[] = $topic;
     }
-    else {
-      // Do not track "new replies" status for topics if the user is anonymous.
-      $topic->new_replies = 0;
-      $topic->new = 0;
-    }
+    
+    if (module_exists('advcache')) {
+      cache_set($cache_key, 'cache_forum', $topics, time() + variable_get('advcache_forum_cache_timeout', 60 * 15));
 
-    if ($topic->num_comments > 0) {
-      $last_reply = new stdClass();
-      $last_reply->timestamp = $topic->last_comment_timestamp;
-      $last_reply->name = $topic->last_comment_name;
-      $last_reply->uid = $topic->last_comment_uid;
-      $topic->last_reply = $last_reply;
+      // cache the pager
+      global $pager_page_array, $pager_total, $pager_total_items;
+      $pager->pager_page_array = $pager_page_array;
+      $pager->pager_total = $pager_total;
+      $pager->pager_total_items = $pager_total_items;
+      cache_set($pager_key, 'cache_forum', $pager);
     }
-    $topics[] = $topic;
   }
-
+  
   return $topics;
 }

