Importing/displaying legacy comments

Having trouble installing serendipity?
Post Reply
laze
Regular
Posts: 9
Joined: Wed Aug 23, 2006 7:48 pm

Importing/displaying legacy comments

Post by laze »

I have a 6+ year old site that I'm going to port to Serendipity... the potential problem is importing the old data because it's a hacked-together bunch of code that uses flat text files.

With entries, it shouldn't be too bad as I can do a generic RSS import.

However, with comments it gets a tad trickier. Here's a little background:

* there is exactly one entry (no more, no less) each day
* each entry has an associated text comment file in the form 20060823. htmf
* the current engine simply displays the text file based on the entry date
* comments haven't necessarily followed any particular format through the years, so they can't really be parsed

I need to be able to display these old comments for the entries. It doesn't matter to me that I won't be able to import them into Serendipity as "official" comments, as long as they can be displayed. There are two potential solutions:

* (less likely) Import each day's entire comments as a single Serendipity comment
* (more likely) Hack up a template (?) to display the comment text file for an entry and then display any "real" comments submitted through Serendipity. It does not matter to me if the old comments and new comments have a different appearance.

Any thoughts about how I might go about this?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Importing/displaying legacy comments

Post by garvinhicking »

Hi!

Do you have the possibility of associating an imported entry from your old blog with the new entries?

So that you can say "comments from 20060823.htmf" belong to "entry #12"? If you can say yes to that, showing the comments will be easy.

But I suppose you can't say that. Then you would have the option of displaying the comment files depending on an entry's age. But if you have more than one entry on a given date, the comments would be displayed to all entries of that date.

What we're gonna do is create a custom smarty function like "show_legacy_comments" which uses the entry-date (or id) to load the proper comment file [and parse it, if needed and possible]. Then you can call that custom smarty function in your entries.tpl template, parallel to the internal comment section of serendipity.

HTH,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
laze
Regular
Posts: 9
Joined: Wed Aug 23, 2006 7:48 pm

Post by laze »

Hi Garvin --

As long as the order of the imported entries makes sense (ie. the first item in the RSS feed is ID #1 and the rest associate sequentially), then yes, it should be easy. Since there's exactly one post every day, it would just be a matter of using a "number of days since" algorithm +1 to figure out the entry ID. We could rename the comment files according that pretty easily.

So, assuming we can do that, how would I then go about tweaking serendipity to display the comments for that entry from the text file first and then from the serendipity database next?

Thanks for your help!


... Ryan
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Actually, since you can associate the comment to the entry with relative ease, I think your "less likely" solution is the easier of the two. Then again, I just remembered that HTML is not allowed in comments. Ick.

To display the comments in the template, you'll want to create a config.inc.php like this:

Code: Select all

<?php
function show_legacy_comments($id) {
  return file_get_contents('./legacy_comments/' . $id . '.htmf');
}

function smarty_show_legacy_comments($params, &$smarty) {
  return show_legacy_comments($params['id']);
}

$serendipity['smarty']->register_function('show_legacy_comments', 'smarty_show_legacy_comments');
?>
Then move the legacy comments into the serendipity/legacy_comments directory, and rename them to match {the entry id}.htmf. Finally, in your template, find entries.tpl and modify it to include this code:

Code: Select all

{if $is_single_entry and not $is_preview}
{show_legacy_comments id=$entry.id}
{/if}
Put that in the comments section, where you'd like the legacy comments to appear, probably around <div class="serendipity_comments serendipity_section_comments> or so. (If you put it *inside* that div, you can leave out the {if} parts, becaues they're already there.

I think that'll do what you want. If the comments are already formatted in HTML, you're golden. If not, you may need to do some formatting in the show_legacy_comments() method.
Judebert
---
Website | Wishlist | PayPal
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

As a sidenote: Judeberts solution should be perfect. A way of modifying the code would involve changing/formatting/introspecting the $entry['timestamp'] value, format that into the format that correlates with the comments HTML filename, and include that one.

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
laze
Regular
Posts: 9
Joined: Wed Aug 23, 2006 7:48 pm

Post by laze »

Thanks so much, guys! I wrote a quickie PHP script to rename all of the files, added the code you all suggested, and it's all working.

I appreciate your help.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

You're welcome! I hope you'll have fun with serendipity. :)

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

<Keanu>Woah.</Keanu>

I'm reasonably surprised that it worked on the first shot. But I'm pleased! I guess I'm getting the hang of this.

I'd love to look at your site. Can we have the URL?
Judebert
---
Website | Wishlist | PayPal
laze
Regular
Posts: 9
Joined: Wed Aug 23, 2006 7:48 pm

Post by laze »

We'll be relaunching tomorrow (hopefully). When it's all up and working, I'll point you towards it!
Post Reply