diff --git a/core/modules/history/history.module b/core/modules/history/history.module index 0b529e0..d9a57b7 100644 --- a/core/modules/history/history.module +++ b/core/modules/history/history.module @@ -30,14 +30,50 @@ * of when the last view occurred; otherwise, zero. */ function history_read($nid) { - global $user; - $history = &drupal_static(__FUNCTION__, array()); + return history_read_multiple(array($nid)); +} + +/** + * Retrieves the timestamp for the current user's last view of a nodes. + * + * @param array $nids + * A node IDs. + * + * @return array + * Array of timestamps keyed by node ID. If a node has been previously viewed + * by the user, the timestamp in seconds of when the last view occurred; + * otherwise, zero. + */ +function history_read_multiple($nids) { + $history = &drupal_static('history_read', array()); + + $return = array(); + + $nodes_to_read = array(); + foreach ($nids as $nid) { + if (isset($history[$nid])) { + $return[$nid] = $history[$nid]; + } + else { + // Initialize value if current user has not viewed the node. + $nodes_to_read[$nid] = 0; + } + } + + if (empty($nodes_to_read)) { + return $return; + } - if (!isset($history[$nid])) { - $history[$nid] = db_query("SELECT timestamp FROM {history} WHERE uid = :uid AND nid = :nid", array(':uid' => $user->id(), ':nid' => $nid))->fetchObject(); + $result = db_query('SELECT timestamp FROM {history} WHERE uid = :uid AND nid IN(:nids)', array( + ':uid' => \Drupal::currentUser()->id(), + ':nids' => array_keys($nodes_to_read), + )); + foreach ($result as $row) { + $nodes_to_read[$row->nid] = $row->timestamp; } + $history += $nodes_to_read; - return (isset($history[$nid]->timestamp) ? $history[$nid]->timestamp : 0); + return $return + $nodes_to_read; } /**