Perl script to import posts from WordPress to Whird blog

Just as a personal project with Perl and Mysql, I decided to start working on a Perl script to import posts from my WordPress blog to my Whird blog.

Whird
Whird was designed by Corenominal, the Crunchbang Linux head developer, and it looked like a nice platform so I installed it to use as a personal blog and leave the WordPress blog for Linux related posts.
It occurred to me that I may like to import some non-linux posts from WordPress, and that’s where my project began.

Perl vs Php
Although I love Perl, importing data from one site to another is far better suited to Php, something which Corenominal agreed with me on, but I decided to use Perl basically because, to me, Perl is king.

I would highly recommend Whird if you are looking for a new blogging platform which is light, fast, and offers you complete control over the design.

The script below was tested on my localhost installs and imported all the WordPress posts to Whird perfectly. However, it still needs some work to also include attachments, tags and comments. It’s also a vast improvement to my original script which was importing the posts from a wordpress.sql dump instead of the database-to-database connections.

#!/usr/bin/env perl
#########################################################
# SCRIPT TO IMPORT WORDPRESS POSTS TO WHIRD
# BY richs scadding - http://debianandi.blogspot.com
#########################################################
use strict;
use warnings;
use DBI;
use DBD::mysql;
#use Mysql;
#########################################################
# ENTER YOUR DATABASE DETAILS
# Assumes whird and wordpress databases are on same host
#########################################################
my $host = "localhost";
my $dbase = "whird";
my $dbase_orig = "wordpress";
my $login = "rich";
my $pw = "4321";
#########################################################
# GET POSTS FROM WORDPRES AND INSERT THEM INTO WHIRD
#########################################################
my $dbh=DBI->connect("dbi:mysql:$dbase:$host", $login, $pw);
my $dbhorig=DBI->connect("dbi:mysql:$dbase_orig:$host", $login, $pw);
my $sthorig = $dbhorig->prepare("select * from wp_posts");
$sthorig->execute;
while (my $ref = $sthorig->fetchrow_hashref) {
my $sql = "insert into whird_content (_date, _title, _excerpt, _content, _slug, _type) values (".$dbh->quote($ref->{post_date}).", ".$dbh->quote($ref->{post_title}).", ".$dbh->quote($ref->{post_excerpt}).", ".$dbh->quote($ref->{post_content}).", ".$dbh->quote($ref->{post_name}).", ".$dbh->quote($ref->{post_type}).")";
$dbh->do($sql);
}
$sthorig->finish;
$dbh->disconnect();
$dbhorig->disconnect();

Advertisements

3 thoughts on “Perl script to import posts from WordPress to Whird blog

  1. This looks nice rich. Now if only I could get my whird installation up and running I'd be laughing 🙂 I seem to have an issue with connecting to the database on my remote host. I probably have the database address wrong in the initial config but can't see the error… sigh

  2. Hi omns, yeah some hosts can be funny with dbase connections, either localhost or something more specific like localhost:/tmp/mysql5.sock. You may even have to enable remote connections for a specific database. I've also had to create a different user for some sites as well. Let me know how you get on.
    I had some fun and games with Whird as I have it in a subdir, and I had to play with the htaccess, but now it's setup, I really like it. Just need to find time to post something 🙂

  3. Hi omns, yeah some hosts can be funny with dbase connections, either localhost or something more specific like localhost:/tmp/mysql5.sock. You may even have to enable remote connections for a specific database. I've also had to create a different user for some sites as well. Let me know how you get on.
    I had some fun and games with Whird as I have it in a subdir, and I had to play with the htaccess, but now it's setup, I really like it. Just need to find time to post something 🙂

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