Chris Schmidt (crschmidt) wrote,
Chris Schmidt
crschmidt

Wordpress and Trackback

For a long time, I've wanted Trackback to be implemented as an option for LiveJournal. I coded it about a year ago, and finally pulled all the code together in a series of patches for LiveJournal (http://zilla.livejournal.org/1613) about 4 months ago. However, in that time, there has been no response from employees asking me how my code works, why it works the way it does, or any kind of suggestion on how to make it better. So, I gave up on waiting on them. LiveJournal may someday implement trackback. They may even accept my code. But they aren't going to do it now, and they aren't going to do it quickly no matter what. So, I was on my own.

I've been thinking about doing it in a couple of ways lately. Writing my own trackback system, while interesting, would be time consuming, probably more so than I have any benefit out of. So, although that's still something that sounds nice, it's still pretty iffy on actually getting it done at any point before LiveJournal implements trackback, which, based on previous experience, should be about 2.5 years from now.

After that, I was considering Movable Type for a while. However, the words that mendel spoke were still plauging me: "Movable Type is not open source." You can, apparently, edit the code, but you can't redistribute the changes after you do - and that's not really any fun. Why code something you can't share with other people? (Unless of course, you're getting paid for it, in which case you sometimes have to.) Then the Movable Type licensing snafu came along, and convinced me that Six Apart and MT really aren't the kind of software I want to use to do this.

Drupal is great code - but it's missing a lot of key features that would make using it for this kind of project any good. Drupal is great as a content management system, but that's not what I wanted for this project. I just want trackbacks to be stored along with journal entries.

So, I looked at WordPress. Coded in PHP, and licensed under the GPL, I wasn't exactly worried about them changing things around underneath me. I also know a decent amount of PHP at this point, so I figured I could do the limited hacking I'd need to do. After confirming that the product could do what I wanted to mostly out of the box, I set to work downloading and installing Wordpress.

Installed from CVS last night. Checked the package out, created a database for WordPress, and went to the install page. Filled in the information that was needed - database name, username, password, site location - all stuff I'm used to from Drupal installs (which I can now do in under 10 minutes). Got the site set up. Simple, really. Less than 20 minutes start to finish, double checking my progress each step of the way.

Ask about the import tools. Find out that in the past, there was someone who volunteered to change LiveJournal's export format to MT's export format for users, who could then import via the MT import method built into WP. Found him on IRC, and he pointed out to me that this was no longer needed: there was now an import-livejournal.php script. All I had to do was edit the XMLINCLUDE file, and dump my entries in. I figured this was a great oppourtunity to test out Mark's jbackup script, which backs up your journal to a database, which you can then dump out in XML.

First, I had to clear the comments out of the way. The import script doesn't understand them (yet - someone started hacking on that as soon as I brought it to their attention last night), so they need to be pulled out. After playing with perl regexs for a bit, I decided to just go ahead and edit the jbackup code. I edited out the cdumper subroutine, so it would no longer dump out comments. Attempted a dump - worked like a charm. Score one for the good guys.

I then tried to import, and found that the script wasn't doing anything with the file I had given it. I couldn't figure out why - I had the right XML file, I had the right page, everything was setup right, but it would just not do anything, and die after a second. Frustrated, I looked at the import script, and discovered that the format which I had been using in my data was wrong. The XML store that is created by Mark's jbackup script is completely non-compliant with the XML dumped by the web XML export routine. Not only that, but the problem is made extra difficult by the fact that the <event> tag was used twice: once as a wrapper around the event, and once as the text in the event. Since the script used regexs to seperate the entries by using this tag, I was screwed on that front - it would end up splitting all body text into an extra entire post. Useless!

So, I set about, changing the XML format to match the web format as closely as possible. There were some snafus, but in general, it worked out pretty well. I eventually got it importing, and dumped all 900+ entries from my current journal into my new WordPress. But wait, there's more!

So, I dumped the entries, right? But that leaves a problem - the XML data has XML escaped information! This means that the entry text was all escaped - none of my links were links, because they all had &lt; and other entities in the middle of them.

I looked at it for a bit, then figured out why this was. I realized then: This is a job for Perl! String manipulation is what it does best! So, I pull out my trusty perl -pe, and do some fancy substitution on the command line. After a couple tries - to get all the entities I forgot - I finally imported one last time. All 939 entries came in without a hitch. The dates and times are preserved, as is the entry text and the subject. Eureka! I've struck gold! I have a fully functional copy of my journal! Not only that, but it's completely searchable! I can find any text string I want to in my entries. That's something that LiveJournal will probably never be able to offer me.

So, now that I have all my entries in, I s... oh crap. I realize it's 3 in the morning. So, that's the end of the adventure for the night. But the story isn't over yet! I still don't have trackback!

This morning, when I woke up, I got right back to work. First task - figure out how to associate my LiveJournal posts with the associated WP posts. This has some problems - the fact that my journal entries are not all in numerical order among them. To be honest, some of these issues aren't worked out yet - my recent posts will work fine in the current system, but older posts won't. I need to work out a solution to that, but that can wait until another day.

So, I go to my S2 layer. I edit my lay_print_commentbar function: that's the function which prints out the links at the end of each of my entries. I create a new integer: $trackback. This variable contains some math necessary to determine what post I'm actually tracking back to on my WP install.

var int trackback = $e.itemid; $trackback = $trackback / 256; $trackback = $trackback + 5461;

I know from long experience that the itemid is 256 times the post ID, plus a random number less than 256. So, by dividing by 256 into an integer, I'm getting the sequential post number, which I can use to identify the post in my WP install. I also know that my first post is at 5461: because of the testing I did, I imported my 900 entries about 6 times before I got it to work :)

So, that's how I get the number. Then, I use the two links that WP offers me:
For receiving: http://crschmidt.net/wordpress/wp-trackback.php/$trackback
For reading: http://crschmidt.net/wordpress/wp-comments-popup.php?p=$trackback&c=1 (this is the popup form of the comments page, with no entry attached).

It works! I can click the link, and it takes me to the appropriate entry. I can confirm that the trackback link is right there. I do need to do some cleanup of the WP comment page code, though. It says "Close this window" at the bottom - it's designed to be opened with javascript, which I'm not doing. So, I get rid of that text, and turn off comments on the posts, as I prefer all comments to show up through my LiveJournal, rather than through my WP.

Now, the problem is that I have two very different style pages: one is a clone of my ex-roomate's Drupal install, one is the default WordPress install. Luckily for me, I love S2, and have a great style that takes just about anything I throw at it. So, I steal the CSS from the Wordpress layout, and dump it into my S2 style. Within 30 minutes of minor tweaking, I have my journal looking almost exactly like my WordPress. There's almost no different visibly in what you see when you click the links, other than the fact that sidebar content changes.

Now that I have a matching wordpress and LJ, my trackback links work, the style is something I can deal with visually, and everything else is working fine, it's time to announce it. I now have Trackback support in my LiveJournal. If you want to send a trackback, the links are below. Autodiscovery will be added in the near future. Thank you for your attention, and your time.

In summary: Trackback is cool. LiveJournal is good. Pulling the two together using WordPress is easy. And that's just one reason why WordPress rocks.
Subscribe
Comments for this post were disabled by the author