Importing from TextPattern to WordPress

There is allegedly a script for moving a TextPattern blog to WordPress but it appears to require db access on the WordPress end. I haven’t looked around too hard and I only signed up to WordPress today but I’m guessing I won’t get that on this nice hosted version.

Instead I figured out I may as well figure out an alternative. If you look on your Manage->Import page you will see 4 different import methods. The one I figured I would be able to use was the WordPress one. With that in mind I exported my current site, all 2 articles at the time to it (one draft and one live). It turns out it pushes out an RSS feed with a few custom elements defined.

For my first attempt I tried importing the raw RSS feed from my textpattern into WordPress. It said it was importing article 1, article 2…. etc. Unfortunately nothing actually came in. A closer look at the WordPress export file shows that the article data isn’t stored in the usual “description” element but in a “content:encoded” element instead.

My second attempt used a quick perl script to mung the items into the correct format. I used the feed I had exported as the template, removed the existing data then added the new items I generated. This file imported fine. One minor problem though, I didn’t pay attention to the dates. It would appear that the wp:post_date and other wp:*_date fields are the ones that WordPress care about because all my articles say they were published in 1970. The dates have a different format to the RSS published date so I need to tweak my script. I still need to pull my draft articles in so I may yet fix it. If I do I will post the fix.


#!/usr/bin/perl

use strict;
use XML::Simple;

my $xs = XML::Simple->new();
my $rss_feed = $xs->XMLin("feed.rss");
my $xml = $xs->XMLout($rss_feed);
my $items = $rss_feed->{channel}->{item};

# I set this as being above the last ID of the posts already on my blog
# I'm not sure if it matters.
my $num = 4;
foreach my $item (@$items)
{
	$item->{link} =~ /.*\/(.*?)$/;
	my $name = $1;
	my $date =
print << "END";
<item>
<title>$item->{title}</title>
<link>https://colinnewell.wordpress.com/?p=$num</link>
<pubDate>$item->{pubDate}</pubDate>
<dc:creator>colinnewell</dc:creator>

		<category><![CDATA[Uncategorized]]></category>

<guid isPermaLink="false"></guid>
<description></description>
<content:encoded><![CDATA[$item->{description}]]></content:encoded>
<wp:post_id>$num</wp:post_id>
<wp:post_date>$item->{pubDate}</wp:post_date>
<wp:comment_status>open</wp:comment_status>
<wp:ping_status>open</wp:ping_status>
<wp:post_name>$name</wp:post_name>
<wp:status>publish</wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type>post</wp:post_type>
</item>
END
	$num++;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s