Categories
Code ruby

Blog Move 1: Getting WordPress data to Ruby using XML

Step 1 in the “Moving my blog” process is “Extract the current site’s data into a manageable format”

Frankly, that’s easy! WordPress has a functionality to export the site’s content to a single XML file containing all the published Categories, Tags, Posts, Pages and Comments. To do this (WordPress v2.9.2) click Tools > Export and save the file. In previous versions of the software I believe it’s under the Manage menu.


I’m aware I could import the data directly from the WordPress database (to wherever it goes in the end) but let’s imagine we can’t. Anyway, database access would be tediously slow and inefficient to test against and implement.

A quick google for “import wordpress xml ruby” threw up nothing helpful so I turned to the Ruby XML libraries. John Nunemaker “feverishly posts everything he learns” at railstips.org and has two articles of use here:

The latter deals with three different ruby xml libraries and compares their speed, ease of use and how nice their names are to say. He puts REXML, hpricot and libxml-ruby. I’ll save you the pleasure of reading the article (if you like) and ccv John’s summary:

“Libxml is blisteringly fast, [but] Hpricot has cooler name, REXML and Hpricot both feel easier to use out of the box”

And there you go. Hpricot it is!

Now to get the data into Ruby. After a quick glance at the rubytips article and The RDocs I put together this code as a starting point:


cats_hierarchy={}
(doc/"wp:category").each do |category|
    cat_name = category.at("wp:category_nicename").innerHTML
    cat_parent = category.at("wp:category_parent").innerHTML

    if cats_hierarchy.include? cat_parent
        cats_hierarchy[cat_parent] = cat_name
    else
        cats_hierarchy[cat_name] = []
    end
end

cats = cats_hierarchy.to_a.flatten

That gives me two each to use Ruby objects each containing all of my category data: a hash which preserves the hierarchy of the structure and all the names in a linear array.


?> cats = cats_hierarchy.to_a.flatten.uniq
=> ["route66", nil, "rails", "american-2008", "reciprocal-affection", "hope-for-the-future", "code", "blog", "review-blog", "rant", "brands", "projects", "yab_shop", "textpattern", "meaningful-labor", "giants", "accessibility", "root", "charity-project", "apple", "xhtml", "america-2006-route-66", "ruby", "learning", "america-2007", "uncategorized", "iphone", "america-2008"]

?> cats_hierarchy
=> {"route66"=>nil, "rails"=>nil, "american-2008"=>nil, "reciprocal-affection"=>nil, "hope-for-the-future"=>nil, "code"=>nil, "blog"=>"review-blog", "rant"=>nil, "brands"=>nil, "projects"=>nil, "yab_shop"=>nil, "textpattern"=>nil, "meaningful-labor"=>nil, "giants"=>nil, "accessibility"=>nil, "root"=>nil, "charity-project"=>nil, "apple"=>nil, "xhtml"=>nil, "america-2006-route-66"=>nil, "ruby"=>nil, "learning"=>nil, "america-2007"=>nil, "uncategorized"=>nil, "iphone"=>nil, "america-2008"=>nil}

And so we have the starting point to getting this WordPress exported XML data into a Ruby application.

More soon.

Categories
blog meaningful labor rails ruby

Rails / LAMP / WordPress Setup Guide

I’ve had huge amounts of pain getting WordPress set up locally over the last few weeks on my old machine and when it struck again with my new development environment it definitely time to write down the required steps to get a great LAMP/Rails setup prepared (for me)!

Leopard has PHP and Apache fairly up to date, and probably MySQL as well, but I decided to get MySQL up to scratch using the Universal Binary along with a few command line instructions available here (guide and links to downloads).

I didn’t use this script myself, and at a year old it may have a few imperfections, but HiveLogic have a fairly well rounded Ruby/Rails install going on.

Finally, WordPress to go on your LAMP stack will be a lot less painful using this guide for Tech Recipes.

Any suggestions additions or replacements for this list? Drop a comment!

Categories
Uncategorized

Installing WordPress on Leopard: Error establishing a database connection

What a ridiculously stupid error.

http://wordpress.org/support/topic/160658

Your wp-config.php file should read:
define('DB_HOST', 'Localhost');
‘Localhost’ not ‘localhost’.

Categories
blog giants

In with the old and in with the new.

I’ve moved my site over to WordPress, which I have used to build both the Dromore Cathedral and the QUB Tennis Club web sites.

In the coming days I’ll be restyling with something individual, as I build up the brand under which I’ll be doing web sites from now on; and whatever work I end up doing on when I finish up with University this summer.That was the launch notice of QUB Tennis by the way; thanks to Chris for all his input and so on!

A night at the Giants tonight, yet another loss in the Cup – about six straight in all competitions now, though the performance lifted greatly tonight and after the first period break (and whatever shouting can be imagined to happen in the right hand locker room) both DeRou and his defence played somewhat better hockey. A few good shots went in and after a couple of fist fights the crowd were rocking behind the home team; though the boos were short, but sour, at the end of the night. Shawn Sutter has been signed, and the Giants need to drop one of their ‘imports’ to make room for him in the team before he arrives… Who goes? Todd and Ed decide!

Some more web sites to get started this week, and an audio visual gig to plan for October.