backdrop-issues
backdrop-issues copied to clipboard
Improve how `node_comment_statistics` works
Description of the bug
Stats update function for node comments does not work if the node_comment_statistics table is missing respective records.
Steps To Reproduce
Encountered this issue when a Views table sorted by Content: Updated/commented date (desc) didn't want to display some nodes. The missing nodes came up only when I deleted the filter. That made me realize the node_comment_statistics table was missing entries for troublesome nodes.
DB was never meddled directly, the respective nodes were never deleted, so not sure how the node_comment_statistics table could've lost some entries. Maybe the comment_node_predelete() was somehow involved.
Anyway, leaving the problem of how exactly the respective entries in the node_comment_statistics table got deleted aside, I believe the db_update function responsible for comment statistics must be improved to become fool-prove against situations when respective records somehow are missing in the node_comment_statistics table.
You can manually delete an entry in node_comment_statistics table remembering nid and then try to add comment to that node and see the stats for the node will never update, which in turn makes lots of statistic based Views settings/filters dysfunctional.
Actual behavior
The following lines of in https://github.com/backdrop/backdrop/blob/be768da3a31a70bfcc4598bcff0c19132f487070/core/modules/comment/comment.entity.inc#L419
db_update('node_comment_statistics')
->fields(array(
'cid' => $last_reply->cid,
'comment_count' => $count,
'last_comment_timestamp' => $last_reply->changed,
'last_comment_name' => $last_reply->uid ? '' : $last_reply->name,
'last_comment_uid' => $last_reply->uid,
))
->condition('nid', $nid)
->execute();
assume the needed $nid is always there, which unfortunately is not the case. I suggest to improve the above lines to ensure the respective entry does exist before attempting to update it.