-
Notifications
You must be signed in to change notification settings - Fork 317
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)' | ||
end | ||
|
||
def self.require_deps | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, you'd receive 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}' " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}' " | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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 afterc.option
so the parser knows it takes an arg. Surprised the ones above aren't like that... Whoops!There was a problem hiding this comment.
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
.