Class: Locomotivecms::Freight::WordpressImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotivecms/freight/wordpress_importer.rb

Constant Summary collapse

SOURCE =
'WordPress Importer'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ WordpressImporter

Returns a new instance of WordpressImporter.



15
16
17
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 15

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 11

def client
  @client
end

Instance Method Details

#all_comments(filter = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 51

def all_comments filter={}
  [].tap do |all_comments|
    page = 1
    while page do
      comments = client.contents.comments.all(filter, page: page)
      all_comments << comments
      page = comments._next_page
    end
  end.flatten
end

#all_images(input_file) ⇒ Object



26
27
28
29
30
31
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 26

def all_images input_file
  parse_all_posts(input_file).map do |item|
    body = item.xpath('content:encoded').text
    body.scan(/<img.*?>/mi).map{|img| img.strip }
  end.flatten
end

#all_imported_posts(filter = { source: SOURCE }) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 40

def all_imported_posts filter={ source: SOURCE }
  [].tap do |all_posts|
    page = 1
    while page do
      posts = client.contents.posts.all(filter, page: page)
      all_posts << posts
      page = posts._next_page
    end
  end.flatten
end


19
20
21
22
23
24
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 19

def all_links input_file
  parse_all_posts(input_file).map do |item|
    body = item.xpath('content:encoded').text
    body.scan(/<a .*?<\/a>/mi).map{|link| link.strip }
  end.flatten
end

#all_tables(input_file) ⇒ Object



33
34
35
36
37
38
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 33

def all_tables input_file
  parse_all_posts(input_file).map do |item|
    body = item.xpath('content:encoded').text
    body.scan(/<table.*?<\/table>/i).map{|table| table.strip }
  end.flatten
end

#clean!Object



155
156
157
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 155

def clean!
  remove_posts
end

#create(content_type, params) ⇒ Object



168
169
170
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 168

def create content_type, params
  contents_action content_type.to_sym, :create, params
end

#destroy(thing) ⇒ Object



163
164
165
166
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 163

def destroy thing
  content_type = thing.content_type_slug.to_sym
  contents_action content_type, :destroy, thing._id
end

#import(import_file, format = html) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 62

def import import_file, format=html
  metrics = OpenStruct.new posts: 0, comments: 0
  parse_all_posts(import_file).each do |item|

    post = post_for item
    puts "creating post [[ #{post.title} ]]"
    p = client.contents.posts.create post.cms_params
    post._id = p._id
    post.save
    metrics.posts += 1

    comments = comments_for item, post
    unless comments.empty?
      puts "  -- creating #{comments.count} comments..."
      comments.each do |comment|
        client.contents.comments.create comment
        metrics.comments += 1
      end
    end
  end
  puts "Imported #{metrics.posts} posts and #{metrics.comments} comments."
end

#remove_non_visible_posts!Object



159
160
161
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 159

def remove_non_visible_posts!
  remove_posts _visible: false
end

#rewrite_imagesObject



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
147
148
149
150
151
152
153
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 106

def rewrite_images
  return unless Post.any?
  metrics = OpenStruct.new images: 0

  # ensure /public/images/posts exists

  Post.each do |post|
    needs_update = false
    default_host = post.id.gsub(/^(https?:\/\/[^\/]*).*$/, '\1')

    post.image_urls.each do |image_url|
      original_image_url = image_url.dup
      image_url = "#{default_host}#{image_url}" if image_url.match(/^\//)
      unless (File.directory?('public/images/posts'))
        Dir.mkdir('public/images/posts')
      end

      image_file_name = image_url.gsub(/^.*\//, '')
      puts <<-TEXT.strip_heredoc

        image_url: #{image_url}
        image_file_name: #{image_file_name}
        ================================================================================
      TEXT
      curl = `curl #{image_url} > public/images/posts/#{image_file_name}`
      puts curl
      if curl.match /<url> malformed/m
        raise <<-ERROR.strip_heredoc
          Problem downloading image "#{image_url}"
          image_file_name: #{image_file_name}
          post.body:

          #{post.body}

        ERROR
      end
      metrics.images += 1

      post.body.gsub! /#{original_image_url}/, "/sites/#{site_id}/theme/images/posts/#{image_file_name}"
      post.save
      needs_update = true
    end
    client.contents.posts.update(post._id, { body: post.body }) if needs_update
  end

  puts "Rewrote #{metrics.images} images in #{Post.count} posts."
  puts "Don't forget to `bundle exec wagon push ENV -r theme_assets`"
end

#rewrite_internal_urlsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/locomotivecms/freight/wordpress_importer.rb', line 85

def rewrite_internal_urls
  return unless Post.any?
  metrics = OpenStruct.new links: 0

  Post.each do |post|
    needs_update = false
    post.links.each do |link|
      if linked_post = Post[link]
        metrics.links += 1
        puts "[ #{post.title} ]: rewriting \"#{link}\" -> \"/posts/#{linked_post._slug}\""
        post.body.gsub! /#{link}/, "/posts/#{linked_post._slug}"
        post.save
        needs_update = true
      end
    end
    client.contents.posts.update(post._id, { body: post.body }) if needs_update
  end

  puts "Rewrote #{metrics.links} links in #{Post.count} posts."
end