? modules/database.mysql Index: modules/forum.module =================================================================== RCS file: /cvs/drupal/drupal/modules/forum.module,v retrieving revision 1.336 diff -u -p -r1.336 forum.module --- modules/forum.module 5 Jul 2006 11:45:51 -0000 1.336 +++ modules/forum.module 7 Jul 2006 22:12:05 -0000 @@ -146,6 +146,23 @@ function forum_nodeapi(&$node, $op, $tea } /** + * Implementation of hook_comment(). + */ +function forum_comment(&$comment, $op) { + switch ($op) { + case 'insert': + // node is already statically cached. + $node = node_load($comment['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); + } + break; + } +} + +/** * Implementation of hook_taxonomy(). */ function forum_taxonomy($op, $type, $term = NULL) { @@ -255,6 +272,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 +281,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, $title); 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, $title); break; } @@ -376,6 +424,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 +462,9 @@ 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); } /** @@ -420,6 +472,9 @@ 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); } /** @@ -708,9 +763,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)); } } @@ -733,15 +802,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; Index: modules/node.module =================================================================== RCS file: /cvs/drupal/drupal/modules/node.module,v retrieving revision 1.654 diff -u -p -r1.654 node.module --- modules/node.module 5 Jul 2006 11:45:51 -0000 1.654 +++ modules/node.module 7 Jul 2006 22:12:07 -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; } /** @@ -2475,6 +2475,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". */