sutty-migration 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/sutty_migration/wordpress.rb +7 -3
- metadata +1 -2
- data/lib/wordpress.rb +0 -192
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bbc62b40240f7bdea15ea0994e1af5db0e7590a500459ca82e4727f86bb791a5
|
|
4
|
+
data.tar.gz: '087218a58be167e9348d4314922a8985a56339b9c6dd340edbfa21be1aabc9ed'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5a6933d3c03a86aa0b469f75afa42c969bc22301672054cba60eb5256b87a4381c325e7d49fd9af1259cef0d44094c5fe78df2aec51f859446b34625b24921a5
|
|
7
|
+
data.tar.gz: 3ce853bddcf43a8c993ef8c8f1e9601fb4da59bdf7eb58a96611baef8e6934375b4bd20e1cb67a008025e03b0c94d62772ab18f4d23550d063d6d381a1190bdd
|
|
@@ -38,15 +38,19 @@ module SuttyMigration
|
|
|
38
38
|
#
|
|
39
39
|
# @return [Hash] { "ID" => SuttyMigration::Wordpress }
|
|
40
40
|
def blogs
|
|
41
|
-
@blogs ||= wp["select
|
|
41
|
+
@blogs ||= wp["select * from #{prefix}blogs"].to_a.map do |blog|
|
|
42
42
|
url = "https://#{blog[:domain]}#{blog[:path]}"
|
|
43
|
-
pfx = "#{prefix}#{blog[:
|
|
43
|
+
pfx = "#{prefix}#{blog[:blog_id]}_" if blog[:blog_id] > 1
|
|
44
44
|
pfx ||= prefix
|
|
45
45
|
|
|
46
|
-
[ blog[:
|
|
46
|
+
[ blog[:blog_id], blog.merge(db: self.class.new(site: site, url: url, prefix: pfx, database: database, limit: limit, multisite: self)) ]
|
|
47
47
|
end.to_h
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
+
def options
|
|
51
|
+
@options ||= wp["select option_name, option_value from #{prefix}options"].to_a.map(&:values).to_h.transform_keys(&:to_sym)
|
|
52
|
+
end
|
|
53
|
+
|
|
50
54
|
# Open the database.
|
|
51
55
|
#
|
|
52
56
|
# @return [Sequel::SQLite::Database]
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sutty-migration
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- f
|
|
@@ -138,7 +138,6 @@ files:
|
|
|
138
138
|
- lib/sutty_migration/data.rb
|
|
139
139
|
- lib/sutty_migration/jekyll/document_creator.rb
|
|
140
140
|
- lib/sutty_migration/wordpress.rb
|
|
141
|
-
- lib/wordpress.rb
|
|
142
141
|
homepage: https://0xacab.org/sutty/jekyll/sutty-migration
|
|
143
142
|
licenses:
|
|
144
143
|
- GPL-3.0
|
data/lib/wordpress.rb
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# Generar UUIDs
|
|
4
|
-
require 'securerandom'
|
|
5
|
-
# Traer resultados de la base de datos
|
|
6
|
-
require 'sequel'
|
|
7
|
-
require 'sqlite3'
|
|
8
|
-
require 'json'
|
|
9
|
-
# Descargar archivos
|
|
10
|
-
require 'faraday'
|
|
11
|
-
require 'progressbar'
|
|
12
|
-
|
|
13
|
-
class Wordpress
|
|
14
|
-
attr_reader :site, :prefix, :limit, :url
|
|
15
|
-
|
|
16
|
-
def initialize(site:, url:, prefix: 'wp_', limit: 10)
|
|
17
|
-
@site = site
|
|
18
|
-
@prefix = prefix.freeze
|
|
19
|
-
@limit = limit.freeze
|
|
20
|
-
@url = url.freeze
|
|
21
|
-
|
|
22
|
-
# Conectarse a la base de datos
|
|
23
|
-
@wp = Sequel.sqlite(File.join(site.source, '_data', 'wordpress', 'post.sqlite3'))
|
|
24
|
-
# Las funciones de JSON usan mucha CPU, vamos a traer de a pocos
|
|
25
|
-
# registros.
|
|
26
|
-
@wp.extension :pagination
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def download(file)
|
|
30
|
-
dest = "wp-content/uploads/#{file}"
|
|
31
|
-
full = File.join(site.source, dest)
|
|
32
|
-
|
|
33
|
-
return dest if File.exist? full
|
|
34
|
-
|
|
35
|
-
Jekyll.logger.info "Downloading #{dest}"
|
|
36
|
-
|
|
37
|
-
FileUtils.mkdir_p File.dirname(full)
|
|
38
|
-
|
|
39
|
-
File.open(full, 'w') do |f|
|
|
40
|
-
url = "#{url}/#{dest}"
|
|
41
|
-
head = Faraday.head(url)
|
|
42
|
-
content_length = head.headers['content-length']
|
|
43
|
-
progress_bar = ProgressBar.new
|
|
44
|
-
|
|
45
|
-
Faraday.get(url) do |req|
|
|
46
|
-
req.options.on_data = Proc.new do |chunk, downloaded_bytes|
|
|
47
|
-
f.write chunk
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
dest
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
# Obtiene todos los tipos de artículos disponibles
|
|
56
|
-
#
|
|
57
|
-
# @return [Array]
|
|
58
|
-
def layouts
|
|
59
|
-
@layouts ||= @wp["select distinct post_type from #{prefix}posts"].to_a.map(&:values).flatten
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
# Obtiene todos los posts opcionalmente filtrando por tipo de post.
|
|
63
|
-
# No es la forma oficial de Sequel pero no tenemos tiempo de
|
|
64
|
-
# aprenderla específicamente y además tenemos las opciones en formato
|
|
65
|
-
# JSON que no estarían soportadas.
|
|
66
|
-
#
|
|
67
|
-
# @param :layout [String] Layout name, one of #layouts
|
|
68
|
-
# @param :with_meta [Boolean]
|
|
69
|
-
# @return [Enumerator]
|
|
70
|
-
def posts(**options)
|
|
71
|
-
if options[:layout] && !layouts.include?(options[:layout])
|
|
72
|
-
raise ArgumentError, "#{layout} must be one of #{layouts.join(', ')}"
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
@posts ||= {}
|
|
76
|
-
@posts[options[:layout] || 'all'] ||= @wp[post_query(**options)].each_page(limit).to_a.map(&:to_a).flatten.tap do |p|
|
|
77
|
-
next unless options[:with_meta]
|
|
78
|
-
|
|
79
|
-
p.map do |post|
|
|
80
|
-
post[:front_matter] = JSON.parse(post[:front_matter]) unless post[:front_matter].nil?
|
|
81
|
-
post[:terms] = JSON.parse(post[:terms]) unless post[:terms].nil?
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
private
|
|
87
|
-
|
|
88
|
-
# Consulta para los posts, incluyendo metadatos en JSON. Los
|
|
89
|
-
# metadatos vienen en dos partes porque tienen dos
|
|
90
|
-
#
|
|
91
|
-
# @return [String]
|
|
92
|
-
def post_query(layout: nil, with_meta: true)
|
|
93
|
-
@post_query ||= <<~EOQ
|
|
94
|
-
select
|
|
95
|
-
p.ID as id,
|
|
96
|
-
p.post_title as title,
|
|
97
|
-
p.post_name as slug,
|
|
98
|
-
p.post_type as layout,
|
|
99
|
-
p.strftime('%Y-%m-%d', post_date) as date,
|
|
100
|
-
p.post_status as status,
|
|
101
|
-
p.post_content as content
|
|
102
|
-
#{", json_group_object(f.meta_key, f.meta_value) as front_matter" if with_meta}
|
|
103
|
-
#{", t.meta as meta" if with_meta}
|
|
104
|
-
from #{prefix}posts as p
|
|
105
|
-
left join #{prefix}postmeta as f on p.ID = f.post_id
|
|
106
|
-
#{"left join (#{meta_query(layout: layout)}) as as t on t.id = p.ID" if with_meta}
|
|
107
|
-
#{"where p.post_type = :layout" if layout}
|
|
108
|
-
group by p.ID
|
|
109
|
-
EOQ
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
#
|
|
113
|
-
def meta_query(layout: nil)
|
|
114
|
-
@meta_query ||= <<~EOQ
|
|
115
|
-
select
|
|
116
|
-
p.ID as id,
|
|
117
|
-
json_group_object(tt.taxonomy, t.name) as meta
|
|
118
|
-
from #{prefix}posts as p
|
|
119
|
-
left join #{prefix}term_relationships as r on r.object_id = p.ID
|
|
120
|
-
left join #{prefix}term_taxonomy as tt on tt.term_taxonomy_id = r.term_taxonomy_id
|
|
121
|
-
left join #{prefix}terms as t on t.term_id = tt.term_id
|
|
122
|
-
#{"where p.post_type = :layout" if layout}
|
|
123
|
-
group by p.ID
|
|
124
|
-
EOQ
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
# Antes de generar el sitio vamos a leer todos los artículos desde la
|
|
129
|
-
# base de datos y generarlos localmente.
|
|
130
|
-
Jekyll::Hooks.register :site, :post_read do |site|
|
|
131
|
-
wp = Wordpress.new(site: site,
|
|
132
|
-
url: site.config.dig('wordpress', 'url'),
|
|
133
|
-
prefix: site.config.dig('wordpress', 'prefix'))
|
|
134
|
-
|
|
135
|
-
collection = site.collections['posts']
|
|
136
|
-
ascii_re = Regexp.new("\P{ASCII}").freeze
|
|
137
|
-
sanitizer = Rails::Html::SafeListSanitizer.new
|
|
138
|
-
|
|
139
|
-
# Traer todas las imágenes cargadas y descargarlas
|
|
140
|
-
attachments = wp.posts(layout: 'attachment').map do |page|
|
|
141
|
-
page.map do |attachment|
|
|
142
|
-
attachment[:data] = JSON.parse(attachment[:data]) unless attachment[:data].nil?
|
|
143
|
-
file = attachment.dig(:data, '_wp_attached_file')
|
|
144
|
-
|
|
145
|
-
next unless file
|
|
146
|
-
|
|
147
|
-
dest = wp.download(file)
|
|
148
|
-
|
|
149
|
-
# Tener un mapa de IDs y archivos destino
|
|
150
|
-
[ attachment[:id], dest ]
|
|
151
|
-
end
|
|
152
|
-
end.compact.flatten(1).to_h
|
|
153
|
-
|
|
154
|
-
%w[post page].each do |type|
|
|
155
|
-
wp.posts(layout: type).each do |page|
|
|
156
|
-
page.each do |post|
|
|
157
|
-
# Convertir los datos extra en un Hash
|
|
158
|
-
post[:data] = JSON.parse(post[:data]) unless post[:data].nil?
|
|
159
|
-
post[:slug] = Jekyll::Utils.slugify(post[:title], mode: 'latin') if post[:slug].empty?
|
|
160
|
-
post[:meta] = wp.meta id: post[:id]
|
|
161
|
-
|
|
162
|
-
path = File.join(site.source, '_posts', post.slice(:date, :slug).values.join('-') + '.markdown')
|
|
163
|
-
|
|
164
|
-
if File.exist? path
|
|
165
|
-
Jekyll.logger.info "#{path} ya fue migrado, actualizando"
|
|
166
|
-
|
|
167
|
-
doc = site.documents.find do |d|
|
|
168
|
-
d['id'] == post[:id]
|
|
169
|
-
end
|
|
170
|
-
else
|
|
171
|
-
# Crear un post nuevo y agregarlo a la colección
|
|
172
|
-
collection.docs << doc = Jekyll::Document.new(path, site: site, collection: collection)
|
|
173
|
-
doc.data['uuid'] = SecureRandom.uuid
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
thumbnail = post.dig(:data, '_thumbnail_id')&.to_i
|
|
177
|
-
|
|
178
|
-
doc.data['layout'] = type
|
|
179
|
-
doc.data['title'] = post[:title]
|
|
180
|
-
doc.data['draft'] = post[:status] != 'publish'
|
|
181
|
-
doc.data['id'] = post[:id]
|
|
182
|
-
doc.data['date'] = Jekyll::Utils.parse_date(post[:date])
|
|
183
|
-
doc.data['tags'] = post[:meta].select { |k| k[:type] == 'post_tag' }.map { |k| k[:name] }
|
|
184
|
-
doc.data['categories'] = post[:meta].select { |k| k[:type] == 'category' }.map { |k| k[:name] }
|
|
185
|
-
doc.data['image'] = attachments[thumbnail] if thumbnail
|
|
186
|
-
|
|
187
|
-
doc.content = ReverseMarkdown.convert(sanitizer.sanitize(post[:content]))
|
|
188
|
-
doc.save
|
|
189
|
-
end
|
|
190
|
-
end
|
|
191
|
-
end
|
|
192
|
-
end
|