Index: modules/forum.module =================================================================== RCS file: /cvs/drupal/drupal/modules/Attic/forum.module,v retrieving revision 1.328.2.3 diff -u -p -r1.328.2.3 forum.module --- modules/forum.module 14 Jul 2006 12:16:01 -0000 1.328.2.3 +++ modules/forum.module 31 Jul 2006 01:10:57 -0000 @@ -146,6 +146,32 @@ function forum_nodeapi(&$node, $op, $tea } /** + * Implementation of hook_comment(). + */ +function forum_comment(&$comment, $op) { + switch ($op) { + case 'delete': + case 'update': + case 'insert': + // I hate inconsistent APIs. + if (is_object($comment)) { + $nid = $comment->nid; + } + else { + $nid = $comment['nid']; + } + $node = node_load($nid); + if ($node->type == 'forum') { + cache_clear_all("block:forum:0:", TRUE); + cache_clear_all('result:forum:forum_get_forums:0:', TRUE); + cache_clear_all("result:forum:forum_get_forums:1:$node->tid:", TRUE); + cache_clear_all("result:forum:forum_topic_navigation:$node->tid:", TRUE); + } + break; + } +} + +/** * Implementation of hook_taxonomy(). */ function forum_taxonomy($op, $type, $term = NULL) { @@ -255,6 +281,7 @@ function forum_block($op = 'list', $delt case 'save': variable_set('forum_block_num_'. $delta, $edit['forum_block_num_'. $delta]); + cache_clear_all("block:forum:$delta:", TRUE); break; case 'view': @@ -263,19 +290,49 @@ function forum_block($op = 'list', $delt case 0: $title = t('Active forum topics'); $sql = db_rewrite_sql("SELECT n.nid, n.title, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 AND n.type = 'forum' ORDER BY l.last_comment_timestamp DESC"); - $result = db_query_range($sql, 0, variable_get('forum_block_num_0', '5')); - if (db_num_rows($result)) { - $content = node_title_list($result); + $md5_sql = md5($sql); + if (node_access_usage()) { + global $user; + $cache_id = "block:forum:0:$md5_sql:$user->uid"; + } + else { + $cache_id = 'block:forum:0:$md5_sql:'; + } + if ($cache = cache_get($cache_id)) { + $items = unserialize($cache->data); } + else { + $result = db_query_range($sql, 0, variable_get('forum_block_num_0', '5')); + if (db_num_rows($result)) { + $items = node_title_list($result); + } + cache_set($cache_id, serialize($items), time() + (60 * 60 * 24)); + } + $content = theme('node_list', $items); break; case 1: $title = t('New forum topics'); $sql = db_rewrite_sql("SELECT n.nid, n.title, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.type = 'forum' AND n.status = 1 ORDER BY n.nid DESC"); - $result = db_query_range($sql, 0, variable_get('forum_block_num_1', '5')); - if (db_num_rows($result)) { - $content = node_title_list($result); + $md5_sql = md5($sql); + if (node_access_usage()) { + global $user; + $cache_id = "block:forum:1:$md5_sql:$user->uid"; + } + else { + $cache_id = "block:forum:1:$md5_sql:"; + } + if ($cache = cache_get($cache_id)) { + $items = unserialize ($cache->data); + } + else { + $result = db_query_range($sql, 0, variable_get('forum_block_num_1', '5')); + if (db_num_rows($result)) { + $items = node_title_list($result); + } + cache_set($cache_id, serialize($items), time() + (60 * 60 * 24)); } + $content = theme('node_list', $items); break; } @@ -376,6 +433,7 @@ function forum_update($node) { else { db_query('UPDATE {forum} SET tid = %d WHERE vid = %d', $node->tid, $node->vid); } + cache_clear_all('block:forum:', TRUE); } /** @@ -413,6 +471,10 @@ function forum_prepare(&$node) { */ function forum_insert($node) { db_query('INSERT INTO {forum} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $node->tid); + cache_clear_all('block:forum:', TRUE); + cache_clear_all('result:forum:forum_get_forums:0:', TRUE); + cache_clear_all("result:forum:forum_get_forums:1:$node->tid:", TRUE); + cache_clear_all("result:forum:forum_topic_navigation:$node->tid:", TRUE); } /** @@ -420,6 +482,10 @@ function forum_insert($node) { */ function forum_delete(&$node) { db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid); + cache_clear_all('block:forum:', TRUE); + cache_clear_all('result:forum:forum_get_forums:0:', TRUE); + cache_clear_all("result:forum:forum_get_forums:1:$node->tid:", TRUE); + cache_clear_all("result:forum:forum_topic_navigation:$node->tid:$node->nid:", TRUE); } /** @@ -696,9 +762,23 @@ function forum_get_forums($tid = 0) { $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; + $md5_sql = md5($sql); + if (node_access_usage()) { + global $user; + $cache_id = "result:forum:forum_get_forums:0:$md5_sql:$user->uid"; + } + else { + $cache_id = "result:forum:forum_get_forums:0:$md5_sql:"; + } + if ($cache = cache_get($cache_id)) { + $counts = unserialize($cache->data); + } + else { + $_counts = db_query($sql, $forum->tid); + while ($count = db_fetch_object($_counts)) { + $counts[$count->tid] = $count; + } + cache_set($cache_id, serialize($counts), time() + (60 * 60 * 24)); } } @@ -721,15 +801,28 @@ function forum_get_forums($tid = 0) { // 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 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; + $md5_sql = md5($sql); + if (node_access_usage()) { + global $user; + $cache_id = "result:forum:forum_get_forums:1:$forum->tid:$md5_sql:$user->uid"; + } + else { + $cache_id = "result:forum:forum_get_forums:1:$forum->tid:$md5_sql:"; + } + if ($cache = cache_get($cache_id)) { + $forums[$forum->tid] = unserialize($cache->data); + } + else { + $topic = db_fetch_object(db_query_range($sql, $forum->tid, 0, 1)); - $forums[$forum->tid] = $forum; + $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; + cache_set($cache_id, serialize($forum), time() + (60 * 60 * 24)); + } } return $forums; @@ -1039,34 +1132,80 @@ function theme_forum_icon($new_posts, $n return $output; } + /** - * Format the next/previous forum topic navigation links. + * A helper function that determines the next/previous forum topics relative to + * a given forum topic. * - * @ingroup themeable - */ -function theme_forum_topic_navigation($node) { - $output = ''; + * @param + * A fully loaded node object of type 'forum' + * + * @return + * An array with elements 'prev' and 'next', which may be NULL or objects. + * If they are not NULL, the nid and title propeties will be defined + * corresponding to the nid and title for the previous and next nodes in that + * particular forum.. + */ + +function prev_next_forum_topic($node) { + + $pn = array(); + if ($node->type != 'forum') { + return $forum_nav; + } + // get previous and next topic $sql = "SELECT n.nid, n.title, n.sticky, l.comment_count, l.last_comment_timestamp FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.status = 1 AND n.type = 'forum' ORDER BY n.sticky DESC, ". _forum_get_topic_order_sql(variable_get('forum_order', 1)); - $result = db_query(db_rewrite_sql($sql), $node->tid); + $md5_sql = md5($sql); + if (node_access_usage()) { + global $user; + $cache_id = "result:forum:forum_topic_navigation:$node->tid:$node->nid:$md5_sql:$user->uid"; + } + else { + $cache_id = "result:forum:forum_topic_navigation:$node->tid:$node->nid:$md5_sql:"; + } + if ($cache = cache_get($cache_id)) { + $forum_nav = unserialize($cache->data); + } + else { + $result = db_query(db_rewrite_sql($sql), $node->tid); - while ($topic = db_fetch_object($result)) { - if ($stop == 1) { - $next = new StdClass(); - $next->nid = $topic->nid; - $next->title = $topic->title; - break; - } - if ($topic->nid == $node->nid) { - $stop = 1; - } - else { - $prev = new StdClass(); - $prev->nid = $topic->nid; - $prev->title = $topic->title; + while ($topic = db_fetch_object($result)) { + if ($stop == 1) { + $next = new StdClass(); + $next->nid = $topic->nid; + $next->title = $topic->title; + break; + } + if ($topic->nid == $node->nid) { + $stop = 1; + } + else { + $prev = new StdClass(); + $prev->nid = $topic->nid; + $prev->title = $topic->title; + } } + $forum_nav['next'] = $next; + $forum_nav['prev'] = $prev; + cache_set($cache_id, serialize($forum_nav), time() + (60 * 60 * 24)); } + + return $forum_nav; +} + +/** + * Format the next/previous forum topic navigation links. + * + * @ingroup themeable + */ +function theme_forum_topic_navigation($node) { + $output = ''; + + $forum_nav = prev_next_forum_topic($node); + $next = $forum_nav['next']; + $prev = $forum_nav['prev']; if ($prev || $next) { $output .= '
'; @@ -1084,7 +1223,6 @@ function theme_forum_topic_navigation($n $output .= '
'; } - return $output; } Index: modules/node.module =================================================================== RCS file: /cvs/drupal/drupal/modules/Attic/node.module,v retrieving revision 1.641.2.12 diff -u -p -r1.641.2.12 node.module --- modules/node.module 26 Jul 2006 12:12:27 -0000 1.641.2.12 +++ modules/node.module 31 Jul 2006 01:10:58 -0000 @@ -64,18 +64,18 @@ function node_cron() { * * @param $result * A DB result object from a query to fetch node objects. If your query joins the node_comment_statistics table so that the comment_count field is available, a title attribute will be added to show the number of comments. - * @param $title - * A heading for the resulting list. * * @return - * An HTML list suitable as content for a block. + * An array of HTML suitable as content for a block. */ -function node_title_list($result, $title = NULL) { +function node_title_list($result) { + $items = array(); + while ($node = db_fetch_object($result)) { $items[] = l($node->title, 'node/'. $node->nid, $node->comment_count ? array('title' => format_plural($node->comment_count, '1 comment', '%count comments')) : ''); } - return theme('node_list', $items, $title); + return $items; } /** @@ -2471,6 +2471,20 @@ function node_db_rewrite_sql($query, $pr } /** + * Returns if the node_access table is used + */ +function node_access_usage() { + static $status = NULL; + + if (!isset($count)) { + $count = db_result(db_query('SELECT COUNT(*) FROM {node_access}')); + $status = ($count > 1) ? TRUE : FALSE; + } + + return $status; +} + +/** * @} End of "defgroup node_access". */