Beginning to remove refinery stuff

This commit is contained in:
2014-03-04 15:47:53 -07:00
parent 1e1f3574eb
commit 60ce62ad1b
29 changed files with 530 additions and 744 deletions

View File

@@ -1,130 +1,128 @@
module Refinery
module WordPress
class Attachment
attr_reader :node
attr_reader :refinery_image
attr_reader :refinery_resource
def initialize(node)
@node = node
end
def title
node.xpath("title").text
end
def description
node.xpath("description").text
end
def file_name
url.split('/').last
end
def post_date
DateTime.parse node.xpath("wp:post_date").text
end
def url
node.xpath("wp:attachment_url").text
end
def url_pattern
url_parts = url.split('.')
extension = url_parts.pop
url_without_extension = url_parts.join('.')
/#{url_without_extension}(-\d+x\d+)?\.#{extension}/
end
def image?
url.match /\.(png|jpg|jpeg|gif)$/
end
def to_refinery
if image?
to_image
else
to_resource
end
end
def replace_url
if image?
replace_image_url
else
replace_resource_url
end
end
private
def to_image
image = ::Image.new
image.created_at = post_date
image.image_url = url
image.save!
@refinery_image = image
image
end
def to_resource
resource = ::Resource.new
resource.created_at = post_date
resource.file_url = url
resource.save!
@refinery_resource = resource
resource
end
def replace_image_url
replace_image_url_in_blog_posts
replace_image_url_in_pages
end
def replace_resource_url
replace_resource_url_in_blog_posts
replace_resource_url_in_pages
end
def replace_image_url_in_blog_posts
replace_url_in_blog_posts(refinery_image.image.url)
end
def replace_image_url_in_pages
replace_url_in_pages(refinery_image.image.url)
end
def replace_resource_url_in_blog_posts
replace_url_in_blog_posts(refinery_resource.file.url)
end
def replace_resource_url_in_pages
replace_url_in_pages(refinery_resource.file.url)
end
def replace_url_in_blog_posts(new_url)
::BlogPost.all.each do |post|
if (! post.body.empty?) && post.body.include?(url)
post.body = post.body.gsub(url_pattern, new_url)
post.save!
end
end
end
def replace_url_in_pages(new_url)
::Page.all.each do |page|
page.parts.each do |part|
if (! part.body.to_s.blank?) && part.body.include?(url)
part.body = part.body.gsub(url_pattern, new_url)
part.save!
end
end
end
end
module WordPressImport
class Attachment
attr_reader :node
attr_reader :refinery_image
attr_reader :refinery_resource
def initialize(node)
@node = node
end
def title
node.xpath("title").text
end
def description
node.xpath("description").text
end
def file_name
url.split('/').last
end
def post_date
DateTime.parse node.xpath("wp:post_date").text
end
def url
node.xpath("wp:attachment_url").text
end
def url_pattern
url_parts = url.split('.')
extension = url_parts.pop
url_without_extension = url_parts.join('.')
/#{url_without_extension}(-\d+x\d+)?\.#{extension}/
end
def image?
url.match /\.(png|jpg|jpeg|gif)$/
end
def to_refinery
if image?
to_image
else
to_resource
end
end
def replace_url
if image?
replace_image_url
else
replace_resource_url
end
end
private
def to_image
image = ::Image.new
image.created_at = post_date
image.image_url = url
image.save!
@refinery_image = image
image
end
def to_resource
resource = ::Resource.new
resource.created_at = post_date
resource.file_url = url
resource.save!
@refinery_resource = resource
resource
end
def replace_image_url
replace_image_url_in_blog_posts
replace_image_url_in_pages
end
def replace_resource_url
replace_resource_url_in_blog_posts
replace_resource_url_in_pages
end
def replace_image_url_in_blog_posts
replace_url_in_blog_posts(refinery_image.image.url)
end
def replace_image_url_in_pages
replace_url_in_pages(refinery_image.image.url)
end
def replace_resource_url_in_blog_posts
replace_url_in_blog_posts(refinery_resource.file.url)
end
def replace_resource_url_in_pages
replace_url_in_pages(refinery_resource.file.url)
end
def replace_url_in_blog_posts(new_url)
::BlogPost.all.each do |post|
if (! post.body.empty?) && post.body.include?(url)
post.body = post.body.gsub(url_pattern, new_url)
post.save!
end
end
end
def replace_url_in_pages(new_url)
::Page.all.each do |page|
page.parts.each do |part|
if (! part.body.to_s.blank?) && part.body.include?(url)
part.body = part.body.gsub(url_pattern, new_url)
part.save!
end
end
end
end
end
end

View File

@@ -1,37 +1,35 @@
module Refinery
module WordPress
class Author
attr_reader :author_node
module WordPressImport
class Author
attr_reader :author_node
def initialize(author_node)
@author_node = author_node
end
def initialize(author_node)
@author_node = author_node
end
def login
author_node.xpath("wp:author_login").text
end
def login
author_node.xpath("wp:author_login").text
end
def email
author_node.xpath("wp:author_email").text
end
def email
author_node.xpath("wp:author_email").text
end
def ==(other)
login == other.login
end
def ==(other)
login == other.login
end
def inspect
"WordPress::Author: #{login} <#{email}>"
end
def inspect
"WordPress::Author: #{login} <#{email}>"
end
def to_refinery
user = User.find_or_initialize_by_username_and_email(login, email)
unless user.persisted?
user.password = 'password'
user.password_confirmation = 'password'
user.save
end
user
def to_refinery
user = User.find_or_initialize_by_username_and_email(login, email)
unless user.persisted?
user.password = 'password'
user.password_confirmation = 'password'
user.save
end
user
end
end
end
end

View File

@@ -1,19 +1,17 @@
module Refinery
module WordPress
class Category
attr_accessor :name
module WordPressImport
class Category
attr_accessor :name
def initialize(text)
@name = text
end
def initialize(text)
@name = text
end
def ==(other)
name == other.name
end
def ==(other)
name == other.name
end
def to_refinery
BlogCategory.find_or_create_by_title(name)
end
def to_refinery
BlogCategory.find_or_create_by_title(name)
end
end
end

View File

@@ -1,48 +1,46 @@
module Refinery
module WordPress
class Comment
attr_reader :node
module WordPressImport
class Comment
attr_reader :node
def initialize(node)
@node = node
end
def initialize(node)
@node = node
end
def author
node.xpath('wp:comment_author').text
end
def author
node.xpath('wp:comment_author').text
end
def email
node.xpath('wp:comment_author_email').text
end
def email
node.xpath('wp:comment_author_email').text
end
def url
node.xpath('wp:comment_author_url').text
end
def url
node.xpath('wp:comment_author_url').text
end
def date
DateTime.parse node.xpath("wp:comment_date").text
end
def date
DateTime.parse node.xpath("wp:comment_date").text
end
def content
node.xpath('wp:comment_content').text
end
def content
node.xpath('wp:comment_content').text
end
def approved?
node.xpath('wp:comment_approved').text.to_i == 1
end
def approved?
node.xpath('wp:comment_approved').text.to_i == 1
end
def ==(other)
(email == other.email) && (date == other.date) && (content == other.content)
end
def ==(other)
(email == other.email) && (date == other.date) && (content == other.content)
end
def to_refinery
comment = BlogComment.new :name => author, :email => email
def to_refinery
comment = BlogComment.new :name => author, :email => email
comment.body = content
comment.created_at = date
comment.state = approved? ? 'approved' : 'rejected'
comment
end
comment.body = content
comment.created_at = date
comment.state = approved? ? 'approved' : 'rejected'
comment
end
end
end

View File

@@ -1,57 +1,55 @@
module Refinery
module WordPress
class Dump
attr_reader :doc
module WordPressImport
class Dump
attr_reader :doc
def initialize(file_name)
file_name = File.expand_path(file_name)
def initialize(file_name)
file_name = File.expand_path(file_name)
raise "Given file '#{file_name}' no file or not readable." \
unless File.file?(file_name) && File.readable?(file_name)
file = File.open(file_name)
@doc = Nokogiri::XML(file)
raise "Given file '#{file_name}' no file or not readable." \
unless File.file?(file_name) && File.readable?(file_name)
file = File.open(file_name)
@doc = Nokogiri::XML(file)
end
def authors
doc.xpath("//wp:author").collect do |author|
Author.new(author)
end
end
def pages(only_published=false)
pages = doc.xpath("//item[wp:post_type = 'page']").collect do |page|
Page.new(page)
end
def authors
doc.xpath("//wp:author").collect do |author|
Author.new(author)
end
pages = pages.select(&:published?) if only_published
pages
end
def posts(only_published=false)
posts = doc.xpath("//item[wp:post_type = 'post']").collect do |post|
Post.new(post)
end
posts = posts.select(&:published?) if only_published
posts
end
def pages(only_published=false)
pages = doc.xpath("//item[wp:post_type = 'page']").collect do |page|
Page.new(page)
end
pages = pages.select(&:published?) if only_published
pages
def tags
doc.xpath("//wp:tag/wp:tag_slug").collect do |tag|
Tag.new(tag.text)
end
end
def posts(only_published=false)
posts = doc.xpath("//item[wp:post_type = 'post']").collect do |post|
Post.new(post)
end
posts = posts.select(&:published?) if only_published
posts
def categories
doc.xpath("//wp:category/wp:cat_name").collect do |category|
Category.new(category.text)
end
end
def tags
doc.xpath("//wp:tag/wp:tag_slug").collect do |tag|
Tag.new(tag.text)
end
end
def categories
doc.xpath("//wp:category/wp:cat_name").collect do |category|
Category.new(category.text)
end
end
def attachments
doc.xpath("//item[wp:post_type = 'attachment']").collect do |attachment|
Attachment.new(attachment)
end
def attachments
doc.xpath("//item[wp:post_type = 'attachment']").collect do |attachment|
Attachment.new(attachment)
end
end
end

View File

@@ -1,107 +1,114 @@
module Refinery
module WordPress
class Page
include ::ActionView::Helpers::TagHelper
include ::ActionView::Helpers::TextHelper
module WordPressImport
class Page
include ::ActionView::Helpers::TagHelper
include ::ActionView::Helpers::TextHelper
attr_reader :node
attr_reader :node
def initialize(node)
@node = node
def initialize(node)
@node = node
end
def inspect
"WordPress::Page(#{post_id}): #{title}"
end
def title
node.xpath("title").text
end
def content
node.xpath("content:encoded").text
end
def content_formatted
formatted = format_syntax_highlighter(format_paragraphs(content))
# remove all tags inside <pre> that simple_format created
# TODO: replace format_paragraphs with a method, that ignores pre-tags
formatted.gsub!(/(<pre.*?>)(.+?)(<\/pre>)/m) do |match|
"#{$1}#{strip_tags($2)}#{$3}"
end
def inspect
"WordPress::Page(#{post_id}): #{title}"
end
def title
node.xpath("title").text
end
def content
node.xpath("content:encoded").text
end
def content_formatted
formatted = format_syntax_highlighter(format_paragraphs(content))
# remove all tags inside <pre> that simple_format created
# TODO: replace format_paragraphs with a method, that ignores pre-tags
formatted.gsub!(/(<pre.*?>)(.+?)(<\/pre>)/m) do |match|
"#{$1}#{strip_tags($2)}#{$3}"
end
formatted
end
def creator
node.xpath("dc:creator").text
end
def post_date
DateTime.parse node.xpath("wp:post_date").text
end
def post_id
node.xpath("wp:post_id").text.to_i
end
def parent_id
dump_id = node.xpath("wp:post_parent").text.to_i
dump_id == 0 ? nil : dump_id
end
def status
node.xpath("wp:status").text
end
def draft?
status != 'publish'
end
def published?
! draft?
end
def ==(other)
post_id == other.post_id
end
def to_refinery
page = ::Page.create!(:id => post_id, :title => title,
:created_at => post_date, :draft => draft?)
page.parts.create(:title => 'Body', :body => content_formatted)
page
end
private
def format_paragraphs(text, html_options={})
# WordPress doesn't export <p>-Tags, so let's run a simple_format over
# the content. As we trust ourselves, no sanatize. This code is heavily
# inspired by the simple_format rails helper
text = ''.html_safe if text.nil?
start_tag = tag('p', html_options, true)
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
text.insert 0, start_tag
formatted
end
text.html_safe.safe_concat("</p>")
end
def creator
node.xpath("dc:creator").text
end
def format_syntax_highlighter(text)
# Support for SyntaxHighlighter (http://alexgorbatchev.com/SyntaxHighlighter/):
# In WordPress you can (via a plugin) enclose code in [lang][/lang]
# blocks, which are converted to a <pre>-tag with a class corresponding
# to the language.
#
# Example:
# [ruby]p "Hello World"[/ruby]
# -> <pre class="brush: ruby">p "Hello world"</pre>
text.gsub(/\[(\w+)\](.+?)\[\/\1\]/m, '<pre class="brush: \1">\2</pre>')
end
def post_date
DateTime.parse node.xpath("wp:post_date").text
end
def post_id
node.xpath("wp:post_id").text.to_i
end
def parent_id
dump_id = node.xpath("wp:post_parent").text.to_i
dump_id == 0 ? nil : dump_id
end
def status
node.xpath("wp:status").text
end
def draft?
status != 'publish'
end
def published?
! draft?
end
def ==(other)
post_id == other.post_id
end
#NEED:
# dc:creator -> "user_id"
# wp:post_name -> "slug"
# pubDate -> "published_at"
#OK:
# title -> "title"
# content:encoded -> "body"
# wp:post_date_gmt -> "created_at"
def to_refinery
page = ::Page.create!(:id => post_id, :title => title,
:created_at => post_date, :draft => draft?)
page.parts.create(:title => 'Body', :body => content_formatted)
page
end
private
def format_paragraphs(text, html_options={})
# WordPress doesn't export <p>-Tags, so let's run a simple_format over
# the content. As we trust ourselves, no sanatize. This code is heavily
# inspired by the simple_format rails helper
text = ''.html_safe if text.nil?
start_tag = tag('p', html_options, true)
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
text.insert 0, start_tag
text.html_safe.safe_concat("</p>")
end
def format_syntax_highlighter(text)
# Support for SyntaxHighlighter (http://alexgorbatchev.com/SyntaxHighlighter/):
# In WordPress you can (via a plugin) enclose code in [lang][/lang]
# blocks, which are converted to a <pre>-tag with a class corresponding
# to the language.
#
# Example:
# [ruby]p "Hello World"[/ruby]
# -> <pre class="brush: ruby">p "Hello world"</pre>
text.gsub(/\[(\w+)\](.+?)\[\/\1\]/m, '<pre class="brush: \1">\2</pre>')
end
end
end

View File

@@ -1,85 +1,83 @@
module Refinery
module WordPress
class Post < Page
def tags
# xml dump has "post_tag" for wordpress 3.1 and "tag" for 3.0
path = if node.xpath("category[@domain='post_tag']").count > 0
"category[@domain='post_tag']"
else
"category[@domain='tag']"
end
node.xpath(path).collect do |tag_node|
Tag.new(tag_node.text)
end
module WordPressImport
class Post < Page
def tags
# xml dump has "post_tag" for wordpress 3.1 and "tag" for 3.0
path = if node.xpath("category[@domain='post_tag']").count > 0
"category[@domain='post_tag']"
else
"category[@domain='tag']"
end
def tag_list
tags.collect(&:name).join(',')
node.xpath(path).collect do |tag_node|
Tag.new(tag_node.text)
end
def categories
node.xpath("category[@domain='category']").collect do |cat|
Category.new(cat.text)
end
end
def comments
node.xpath("wp:comment").collect do |comment_node|
Comment.new(comment_node)
end
end
def to_refinery
user = ::User.find_by_username(creator) || ::User.first
raise "Referenced User doesn't exist! Make sure the authors are imported first." \
unless user
begin
post = ::BlogPost.new :title => title, :body => content_formatted,
:draft => draft?, :published_at => post_date, :created_at => post_date,
:user_id => user.id, :tag_list => tag_list
post.save!
::BlogPost.transaction do
categories.each do |category|
post.categories << category.to_refinery
end
comments.each do |comment|
comment = comment.to_refinery
comment.post = post
comment.save
end
end
rescue ActiveRecord::RecordInvalid
# if the title has already been taken (WP allows duplicates here,
# refinery doesn't) append the post_id to it, making it unique
post.title = "#{title}-#{post_id}"
post.save
end
post
end
def self.create_blog_page_if_necessary
# refinerycms wants a page at /blog, so let's make sure there is one
# taken from the original db seeds from refinery-blog
unless ::Page.where("link_url = ?", '/blog').exists?
page = ::Page.create(
:title => "Blog",
:link_url => "/blog",
:deletable => false,
:position => ((::Page.maximum(:position, :conditions => {:parent_id => nil}) || -1)+1),
:menu_match => "^/blogs?(\/|\/.+?|)$"
)
::Page.default_parts.each do |default_page_part|
page.parts.create(:title => default_page_part, :body => nil)
end
end
end
end
def tag_list
tags.collect(&:name).join(',')
end
def categories
node.xpath("category[@domain='category']").collect do |cat|
Category.new(cat.text)
end
end
def comments
node.xpath("wp:comment").collect do |comment_node|
Comment.new(comment_node)
end
end
def to_refinery
user = ::User.find_by_username(creator) || ::User.first
raise "Referenced User doesn't exist! Make sure the authors are imported first." \
unless user
begin
post = ::BlogPost.new :title => title, :body => content_formatted,
:draft => draft?, :published_at => post_date, :created_at => post_date,
:user_id => user.id, :tag_list => tag_list
post.save!
::BlogPost.transaction do
categories.each do |category|
post.categories << category.to_refinery
end
comments.each do |comment|
comment = comment.to_refinery
comment.post = post
comment.save
end
end
rescue ActiveRecord::RecordInvalid
# if the title has already been taken (WP allows duplicates here,
# refinery doesn't) append the post_id to it, making it unique
post.title = "#{title}-#{post_id}"
post.save
end
post
end
def self.create_blog_page_if_necessary
# refinerycms wants a page at /blog, so let's make sure there is one
# taken from the original db seeds from refinery-blog
unless ::Page.where("link_url = ?", '/blog').exists?
page = ::Page.create(
:title => "Blog",
:link_url => "/blog",
:deletable => false,
:position => ((::Page.maximum(:position, :conditions => {:parent_id => nil}) || -1)+1),
:menu_match => "^/blogs?(\/|\/.+?|)$"
)
::Page.default_parts.each do |default_page_part|
page.parts.create(:title => default_page_part, :body => nil)
end
end
end
end
end

View File

@@ -1,9 +1,7 @@
module Refinery
module WordPress
class Railtie < Rails::Railtie
rake_tasks do
load "tasks/wordpress.rake"
end
module WordPressImport
class Railtie < Rails::Railtie
rake_tasks do
load "tasks/wordpress.rake"
end
end
end

View File

@@ -1,20 +1,18 @@
module Refinery
module WordPress
class Tag
attr_accessor :name
def initialize(text)
@name = text
end
def ==(other)
name == other.name
end
def to_refinery
::ActsAsTaggableOn::Tag.find_or_create_by_name(name)
end
module WordPressImport
class Tag
attr_accessor :name
def initialize(text)
@name = text
end
def ==(other)
name == other.name
end
def to_refinery
::ActsAsTaggableOn::Tag.find_or_create_by_name(name)
end
end
end