Class: JekyllImport::Importers::Drupal6

Inherits:
JekyllImport::Importer show all
Defined in:
lib/jekyll-import/importers/drupal6.rb

Constant Summary collapse

QUERY =

Reads a MySQL database via Sequel and creates a post file for each story and blog node.

"SELECT n.nid, \
         n.title, \
         nr.body, \
         n.created, \
         n.status, \
         GROUP_CONCAT( td.name SEPARATOR '|' ) AS 'tags' \
    FROM node_revisions AS nr, \
         node AS n \
    LEFT OUTER JOIN term_node AS tn ON tn.nid = n.nid \
    LEFT OUTER JOIN term_data AS td ON tn.tid = td.tid \
   WHERE (%types%) \
     AND n.vid = nr.vid \
GROUP BY n.nid"

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.process(options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/jekyll-import/importers/drupal6.rb', line 47

def self.process(options)
  dbname = options.fetch('dbname')
  user   = options.fetch('user')
  pass   = options.fetch('password', "")
  host   = options.fetch('host', "localhost")
  prefix = options.fetch('prefix', "")
  types  = options.fetch('types', ['blog', 'story', 'article'])

  db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host, :encoding => 'utf8')

  if prefix != ''
    QUERY[" node "] = " " + prefix + "node "
    QUERY[" node_revisions "] = " " + prefix + "node_revisions "
    QUERY[" term_node "] = " " + prefix + "term_node "
    QUERY[" term_data "] = " " + prefix + "term_data "
  end

  types = types.join("' OR n.type = '")
  QUERY[" WHERE (%types%) "] = " WHERE (n.type = '#{types}') "

  FileUtils.mkdir_p "_posts"
  FileUtils.mkdir_p "_drafts"
  FileUtils.mkdir_p "_layouts"

  # Create the refresh layout
  # Change the refresh url if you customized your permalink config
  File.open("_layouts/refresh.html", "w") do |f|
    f.puts <<EOF
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url={{ page.refresh_to_post_id }}.html" />
</head>
</html>
EOF
  end

  db[QUERY].each do |post|
    # Get required fields and construct Jekyll compatible name
    node_id = post[:nid]
    title = post[:title]
    content = post[:body]
    tags = (post[:tags] || '').downcase.strip
    created = post[:created]
    time = Time.at(created)
    is_published = post[:status] == 1
    dir = is_published ? "_posts" : "_drafts"
    slug = title.strip.downcase.gsub(/(&|&amp;)/, ' and ').gsub(/[\s\.\/\\]/, '-').gsub(/[^\w-]/, '').gsub(/[-_]{2,}/, '-').gsub(/^[-_]/, '').gsub(/[-_]$/, '')
    name = time.strftime("%Y-%m-%d-") + slug + '.md'

    # Get the relevant fields as a hash, delete empty fields and convert
    # to YAML for the header
    data = {
       'layout' => 'post',
       'title' => title.to_s,
       'created' => created,
       'categories' => tags.split('|')
     }.delete_if { |k,v| v.nil? || v == ''}.each_pair {
        |k,v| ((v.is_a? String) ? v.force_encoding("UTF-8") : v)
     }.to_yaml

    # Write out the data and content to file
    File.open("#{dir}/#{name}", "w") do |f|
      f.puts data
      f.puts "---"
      f.puts content
    end

    # Make a file to redirect from the old Drupal URL
    if is_published
      aliases = db["SELECT dst FROM #{prefix}url_alias WHERE src = ?", "node/#{node_id}"].all

      aliases.push(:dst => "node/#{node_id}")

      aliases.each do |url_alias|
        FileUtils.mkdir_p url_alias[:dst]
        File.open("#{url_alias[:dst]}/index.md", "w") do |f|
          f.puts "---"
          f.puts "layout: refresh"
          f.puts "refresh_to_post_id: /#{time.strftime("%Y/%m/%d/") + slug}"
          f.puts "---"
        end
      end
    end
  end

  # TODO: Make dirs & files for nodes of type 'page'
  # Make refresh pages for these as well
end

.require_depsObject



37
38
39
40
41
42
43
44
45
# File 'lib/jekyll-import/importers/drupal6.rb', line 37

def self.require_deps
  JekyllImport.require_with_fallback(%w[
    rubygems
    sequel
    fileutils
    safe_yaml
    mysql
  ])
end

.specify_options(c) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/jekyll-import/importers/drupal6.rb', line 28

def self.specify_options(c)
  c.option 'dbname', '--dbname DB', 'Database name'
  c.option 'user', '--user USER', 'Database user name'
  c.option 'password', '--password PW', "Database user's password (default: '')"
  c.option 'host', '--host HOST', 'Database host name (default: "localhost")'
  c.option 'prefix', '--prefix PREFIX', 'Table prefix name'
  c.option 'types', '--types TYPE1[,TYPE2[,TYPE3...]]', Array, 'The Drupal content types to be imported.'
end

.validate(options) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/jekyll-import/importers/drupal6.rb', line 20

def self.validate(options)
  %w[dbname user].each do |option|
    if options[option].nil?
      abort "Missing mandatory option --#{option}."
    end
  end
end