Commit a723204e authored by Dries's avatar Dries
Browse files

- Patch #47691 by wtanaka: performance improvement for _forum_topics_read.

parent df492f35
......@@ -703,18 +703,14 @@ function forum_get_forums($tid = 0) {
return $forums;
}
function _forum_topics_read($term, $uid) {
// Calculate the number of topics the user has read. Assume all entries older
// than NODE_NEW_LIMIT are read, and include the recent posts that user has
// read.
$sql = "SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.created <= %d AND n.status = 1 AND n.type = 'forum'";
$sql = db_rewrite_sql($sql);
$ancient = db_result(db_query($sql, $term, NODE_NEW_LIMIT));
$sql = "SELECT COUNT(n.nid) FROM {node} n INNER JOIN {history} h ON n.nid = h.nid AND h.uid = %d INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.status = 1 AND n.type = 'forum' AND n.created > %d";
/**
* Calculate the number of nodes the user has not yet read and are newer
* than NODE_NEW_LIMIT.
*/
function _forum_topics_unread($term, $uid) {
$sql = 'SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid AND tn.tid = %d LEFT JOIN {history} h ON n.nid = h.nid AND h.uid = %d WHERE n.status = 1 AND n.type = "forum" AND n.created > %d AND h.nid IS NULL';
$sql = db_rewrite_sql($sql);
$recent = db_result(db_query($sql, $uid, $term, NODE_NEW_LIMIT));
return $ancient + $recent;
return db_result(db_query($sql, $term, $uid, NODE_NEW_LIMIT));
}
function forum_get_topics($tid, $sortby, $forum_per_page) {
......@@ -915,11 +911,9 @@ function theme_forum_list($forums, $parents, $tid) {
$rows[] = array(array('data' => $description, 'class' => 'container', 'colspan' => '4'));
}
else {
$forum->old_topics = _forum_topics_read($forum->tid, $user->uid);
if ($user->uid) {
$new_topics = $forum->num_topics - $forum->old_topics;
}
else {
$new_topics = _forum_topics_unread($forum->tid, $user->uid);
$forum->old_topics = $forum->num_topics - $new_topics;
if (!$user->uid) {
$new_topics = 0;
}
......
......@@ -703,18 +703,14 @@ function forum_get_forums($tid = 0) {
return $forums;
}
function _forum_topics_read($term, $uid) {
// Calculate the number of topics the user has read. Assume all entries older
// than NODE_NEW_LIMIT are read, and include the recent posts that user has
// read.
$sql = "SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.created <= %d AND n.status = 1 AND n.type = 'forum'";
$sql = db_rewrite_sql($sql);
$ancient = db_result(db_query($sql, $term, NODE_NEW_LIMIT));
$sql = "SELECT COUNT(n.nid) FROM {node} n INNER JOIN {history} h ON n.nid = h.nid AND h.uid = %d INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.status = 1 AND n.type = 'forum' AND n.created > %d";
/**
* Calculate the number of nodes the user has not yet read and are newer
* than NODE_NEW_LIMIT.
*/
function _forum_topics_unread($term, $uid) {
$sql = 'SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid AND tn.tid = %d LEFT JOIN {history} h ON n.nid = h.nid AND h.uid = %d WHERE n.status = 1 AND n.type = "forum" AND n.created > %d AND h.nid IS NULL';
$sql = db_rewrite_sql($sql);
$recent = db_result(db_query($sql, $uid, $term, NODE_NEW_LIMIT));
return $ancient + $recent;
return db_result(db_query($sql, $term, $uid, NODE_NEW_LIMIT));
}
function forum_get_topics($tid, $sortby, $forum_per_page) {
......@@ -915,11 +911,9 @@ function theme_forum_list($forums, $parents, $tid) {
$rows[] = array(array('data' => $description, 'class' => 'container', 'colspan' => '4'));
}
else {
$forum->old_topics = _forum_topics_read($forum->tid, $user->uid);
if ($user->uid) {
$new_topics = $forum->num_topics - $forum->old_topics;
}
else {
$new_topics = _forum_topics_unread($forum->tid, $user->uid);
$forum->old_topics = $forum->num_topics - $new_topics;
if (!$user->uid) {
$new_topics = 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment