|
| 1 | +require 'rubygems' |
| 2 | +require 'sequel' |
| 3 | +require 'fileutils' |
| 4 | +require 'yaml' |
| 5 | + |
| 6 | +# NOTE: This migrator is made for Joomla 1.5 databases. |
| 7 | +# NOTE: This converter requires Sequel and the MySQL gems. |
| 8 | +# The MySQL gem can be difficult to install on OS X. Once you have MySQL |
| 9 | +# installed, running the following commands should work: |
| 10 | +# $ sudo gem install sequel |
| 11 | +# $ sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config |
| 12 | + |
| 13 | +module Jekyll |
| 14 | + module Joomla |
| 15 | + def self.process(dbname, user, pass, host = 'localhost', table_prefix = 'jos_', section = '1') |
| 16 | + db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host, :encoding => 'utf8') |
| 17 | + |
| 18 | + FileUtils.mkdir_p("_posts") |
| 19 | + |
| 20 | + # Reads a MySQL database via Sequel and creates a post file for each |
| 21 | + # post in wp_posts that has post_status = 'publish'. This restriction is |
| 22 | + # made because 'draft' posts are not guaranteed to have valid dates. |
| 23 | + query = "SELECT `title`, `alias`, CONCAT(`introtext`,`fulltext`) as content, `created`, `id` FROM #{table_prefix}content WHERE state = '0' OR state = '1' AND sectionid = '#{section}'" |
| 24 | + |
| 25 | + db[query].each do |post| |
| 26 | + # Get required fields and construct Jekyll compatible name. |
| 27 | + title = post[:title] |
| 28 | + slug = post[:alias] |
| 29 | + date = post[:created] |
| 30 | + content = post[:content] |
| 31 | + name = "%02d-%02d-%02d-%s.markdown" % [date.year, date.month, date.day, |
| 32 | + slug] |
| 33 | + |
| 34 | + # Get the relevant fields as a hash, delete empty fields and convert |
| 35 | + # to YAML for the header. |
| 36 | + data = { |
| 37 | + 'layout' => 'post', |
| 38 | + 'title' => title.to_s, |
| 39 | + 'joomla_id' => post[:id], |
| 40 | + 'joomla_url' => post[:alias], |
| 41 | + 'date' => date |
| 42 | + }.delete_if { |k,v| v.nil? || v == '' }.to_yaml |
| 43 | + |
| 44 | + # Write out the data and content to file |
| 45 | + File.open("_posts/#{name}", "w") do |f| |
| 46 | + f.puts data |
| 47 | + f.puts "---" |
| 48 | + f.puts content |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | + end |
| 53 | +end |
0 commit comments