Deleting old comments from Drupal 5 site
I have a D5 site with thousands of nodes and many, many more comments. Since this site is news-oriented,many of the stories are no longer relevant and are kept for archival reasons. I would like to keep the nodes and delete all the comments relating to nodes that are older then one year ag开发者_运维技巧o.
I'm comfortable with SQL and building my own module, I'm wondering what issues I might run into if I delete old comments. Any tips or advice would be most welcome.
:]
Deleting a comment is not as simple as deleting a row from the database. I wrote this sippet a while ago to delete all comments from a Drupal 5 site:
<?php
$result = db_query('SELECT c.*, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid');
while ($comment = db_fetch_object($result)) {
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
_comment_delete_thread($comment);
_comment_update_node_statistics($comment->nid);
}
?>
It's based on the code in comment_delete() and comment_confirm_delete_submit() and makes sure that all relevant actions like invoking hooks and updating statistics are performed. In your case, you would have to add a where clause to the query to make sure you're only deleting the old comments.
Depending on the performance of your server and the number of comments you have to delete, you may want to add a set_time_limit() in the while block to prevent a php timeout.
This code snippet can be run from a custom module, but you can also install Devel module and run the code from it's 'execute php' block.
If you are familiar with the Views module, I think you'd be able to delete old comments in bulk using the Views Bulk Operations module. The Drupal 5 version is no longer listed on the module page, but you can find it by clicking on View All Releases.
There shouldn't be any problems. Wipe them out!
DISCLAIMER: I have never developed using drupal 5.x. Only 6. So I may need some correction on some small details.
I was going to recommend Views Bulk Operations to delete the comments per node type, however, it doesnt seem to be developed for 5.x.
You have a few options on how to do this. Here is one:
UPDATE HOOK:
If you have a helper module, create an update hook. Then you can run this using update.php. The logic behind this would be something like (i haven't tested the query)
SELECT c.cid FROM comments c
INNER JOIN node ON n.nid = c.nid
WHERE n.type = 'story'
then
foreach ($cids as $cid) {
comment_delete($cid);
}
However, if there are too many comments, your request may time out.
精彩评论