Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

joomla3: allow importing drafts #272

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions lib/jekyll-import/importers/joomla3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def self.specify_options(c)
c.option 'host', '--host', 'Database host name'
c.option 'category', '--category', 'ID of the category'
c.option 'prefix', '--prefix', 'Table prefix name'
c.option 'state', '--state', 'Which publishing state to select. 0: unpublished, 1: published, 2: archived (default: 1)'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to change to state STATE right after c.option so the parser knows it takes an arg. Surprised the ones above aren't like that... Whoops!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I would consider using the full words here – it's much more readable to use --state unpublished than --state 0.

end

def self.require_deps
Expand All @@ -34,18 +35,24 @@ def self.process(options)
host = options.fetch('host', "localhost")
cid = options.fetch('category', 0)
table_prefix = options.fetch('prefix', "jos_")
state = options.fetch('state', 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you'd receive state = "unpublished", which you could then map into the integer value using a hash:

STATE_NUMS = {
  "unpublished" => 0, 
  # etc
}


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

FileUtils.mkdir_p("_posts")
if state == 0
directory = "_drafts"
else
directory = "_posts"
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can squish this down to:

directory = (state == "unpublished") ? "_drafts" : "_posts"

FileUtils.mkdir_p(directory)

# Reads a MySQL database via Sequel and creates a post file for each
# post in #__content that is published.
query = "SELECT `cn`.`title`, `cn`.`alias`, `cn`.`introtext`, CONCAT(`cn`.`introtext`,`cn`.`fulltext`) AS `content`, "
query << "`cn`.`created`, `cn`.`id`, `ct`.`title` AS `category`, `u`.`name` AS `author` "
query << "FROM `#{table_prefix}content` AS `cn` JOIN `#{table_prefix}categories` AS `ct` ON `cn`.`catid` = `ct`.`id` "
query << "JOIN `#{table_prefix}users` AS `u` ON `cn`.`created_by` = `u`.`id` "
query << "WHERE (`cn`.`state` = '1' OR `cn`.`state` = '2') " # Only published and archived content items to be imported
query << "WHERE `cn`.`state` = '#{state}' "
Copy link
Member

@parkr parkr Aug 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you'd use the hash we created above to grab the relevant integer value:

query << "WHERE `cn`.`state` = '#{STATE_NUMS.fetch(state)}' "


if cid > 0
query << " AND `cn`.`catid` = '#{cid}' "
Expand All @@ -58,12 +65,16 @@ def self.process(options)
title = post[:title]
slug = post[:alias]
date = post[:created]
author = post[:author]
category = post[:category]
author = post[:author]
category = post[:category]
content = post[:content]
excerpt = post[:introtext]
name = "%02d-%02d-%02d-%s.markdown" % [date.year, date.month, date.day,
slug]
excerpt = post[:introtext]
if state == 0
name = "%s.markdown" % [slug]
else
name = "%02d-%02d-%02d-%s.markdown" % [date.year, date.month, date.day,
slug]
end
Copy link
Member

@parkr parkr Aug 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I'd go with a string modification, actually:

name = "#{slug}.markdown"
name.insert(0, "%02d-%02d-%02d-" % [date.year, date.month, date.day]) unless state.eql?("unpublished")


# Get the relevant fields as a hash, delete empty fields and convert
# to YAML for the header.
Expand All @@ -73,13 +84,13 @@ def self.process(options)
'joomla_id' => post[:id],
'joomla_url' => slug,
'date' => date,
'author' => author,
'excerpt' => excerpt.strip.to_s,
'category' => category
'author' => author,
'excerpt' => excerpt.strip.to_s,
'category' => category
}.delete_if { |k,v| v.nil? || v == '' }.to_yaml

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