I would very much like to modify the tracker on my site so that it includes, for every node, a link to the last comment that was posted. This feature is pretty common in forum software like PHPBB, Simple Machines, etc.
However, after filing a feature request for the Views module (http://drupal.org/node/292126 for details) I received a wontfix response from the developer.
After investigating the reasons given, I have to agree with him that it's pretty difficult to do. Yes, it's possible, but it's certainly not trivial. And the thing is, it could be trivial!
All that needs to happen is for the comment module to store the last comment's cid in the node_comment_statistics table whenever it updates that table. I mean, if we look at the code, the cid even gets retrieved from the database, it just never gets used...
$last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = %d ORDER BY cid DESC', $nid, COMMENT_PUBLISHED, 0, 1));
db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = %d, last_comment_name = '%s', last_comment_uid = %d WHERE nid = %d", $count, $last_reply->timestamp, $last_reply->uid ? '' : $last_reply->name, $last_reply->uid, $nid);
Comment | File | Size | Author |
---|---|---|---|
#14 | drupal.node-comment-stats-cid.13.patch | 6.43 KB | sun |
#12 | drupal.node-comment-stats-cid.11.patch | 6.11 KB | sun |
#9 | drupal.node-comment-stats-cid.9.patch | 6.12 KB | sun |
#7 | drupal.node-comment-stats-cid.patch | 5.33 KB | sun |
Comments
Comment #1
Damien Tournoud CreditAttribution: Damien Tournoud commentedWell, isn't that what comment_new_page_count() is for? [1]
The current data structure assume that most of the time the number of comments of a node will not be superior to the maximum number of comment per page.
A last_cid could be a nice addition, anyway.
[1] http://api.drupal.org/api/function/comment_new_page_count/6
Comment #2
Daitaka CreditAttribution: Daitaka commentedI'll certainly look into it.
However, so far I've found that the 'new ' status of comments and nodes does not accurately reflect what I'm looking for. I want a link to the last comment made in a forum thread, no matter how many years ago it was posted. Drupal's 'new' status seems to be based solely on what was posted since my last visit to the site, and on any subsequent visits, if there is nothing new then there's also no link available.
Comment #3
Pasqualle#188847: ifac follow-up: easy way to get to end of issue
I think if a simple SQL to get the last comment can run on d.o. then it can run on your site also..
Comment #4
MichelleJust to add another use case, I finally had to drop the last comment from the last post info in advanced forum because it was just to costly to get. Having this would have made my life much easier. I think it would be a good addition.
Michelle
Comment #5
sunViews is unable to display anything for the last comment: #196439: A 'Last Comment' field/relationship would be nice
I have no idea why we store arbitrary values instead of the {comment}.cid - that's totally insane.
Comment #6
merlinofchaos CreditAttribution: merlinofchaos commentedStoring arbitrary values is actually rather handy for the nodecomment.module
That said, a cid would still be very valuable.
Comment #7
sunQuite simple + even allows to simplify some existing queries (perhaps even more, but these were the obvious instances I found by grepping).
Comment #9
sunoopsie. ;)
Comment #11
catchThis would almost certainly help with some barmy forum queries too (and if not, forum feature requests).
Comment #12
sunThis one should pass again.
Comment #14
sunNow for real.
Also fixing a missing 'node_access' query tag for the recent comments block that's touched by this patch.
Comment #15
sunok - final review, anyone?
Comment #16
Dries CreditAttribution: Dries commentedI vaguely remember we spent a lot of time optimizing that code in presence of large data sets. Maybe it has been deprecated, maybe it had good reasons, maybe I'm mistaken. I'll try to dig the archives tomorrow to see if I can dig up the details.
Comment #17
Dries CreditAttribution: Dries commentedMmm, I tried to look for it but can't find anything. This is probably RTBC then. Let's mark it as RTBC and see if someone objects ...
Comment #18
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks!
Comment #20
Nick Brown CreditAttribution: Nick Brown commentedAny chance of this being backported to druapl 6.x ?
Comment #21
Tarsjusz CreditAttribution: Tarsjusz commentedbackporting is definately a good idea
Comment #22
grendzy CreditAttribution: grendzy commentedOpened a follow-up here: #1149912: Recent comments block displays old, out-of-order comments.
The reason for orderBy('ncs.last_comment_timestamp') isn't clear... It seems to produce nonsensical results.
Comment #23
Dean Reilly CreditAttribution: Dean Reilly commentedIn case anyone stumbles on this like I did, I've been working on back-porting this patch to Drupal 6 via a module. You can take a look at how far I've got here:
http://drupal.org/sandbox/DeanReilly/1587188
It seems to be working pretty well but it won't match the core patch for performance due to the nature of comment hooks in Drupal 6.