Index: forum.module
===================================================================
RCS file: /var/cvsroot/drupal/modules/forum/forum.module,v
retrieving revision 1.1.1.2
retrieving revision 1.10
diff -u -p -r1.1.1.2 -r1.10
--- forum.module	30 Aug 2007 00:53:22 -0000	1.1.1.2
+++ forum.module	31 Oct 2007 05:39:19 -0000	1.10
@@ -713,48 +725,113 @@ function _forum_format($topic) {
  */
 function forum_get_forums($tid = 0) {
 
+  global $user;
+  
   $forums = array();
+
   $_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;
   }
 
@@ -774,59 +851,73 @@ function _forum_topics_unread($term, $ui
 function forum_get_topics($tid, $sortby, $forum_per_page) {
   global $user, $forum_topic_list_header;
 
-  $forum_topic_list_header = array(
-    array('data' => '&nbsp;', 'field' => NULL),
-    array('data' => t('Topic'), 'field' => 'n.title'),
-    array('data' => t('Replies'), 'field' => 'l.comment_count'),
-    array('data' => t('Created'), 'field' => 'n.created'),
-    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);
+    if ($cache = cache_get($cache_key, 'cache_forum')) {
+      $topics = $cache->data;
+    }
+  }
+  
+  if (!$topics) {
+    
+    $forum_topic_list_header = array(
+      array('data' => '&nbsp;', 'field' => NULL),
+      array('data' => t('Topic'), 'field' => 'n.title'),
+      array('data' => t('Replies'), 'field' => 'l.comment_count'),
+      array('data' => t('Created'), 'field' => 'n.created'),
+      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'];
+      }
     }
-  }
-
-  $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;
+  
+    $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 ($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;
+    
+    if (module_exists('advcache')) {
+      cache_set($cache_key, 'cache_forum', $topics, time() + variable_get('advcache_forum_cache_timeout', 60 * 15));
     }
-    $topics[] = $topic;
   }
-
   return $topics;
 }

