Posts Tagged ‘Wordpress’

Migrating homebrew Rails blog-entries to Wordpress with Perl

I was facing a challenge recently at work: migrating a bunch of blog entries stored in a Postgres database of a home-made Rails application to a Wordpress blog.

This challenge was trickier than you may first think because of the following reasons:

  1. I don’t have manual access to the Postgres DB, I can only run a Rails console that connects to it
  2. The Postgres DB and the Wordpress Blog are installed on two different servers that are not in the same LAN.

Here is how I did the job, of course, using Perl as my weapon was the only option I considered, and I’m glad of it.

First step : exporting the data, JSON FTW

As the only way I had to handle the data source was a Rails console, I chose first to use YAML for exporting the data into a file, but I wasn’t able to parse it with Perl then, because of badly-written multiline scalars (I don’t know whose fault it is, either Ruby or Perl YAML modules, but it didn’t work out of the box).

Then I tried JSON:

# dump_posts.rb (to be run with ./script/runner -e production)
posts = MyRailsPost.find(:all)
f = File.new("/tmp/posts.json", 'w')
f.write(posts.to_json)
f.close

I then uploaded the json file to my second server, where I have access to the Wordpress DB and started writing an importer.

From a JSON file to a Wordpress DB

Now I have my data in JSON format, I can write a Perl script that will parse it and insert each post item found in the Wordpress “wp_posts” table.

First, I’ll write a Coat::Persistent class to handle Wordpress blog posts:

package WPPost;
use Coat;
use Coat::Persistent
    table_name => "wp_posts",
    primary_key => "ID";

has_p post_author => (isa => 'Int', required => 1);
has_p post_date => (isa => 'DateTime', coerce => 1);
has_p post_excerpt => (isa => 'Str');
has_p post_content => (isa => 'Str');
has_p post_title => (isa => 'Str');
has_p post_name => (isa => 'Str');

sub BUILD {
    my $self = shift;
    my $class = ref($self);
    $class->dbh->do('SET NAMES utf8') or die $!;
}

Coat::Persistent->disable_internal_sequence_engine();
__PACKAGE__->map_to_dbi(mysql => "MYDATABASE", "DBUSER", "DBPASS")

Please note that we tells DBD::mysql to send data in utf8 (SET NAMES utf8), this is very important, if we don’t, we’ll endup in Wordpress with a mess of utf8/latin1 crap.

The post_date field will be set with Rails’ created_at attribute, and it might be in a different format than YYYY-MM-DD HH:MM:SS, so I’ll write subtypes and coercions to handle that cleanly:

use Coat::Types;
use Coat::Persistent::Types;

subtype 'DateTimeSec'
    => as 'Str'
    => where { /\d{4}-\d\d-\d\d \d+:\d+:\d+\.\d+/ };

subtype 'DateTimeWithTZ'
    => as 'Str'
    => where { /\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\+\d\d:\d\d/ };

subtype 'DateTimeWithT'
    => as 'Str'
    => where { /\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d/ };

coerce 'DateTime'
    => from 'DateTimeWithTZ'
    => where { s/\+.+$//; s/T/ /; return $_ };

coerce 'DateTime'
    => from 'DateTimeWithT'
    => via { s/T/ /; return $_ };

coerce 'DateTime'
    => from 'DateTimeSec'
    => via { s/\.\d+$//; return $_ };

Now, I have all I need, I can write the scrit:

#!/usr/bin/perl
use strict;
use warnings;

# slurping my JSON file
open FIC, '< ', $ARGV[0] or die $!;
my @lines = ;
close FIC;
my $content = join '', @lines;

# processing posts
my $data = from_json($content);
for my $post (@$data) {

    my $p = $post->{rails_post_class}; # JSON output in Rails works this way

    my $wp = WPPost->create(
        post_author => 1,               # admin
        post_date => $p->{created_at},  # coercion will occur here
        post_excerpt => $p->{excerpt},
        post_content => $p->{body},
        post_title => $p->{title},
        post_name => $p->{permalink},
    );
    print "+ ".$wp->post_title." -> #".$wp->ID."\n";
}

Note: you can consider this post as the first-and-last item of my 2009 Perl Advent Calendar ;)

Tags: , , ,
Posted in Programming Comments Off

Weekly Random Links : Security

Dear lazy web, this is my first attempt at publishing a selection of links I’ve recently bookmarked. Those links came to my attention mostly thanks to my twitter account. I’ll try to publish such a list every week (on sundays). Feel free to suggest me links either via Twitter or through this blog (comments are welcome).

If a topic emerges from all the links I’ve bookmarked, I’ll put it in the title of the post, like you can see, this week, the trend topic is “Security”.

So here we go, happy links and if I give no news until then, have a good week.

  1. Old Wordpress Sites Exploited And Security Questioned

    Apparently this weekend, a major exploit attack has been taking place on old versions of self-hosted blogging platform Wordpress. If you are using Wordpress for your blog, you should update immediately.

  2. I don’t feel safe with Wordpress, hackers …

    Once his blog has been exploited, a wordpress user doesn’t feel safe anymore. A discussion takes place in Friendfeed between him, Matt Mullenweg (Wordpress author) and others…

  3. How to Keep WordPress Secure

    Matt Mullenweg – Wordpress author – writes some advices to keep your Wordpress blog safe from exploits. “Upgrading is taking your vitamins; fixing a hack is open heart surgery.” he says.

  4. How To Be A Samurai Designer

    What if designing a web site was like a Samurai fight? Then Samurai’s good practices should apply to web designers: “Matters of great concern should be treated lightly. Matters of small concern should be treated seriously.”

  5. About The Capslock Key

    The Capslock key : an appropriate demotivator

  6. Mozilla To Protect Adobe Flash Users

    Mozilla is going to check the version of installed Adobe Flash plug-ins and warn users if it discovers an outdated version with potential security holes. [...] Just recently, a study confirmed that 80 per cent of users surf with a vulnerable version of Adobe’s plug-in.”

Tags: , ,
Posted in Links Comments Off

Nouvelle version du plugin Wordpress pour Yoolink

Une mise à jour du plugin Wordpress est disponible depuis peu sur Yoolink.

Cette version propose une nouvelle option concernant le flux RSS du blog : vous pouvez désormais choisir si vous voulez un lien “Ajouter sur Yoolink” dans votre flux RSS ou non.

Au passage, l’ajout de cette option corrige un bug qui était présent dans la version 1.0 du plugin, par rapport justement aux flux RSS.

Si vous avez installé la version 1.0 du plugin Yoolink Tools, je vous invite donc à effectuer la mise à jour sans attendre.

Tags: , ,
Posted in Internet Comments Off

Yoolink s’ouvre aux blogs

Depuis aujourd’hui, les utilisateurs de Yoolink qui disposent d’un blog (ou d’un site web classique) peuvent exporter diverses informations de leur profil.

Cela est possible grâce aux premiers services fournis par l’API de Yoolink. Pour l’occasion, un plugin est proposé aux heureux possesseurs de blogs Wordpress pour une intégration complète est indolore (vous pouvez voir ce plugin à l’oeuvre sur ce blog, notamment sur la sidebar).

Pour plus de détails sur le sujet, je vous invite à consuilter le blog officiel de Yoolink, ou un article complet expliquera tout ça.

Tags: , ,
Posted in Internet Comments Off

Get Adobe Flash playerPlugin by wpburn.com wordpress themes