Module: JekyllImport::Importers::DrupalCommon::ClassMethods

Included in:
JekyllImport::Importers::Drupal6, JekyllImport::Importers::Drupal7
Defined in:
lib/jekyll-import/importers/drupal_common.rb

Overview

This module provides a base for the Drupal importers (at least for 6 and 7; since 8 will be a different beast). Version-specific importers will need to implement the missing methods from the Importer class.

The general idea is that this importer reads a MySQL database via Sequel and creates a post file for each node it finds in the Drupal database.

Constant Summary collapse

DEFAULTS =
{
  "password" => "",
  "host"     => "localhost",
  "prefix"   => "",
  "types"    => %w(blog story article),
}.freeze

Instance Method Summary collapse

Instance Method Details

#process(options) ⇒ Object



41
42
43
44
45
46
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
# File 'lib/jekyll-import/importers/drupal_common.rb', line 41

def process(options)
  dbname = options.fetch("dbname")
  user   = options.fetch("user")
  pass   = options.fetch("password", DEFAULTS["password"])
  host   = options.fetch("host",     DEFAULTS["host"])
  prefix = options.fetch("prefix",   DEFAULTS["prefix"])
  types  = options.fetch("types",    DEFAULTS["types"])

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

  query = self.build_query(prefix, types)

  conf = Jekyll.configuration({})
  src_dir = conf["source"]

  dirs = {
    :_posts   => File.join(src_dir, "_posts").to_s,
    :_drafts  => File.join(src_dir, "_drafts").to_s,
    :_layouts => Jekyll.sanitized_path(src_dir, conf["layouts_dir"].to_s),
  }

  dirs.each do |_key, dir|
    FileUtils.mkdir_p dir
  end

  # Create the refresh layout
  # Change the refresh url if you customized your permalink config
  File.open(File.join(dirs[:_layouts], "refresh.html"), "w") do |f|
    f.puts <<-HTML
<!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>
HTML
  end

  db[query].each do |post|
    # Get required fields
    data, content = self.post_data(post)

    data["layout"] = post[:type]
    title = data["title"] = post[:title].strip.force_encoding("UTF-8")
    time = data["created"] = post[:created]

    # Get the relevant fields as a hash and delete empty fields
    data = data.delete_if { |_k, v| v.nil? || v == "" }.each_pair do |_k, v|
      (v.is_a? String ? v.force_encoding("UTF-8") : v)
    end

    # Construct a Jekyll compatible file name
    is_published = post[:status] == 1
    node_id = post[:nid]
    dir = is_published ? dirs[:_posts] : dirs[:_drafts]
    slug = title.strip.downcase.gsub(%r!(&|&amp;)!, " and ").gsub(%r![\s\.\/\\]!, "-").gsub(%r![^\w-]!, "").gsub(%r![-_]{2,}!, "-").gsub(%r!^[-_]!, "").gsub(%r![-_]$!, "")
    filename = Time.at(time).to_datetime.strftime("%Y-%m-%d-") + slug + ".md"

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

    # Make a file to redirect from the old Drupal URL
    next unless is_published
    alias_query = self.aliases_query(prefix)
    type = post[:type]

    aliases = db[alias_query, "#{type}/#{node_id}"].all

    aliases.push(:alias => "#{type}/#{node_id}")

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

#require_depsObject



31
32
33
34
35
36
37
38
39
# File 'lib/jekyll-import/importers/drupal_common.rb', line 31

def require_deps
  JekyllImport.require_with_fallback(%w(
    rubygems
    sequel
    mysql2
    fileutils
    safe_yaml
  ))
end

#specify_options(c) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/jekyll-import/importers/drupal_common.rb', line 21

def 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: #{DEFAULTS["password"].inspect})"
  c.option "host", "--host HOST", "Database host name (default: #{DEFAULTS["host"].inspect})"
  c.option "prefix", "--prefix PREFIX", "Table prefix name (default: #{DEFAULTS["prefix"].inspect})"
  c.option "types", "--types TYPE1[,TYPE2[,TYPE3...]]", Array,
    "The Drupal content types to be imported  (default: #{DEFAULTS["types"].join(",")})"
end