Class: JekyllImport::Importers::Drupal7

Inherits:
JekyllImport::Importer show all
Defined in:
lib/jekyll-import/importers/drupal7.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, \
       fdb.body_value, \
       n.created, \
       n.status \
FROM node AS n, \
     field_data_body AS fdb \
WHERE (n.type = 'blog' OR n.type = 'story' OR n.type = 'article') \
AND n.nid = fdb.entity_id \
AND n.vid = fdb.revision_id"

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.process(options) ⇒ Object



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
# File 'lib/jekyll-import/importers/drupal7.rb', line 48

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', "")

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

  unless prefix.empty?
    QUERY[" node "] = " " + prefix + "node "
    QUERY[" field_data_body "] = " " + prefix + "field_data_body "
  end

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

  db[QUERY].each do |post|
    # Get required fields and construct Jekyll compatible name
    node_id = post[:nid]
    title = post[:title]
    content = post[:body_value]
    created = post[:created]
    time = Time.at(created)
    is_published = post[:status] == 1
    dir = is_published ? "_posts" : "_drafts"
    slug = title.strip.downcase.gsub(/(&|&)/, ' 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' => 'default',
       'title' => title.to_s,
       'created' => created,
     }.delete_if { |k,v| v.nil? || 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

  end

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

  # TODO: Make refresh dirs & files according to entries in url_alias table
end

.require_depsObject



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

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

.specify_options(c) ⇒ Object



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

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'
end

.validate(options) ⇒ Object



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

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