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 import #184

Merged
merged 3 commits into from
Nov 3, 2015
Merged
Show file tree
Hide file tree
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
91 changes: 91 additions & 0 deletions lib/jekyll-import/importers/joomla3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
module JekyllImport
module Importers
class Joomla3 < Importer
def self.validate(options)
%w[dbname user prefix].each do |option|
if options[option].nil?
abort "Missing mandatory option --#{option}."
end
end
end

def self.specify_options(c)
c.option 'dbname', '--dbname', 'Database name'
c.option 'user', '--user', 'Database user name'
c.option 'password', '--password', "Database user's password (default: '')"
c.option 'host', '--host', 'Database host name'
c.option 'category', '--category', 'ID of the category'
c.option 'prefix', '--prefix', 'Table prefix name'
end

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

def self.process(options)
dbname = options.fetch('dbname')
user = options.fetch('user')
pass = options.fetch('password', '')
host = options.fetch('host', "localhost")
cid = options.fetch('category', 0)
table_prefix = options.fetch('prefix', "jos_")

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

FileUtils.mkdir_p("_posts")

# 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

if cid > 0
query << " AND `cn`.`catid` = '#{cid}' "
else
query << " AND `cn`.`catid` != '2' " #Filter out uncategorized content
end

db[query].each do |post|
# Get required fields and construct Jekyll compatible name.
title = post[:title]
slug = post[:alias]
date = post[:created]
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]

# Get the relevant fields as a hash, delete empty fields and convert
# to YAML for the header.
data = {
'layout' => 'post',
'title' => title.to_s,
'joomla_id' => post[:id],
'joomla_url' => slug,
'date' => date,
'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|
f.puts data
f.puts "---"
f.puts content
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/jekyll/commands/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Import < Command
:drupal7 => 'Drupal7',
:enki => 'Enki',
:joomla => 'Joomla',
:joomla3 => 'Joomla3',
:jrnl => 'Jrnl',
:ghost => 'Ghost',
:google_reader => 'GoogleReader',
Expand Down
2 changes: 1 addition & 1 deletion site/_importers/joomla.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ layout: docs
title: Joomla
prev_section: google_reader
link_source: joomla
next_section: jrnl
next_section: joomla3
permalink: /docs/joomla/
---

Expand Down
28 changes: 28 additions & 0 deletions site/_importers/joomla3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
layout: docs
title: Joomla 3
prev_section: joomla
link_source: joomla3
next_section: jrnl
permalink: /docs/joomla3/
---

To import your posts from a [Joomla 3](http://joomla.org) installation, run:

{% highlight bash %}
$ ruby -rubygems -e 'require "jekyll-import";
JekyllImport::Importers::Joomla.run({
"dbname" => "name",
"user" => "myuser",
"password" => "mypassword",
"host" => "myhost",
"category" => "category",
"prefix" => "mytableprefix"
})'
{% endhighlight %}

The only required fields are `dbname`, `prefix` and `user`. `password` defaults to `""`,
and `host` defaults to `"localhost"`.

If the `category` field is not filled, all articles will be imported, except the ones that are
uncategorized.
2 changes: 1 addition & 1 deletion site/_importers/jrnl.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: docs
title: Jrnl
prev_section: joomla
prev_section: joomla3
link_source: jrnl
next_section: marley
permalink: /docs/jrnl/
Expand Down