domma-cms 1.69.2 → 1.70.0
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.
- package/CHANGELOG.md +10 -0
- package/admin/js/lib/wordpress-import-arrange.js +1 -1
- package/admin/js/templates/docs/usage-tools.html +11 -0
- package/admin/js/templates/wordpress-import.html +25 -3
- package/admin/js/views/wordpress-import.js +10 -10
- package/docs/DommaFeatures.md +3 -1
- package/package.json +1 -1
- package/server/routes/api/wordpress-import.js +60 -3
- package/server/services/wpImport/crawl.js +12 -3
- package/server/services/wpImport/custom.js +1 -0
- package/server/services/wpImport/index.js +24 -4
- package/server/services/wpImport/plan.js +82 -5
- package/server/services/wpImport/redirects.js +436 -0
- package/server/services/wpImport/wxr.js +5 -1
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
* DELETE /staged/:id/uploads drop that zip
|
|
15
15
|
* POST /staged/:id/widgets the widgets, as a .wie file (the "Widget Importer & Exporter" plugin's) - multipart, field "file"
|
|
16
16
|
* DELETE /staged/:id/widgets drop that file
|
|
17
|
+
* POST /staged/:id/redirects the old site's own redirects, as a list: a file (multipart, field "file" - .csv, .tsv,
|
|
18
|
+
* .txt, .json, .htaccess) or pasted ({text}); services/wpImport/redirects.js reads it
|
|
19
|
+
* DELETE /staged/:id/redirects drop that list
|
|
17
20
|
* POST /staged/:id/preview {options} -> the plan; nothing is written
|
|
18
21
|
* POST /staged/:id/run {options} -> a background job
|
|
19
22
|
* DELETE /staged/:id discard an upload
|
|
@@ -39,9 +42,10 @@ import {adminOrigin} from '../../services/passwordReset.js';
|
|
|
39
42
|
import {getConfig} from '../../config.js';
|
|
40
43
|
import {
|
|
41
44
|
blogTarget, shopTarget, cleanOptions, createStaging, discardStaged, getJob, inspectStaged, previewImport, runningJob, stagingMeta,
|
|
42
|
-
stagingPath, startRun, updateStagingMeta, UPLOADS_ZIP_MAX_BYTES, WIE_MAX_BYTES, widgetsMeta, WXR_MAX_BYTES, zipRel
|
|
45
|
+
redirectsMeta, REDIRECT_LIST_MAX_BYTES, stagingPath, startRun, updateStagingMeta, UPLOADS_ZIP_MAX_BYTES, WIE_MAX_BYTES, widgetsMeta, WXR_MAX_BYTES, zipRel
|
|
43
46
|
} from '../../services/wpImport/index.js';
|
|
44
47
|
import {readWie} from '../../services/wpImport/widgets.js';
|
|
48
|
+
import {parseRedirectList} from '../../services/wpImport/redirects.js';
|
|
45
49
|
import {crawlSite, CrawlError, startUrl} from '../../services/wpImport/crawl.js';
|
|
46
50
|
import {getReport, isId, listHistory, listStaging, removeHistory} from '../../services/wpImport/store.js';
|
|
47
51
|
import {openZip} from '../../services/wpImport/zip.js';
|
|
@@ -185,7 +189,7 @@ export function planForScreen(plan, blog, shop = {}) {
|
|
|
185
189
|
...(r.front && {front: true}), ...(r.moveFrom && {moveFrom: r.moveFrom}),
|
|
186
190
|
...(r.homeLatest && {homeLatest: true}), ...(r.postsPage && {postsPage: true}), ...(r.postsIndex && {postsIndex: true})
|
|
187
191
|
})),
|
|
188
|
-
redirects: plan.redirects.map(r => ({from: r.from, to: r.to, action: r.action, reason: r.reason || ''}))
|
|
192
|
+
redirects: plan.redirects.map(r => ({from: r.from, to: r.to, action: r.action, reason: r.reason || '', ...(r.old && {old: true, code: r.code || 301}), ...(r.unread && {unread: true})}))
|
|
189
193
|
};
|
|
190
194
|
}
|
|
191
195
|
|
|
@@ -209,7 +213,7 @@ export async function wordpressImportRoutes(fastify, opts = {}) {
|
|
|
209
213
|
blog: {available: blog.available, basePath: blog.basePath, comments: Boolean(blog.comments), reason: blog.reason || ''},
|
|
210
214
|
redirects: Boolean(seoEngine()),
|
|
211
215
|
people: await peopleContext(request.user),
|
|
212
|
-
limits: {wxr: WXR_MAX_BYTES, uploads: UPLOADS_ZIP_MAX_BYTES, widgets: WIE_MAX_BYTES}
|
|
216
|
+
limits: {wxr: WXR_MAX_BYTES, uploads: UPLOADS_ZIP_MAX_BYTES, widgets: WIE_MAX_BYTES, redirects: REDIRECT_LIST_MAX_BYTES}
|
|
213
217
|
};
|
|
214
218
|
});
|
|
215
219
|
|
|
@@ -345,6 +349,59 @@ export async function wordpressImportRoutes(fastify, opts = {}) {
|
|
|
345
349
|
return updateStagingMeta(id, {widgets: null});
|
|
346
350
|
});
|
|
347
351
|
|
|
352
|
+
fastify.post('/staged/:id/redirects', {...may, bodyLimit: REDIRECT_LIST_MAX_BYTES + 65536}, async (request, reply) => {
|
|
353
|
+
const {id} = request.params;
|
|
354
|
+
const meta = isId(id) ? await stagingMeta(id) : null;
|
|
355
|
+
if (!meta) return reply.code(404).send({error: 'That upload has gone - upload the export again.'});
|
|
356
|
+
if (runningJob()?.staged === id) return reply.code(409).send({error: 'That import is running.'});
|
|
357
|
+
const dest = stagingPath(id, 'redirects.txt');
|
|
358
|
+
const tooBig = () => reply.code(413).send({error: `The list is over ${mb(REDIRECT_LIST_MAX_BYTES)} - that is far bigger than any real one.`});
|
|
359
|
+
try {
|
|
360
|
+
let name = 'Pasted list';
|
|
361
|
+
let size = 0;
|
|
362
|
+
let text;
|
|
363
|
+
if (request.isMultipart?.()) {
|
|
364
|
+
const file = await request.file({limits: {fileSize: REDIRECT_LIST_MAX_BYTES + 1, files: 1}}).catch(() => null);
|
|
365
|
+
if (!file) return reply.code(400).send({error: 'Choose the redirects file.'});
|
|
366
|
+
name = String(file.filename || '');
|
|
367
|
+
if (!/(\.(csv|tsv|txt|json|conf)|htaccess)$/i.test(name)) {
|
|
368
|
+
file.file.resume();
|
|
369
|
+
return reply.code(400).send({error: 'Upload the redirects as a .csv, .txt, .json or .htaccess file - or paste the list.'});
|
|
370
|
+
}
|
|
371
|
+
const tmp = `${dest}.new`;
|
|
372
|
+
try {
|
|
373
|
+
size = await saveUpload(file.file, tmp, REDIRECT_LIST_MAX_BYTES);
|
|
374
|
+
text = await fs.promises.readFile(tmp, 'utf8');
|
|
375
|
+
} finally {
|
|
376
|
+
fs.rmSync(tmp, {force: true});
|
|
377
|
+
}
|
|
378
|
+
} else {
|
|
379
|
+
text = typeof request.body?.text === 'string' ? request.body.text : '';
|
|
380
|
+
size = Buffer.byteLength(text);
|
|
381
|
+
if (size > REDIRECT_LIST_MAX_BYTES) return tooBig();
|
|
382
|
+
}
|
|
383
|
+
if (text.includes('\u0000')) return reply.code(400).send({error: 'That is not a text file.'});
|
|
384
|
+
const read = parseRedirectList(text);
|
|
385
|
+
if (!read.rows.length) {
|
|
386
|
+
const why = read.skipped[0]?.reason;
|
|
387
|
+
return reply.code(400).send({error: `No redirects could be read from that${why ? ` - the first line it stopped at: ${why}` : '. Each line needs the old address and the new one: /old-page -> /new-page 301, or a sheet with the two in columns.'}`});
|
|
388
|
+
}
|
|
389
|
+
await fs.promises.writeFile(dest, text, {mode: 0o600});
|
|
390
|
+
return await updateStagingMeta(id, {redirects: redirectsMeta(name, size, read)});
|
|
391
|
+
} catch (err) {
|
|
392
|
+
if (err.code === 'ETOOBIG') return tooBig();
|
|
393
|
+
return fail(reply, err);
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
fastify.delete('/staged/:id/redirects', may, async (request, reply) => {
|
|
398
|
+
const {id} = request.params;
|
|
399
|
+
if (!isId(id) || !(await stagingMeta(id))) return reply.code(404).send({error: 'That upload has gone.'});
|
|
400
|
+
if (runningJob()?.staged === id) return reply.code(409).send({error: 'That import is running.'});
|
|
401
|
+
fs.rmSync(stagingPath(id, 'redirects.txt'), {force: true});
|
|
402
|
+
return updateStagingMeta(id, {redirects: null});
|
|
403
|
+
});
|
|
404
|
+
|
|
348
405
|
fastify.post('/staged/:id/preview', may, async (request, reply) => {
|
|
349
406
|
const {id} = request.params;
|
|
350
407
|
if (!isId(id)) return reply.code(404).send({error: 'That upload has gone.'});
|
|
@@ -453,6 +453,7 @@ export async function crawlSite(raw, opts = {}) {
|
|
|
453
453
|
|
|
454
454
|
const read = new Map();
|
|
455
455
|
read.set([...pages.keys()][0], homePage);
|
|
456
|
+
const moves = [];
|
|
456
457
|
const failed = [];
|
|
457
458
|
const queue = [...pages.keys()].slice(1);
|
|
458
459
|
let capped = false;
|
|
@@ -470,6 +471,8 @@ export async function crawlSite(raw, opts = {}) {
|
|
|
470
471
|
if (!res.ok) throw new Error(`answered ${res.status}`);
|
|
471
472
|
if (!/html/i.test(String(res.headers['content-type'] || 'text/html'))) throw new Error('not a web page');
|
|
472
473
|
const finalUrl = pageAddress(res.url, res.url, site) || url;
|
|
474
|
+
// The old site sent this address on to another of its own: one of its redirects.
|
|
475
|
+
if (finalUrl !== url && moves.length < 500) moves.push({from: url, to: finalUrl});
|
|
473
476
|
if (read.has(finalUrl)) continue;
|
|
474
477
|
const page = readPage(res.text(), finalUrl, site, pages.get(url) || {});
|
|
475
478
|
read.set(finalUrl, page);
|
|
@@ -489,7 +492,7 @@ export async function crawlSite(raw, opts = {}) {
|
|
|
489
492
|
|
|
490
493
|
progress.phase = 'Writing the export';
|
|
491
494
|
tell();
|
|
492
|
-
const xml = toWxr({site, homeUrl, homePage, pages: [...read.values()], builder});
|
|
495
|
+
const xml = toWxr({site, homeUrl, homePage, pages: [...read.values()], builder, moves});
|
|
493
496
|
const all = [...read.values()];
|
|
494
497
|
return {
|
|
495
498
|
xml,
|
|
@@ -497,7 +500,7 @@ export async function crawlSite(raw, opts = {}) {
|
|
|
497
500
|
url: homeUrl, builder: builder || 'website', siteName: homePage.siteName || homePage.title,
|
|
498
501
|
pages: all.filter(p => p.kind === 'page').length, posts: all.filter(p => p.kind === 'post').length,
|
|
499
502
|
files: new Set(all.flatMap(p => p.files)).size, menu: homePage.nav?.length || 0,
|
|
500
|
-
sitemap: usedSitemap, capped, failed: failed.slice(0, 100), failedCount: failed.length,
|
|
503
|
+
redirects: moves.length, sitemap: usedSitemap, capped, failed: failed.slice(0, 100), failedCount: failed.length,
|
|
501
504
|
thin: all.filter(p => p.html.replace(/<[^>]+>/g, '').trim().length < 40).map(p => p.url).slice(0, 100)
|
|
502
505
|
}
|
|
503
506
|
};
|
|
@@ -522,7 +525,7 @@ const slugOf = (url, site) => {
|
|
|
522
525
|
};
|
|
523
526
|
|
|
524
527
|
/** The pages read, as a WXR 1.2 document ./wxr.js reads like any other. */
|
|
525
|
-
export function toWxr({site, homeUrl, homePage, pages, builder}) {
|
|
528
|
+
export function toWxr({site, homeUrl, homePage, pages, builder, moves = []}) {
|
|
526
529
|
const now = wpDate(new Date().toISOString());
|
|
527
530
|
const items = [];
|
|
528
531
|
const ids = new Set();
|
|
@@ -556,6 +559,12 @@ export function toWxr({site, homeUrl, homePage, pages, builder}) {
|
|
|
556
559
|
const name = decodeURIComponent(new URL(url).pathname.split('/').pop() || 'file');
|
|
557
560
|
items.push(itemXml({id: idFor(`file:${url}`), type: 'attachment', title: f.alt || name.replace(/\.[a-z0-9]+$/i, ''), metas: f.alt ? {_wp_attachment_image_alt: f.alt} : {}, slug: '', link: url, date: f.date, creator: '', parent: f.parent, attachmentUrl: url, status: 'inherit'}));
|
|
558
561
|
}
|
|
562
|
+
// The redirects met on the way, kept the way ./redirects.js reads them from an export (as redirect_rule items).
|
|
563
|
+
for (const m of moves) {
|
|
564
|
+
const from = new URL(m.from).pathname;
|
|
565
|
+
items.push(itemXml({id: idFor(`redirect:${from}`), type: 'redirect_rule', title: from, slug: '', link: '', date: now, creator: '',
|
|
566
|
+
metas: {_redirect_rule_from: from, _redirect_rule_to: m.to, _redirect_rule_status_code: '301'}}));
|
|
567
|
+
}
|
|
559
568
|
// The menu.
|
|
560
569
|
let menuXml = '';
|
|
561
570
|
if (homePage.nav?.length) {
|
|
@@ -38,6 +38,7 @@ export const INTERNAL_TYPES = [
|
|
|
38
38
|
{match: 'oembed_cache', reason: 'WordPress\'s cache of embedded videos and posts.'},
|
|
39
39
|
{match: 'user_request', reason: 'Privacy requests - they hold people\'s personal details.'},
|
|
40
40
|
{match: /^acf-(field-group|field|post-type|taxonomy|ui-options-page)$/, reason: 'Advanced Custom Fields settings - the field values come across with the content that uses them.'},
|
|
41
|
+
{match: 'redirect_rule', reason: 'The old site\'s own redirects - they come across as redirects, in SEO › Redirects.'},
|
|
41
42
|
{match: 'wpcf7_contact_form', reason: 'Contact Form 7 forms - rebuild them with Forms.'},
|
|
42
43
|
{match: /^(wpforms|wpforms_log|nf_sub|frm_form_actions|frm_styles|mc4wp-form|flamingo_\w+|feedback)$/, reason: 'A forms plugin\'s forms or submissions - submissions hold people\'s details; rebuild forms with Forms.'},
|
|
43
44
|
{match: /^(elementor_library|elementor_\w+|e-landing-page|et_pb_layout|fl-builder-template|vc_grid_item|templatera|ct_template|oxy_user_library|brizy_template)$/, reason: 'A page builder\'s saved templates - layout, not content.'},
|
|
@@ -88,6 +88,7 @@ import {getConfig, saveConfig} from '../../config.js';
|
|
|
88
88
|
import * as cache from '../cache/index.js';
|
|
89
89
|
import {getSearchSettings} from '../search.js';
|
|
90
90
|
import {readWie, widgetAreaHtml, WIE_MAX_BYTES} from './widgets.js';
|
|
91
|
+
import {parseRedirectList, REDIRECT_LIST_MAX_BYTES} from './redirects.js';
|
|
91
92
|
import {FOOTER_BLOCKS_MAX} from '../renderer.js';
|
|
92
93
|
import {allowInGitignore} from '../siteGitignore.js';
|
|
93
94
|
import fsSync from 'node:fs';
|
|
@@ -95,7 +96,7 @@ import {answeredElsewhere} from '../hostClaims.js';
|
|
|
95
96
|
|
|
96
97
|
export const WXR_MAX_BYTES = 50 * 1024 * 1024;
|
|
97
98
|
export const UPLOADS_ZIP_MAX_BYTES = 1024 * 1024 * 1024;
|
|
98
|
-
export {WIE_MAX_BYTES};
|
|
99
|
+
export {WIE_MAX_BYTES, REDIRECT_LIST_MAX_BYTES};
|
|
99
100
|
const DOWNLOAD_CONCURRENCY = 4;
|
|
100
101
|
|
|
101
102
|
const BLOG = {posts: 'blog-posts', categories: 'blog-categories', comments: 'blog-comments'};
|
|
@@ -387,6 +388,23 @@ export async function loadWidgets(id) {
|
|
|
387
388
|
return readWie(await fs.readFile(stagingPath(id, 'widgets.wie')));
|
|
388
389
|
}
|
|
389
390
|
|
|
391
|
+
/** What the staging meta says about a redirects list. */
|
|
392
|
+
export function redirectsMeta(fileName, size, read) {
|
|
393
|
+
return {
|
|
394
|
+
fileName: String(fileName || '').slice(0, 200).replace(/[^\w.() -]/g, '_'), size,
|
|
395
|
+
format: read.format, count: read.rows.length, unread: read.skipped.length, more: read.more || 0
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** The staged redirects list (the old site's own redirects), read, or null when there is none. */
|
|
400
|
+
export async function loadRedirectList(id) {
|
|
401
|
+
const meta = await stagingMeta(id);
|
|
402
|
+
if (!meta?.redirects) return null;
|
|
403
|
+
try {
|
|
404
|
+
return parseRedirectList(await fs.readFile(stagingPath(id, 'redirects.txt'), 'utf8'));
|
|
405
|
+
} catch { return null; }
|
|
406
|
+
}
|
|
407
|
+
|
|
390
408
|
export async function discardStaged(id) {
|
|
391
409
|
parsed.delete(id);
|
|
392
410
|
await dropStaging(id);
|
|
@@ -435,8 +453,9 @@ export async function previewImport(id, options = {}) {
|
|
|
435
453
|
const state = await siteState(mapping, sourceKey(wxr.site));
|
|
436
454
|
const zipPaths = options.media === 'zip' ? await uploadsIndex(id) : null;
|
|
437
455
|
const wie = await loadWidgets(id);
|
|
456
|
+
const oldRedirects = await loadRedirectList(id);
|
|
438
457
|
// `people` (what the person may do with users) comes from the route, never from the request body.
|
|
439
|
-
const plan = planImport(wxr, state, mapping, {...cleanOptions(options), people: options.people || null, zipPaths, wie, anyPort: testHostsAllowed()});
|
|
458
|
+
const plan = planImport(wxr, state, mapping, {...cleanOptions(options), people: options.people || null, zipPaths, wie, oldRedirects, anyPort: testHostsAllowed()});
|
|
440
459
|
plan.notes.unshift(...platformNotes(wxr, meta));
|
|
441
460
|
return {plan, state, wxr, mapping, meta, wie};
|
|
442
461
|
}
|
|
@@ -1126,8 +1145,9 @@ async function runPhases(job, {plan, wxr, oldMapping, meta, inviteOrigin, wie},
|
|
|
1126
1145
|
const own = mapping.redirects[r.key];
|
|
1127
1146
|
const rule = own && seo.store.rules().find(x => x.id === own.id);
|
|
1128
1147
|
let out;
|
|
1129
|
-
|
|
1130
|
-
|
|
1148
|
+
const code = r.old && r.code ? r.code : 301;
|
|
1149
|
+
if (rule) out = seo.updateRule(rule.id, {from: r.from, to: r.to, code});
|
|
1150
|
+
else out = seo.addRule({from: r.from, to: r.to, code, note: r.old ? `${sourceName(wxr, true)} import - a redirect the old site had` : `${sourceName(wxr, true)} import`});
|
|
1131
1151
|
if (out.errors) throw new Error(Object.values(out.errors).join(' '));
|
|
1132
1152
|
mapping.redirects[r.key] = {id: out.rule.id, from: out.rule.from, to: out.rule.to};
|
|
1133
1153
|
redirectResults.push({...r, result: rule ? 'updated' : 'created'});
|
|
@@ -48,6 +48,8 @@ import {planPeople} from './people.js';
|
|
|
48
48
|
import {planWidgets} from './widgets.js';
|
|
49
49
|
import {platformFilePath} from './platforms.js';
|
|
50
50
|
import {detectFrontPage, suggestPostsPage} from './front.js';
|
|
51
|
+
import {exportRedirects, resolveOldRedirects} from './redirects.js';
|
|
52
|
+
import {checkRule} from '../../../admin/js/lib/seo-redirects.js';
|
|
51
53
|
|
|
52
54
|
/** Addresses under the Shop's that are its own pages, never a product's. */
|
|
53
55
|
const SHOP_RESERVED = ['cart', 'checkout', 'basket', 'account', 'order'];
|
|
@@ -221,6 +223,8 @@ export function isoDate(gmt, local) {
|
|
|
221
223
|
* @param {boolean} [options.comments=true]
|
|
222
224
|
* @param {boolean} [options.menus=true]
|
|
223
225
|
* @param {boolean} [options.redirects=true]
|
|
226
|
+
* @param {{rows: object[], skipped: object[], more?: number}|null} [options.oldRedirects] - the old site's own redirects, from a
|
|
227
|
+
* list the person gave (./redirects.js parseRedirectList); the ones the export itself holds are read here
|
|
224
228
|
* @param {boolean} [options.drafts=true] - import drafts, pending and private items (as drafts)
|
|
225
229
|
* @param {boolean} [options.products=true] - WooCommerce products to the Shop (when one is on)
|
|
226
230
|
* @param {number} [options.now] - the time sales are judged at (tests)
|
|
@@ -246,7 +250,7 @@ export function planImport(wxr, site, mapping = {}, options = {}) {
|
|
|
246
250
|
const opt = {
|
|
247
251
|
posts: site.blog?.available ? 'blog' : 'pages',
|
|
248
252
|
media: 'download', comments: true, menus: true, redirects: true, drafts: true, products: true, choices: {}, zipPaths: null,
|
|
249
|
-
types: {}, listPages: {}, users: {}, userRole: '', invite: false, footer: {}, wie: null,
|
|
253
|
+
types: {}, listPages: {}, users: {}, userRole: '', invite: false, footer: {}, wie: null, oldRedirects: null,
|
|
250
254
|
home: true, postsPage: 0, latestPosts: false, homeReplace: null,
|
|
251
255
|
...options
|
|
252
256
|
};
|
|
@@ -747,6 +751,42 @@ export function planImport(wxr, site, mapping = {}, options = {}) {
|
|
|
747
751
|
}
|
|
748
752
|
if (posts.length) redirects.push({from: `${base}/feed`, to: `${blogBase}/feed.xml`, key: 'feed'});
|
|
749
753
|
}
|
|
754
|
+
// ---- The old site's own redirects (./redirects.js) ----
|
|
755
|
+
// They come after the import's own, so an address the import answers itself keeps that answer.
|
|
756
|
+
const oldSkipped = [];
|
|
757
|
+
let oldFound = 0;
|
|
758
|
+
{
|
|
759
|
+
// Where the import puts each old address: a redirect that led there leads straight on (no chains).
|
|
760
|
+
const moved = new Map();
|
|
761
|
+
for (const r of redirects) {
|
|
762
|
+
if (!r.to || r.from.endsWith('/*')) continue;
|
|
763
|
+
const f = normalisePath(r.from);
|
|
764
|
+
if (!moved.has(f)) moved.set(f, r.to);
|
|
765
|
+
}
|
|
766
|
+
// WordPress's: an item renamed there still answered at each address it had before.
|
|
767
|
+
for (const r of items) {
|
|
768
|
+
const to = r.kind === 'custom' && r.as === 'collection' && r.action !== 'skip' ? r.listAt : live(r) ? r.target : '';
|
|
769
|
+
if (!to || !r.oldPath || !['page', 'post', 'custom', 'product'].includes(r.kind) || r.listFor || r.postsPage) continue;
|
|
770
|
+
const it = byId.get(r.wpId);
|
|
771
|
+
const at = r.oldPath.lastIndexOf('/');
|
|
772
|
+
if (!it?.oldSlugs?.length || r.oldPath.slice(at + 1) !== String(it.slug || '').toLowerCase()) continue;
|
|
773
|
+
for (const was of it.oldSlugs) {
|
|
774
|
+
const s = slugify(was);
|
|
775
|
+
if (!s || s === r.oldPath.slice(at + 1)) continue;
|
|
776
|
+
oldFound++;
|
|
777
|
+
redirects.push({from: `${r.oldPath.slice(0, at)}/${s}`, to, key: `${r.key}/was:${s}`, old: true});
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
// The ones its owner set up: a list given with the import first, then any the export holds.
|
|
781
|
+
const inExport = exportRedirects(wxr);
|
|
782
|
+
const given = [...(opt.oldRedirects?.rows || []), ...inExport.rows];
|
|
783
|
+
const fitted = resolveOldRedirects(given, {hosts: wxr.site.hosts || [], moved, normalisePath});
|
|
784
|
+
oldFound += given.length + (opt.oldRedirects?.skipped?.length || 0) + inExport.skipped.length;
|
|
785
|
+
redirects.push(...fitted.redirects);
|
|
786
|
+
oldSkipped.push(...(opt.oldRedirects?.skipped || []), ...inExport.skipped, ...fitted.skipped);
|
|
787
|
+
if (inExport.more) notes.push(`The export holds ${inExport.more} more redirects than the ${inExport.rows.length} taken in one import - add the rest in SEO › Redirects.`);
|
|
788
|
+
if (opt.oldRedirects?.more) notes.push(`The redirects list has ${opt.oldRedirects.more} more than the ${given.length - inExport.rows.length} taken in one import - add the rest in SEO › Redirects.`);
|
|
789
|
+
}
|
|
750
790
|
const liveTargets = new Set(items.filter(live).map(x => x.target));
|
|
751
791
|
// An address the Shop answers itself (one of its products, its cart…) is never redirected away.
|
|
752
792
|
const shopSlugs = new Set([...(shop.productSlugs || []), ...SHOP_RESERVED]);
|
|
@@ -761,25 +801,61 @@ export function planImport(wxr, site, mapping = {}, options = {}) {
|
|
|
761
801
|
const redirectPlan = [];
|
|
762
802
|
const ownRedirects = new Set(Object.values(mapping.redirects || {}).map(r => r.from));
|
|
763
803
|
const seen = new Set();
|
|
804
|
+
// A "/old/*" rule answers before any page does: one over an address in use here would hide it.
|
|
805
|
+
const inUse = [...site.pagePaths, ...liveTargets, blogBase, ...(shop.available ? [shopBase] : [])];
|
|
806
|
+
const hides = (from) => {
|
|
807
|
+
const prefix = from.slice(0, -2);
|
|
808
|
+
return inUse.some(p => typeof p === 'string' && (p === prefix || p.startsWith(`${prefix}/`)));
|
|
809
|
+
};
|
|
810
|
+
// What this site answers itself whatever pages it has: the Blog's addresses (its list, posts, categories,
|
|
811
|
+
// tags, feed), anything under the Shop, the sitemap and robots.txt. None of the old site's redirects goes over one.
|
|
812
|
+
const under = (from, basePath) => {
|
|
813
|
+
const f = from.endsWith('/*') ? from.slice(0, -2) : from;
|
|
814
|
+
return basePath !== '/' && (f === basePath || f.startsWith(`${basePath}/`));
|
|
815
|
+
};
|
|
816
|
+
const siteOwns = (from) => {
|
|
817
|
+
if (site.blog?.available && under(from, blogBase)) {
|
|
818
|
+
// One plain address under the Blog that no post has (a post's earlier name, say) is free to redirect.
|
|
819
|
+
const rest = from.endsWith('/*') || from === blogBase ? '' : from.slice(blogBase.length + 1);
|
|
820
|
+
if (!rest || rest.includes('/') || rest.includes('.') || site.blog.postSlugs?.has(rest)) return 'The Blog answers at that address - it is not redirected.';
|
|
821
|
+
}
|
|
822
|
+
if (shop.available && from.endsWith('/*') && under(from, shopBase)) return 'The Shop answers under that address - it is not redirected.';
|
|
823
|
+
if (from === '/sitemap.xml' || from === '/robots.txt') return 'This site answers that address itself.';
|
|
824
|
+
return '';
|
|
825
|
+
};
|
|
764
826
|
for (const r of redirects) {
|
|
765
827
|
const from = r.from.endsWith('/*') ? r.from : normalisePath(r.from);
|
|
766
828
|
const to = r.to;
|
|
829
|
+
const code = r.old && r.code ? r.code : 301;
|
|
767
830
|
if (seen.has(from)) continue;
|
|
768
831
|
seen.add(from);
|
|
769
832
|
let action = 'create';
|
|
770
833
|
let reason = '';
|
|
834
|
+
let wrong = '';
|
|
771
835
|
if (!opt.redirects) { action = 'skip'; reason = 'Redirects are not being added.'; }
|
|
772
|
-
else if (!
|
|
836
|
+
else if (r.old && !site.redirectsAvailable) { action = 'skip'; reason = 'SEO is switched off on this site, so redirects cannot be added.'; }
|
|
837
|
+
else if (r.old && (wrong = Object.values(checkRule({from, to, code}, []).errors)[0])) { action = 'skip'; reason = wrong; }
|
|
838
|
+
else if (r.old && (wrong = siteOwns(from))) { action = 'skip'; reason = wrong; }
|
|
839
|
+
else if (r.old && from.endsWith('/*') && hides(from)) { action = 'skip'; reason = 'Pages on this site sit under that address - it would hide them.'; }
|
|
840
|
+
else if (!to && code !== 410) { action = 'skip'; reason = 'Its type has no list page to lead to - switch the list page on to give these old addresses a home.'; }
|
|
773
841
|
else if (!site.redirectsAvailable) { action = 'skip'; reason = 'SEO is switched off on this site, so redirects cannot be added.'; }
|
|
774
|
-
else if (from === normalisePath(to)) { action = 'skip'; reason = 'Same address.'; }
|
|
775
842
|
else if (from === '/' || from === '/*') { action = 'skip'; reason = 'That would redirect the home page.'; }
|
|
843
|
+
else if (code !== 410 && from === normalisePath(to)) { action = 'skip'; reason = 'Same address.'; }
|
|
776
844
|
else if (liveTargets.has(from)) { action = 'skip'; reason = 'An imported item takes that address.'; }
|
|
777
845
|
else if (site.pagePaths.has(from) && !ownPaths.has(from)) { action = 'skip'; reason = 'A page on this site has that address - it is not redirected.'; }
|
|
778
846
|
else if (shopOwns(from)) { action = 'skip'; reason = 'The Shop has a page at that address - it is not redirected.'; }
|
|
779
847
|
else if (site.redirectFroms.has(from) && !ownRedirects.has(from)) { action = 'skip'; reason = 'This site already redirects it.'; }
|
|
780
848
|
else if (ownRedirects.has(from)) action = 'update';
|
|
781
|
-
redirectPlan.push({from, to, key: r.key, action, reason});
|
|
849
|
+
redirectPlan.push({from, to, key: r.key, action, reason, ...(r.old && {old: true, code})});
|
|
850
|
+
}
|
|
851
|
+
// What could not be read as a redirect this site can keep - listed, never guessed at.
|
|
852
|
+
for (const x of oldSkipped) {
|
|
853
|
+
const from = String(x.text || '');
|
|
854
|
+
if (!from || seen.has(from)) continue;
|
|
855
|
+
seen.add(from);
|
|
856
|
+
redirectPlan.push({from, to: '', key: '', action: 'skip', reason: x.reason, old: true, unread: true});
|
|
782
857
|
}
|
|
858
|
+
const oldPlanned = redirectPlan.filter(r => r.old && r.action !== 'skip').length;
|
|
783
859
|
|
|
784
860
|
for (const r of items) if (r.builder) notes.push(`"${r.title}" was built with ${r.builder} - its layout will need rebuilding.`);
|
|
785
861
|
const productRows = items.filter(r => r.kind === 'product');
|
|
@@ -791,6 +867,7 @@ export function planImport(wxr, site, mapping = {}, options = {}) {
|
|
|
791
867
|
counts,
|
|
792
868
|
comments,
|
|
793
869
|
redirects: redirectPlan.filter(r => r.action !== 'skip').length,
|
|
870
|
+
oldRedirects: {found: oldFound, planned: oldPlanned},
|
|
794
871
|
shortcodes: shortcodeCount,
|
|
795
872
|
builders: items.filter(r => r.builder).length,
|
|
796
873
|
customTypes,
|
|
@@ -812,7 +889,7 @@ export function planImport(wxr, site, mapping = {}, options = {}) {
|
|
|
812
889
|
return {
|
|
813
890
|
source: sourceKey(wxr.site),
|
|
814
891
|
site: {title: wxr.site.title, url: wxr.site.baseSiteUrl || wxr.site.link, wxrVersion: wxr.site.wxrVersion, hosts: wxr.site.hosts, platform: wxr.site.platform || 'wordpress', platformName: wxr.site.platformName || 'WordPress'},
|
|
815
|
-
options: {...opt, zipPaths: undefined, anyPort: undefined, people: undefined, inviteOrigin: undefined, wie: undefined, invite: people.invite, choices},
|
|
892
|
+
options: {...opt, zipPaths: undefined, anyPort: undefined, people: undefined, inviteOrigin: undefined, wie: undefined, oldRedirects: undefined, invite: people.invite, choices},
|
|
816
893
|
blogBase,
|
|
817
894
|
shopBase,
|
|
818
895
|
items,
|