Class: JekyllImport::Importers::S9YDatabase

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

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.clean_entities(text) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/jekyll-import/importers/s9y_database.rb', line 340

def self.clean_entities(text)
  if text.respond_to?(:force_encoding)
    text.force_encoding("UTF-8")
  end
  text = HTMLEntities.new.encode(text, :named)
  # We don't want to convert these, it would break all
  # HTML tags in the post and comments.
  text.gsub!("&", "&")
  text.gsub!("&lt;", "<")
  text.gsub!("&gt;", ">")
  text.gsub!("&quot;", '"')
  text.gsub!("&apos;", "'")
  text.gsub!("/", "&#47;")
  text
end

.page_path(page_id, page_name_list) ⇒ Object



360
361
362
363
364
365
366
367
368
369
# File 'lib/jekyll-import/importers/s9y_database.rb', line 360

def self.page_path(page_id, page_name_list)
  if page_name_list.key?(page_id)
    [
      page_name_list[page_id][:slug],
      "/",
    ].join("")
  else
    ""
  end
end

.process(opts) ⇒ Object

Main migrator function. Call this to perform the migration.

dbname

The name of the database

user

The database user name

pass

The database user’s password

host

The address of the MySQL database host. Default: ‘localhost’

port

The port of the MySQL database server. Default: 3306

socket

The database socket’s path

options

A hash table of configuration options.

Supported options are:

:table_prefix

Prefix of database tables used by WordPress. Default: ‘serendipity_’

:clean_entities

If true, convert non-ASCII characters to HTML entities in the posts, comments, titles, and names. Requires the ‘htmlentities’ gem to work. Default: true.

:comments

If true, migrate post comments too. Comments are saved in the post’s YAML front matter. Default: true.

:categories

If true, save the post’s categories in its YAML front matter. Default: true.

:tags

If true, save the post’s tags in its YAML front matter. Default: true.

:extension

Set the post extension. Default: “html”

:drafts

If true, export drafts as well Default: true.

:markdown

If true, convert the content to markdown Default: false

:permalinks

If true, save the post’s original permalink in its YAML front matter. Default: false.



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
137
138
139
140
141
142
143
144
145
146
# File 'lib/jekyll-import/importers/s9y_database.rb', line 66

def self.process(opts)
  options = {
    :user           => opts.fetch("user", ""),
    :pass           => opts.fetch("password", ""),
    :host           => opts.fetch("host", "localhost"),
    :port						=> opts.fetch("port", 3306),
    :socket         => opts.fetch("socket", nil),
    :dbname         => opts.fetch("dbname", ""),
    :table_prefix   => opts.fetch("table_prefix", "serendipity_"),
    :clean_entities => opts.fetch("clean_entities", true),
    :comments       => opts.fetch("comments", true),
    :categories     => opts.fetch("categories", true),
    :tags           => opts.fetch("tags", true),
    :extension      => opts.fetch("extension", "html"),
    :drafts         => opts.fetch("drafts", true),
    :markdown       => opts.fetch("markdown", false),
    :permalinks     => opts.fetch("permalinks", false),
  }

  if options[:clean_entities]
    options[:clean_entities] = require_if_available("htmlentities", "clean_entities")
  end

  if options[:markdown]
    options[:markdown] = require_if_available("reverse_markdown", "markdown")
  end

  FileUtils.mkdir_p("_posts")
  FileUtils.mkdir_p("_drafts") if options[:drafts]

  db = Sequel.mysql2(options[:dbname], 
    :user     => options[:user], 
    :password => options[:pass],
    :socket   => options[:socket], 
    :host     => options[:host], 
    :port     => options[:port],
    :encoding => "utf8"
  )

  px = options[:table_prefix]

  page_name_list = {}

  page_name_query = %(
     SELECT
       entries.ID             AS `id`,
       entries.title          AS `title`
     FROM #{px}entries AS `entries`
  )

  db[page_name_query].each do |page|
    page[:slug] = sluggify(page[:title])

    page_name_list[ page[:id] ] = {
      :slug => page[:slug],
    }
  end

  posts_query = "
     SELECT
       entries.ID             AS `id`,
       entries.isdraft        AS `isdraft`,
       entries.title          AS `title`,
       entries.timestamp      AS `timestamp`,
       entries.body           AS `body`,
       entries.extended       AS `body_extended`,
       authors.realname     AS `author`,
       authors.username     AS `author_login`,
       authors.email        AS `author_email`
     FROM #{px}entries AS `entries`
       LEFT JOIN #{px}authors AS `authors`
         ON entries.authorid = authors.authorid"

  unless options[:drafts]
    posts_query << "WHERE posts.isdraft = 'false'"
  end

  db[posts_query].each do |post|
    process_post(post, db, options, page_name_list)
  end
end

.process_categories(db, options, post) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/jekyll-import/importers/s9y_database.rb', line 229

def self.process_categories(db, options, post)
  return [] unless options[:categories]

  px = options[:table_prefix]

  cquery = %(
      SELECT
         categories.category_name AS `name`
       FROM
        #{px}entrycat AS `entrycat`,
        #{px}category AS `categories`
       WHERE
         entrycat.entryid = '#{post[:id]}' AND
         entrycat.categoryid = categories.categoryid
  )

  db[cquery].each_with_object([]) do |category, categories|
    categories << if options[:clean_entities]
                    clean_entities(category[:name])
                  else
                    category[:name]
                  end
  end
end

.process_comments(db, options, post) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/jekyll-import/importers/s9y_database.rb', line 254

def self.process_comments(db, options, post)
  return [] unless options[:comments]

  px = options[:table_prefix]

  cquery = %(
      SELECT
         id           AS `id`,
         author       AS `author`,
         email        AS `author_email`,
         url          AS `author_url`,
         timestamp    AS `date`,
         body         AS `content`
       FROM #{px}comments
       WHERE
         entry_id = '#{post[:id]}' AND
         status = 'approved'
  )

  db[cquery].each_with_object([]) do |comment, comments|
    comcontent = comment[:content].to_s
    comauthor = comment[:author].to_s

    if comcontent.respond_to?(:force_encoding)
      comcontent.force_encoding("UTF-8")
    end

    if options[:clean_entities]
      comcontent = clean_entities(comcontent)
      comauthor = clean_entities(comauthor)
    end

    comments << {
      "id"           => comment[:id].to_i,
      "author"       => comauthor,
      "author_email" => comment[:author_email].to_s,
      "author_url"   => comment[:author_url].to_s,
      "date"         => comment[:date].to_s,
      "content"      => comcontent,
    }
  end.sort! { |a, b| a["id"] <=> b["id"] }
end


320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/jekyll-import/importers/s9y_database.rb', line 320

def self.process_permalink(db, options, post)
  return unless options[:permalinks]

  px = options[:table_prefix]

  cquery = %(
      SELECT
         permalinks.permalink AS `permalink`
       FROM
  #{px}permalinks AS `permalinks`
       WHERE
         permalinks.entry_id = '#{post[:id]}' AND
         permalinks.type = 'entry'
  )

  db[cquery].each do |link|
    return "/#{link[:permalink]}"
  end
end

.process_post(post, db, options, page_name_list) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/jekyll-import/importers/s9y_database.rb', line 148

def self.process_post(post, db, options, page_name_list)
  extension = options[:extension]

  title = post[:title]
  if options[:clean_entities]
    title = clean_entities(title)
  end

  slug = post[:slug]
  if !slug || slug.empty?
    slug = sluggify(title)
  end

  status = post[:isdraft] == "true" ? "draft" : "published"
  date = Time.at(post[:timestamp]).utc || Time.now.utc
  name = format("%02d-%02d-%02d-%s.%s", date.year, date.month, date.day, slug, extension)

  content = post[:body].to_s
  unless post[:body_extended].to_s.empty?
					content += "\n\n" + post[:body_extended].to_s
  end

  if options[:clean_entities]
    content = clean_entities(content)
  end

  if options[:markdown]
    content = ReverseMarkdown.convert(content)
  end

  categories = process_categories(db, options, post)
  comments = process_comments(db, options, post)
  tags = process_tags(db, options, post)
  permalink = process_permalink(db, options, post)

  # Get the relevant fields as a hash, delete empty fields and
  # convert to YAML for the header.
  data = {
    "layout"       => post[:type].to_s,
    "status"       => status.to_s,
    "published"    => status.to_s == "draft" ? nil : (status.to_s == "published"),
    "title"        => title.to_s,
    "author"       => {
      "display_name" => post[:author].to_s,
      "login"        => post[:author_login].to_s,
      "email"        => post[:author_email].to_s,
    },
    "author_login" => post[:author_login].to_s,
    "author_email" => post[:author_email].to_s,
    "date"         => date.to_s,
    "permalink"    => options[:permalinks] ? permalink : nil,
    "categories"   => options[:categories] ? categories : nil,
    "tags"         => options[:tags] ? tags : nil,
    "comments"     => options[:comments] ? comments : nil,
  }.delete_if { |_k, v| v.nil? || v == "" }.to_yaml

  if post[:type] == "page"
    filename = page_path(post[:id], page_name_list) + "index.#{extension}"
    FileUtils.mkdir_p(File.dirname(filename))
  elsif status == "draft"
    filename = "_drafts/#{slug}.#{extension}"
  else
    filename = "_posts/#{name}"
  end

  # Write out the data and content to file
  File.open(filename, "w") do |f|
    f.puts data
    f.puts "---"
    f.puts Util.wpautop(content)
  end
end

.process_tags(db, options, post) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/jekyll-import/importers/s9y_database.rb', line 297

def self.process_tags(db, options, post)
  return [] unless options[:categories]

  px = options[:table_prefix]

  cquery = %(
      SELECT
         entrytags.tag AS `name`
       FROM
        #{px}entrytags AS `entrytags`
       WHERE
         entrytags.entryid = '#{post[:id]}'
  )

  db[cquery].each_with_object([]) do |tag, tags|
    tags << if options[:clean_entities]
              clean_entities(tag[:name])
            else
              tag[:name]
            end
  end
end

.require_depsObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/jekyll-import/importers/s9y_database.rb', line 4

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

.require_if_available(gem_name, option_name) ⇒ Object



221
222
223
224
225
226
227
# File 'lib/jekyll-import/importers/s9y_database.rb', line 221

def self.require_if_available(gem_name, option_name)
  require gem_name
  return true
rescue LoadError
  STDERR.puts "Could not require '#{gem_name}', so the :#{option_name} option is now disabled."
  return true
end

.sluggify(title) ⇒ Object



356
357
358
# File 'lib/jekyll-import/importers/s9y_database.rb', line 356

def self.sluggify(title)
  title.to_ascii.downcase.gsub(%r![^0-9A-Za-z]+!, " ").strip.tr(" ", "-")
end

.specify_options(c) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jekyll-import/importers/s9y_database.rb', line 16

def self.specify_options(c)
  c.option "dbname", "--dbname DB", "Database name (default: '')"
  c.option "socket", "--socket SOCKET", "Database socket (default: '')"
  c.option "user", "--user USER", "Database user name (default: '')"
  c.option "password", "--password PW", "Database user's password (default: '')"
  c.option "host", "--host HOST", "Database host name (default: 'localhost')"
  c.option "port", "--port PORT", "Custom database port connect to (default: 3306)"
  c.option "table_prefix", "--table_prefix PREFIX", "Table prefix name (default: 'serendipity_')"
  c.option "clean_entities", "--clean_entities", "Whether to clean entities (default: true)"
  c.option "comments", "--comments", "Whether to import comments (default: true)"
  c.option "categories", "--categories", "Whether to import categories (default: true)"
  c.option "tags", "--tags", "Whether to import tags (default: true)"
  c.option "drafts", "--drafts", "Whether to export drafts as well"
  c.option "markdown", "--markdown", "convert into markdown format (default: false)"
  c.option "permalinks", "--permalinks", "preserve S9Y permalinks (default: false)"
end