From df2e5ef505d954230956a4109aebf72198e4a3f4 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Tue, 13 Jul 2004 20:44:13 +0000 Subject: - Patch #8942 by Morbus: when testing under devel.module, the "SELECT timestamp from {history}" SQL statement is executed multiple times in two different functions. This duplicated code should be placed in a function, and that's been done already with node_last_visited() - the remaining code was just never updated to use the new routine. This patch changes the old code to use node_last_visited, and also modifies node_last_visited() to cache the result of the database call. --- modules/node.module | 14 ++++++++------ modules/node/node.module | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/modules/node.module b/modules/node.module index af41ab7a2..4044d64cd 100644 --- a/modules/node.module +++ b/modules/node.module @@ -104,8 +104,7 @@ function node_tag_new($nid) { global $user; if ($user->uid) { - $result = db_query('SELECT timestamp FROM {history} WHERE uid = %d AND nid = %d', $user->uid, $nid); - if (db_fetch_object($result)) { + if (node_last_viewed($nid)) { db_query('UPDATE {history} SET timestamp = %d WHERE uid = %d AND nid = %d', time(), $user->uid, $nid); } else { @@ -120,9 +119,13 @@ function node_tag_new($nid) { */ function node_last_viewed($nid) { global $user; + static $history; - $history = db_fetch_object(db_query("SELECT timestamp FROM {history} WHERE uid = '$user->uid' AND nid = %d", $nid)); - return ($history->timestamp ? $history->timestamp : NODE_NEW_LIMIT); + if (!isset($history[$nid])) { + $history[$nid] = db_fetch_object(db_query("SELECT timestamp FROM {history} WHERE uid = '$user->uid' AND nid = %d", $nid)); + } + + return ($history[$nid]->timestamp ? $history[$nid]->timestamp : 0); } /** @@ -140,8 +143,7 @@ function node_is_new($nid, $timestamp) { if (!isset($cache[$nid])) { if ($user->uid) { - $history = db_fetch_object(db_query('SELECT timestamp FROM {history} WHERE uid = %d AND nid = %d', $user->uid, $nid)); - $cache[$nid] = $history->timestamp ? $history->timestamp : 0; + $cache[$nid] = node_last_viewed($nid); } else { $cache[$nid] = time(); diff --git a/modules/node/node.module b/modules/node/node.module index af41ab7a2..4044d64cd 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -104,8 +104,7 @@ function node_tag_new($nid) { global $user; if ($user->uid) { - $result = db_query('SELECT timestamp FROM {history} WHERE uid = %d AND nid = %d', $user->uid, $nid); - if (db_fetch_object($result)) { + if (node_last_viewed($nid)) { db_query('UPDATE {history} SET timestamp = %d WHERE uid = %d AND nid = %d', time(), $user->uid, $nid); } else { @@ -120,9 +119,13 @@ function node_tag_new($nid) { */ function node_last_viewed($nid) { global $user; + static $history; - $history = db_fetch_object(db_query("SELECT timestamp FROM {history} WHERE uid = '$user->uid' AND nid = %d", $nid)); - return ($history->timestamp ? $history->timestamp : NODE_NEW_LIMIT); + if (!isset($history[$nid])) { + $history[$nid] = db_fetch_object(db_query("SELECT timestamp FROM {history} WHERE uid = '$user->uid' AND nid = %d", $nid)); + } + + return ($history[$nid]->timestamp ? $history[$nid]->timestamp : 0); } /** @@ -140,8 +143,7 @@ function node_is_new($nid, $timestamp) { if (!isset($cache[$nid])) { if ($user->uid) { - $history = db_fetch_object(db_query('SELECT timestamp FROM {history} WHERE uid = %d AND nid = %d', $user->uid, $nid)); - $cache[$nid] = $history->timestamp ? $history->timestamp : 0; + $cache[$nid] = node_last_viewed($nid); } else { $cache[$nid] = time(); -- cgit v1.2.3