From 170b674a0953ce95e06f7a8442eb0f1097e7ee72 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Sun, 16 Mar 2003 07:02:20 +0000 Subject: - All LIMIT queries must go through the pager or through db_query_range(). The syntax for db_query_range() was enhanced so it matches db_query(). So you may pass extra arguments of the SQL statement which are checked via check_query() and then substituted into the SQL statement. After these optional arguments, you always pass $from and $count parameters which define your range. Most often, the $from is 0 and the count is the max number of records you want returned. Patch by Moshe. - The pager_query() function for PEAR was enhanced so that it adds proper GROUP BY statement counting the number of records to be paged. Patch by James Arthur. - MSSQL database scheme by Moshe. --- modules/forum.module | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'modules/forum.module') diff --git a/modules/forum.module b/modules/forum.module index 3b87fc97c..cf4d9fd1b 100644 --- a/modules/forum.module +++ b/modules/forum.module @@ -85,11 +85,11 @@ function forum_block($op = "list", $delta = 0) { if (empty($cache)) { unset($items); - $content = node_title_list(db_query("SELECT n.nid, n.title, u.uid, u.name, GREATEST(n.created, MAX(c.timestamp)) AS sort FROM node n, forum f LEFT JOIN comments c ON c.nid = n.nid LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'forum' AND n.nid = f.nid AND n.status = 1 GROUP BY n.nid ORDER BY sort DESC LIMIT ". variable_get("forum_block_num", "5")), t("Active forum topics:")); + $content = node_title_list(db_query_range("SELECT n.nid, n.title, u.uid, u.name, GREATEST(n.created, MAX(c.timestamp)) AS sort FROM node n, forum f LEFT JOIN comments c ON c.nid = n.nid LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'forum' AND n.nid = f.nid AND n.status = 1 GROUP BY n.nid ORDER BY sort DESC", 0, variable_get("forum_block_num", "5")), t("Active forum topics:")); $content .= "
"; unset ($items); - $content .= node_title_list(db_query("SELECT n.nid, n.title, u.uid, u.name FROM node n LEFT JOIN forum f ON n.nid = f.nid LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'forum' ORDER BY n.nid DESC LIMIT ". variable_get("forum_block_num", "5")), t("New forum topics:")); + $content .= node_title_list(db_query_range("SELECT n.nid, n.title, u.uid, u.name FROM node n LEFT JOIN forum f ON n.nid = f.nid LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'forum' ORDER BY n.nid DESC", 0, variable_get("forum_block_num", "5")), t("New forum topics:")); if ($content) { $content .= "
". l(t("more"), "forum") ."
"; @@ -272,12 +272,12 @@ function _forum_num_comments($nid) { } function _forum_last_comment($nid) { - $value = db_fetch_object(db_query("SELECT timestamp FROM comments WHERE nid = '%d' AND status = 0 ORDER BY timestamp DESC LIMIT 1", $nid)); + $value = db_fetch_object(db_query_range("SELECT timestamp FROM comments WHERE nid = '%d' AND status = 0 ORDER BY timestamp DESC", $nid, 0, 1)); return ($value) ? format_date($value->timestamp, "small") : " "; } function _forum_last_reply($nid) { - $value = db_fetch_object(db_query("SELECT c.timestamp, u.name, u.uid FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.nid = '%d' AND c.status = 0 ORDER BY c.timestamp DESC LIMIT 1", $nid)); + $value = db_fetch_object(db_query_range("SELECT c.timestamp, u.name, u.uid FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.nid = '%d' AND c.status = 0 ORDER BY c.timestamp DESC", $nid, 0, 1)); return $value; } @@ -366,9 +366,9 @@ function _forum_topics_read($uid) { } function _forum_last_post($term) { - $topic = db_fetch_object(db_query("SELECT n.nid, n.created AS timestamp, u.name AS name, u.uid AS uid FROM node n, forum f LEFT JOIN users u ON n.uid = u.uid WHERE f.tid = '%d' AND n.nid = f.nid AND n.type = 'forum' AND n.status = 1 ORDER BY timestamp DESC LIMIT 1", $term)); + $topic = db_fetch_object(db_query_range("SELECT n.nid, n.created AS timestamp, u.name AS name, u.uid AS uid FROM forum f, node n LEFT JOIN users u ON n.uid = u.uid WHERE f.tid = '%d' AND n.nid = f.nid AND n.type = 'forum' AND n.status = 1 ORDER BY timestamp DESC", $term, 0, 1)); - $reply = db_fetch_object(db_query("SELECT n.nid, c.timestamp, u.name AS name, u.uid AS uid FROM forum f, node n LEFT JOIN comments c ON n.nid = c.nid LEFT JOIN users u ON c.uid = u.uid WHERE f.tid = '%d' AND n.nid = f.nid AND n.type = 'forum' AND n.status = 1 AND c.status = 0 ORDER BY c.timestamp DESC LIMIT 1", $term)); + $reply = db_fetch_object(db_query_range("SELECT n.nid, c.timestamp, u.name AS name, u.uid AS uid FROM forum f, node n LEFT JOIN comments c ON n.nid = c.nid LEFT JOIN users u ON c.uid = u.uid WHERE f.tid = '%d' AND n.nid = f.nid AND n.type = 'forum' AND n.status = 1 AND c.status = 0 ORDER BY c.timestamp DESC", $term, 0, 1)); $value = ($topic->timestamp > $reply->timestamp) ? $topic : $reply; @@ -386,7 +386,7 @@ function forum_get_topics($tid, $sortby, $forum_per_page) { // show topics with the correct tid, or in the forum but with shadow = 1 $sql = "SELECT n.nid, title, u.name AS name, u.uid AS uid, n.created AS timestamp, GREATEST(n.created, MAX(c.timestamp)) AS date_sort, COUNT(c.nid) AS num_comments, icon, n.comment AS comment_mode, f.tid FROM node n, term_node r LEFT JOIN users u ON n.uid = u.uid LEFT JOIN comments c ON c.nid = n.nid LEFT JOIN forum f ON n.nid = f.nid WHERE n.nid = r.nid AND ( (r.tid = '".check_query($tid)."' AND f.shadow = 1) OR f.tid = '".check_query($tid)."' ) AND n.status = 1 AND n.type = 'forum' GROUP BY n.nid ORDER BY $sql_sortby"; - $sql_count = "SELECT COUNT(DISTINCT(n.nid)) FROM node n, term_node r LEFT JOIN forum f ON n.nid = f.nid WHERE n.nid = r.nid AND ( (r.tid = '".check_query($tid)."' AND f.shadow = 1) OR f.tid = '".check_query($tid)."' ) AND n.status = 1 AND n.type = 'forum'"; + $sql_count = "SELECT COUNT(DISTINCT(n.nid)) FROM term_node r, node n LEFT JOIN forum f ON n.nid = f.nid WHERE n.nid = r.nid AND ( (r.tid = '".check_query($tid)."' AND f.shadow = 1) OR f.tid = '".check_query($tid)."' ) AND n.status = 1 AND n.type = 'forum'"; $result = pager_query($sql, $forum_per_page, 0, $sql_count); $topic_num = db_num_rows($result); @@ -437,7 +437,7 @@ function _forum_new($tid) { $read[] = $r->nid; } - $nid = db_result(db_query("SELECT n.nid FROM node n, forum f WHERE n.type = 'forum' AND f.nid = n.nid AND n.status = 1 AND f.tid = '%d' ".($read ? "AND NOT (n.nid IN (".implode(",", $read).")) " : "") ."ORDER BY created LIMIT 1", $tid)); + $nid = db_result(db_query_range("SELECT n.nid FROM node n, forum f WHERE n.type = 'forum' AND f.nid = n.nid AND n.status = 1 AND f.tid = '%d' ".($read ? "AND NOT (n.nid IN (".implode(",", $read).")) " : "") ."ORDER BY created", $tid, 0, 1)); return $nid ? $nid : 0; } -- cgit v1.2.3