I've been doing some hand fiddling with the Drupal comments file recently and have needed to rebuild the statistics and the tread field in the comments table. Where there are many scripts I've found to rebuild comment_statistics, there's little out there on rebuilding the comment thread field (which at first looks like a little black magic). I've built a script which walks all nodes and rebuilds both. (Parts of the script are mine, parts are lifted out of comments.module parts are bits of code I've found on drupal.org). I figured I'd post the code for it here (it's also the baseline for some code I'm going to post later in the week).
To be honest, I've lost track which lines are owned by which programmer, but all of it is either from the comments module or code I pulled from the drupal forum (If I find that post I'll credit it here). I'll also post the code over on drupal.org.
Sean Reiser, 40, is a developer, technologist, and amateur photographer. Sean has spent the past 20 years as a programmer, system architect and development manager. He is a life long New York resident.
Sean currently serves as the President and Chief Geek Officer of Repair Sense, Inc.. Please go to that site with any professional inquiries.
Sean can be found using a number of social networks. These are the ones he's most active on:
Thanks for this snippet - it might be really helpful to a lot of people. I'm painstakingly porting a database over from scoop, and I need to rebuild the threading. This might do it.
Can you post something about how you deploy this code? Where would you put it to run? How does it know about Drupal's internals?
If you have the development module installed you should be able to just do a copy / paste and hit execute from the "Run PHP Code" page. Alternatively, you could put it in a function in your template file and fire it off using Drush.
Awesome. Thanks!
Just a quick follow-up: the code worked perfectly with no modifications. Paste and run. It traversed an import of 40,000 comments and didn't even hiccup.
Thanks! You saved me many braincells.
HiSean,
I thank you again for the excellent script you did for thread and comment statistics.
It started to work very smothly yesterday night and showed number of comments for the first comments on my site correctly. Since I am Drupilyzing a huge DB and it seemed to work very long time I just went sleeping. Today I've found out itstuck at node 15945, for which and all nodes after which it did not show correct number of comments. I had run the script again and it stuck exactly at the same node.
So my question, how can I modify the script so that it started to crawl from the node 15945 and not from the very beginning if I start it again?
Thanks,
Yngens
Hi Yngens-
I know you've been persistant trying to get in touch with me today about this. I'm sorry I haven't been available sooner, I was taking a day off. I really haven't thought about this code in about a year (I wrote it, it did what I needed, I moved on), but looking at the code I don't see why it would stop at 15945 (or any other node).
First off, where the code hasn't been written to limit the run to a subset of nodes you could add a "where nid => XXXXX" to the first 3 lines in order to run against a certain group of nodes.
To debug this I'd start off starting a run at node 15945. If it hangs up again I'd start at 15946 and see if it runs through to the end. If it does there is some corruption in the comment chain for 15495 (example 2 nodes pointing at each other as their parent).
I will repost this the other places you've left this comment and reply to your email.