Blogs

RedMonk

Skip to content

Reading the MT DB w/ Ruby

I know I said I was going to do this in PHP, but I’m tired of trying to figure out what’s wrong w/ my Eclipse PHP editor so I thought I’d just take a quick hack at the MT migration script in Ruby. Looking around for some stock MySQL connection code this morning (laziest programmer ever), I found this.

A couple of minutes later, I turned that into this:

#! /usr/bin/ruby

require 'rubygems'
require 'mysql'

def with_db
dbh = Mysql.real_connect('localhost', 'username','password','databasename')
begin
yield dbh
ensure
dbh.close
end
end
#---
with_db do |db|
res = db.query('SELECT entry_id,entry_created_on,entry_basename FROM mt_entry WHERE entry_blog_id = 3;')
res.each { |row| puts "Redirect /archives/#{row[0]}.html http://www.redmonk.com/sogrady/archives/#{row[1]}/#{row[2]}.php" }
res.free
end

Which spits out a whole bunch of this:

Redirect /archives/2399.html http://www.redmonk.com/sogrady/archives/2006-10-19 11:00:39/scan_your_php_a.php
Redirect /archives/2400.html http://www.redmonk.com/sogrady/archives/2006-10-19 16:07:46/startup_camps_f.php
Redirect /archives/2401.html http://www.redmonk.com/sogrady/archives/2006-10-19 23:24:52/links_for_20061_16.php
Redirect /archives/2406.html http://www.redmonk.com/sogrady/archives/2006-10-20 23:22:05/links_for_20061_17.php
Redirect /archives/2409.html http://www.redmonk.com/sogrady/archives/2006-10-22 15:07:48/progress_w_trix.php
Redirect /archives/2410.html http://www.redmonk.com/sogrady/archives/2006-10-22 23:19:37/links_for_20061_18.php
Redirect /archives/2412.html http://www.redmonk.com/sogrady/archives/2006-10-23 11:27:20/attending_the_c.php
Redirect /archives/2413.html http://www.redmonk.com/sogrady/archives/2006-10-23 11:35:27/and_speaking_of_1.php
Redirect /archives/2416.html http://www.redmonk.com/sogrady/archives/2006-10-23 16:56:37/apologies_in_ad.php
Redirect /archives/2419.html http://www.redmonk.com/sogrady/archives/2006-10-23 23:25:56/links_for_20061_19.php

That’s mostly what I need, though I need to a.) pad the entry_id to six characters and b.) lose the time stamp in the date returns. Once that’s done, I can spit this out into a text file and I’ll more or less have a .htaccess file capable of handling the migration (I think). This is presuming, of course, that I can get WordPress to preserve the basenames from the existing MT installation – I’ll have to confirm that.

What am I missing?

Categories: RedMonk Miscellaneous.

  • http://perkypants.org/blog/ Jeff Waugh

    The only problem is that those are not “good” WordPress URLs. I mentioned on IRC that the best way to do this is to create a WordPress plugin: To do it beautifully, you need WordPress to resolve its own URLs, because they’re not going to be related to your old ones, and you won’t be able to robustly generate them.

    I’m seriously considering writing a WordPress “legacy URLs” plugin, that would interpret a legacy URL scheme (possibly configured by choosing the old blogging tool, but certainly an arbitrary pattern) and do the redirect itself.

    Then you can very comfortably redirect /archives/9999.html to /2006/12/25/christmas-lights-are-lovely/ -> the most search-optimal way of doing it. :-)

  • http://www.redmonk.com/sogrady stephen o’grady

    i see what you mean. i’d hoped the import would bring over either the numeric id or the basename or both, but it doesn’t – just the content. i’d guess there’s some way to directly populate the wp database via the custom field extension and use a custom URI extension from there, but it just seems like an awful lot of work. there have to be easier ways of doing that.

    i like your custom plugin idea (and would definitely use it if you build it), but i’m still a bit unclear on what the key is that will tie to the two repositories together. if i had that, the whole thing becomes easier. but so far, i haven’t found that.