-
Notifications
You must be signed in to change notification settings - Fork 0
/
backupxml.js
401 lines (367 loc) · 12.9 KB
/
backupxml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
const p = require('@clack/prompts');
const { setTimeout } = require('node:timers/promises');
const color = require('picocolors');
const AdmZip = require('adm-zip')
const fs = require('fs-extra')
const path = require('path')
const { create } = require('xmlbuilder2');
const config = require(process.cwd() + '/config/app');
const urljoin = require('url-join')
const moment = require('moment')
const cheerio = require('cheerio')
const mongoose = require('mongoose');
const db_config = require(process.cwd() + '/config/database');
const connectDb = async () => {
return mongoose
.connect(db_config.url, db_config.options)
.then(() => {
})
.catch((e) => {
console.error(e.message);
throw new Error('Error connect Mongoose!');
});
};
// ==================================
global.__version = config.app.version;
global.__modules = `${__dirname}/modules`;
global.__config = `${__dirname}/config`;
global.__root = __dirname;
// ==================================
const ReviewList = require(__modules + '/page/helpers/ReviewList');
const PostList = require(__modules + '/page/helpers/PostList');
const ReviewModel = require(__modules + '/review/models/Review')
const ProductwModel = require(__modules + '/product/models/Product')
const CateModel = require(__modules + '/category/models/Category')
const PostModel = require(__modules + '/post/models/Post')
const UserModel = require(__modules + '/user/models/User')
const { get_settings } = require(__modules + '/app/helpers/system-setting');
function sectionsToContent({ meta_content = "", sections = [] }) {
let content = meta_content && meta_content.main ? meta_content.main : ''
sections.sort((a, b) => a.sort - b.sort)
for (const section of sections) {
const is_comparison_table = section.id == "compare"
if (!is_comparison_table) {
content += section.title ? `<h2>${section.title}</h2>` : ""
content += section.content ? section.content : ""
}
}
return content
}
function Zip({ out_upload_path }) {
const zip = new AdmZip();
zip.addLocalFolder("./upload/")
try {
zip.writeZip(out_upload_path);
} catch (e) {
console.log(e.message)
}
}
function replaceDomain(url, domain) {
if (!url || url == "undefined" || !domain) {
return ""
}
if (url.indexOf('amazon.com') > -1) {
return url
}
if (url.indexOf('/') == 0) {
return urljoin(domain, url)
}
try {
const parsed = new URL(url)
const new_url = urljoin(domain, parsed.pathname + parsed.search)
return new_url
} catch (e) {
console.log(url, typeof url, e)
return url
}
}
const url_pattern = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)*:\d*/g;
async function init(out_folder, web_folder) {
const backup = await p.group({
change_domain: () =>
p.confirm({
message: `Do you want to restore the website to a new domain?`,
initialValue: false
}),
old_domain: ({ results }) => {
if (!results.change_domain) return;
return p.text({
message: 'Old domain:',
placeholder: 'Ex: https://example.com or http://123.456.789.8000...',
validate: (value) => {
if (value && !url_pattern.test(value)) {
return 'Please enter a valid domain or ip address.';
}
},
})
},
// has_posts: () =>
// p.confirm({
// message: `Does your new website already has some posts?`,
// initialValue: false
// }),
// start_id: ({ results }) => {
// if (!results.has_posts) return;
// return p.text({
// message: 'You must provide a starting ID to restore backed-up posts. Without a starting ID, there could be conflicts with your existing posts',
// placeholder: 'Start ID...',
// validate: (value) => {
// if (value && !/^[0-9]*$/.test(value)) {
// return 'Start ID must be a number';
// }
// },
// })
// }
},
{
onCancel: () => {
p.cancel('Operation cancelled.');
process.exit(0);
},
}
)
const out_file_path = path.join(out_folder, `${web_folder}.xml`)
const out_upload_path = path.join(out_folder, `${web_folder}.zip`)
const s_db = p.spinner()
s_db.start("Connecting to database...")
await connectDb()
s_db.stop("Database connected!")
const s = p.spinner()
s.start("Generating XML...")
await setTimeout(1000)
const result = await startBackupContent({
out_file_path,
old_domain: backup.old_domain,
})
s.stop("XML file generated!")
const s_upload = p.spinner()
s_upload.start("Backing up upload folder...");
await setTimeout(1000)
Zip({ out_upload_path })
s_upload.stop("Upload folder packed!")
await setTimeout(1000)
const note = `- Total: ${result.total}\n- Exported: ${result.exported}\n- No content: ${result.error}\n- Old domain: ${result.old_domain || "-"}\n- Exported XML: ${out_file_path}\n- Exported Upload: ${out_upload_path}`
p.note(note, "Export result:")
p.log.info("Done!")
}
async function main() {
const [root_folder, backup_folder] = process.argv.slice(2);
p.intro(`${color.bgYellow(color.black(` Backing up: ${backup_folder} `))}`)
await init(root_folder, backup_folder)
p.outro(`Problems? Please contact us at ${color.underline(color.cyan('https://affiliatecms.com'))}`);
process.exit(0)
}
async function startBackupContent({ out_file_path, old_domain }) {
const stats = {
total: 0,
exported: 0,
error: 0,
out_file_path,
old_domain
}
let start_id = 1
try {
const users = await UserModel
.find({}, { username: true, fullname: true, email: true })
.lean()
.exec()
const { main: settings } = await get_settings(["main"])
old_domain = old_domain ? old_domain : settings.domain
const default_list_agrs = {
cond: {},
req: {
query: {
page: 1
},
},
customPageSize: Infinity
}
const { reviews } = await ReviewList(default_list_agrs);
const { posts } = await PostList(default_list_agrs);
const items = []
const all_items = [
...reviews.map(item => Object.assign(item, { _type: "review" })),
...posts.map(item => Object.assign(item, { _type: "post" }))
];
stats.total = all_items.length
all_items.map((post, i) => {
let content = post.content
// plain content from sections
if (settings.edit_mode == 'block' || !content) {
content = post._type == "review"
? sectionsToContent({
meta_content: post.meta_content,
sections: post.custom_sections
})
: sectionsToContent({
meta_content: post.meta_content.main.content,
sections: post.content_blocks
})
}
const $ = cheerio.load(content)
if ($('body').text() != "") {
stats.exported += 1
const date_string = moment(post.created_at).utcOffset(0).toString()
const date_format = moment(post.created_at).format("YYYY-MM-DD H:m:s")
let creator = ""
if (post._type == "review") {
creator = post.authors & post.authors.length ? post.authors[0].username : "admin"
} else {
creator = post.author ? post.author.username : "admin"
}
const attachment_file = replaceDomain(post.image, old_domain)
// replace images src urls
$('img').each((_i, img) => {
const src = $(img).attr('src')
if (src) {
let new_src = src
if (src.indexOf('/upload') == 0) {
new_src = src.replace('/upload', '/wp-content/uploads')
}
// new_src = replaceDomain(new_src, new_domain)
$(img).attr('src', new_src)
}
})
// replace amz tags
$('a').each((_i, a) => {
const href = $(a).attr('href')
if (href && href.indexOf("amazon.com") > -1) {
const [new_href] = href.split('?')
$(a).attr('href', new_href)
}
})
content = $('body').html()
content = content.replace("%comparison_table%", '')
const post_id = start_id
start_id = start_id + 2
// console.log("⭐ ", post.title)
const post_with_thumbnail = [
// post
{
title: { $: post.title },
link: urljoin(old_domain, post.meta_slug),
pubDate: date_string,
"dc:creator": { $: creator },
guid: {
"@isPermaLink": "fase",
$: urljoin(old_domain, '/?p=' + post_id)
},
description: { $: post.meta_desc.replace("%keyword%", "") },
"excerpt:encoded": { $: post.meta_desc.replace("%keyword%", "") },
"content:encoded": { $: content.replace("%keyword%", "") },
"wp:post_id": post_id,
"wp:post_date": { $: date_format },
"wp:post_date_gmt": { $: date_format },
"wp:post_modified": { $: date_format },
"wp:post_modified_gmt": { $: date_format },
"wp:comment_status": { $: 'open' },
"wp:ping_status": { $: 'open' },
"wp:post_name": { $: post.meta_slug.split('/').join('') },
"wp:status": { $: 'publish' },
"wp:post_type": { $: "post" },
"wp:post_password": { $: "" },
"wp:is_sticky": 0,
"wp:postmeta": [{
"wp:meta_key": { $: "_edit_last" },
"wp:meta_value": { $: 1 }
}, {
"wp:meta_key": { $: "_thumbnail_id" },
"wp:meta_value": { $: post_id + 1 }
}],
...(
post.category
? {
category: {
"@domain": "category",
"@nicename": post.category.path.split('/').join(''),
$: post.category.name
}
}
: {}
)
},
// featured image
attachment_file
? {
title: { $: post.title },
link: urljoin(old_domain, post.meta_slug),
pubDate: date_string,
"dc:creator": { $: creator },
guid: {
"@isPermaLink": "fase",
$: attachment_file || ""
},
description: "",
"content:encoded": { $: "" },
"excerpt:encoded": { $: "" },
"wp:post_id": post_id + 1,
"wp:post_date": { $: date_format },
"wp:post_date_gmt": { $: date_format },
"wp:post_modified": { $: date_format },
"wp:post_modified_gmt": { $: date_format },
"wp:comment_status": { $: 'open' },
"wp:post_name": { $: post.meta_slug.split('/').join('') },
"wp:post_parent": post_id,
"wp:ping_status": { $: 'closed' },
"wp:status": { $: 'inherit' },
"wp:post_type": { $: "attachment" },
"wp:post_password": { $: "" },
"wp:is_sticky": 0,
"wp:attachment_url": { $: attachment_file || "" },
"wp:postmeta": [{
"wp:meta_key": { $: "_wp_attached_file" },
"wp:meta_value": { $: attachment_file }
}, {
"wp:meta_key": { $: "_wp_attachment_image_alt" },
"wp:meta_value": { $: post.title }
}]
}
: false
]
items.push(...post_with_thumbnail)
} else {
stats.error += 1
}
})
const data = {
rss: {
"@xmlns:excerpt": "http://wordpress.org/export/1.2/excerpt/",
"@xmlns:content": "http://purl.org/rss/1.0/modules/content/",
"@xmlns:wfw": "http://wellformedweb.org/CommentAPI/",
"@xmlns:dc": "http://purl.org/dc/elements/1.1/",
"@xmlns:wp": "http://wordpress.org/export/1.2/",
channel: {
title: { $: settings.page_title },
link: settings.domain,
description: settings.description,
language: "en-US",
"wp:wxr_version": 1.2,
"wp:base_site_url": settings.domain,
"wp:base_blog_url": settings.domain,
"wp:author": users.map((author, i) => ({
"wp:author_id": i,
"wp:author_login": { $: author.username },
"wp:author_email": { $: author.email || "" },
"wp:author_display_name": { $: author.fullname },
"wp:author_first_name": { $: author.fullname },
})),
image: {
url: urljoin(old_domain, settings.favicon),
height: 100,
width: 100,
title: settings.page_title,
},
item: items.filter(Boolean)
},
}
}
// fs.writeJSONSync(out_file_path.replace("xml", "json"), data)
const xml = create({ version: "1.0", encoding: "UTF-8" }, data)
fs.writeFileSync(out_file_path, xml.end({ prettyPrint: true }))
return stats
} catch (e) {
console.error(e.message);
return { error: e.message }
}
}
main().catch(console.error)