domma-cms 1.69.2 → 1.71.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 +16 -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/plugins/feedback/CHANGELOG.md +5 -0
- package/plugins/feedback/CLAUDE.md +26 -0
- package/plugins/feedback/admin/css/index.css +11 -0
- package/plugins/feedback/admin/lib/ai.js +41 -0
- package/plugins/feedback/admin/lib/kit.js +3 -1
- package/plugins/feedback/admin/views/feedback.js +1 -1
- package/plugins/feedback/admin/views/inbox.js +58 -2
- package/plugins/feedback/docs/guide.md +2 -0
- package/plugins/feedback/lib/ai-text.js +217 -0
- package/plugins/feedback/lib/receiver.js +10 -0
- package/plugins/feedback/lib/themes.js +75 -0
- package/plugins/feedback/plugin.js +39 -2
- package/plugins/feedback/plugin.json +2 -2
- package/plugins/feedback/tests/ai.test.js +248 -0
- package/plugins/free-tier.lock.json +31 -21
- package/plugins/support/CHANGELOG.md +5 -0
- package/plugins/support/CLAUDE.md +33 -0
- package/plugins/support/admin/css/index.css +14 -0
- package/plugins/support/admin/lib/ai.js +68 -0
- package/plugins/support/admin/lib/kit.js +5 -1
- package/plugins/support/admin/views/inbox.js +121 -4
- package/plugins/support/admin/views/support.js +1 -1
- package/plugins/support/docs/guide.md +5 -0
- package/plugins/support/lib/ai-text.js +217 -0
- package/plugins/support/lib/assist.js +74 -0
- package/plugins/support/lib/receiver.js +2 -1
- package/plugins/support/plugin.js +73 -3
- package/plugins/support/plugin.json +2 -2
- package/plugins/support/tests/ai.test.js +245 -0
- 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
|
@@ -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,
|
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The old site's own redirects - the ones its owner set up there (a moved
|
|
3
|
+
* page, a renamed product, a campaign address), which no export of the
|
|
4
|
+
* content carries as content. Two sources, both read here; no I/O.
|
|
5
|
+
*
|
|
6
|
+
* exportRedirects(wxr) what a WordPress export does hold: the "Safe
|
|
7
|
+
* Redirect Manager" plugin keeps each redirect as a
|
|
8
|
+
* post (redirect_rule), so they are in the file.
|
|
9
|
+
* (WordPress's own old-slug redirects are read by
|
|
10
|
+
* the planner from each item's _wp_old_slug.)
|
|
11
|
+
* parseRedirectList(text) a list the person gives: pasted or uploaded.
|
|
12
|
+
* - Squarespace's URL mappings /old -> /new 301
|
|
13
|
+
* - a CSV or tab-separated sheet (the Redirection,
|
|
14
|
+
* Yoast and Rank Math plugins' exports, Wix's
|
|
15
|
+
* and Shopify's, or any "old, new[, code]" sheet)
|
|
16
|
+
* - the Redirection plugin's JSON export
|
|
17
|
+
* - Apache lines (.htaccess): Redirect,
|
|
18
|
+
* RedirectMatch, RedirectPermanent, RedirectTemp,
|
|
19
|
+
* RewriteRule with an R flag
|
|
20
|
+
*
|
|
21
|
+
* Both give rows {from, to, code, line?} in the shape SEO › Redirects keeps
|
|
22
|
+
* (admin/js/lib/seo-redirects.js): `from` a path, or a prefix ending "/*";
|
|
23
|
+
* `to` a path or an http(s) address, "*" taking what the prefix matched; 410
|
|
24
|
+
* has no `to`. A rule that shape cannot say (a pattern, a rule that depends on
|
|
25
|
+
* the visitor) is not guessed at: it is listed in `skipped` with the reason.
|
|
26
|
+
*
|
|
27
|
+
* resolveOldRedirects() then fits the rows to this import: an address on the
|
|
28
|
+
* old site's own domain becomes a path, and a redirect that led to an address
|
|
29
|
+
* the import is moving leads straight to where that went (no chains).
|
|
30
|
+
*
|
|
31
|
+
* @module services/wpImport/redirects
|
|
32
|
+
*/
|
|
33
|
+
import {CODES} from '../../../admin/js/lib/seo-redirects.js';
|
|
34
|
+
|
|
35
|
+
export const REDIRECT_LIST_MAX_BYTES = 2 * 1024 * 1024;
|
|
36
|
+
/** The most redirects taken from the old site in one import (SEO › Redirects holds 5,000 in all). */
|
|
37
|
+
export const OLD_REDIRECTS_MAX = 2000;
|
|
38
|
+
|
|
39
|
+
/** No real address is longer; a longer one is not read at all (and never reaches a pattern). */
|
|
40
|
+
const ADDRESS_MAX = 2048;
|
|
41
|
+
const SKIP_PATTERN = 'It uses a pattern this site\'s redirects cannot express - add it by hand in SEO › Redirects if it still matters.';
|
|
42
|
+
const clip = (s, n = 160) => { const t = String(s ?? '').replace(/\s+/g, ' ').trim(); return t.length > n ? `${t.slice(0, n - 1)}…` : t; };
|
|
43
|
+
const isAbsolute = (s) => /^https?:\/\//i.test(String(s));
|
|
44
|
+
|
|
45
|
+
/** 301 from "301", "permanent", "moved permanently"…; 0 when it says nothing usable. */
|
|
46
|
+
function codeOf(v, fallback = 301) {
|
|
47
|
+
const s = String(v ?? '').trim().toLowerCase();
|
|
48
|
+
if (!s) return fallback;
|
|
49
|
+
const n = Number.parseInt(s, 10);
|
|
50
|
+
if (CODES.includes(n)) return n;
|
|
51
|
+
if (n === 303) return 302; // "see other": the nearest this site keeps
|
|
52
|
+
if (Number.isFinite(n) && n >= 100) return 0;
|
|
53
|
+
if (/^(permanent|moved permanently|perm)$/.test(s)) return 301;
|
|
54
|
+
if (/^(temp|temporary|found)$/.test(s)) return 302;
|
|
55
|
+
if (/^(gone|deleted)$/.test(s)) return 410;
|
|
56
|
+
if (s === 'seeother') return 302;
|
|
57
|
+
return fallback;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A regular expression as a redirect this site can keep, or null.
|
|
62
|
+
* ^/old/?$ -> /old
|
|
63
|
+
* ^/old/(.*)$ + /new/$1 -> /old/* + /new/*
|
|
64
|
+
* Only a plain address, or a plain prefix with one catch-all at the end.
|
|
65
|
+
*/
|
|
66
|
+
export function fromPattern(pattern, to = '') {
|
|
67
|
+
let p = String(pattern ?? '').trim();
|
|
68
|
+
if (!p.startsWith('^')) return null; // unanchored: "contains", which a path rule cannot say
|
|
69
|
+
p = p.slice(1);
|
|
70
|
+
if (p.endsWith('$')) p = p.slice(0, -1);
|
|
71
|
+
let wildcard = false;
|
|
72
|
+
const tail = /(\(\.[*+]\)|\.\*)$/.exec(p);
|
|
73
|
+
if (tail) { wildcard = true; p = p.slice(0, -tail[0].length); }
|
|
74
|
+
p = p.replace(/\/\?$/, wildcard ? '/' : '');
|
|
75
|
+
// What is left must be literal. "\." and "\/" are; so, in practice, is a bare "." (page.html).
|
|
76
|
+
const literal = p.replace(/\\([.\/\-_~%])/g, '$1');
|
|
77
|
+
if (/[\\^$*+?()[\]{}|]/.test(literal)) return null;
|
|
78
|
+
if (!literal.startsWith('/')) return null;
|
|
79
|
+
let target = String(to ?? '').trim();
|
|
80
|
+
if (wildcard) {
|
|
81
|
+
if (!literal.endsWith('/')) return null; // "/old(.*)" also takes /older - not a prefix of whole segments
|
|
82
|
+
if (/\$[2-9]/.test(target) || (target.match(/\$1/g) || []).length > 1) return null;
|
|
83
|
+
target = target.replace('$1', '*');
|
|
84
|
+
if (/\$\d/.test(target)) return null;
|
|
85
|
+
return {from: `${literal}*`, to: target};
|
|
86
|
+
}
|
|
87
|
+
if (/\$\d/.test(target)) return null;
|
|
88
|
+
return {from: literal, to: target};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Squarespace's "[name]" variables: /blog/[name] -> /news/[name] is /blog/* -> /news/*. */
|
|
92
|
+
function squarespaceVars(from, to) {
|
|
93
|
+
if (!from.includes('[') && !to.includes('[')) return {from, to};
|
|
94
|
+
const vars = from.match(/\[[^\]/]+\]/g) || [];
|
|
95
|
+
if (!vars.length && !/\[[^\]/]+\]/.test(to)) return {from, to};
|
|
96
|
+
if (vars.length !== 1 || !from.endsWith(`/${vars[0]}`)) return null;
|
|
97
|
+
const rest = to.split(vars[0]);
|
|
98
|
+
if (rest.length > 2 || /\[[^\]/]+\]/.test(rest.join(''))) return null;
|
|
99
|
+
return {from: `${from.slice(0, -vars[0].length)}*`, to: rest.join('*')};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** One row checked into shape, or the reason it is left out. */
|
|
103
|
+
function shape({from, to, code = 301, regex = false, line = 0, text = ''}) {
|
|
104
|
+
const src = clip(text || `${from} -> ${to}`);
|
|
105
|
+
let f = String(from ?? '').trim();
|
|
106
|
+
let t = String(to ?? '').trim();
|
|
107
|
+
if (!f) return {skip: {line, text: src, reason: 'It does not say which address to redirect.'}};
|
|
108
|
+
if (f.length > ADDRESS_MAX || t.length > ADDRESS_MAX) return {skip: {line, text: src, reason: 'The address is too long to be a real one.'}};
|
|
109
|
+
if (!code) return {skip: {line, text: src, reason: 'It is not a redirect (its status code is not 301, 302, 307, 308 or 410).'}};
|
|
110
|
+
if (regex) {
|
|
111
|
+
// Yoast, Rank Math and an .htaccess RewriteRule write the pattern without its leading slash.
|
|
112
|
+
if (f.startsWith('^') && !f.startsWith('^/')) f = `^/${f.slice(1)}`;
|
|
113
|
+
const out = fromPattern(f, t);
|
|
114
|
+
if (!out) return {skip: {line, text: src, reason: SKIP_PATTERN}};
|
|
115
|
+
f = out.from; t = out.to;
|
|
116
|
+
} else {
|
|
117
|
+
const out = squarespaceVars(f, t);
|
|
118
|
+
if (!out || /\$\d/.test(t)) return {skip: {line, text: src, reason: SKIP_PATTERN}};
|
|
119
|
+
f = out.from; t = out.to;
|
|
120
|
+
}
|
|
121
|
+
if (f.replace(/\/\*$/, '').includes('*')) return {skip: {line, text: src, reason: SKIP_PATTERN}};
|
|
122
|
+
if (code !== 410 && !t) return {skip: {line, text: src, reason: 'It does not say where it goes.'}};
|
|
123
|
+
return {row: {from: f, to: code === 410 ? '' : t, code, line}};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ---- The formats -----------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
/** Split one CSV record; quotes may hold the delimiter, "" is a quote. */
|
|
129
|
+
function splitCsv(text, delim) {
|
|
130
|
+
const rows = [];
|
|
131
|
+
let row = [];
|
|
132
|
+
let cell = '';
|
|
133
|
+
let quoted = false;
|
|
134
|
+
let blank = true; // the cell so far is only spaces (so a quote opens it)
|
|
135
|
+
let line = 1;
|
|
136
|
+
let startLine = 1;
|
|
137
|
+
const endCell = () => { row.push(cell.trim()); cell = ''; blank = true; };
|
|
138
|
+
const endRow = () => { endCell(); if (row.some(c => c !== '')) rows.push({cells: row, line: startLine}); row = []; startLine = line + 1; };
|
|
139
|
+
for (let i = 0; i < text.length; i++) {
|
|
140
|
+
const ch = text[i];
|
|
141
|
+
if (quoted) {
|
|
142
|
+
if (ch === '"' && text[i + 1] === '"') { cell += '"'; i++; }
|
|
143
|
+
else if (ch === '"') quoted = false;
|
|
144
|
+
else { if (ch === '\n') line++; cell += ch; }
|
|
145
|
+
} else if (ch === '"' && blank) { quoted = true; cell = ''; blank = false; }
|
|
146
|
+
else if (ch === delim) endCell();
|
|
147
|
+
else if (ch === '\n') { endRow(); line++; }
|
|
148
|
+
else if (ch !== '\r') { cell += ch; if (blank && ch !== ' ' && ch !== '\t') blank = false; }
|
|
149
|
+
}
|
|
150
|
+
if (cell !== '' || row.length) endRow();
|
|
151
|
+
return rows;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const HEADS = {
|
|
155
|
+
from: /^(source|source ?url|from|old|old ?url|old ?path|origin|redirect ?from|request|url|path|url ?from|from ?url)$/,
|
|
156
|
+
to: /^(target|target ?url|to|new|new ?url|new ?path|destination|destination ?url|redirect ?to|action_data|url ?to|to ?url)$/,
|
|
157
|
+
code: /^(code|http ?code|status ?code|action_code|redirect ?type|type|header ?code)$/,
|
|
158
|
+
regex: /^(regex|matching|format|match ?type)$/,
|
|
159
|
+
state: /^(status|enabled|active|state)$/
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
function parseSheet(text) {
|
|
163
|
+
const first = text.split('\n').find(l => l.trim()) || '';
|
|
164
|
+
const delim = first.includes('\t') ? '\t' : first.includes(',') ? ',' : first.includes(';') ? ';' : /\s/.test(first.trim()) ? ' ' : ',';
|
|
165
|
+
let records = splitCsv(text, delim);
|
|
166
|
+
if (delim === ' ') records = records.map(r => ({...r, cells: r.cells.filter(c => c !== '')}));
|
|
167
|
+
if (!records.length) return {rows: [], skipped: []};
|
|
168
|
+
const head = records[0].cells.map(c => c.toLowerCase().replace(/[_-]+/g, ' ').replace(/\s+/g, ' ').trim());
|
|
169
|
+
const col = {from: -1, to: -1, code: -1, regex: -1, state: -1};
|
|
170
|
+
const rows = [];
|
|
171
|
+
const skipped = [];
|
|
172
|
+
let kind = -1;
|
|
173
|
+
const looksLikeData = /^(\/|https?:\/\/)/i.test(records[0].cells[0] || '');
|
|
174
|
+
if (!looksLikeData) {
|
|
175
|
+
for (const [i, h] of head.entries()) {
|
|
176
|
+
for (const k of Object.keys(col)) {
|
|
177
|
+
// An exact "code" beats the Redirection plugin's "type" column (which says "url").
|
|
178
|
+
if (HEADS[k].test(h) && (col[k] === -1 || (k === 'code' && /code/.test(h)))) col[k] = i;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// The Redirection plugin's own sheet: "type" says what the rule does (url, error, pass, random…).
|
|
182
|
+
if (col.code !== -1 && head[col.code] !== 'type') kind = head.indexOf('type');
|
|
183
|
+
// A first row this does not know as a heading: the first two columns, as people write them. The row
|
|
184
|
+
// itself is listed rather than guessed at - it may be a redirect written without its leading slash.
|
|
185
|
+
if (col.from === -1 || col.to === -1 || col.from === col.to) {
|
|
186
|
+
col.from = 0; col.to = 1; col.code = -1; col.regex = -1; col.state = -1; kind = -1;
|
|
187
|
+
if (!head.some(h => Object.values(HEADS).some(re => re.test(h)))) {
|
|
188
|
+
skipped.push({line: records[0].line, text: clip(records[0].cells.filter(Boolean).join(', ')), reason: 'It was read as the sheet\'s heading - if it is a redirect, start its old address with "/".'});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// "status" is on/off in Redirection and Rank Math; in other sheets it is the code.
|
|
192
|
+
if (col.state !== -1 && col.code === -1 && records.slice(1, 6).some(r => /^\d{3}$/.test(r.cells[col.state] || ''))) { col.code = col.state; col.state = -1; }
|
|
193
|
+
records = records.slice(1);
|
|
194
|
+
} else {
|
|
195
|
+
col.from = 0; col.to = 1;
|
|
196
|
+
if (records.some(r => /^\d{3}$/.test(r.cells[2] || '') || codeOf(r.cells[2], 0))) col.code = 2;
|
|
197
|
+
}
|
|
198
|
+
for (const r of records) {
|
|
199
|
+
const c = r.cells;
|
|
200
|
+
const text2 = c.filter(Boolean).join(', ');
|
|
201
|
+
const what = kind === -1 ? '' : String(c[kind] ?? '').toLowerCase();
|
|
202
|
+
if (what && !/^\d+$/.test(what) && what !== 'url' && what !== 'error') { skipped.push({line: r.line, text: clip(text2), reason: 'It is not a plain redirect to one address.'}); continue; }
|
|
203
|
+
// Redirection writes "/unknown" for a rule whose target depends on the visitor (signed in, referrer…).
|
|
204
|
+
if (kind !== -1 && String(c[col.to] ?? '') === '/unknown') { skipped.push({line: r.line, text: clip(text2), reason: 'It depends on who is visiting (signed in, browser, referrer…), which a redirect here cannot check.'}); continue; }
|
|
205
|
+
const state = col.state === -1 ? '' : String(c[col.state] ?? '').toLowerCase();
|
|
206
|
+
if (/^(inactive|disabled|off|0|false|no|trash)$/.test(state)) { skipped.push({line: r.line, text: clip(text2), reason: 'It was switched off on the old site.'}); continue; }
|
|
207
|
+
const how = col.regex === -1 ? '' : String(c[col.regex] ?? '').toLowerCase();
|
|
208
|
+
let from = c[col.from] ?? '';
|
|
209
|
+
const to = c[col.to] ?? '';
|
|
210
|
+
let regex = /^(1|true|yes|regex)$/.test(how);
|
|
211
|
+
// Rank Math: exact | start | contains | end | regex.
|
|
212
|
+
if (how === 'start') from = `${String(from).replace(/\/+$/, '')}/*`;
|
|
213
|
+
else if (how === 'contains' || how === 'end') { skipped.push({line: r.line, text: clip(text2), reason: SKIP_PATTERN}); continue; }
|
|
214
|
+
if (!regex && /^\^/.test(from)) regex = true;
|
|
215
|
+
const out = shape({from, to, code: col.code === -1 ? 301 : codeOf(c[col.code]), regex, line: r.line, text: text2});
|
|
216
|
+
if (out.row) rows.push(out.row); else skipped.push(out.skip);
|
|
217
|
+
}
|
|
218
|
+
return {rows, skipped};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function parseArrows(text) {
|
|
222
|
+
const rows = [];
|
|
223
|
+
const skipped = [];
|
|
224
|
+
for (const [i, raw] of text.split('\n').entries()) {
|
|
225
|
+
const l = raw.trim();
|
|
226
|
+
if (!l || l.startsWith('#')) continue;
|
|
227
|
+
const m = /^(\S+)\s*(?:->|→|=>)\s*(\S*)(?:\s+(\S+))?\s*$/.exec(l);
|
|
228
|
+
if (!m) { skipped.push({line: i + 1, text: clip(l), reason: 'It is not written as "/old -> /new 301".'}); continue; }
|
|
229
|
+
const out = shape({from: m[1], to: m[2], code: codeOf(m[3]), line: i + 1, text: l});
|
|
230
|
+
if (out.row) rows.push(out.row); else skipped.push(out.skip);
|
|
231
|
+
}
|
|
232
|
+
return {rows, skipped};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// A directive and what can follow one - so a sheet headed "Redirect from,Redirect to" is not taken for Apache.
|
|
236
|
+
const APACHE = /^\s*(?:RewriteRule\s+\S|Redirect(?:Match|Permanent|Temp)?\s+(?:["/^(.]|\d{3}\s|(?:permanent|temp|seeother|gone)\s))/i;
|
|
237
|
+
|
|
238
|
+
/** Split on spaces, keeping "quoted parts" whole. */
|
|
239
|
+
const words = (l) => (l.match(/"[^"]*"|\S+/g) || []).map(w => w.replace(/^"|"$/g, ''));
|
|
240
|
+
|
|
241
|
+
function parseApache(text) {
|
|
242
|
+
const rows = [];
|
|
243
|
+
const skipped = [];
|
|
244
|
+
let conditional = false;
|
|
245
|
+
for (const [i, raw] of text.split('\n').entries()) {
|
|
246
|
+
const l = raw.trim();
|
|
247
|
+
if (!l || l.startsWith('#')) continue;
|
|
248
|
+
if (/^RewriteCond\b/i.test(l)) { conditional = true; continue; }
|
|
249
|
+
if (!APACHE.test(l)) continue; // the rest of an .htaccess is not about redirects
|
|
250
|
+
const w = words(l);
|
|
251
|
+
const directive = w[0].toLowerCase();
|
|
252
|
+
const line = i + 1;
|
|
253
|
+
let row;
|
|
254
|
+
if (directive === 'rewriterule') {
|
|
255
|
+
const flags = (/^\[(.*)\]$/.exec(w.slice(3).join('') || '')?.[1] || '').split(',').map(f => f.trim().toLowerCase());
|
|
256
|
+
const r = flags.find(f => f === 'r' || f.startsWith('r='));
|
|
257
|
+
const gone = flags.includes('g');
|
|
258
|
+
const had = conditional;
|
|
259
|
+
conditional = false;
|
|
260
|
+
if (had) { skipped.push({line, text: clip(l), reason: 'It only applies on a condition (RewriteCond), which a redirect here cannot check.'}); continue; }
|
|
261
|
+
if (!r && !gone) continue; // an internal rewrite, not a redirect
|
|
262
|
+
row = {from: w[1] || '', to: gone ? '' : w[2], code: gone ? 410 : codeOf((r || '').split('=')[1], 302), regex: true};
|
|
263
|
+
} else {
|
|
264
|
+
conditional = false;
|
|
265
|
+
let code = directive === 'redirectpermanent' ? 301 : 302;
|
|
266
|
+
let args = w.slice(1);
|
|
267
|
+
if (directive === 'redirect' || directive === 'redirectmatch') {
|
|
268
|
+
if (args.length >= 3 || (args.length === 2 && codeOf(args[0], 0) === 410)) { code = codeOf(args[0], 0); args = args.slice(1); }
|
|
269
|
+
else if (args.length === 2 && /^\d{3}$/.test(args[0])) { code = 0; args = args.slice(1); }
|
|
270
|
+
}
|
|
271
|
+
// Apache's Redirect also takes what is under the address; here it is the address itself - what
|
|
272
|
+
// these lines are written for - so a page imported beneath it is never hidden.
|
|
273
|
+
row = {from: args[0], to: args[1] || '', code, regex: directive === 'redirectmatch'};
|
|
274
|
+
}
|
|
275
|
+
const out = shape({...row, line, text: l});
|
|
276
|
+
if (out.row) rows.push(out.row); else skipped.push(out.skip);
|
|
277
|
+
}
|
|
278
|
+
return {rows, skipped};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function parseJson(text) {
|
|
282
|
+
let data;
|
|
283
|
+
try { data = JSON.parse(text); } catch { return null; }
|
|
284
|
+
const list = Array.isArray(data) ? data : Array.isArray(data?.redirects) ? data.redirects : null;
|
|
285
|
+
if (!list) return {rows: [], skipped: []};
|
|
286
|
+
const rows = [];
|
|
287
|
+
const skipped = [];
|
|
288
|
+
for (const [i, r] of list.entries()) {
|
|
289
|
+
if (!r || typeof r !== 'object') continue;
|
|
290
|
+
const from = r.url ?? r.source ?? r.from ?? r.origin ?? '';
|
|
291
|
+
const data2 = r.action_data;
|
|
292
|
+
const to = (data2 && typeof data2 === 'object' ? data2.url : data2) ?? r.target ?? r.to ?? r.destination ?? '';
|
|
293
|
+
const text2 = `${from} -> ${typeof to === 'string' ? to : ''}`;
|
|
294
|
+
const line = i + 1;
|
|
295
|
+
if (r.enabled === false || r.status === 'disabled' || r.status === 'inactive') { skipped.push({line, text: clip(text2), reason: 'It was switched off on the old site.'}); continue; }
|
|
296
|
+
// Redirection: only a plain "redirect to URL" / "error" that matches on the address alone.
|
|
297
|
+
if (r.match_type && r.match_type !== 'url') { skipped.push({line, text: clip(text2), reason: 'It depends on who is visiting (signed in, browser, referrer…), which a redirect here cannot check.'}); continue; }
|
|
298
|
+
if (r.action_type && !['url', 'error'].includes(r.action_type)) { skipped.push({line, text: clip(text2), reason: 'It is not a plain redirect to one address.'}); continue; }
|
|
299
|
+
if (typeof to !== 'string') { skipped.push({line, text: clip(text2), reason: 'It is not a plain redirect to one address.'}); continue; }
|
|
300
|
+
const out = shape({from, to, code: codeOf(r.action_code ?? r.code ?? r.type), regex: r.regex === true || r.regex === 1 || r.regex === '1', line, text: text2});
|
|
301
|
+
if (out.row) rows.push(out.row); else skipped.push(out.skip);
|
|
302
|
+
}
|
|
303
|
+
return {rows, skipped};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Read a list of redirects in any of the formats above.
|
|
308
|
+
*
|
|
309
|
+
* @param {string} text
|
|
310
|
+
* @returns {{format: 'json'|'apache'|'arrows'|'sheet', rows: {from: string, to: string, code: number, line: number}[],
|
|
311
|
+
* skipped: {line: number, text: string, reason: string}[], more: number}}
|
|
312
|
+
* `more`: rows left out because the list is over OLD_REDIRECTS_MAX.
|
|
313
|
+
*/
|
|
314
|
+
export function parseRedirectList(text) {
|
|
315
|
+
const raw = String(text ?? '').replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n');
|
|
316
|
+
let format = 'sheet';
|
|
317
|
+
let out = null;
|
|
318
|
+
if (/^\s*[[{]/.test(raw)) { out = parseJson(raw); if (out) format = 'json'; }
|
|
319
|
+
// No line of a real list is this long: a longer one is dropped before anything reads it.
|
|
320
|
+
const tooLong = [];
|
|
321
|
+
const s = out ? '' : raw.split('\n').map((l, i) => { if (l.length <= ADDRESS_MAX * 3) return l; tooLong.push({line: i + 1, text: clip(l.slice(0, 400)), reason: 'The line is too long to be a redirect.'}); return ''; }).join('\n');
|
|
322
|
+
const lines = s.split('\n').filter(l => l.trim() && !l.trim().startsWith('#'));
|
|
323
|
+
if (!out && lines.some(l => APACHE.test(l))) { format = 'apache'; out = parseApache(s); }
|
|
324
|
+
if (!out && lines.some(l => /\s(->|→|=>)\s/.test(l))) { format = 'arrows'; out = parseArrows(s); }
|
|
325
|
+
if (!out) out = parseSheet(s);
|
|
326
|
+
// The same address twice: the first one is the one the old site answered with.
|
|
327
|
+
const seen = new Set();
|
|
328
|
+
const rows = [];
|
|
329
|
+
for (const r of out.rows) {
|
|
330
|
+
const k = r.from.toLowerCase().replace(/\/+$/, '');
|
|
331
|
+
if (seen.has(k)) continue;
|
|
332
|
+
seen.add(k);
|
|
333
|
+
rows.push(r);
|
|
334
|
+
}
|
|
335
|
+
const more = Math.max(0, rows.length - OLD_REDIRECTS_MAX);
|
|
336
|
+
return {format, rows: rows.slice(0, OLD_REDIRECTS_MAX), skipped: [...tooLong, ...out.skipped].slice(0, 500), more};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* The redirects a WordPress export holds as posts: Safe Redirect Manager's
|
|
341
|
+
* (post type redirect_rule; the rule is in its meta). Drafts and binned rules
|
|
342
|
+
* were not answering on the old site, so they are left.
|
|
343
|
+
*
|
|
344
|
+
* @param {object} wxr - from readWxr()
|
|
345
|
+
* @returns {{rows: object[], skipped: object[]}}
|
|
346
|
+
*/
|
|
347
|
+
export function exportRedirects(wxr) {
|
|
348
|
+
const rows = [];
|
|
349
|
+
const skipped = [];
|
|
350
|
+
for (const it of wxr?.items || []) {
|
|
351
|
+
if (it.type !== 'redirect_rule' || it.status !== 'publish') continue;
|
|
352
|
+
const from = it.meta?._redirect_rule_from || '';
|
|
353
|
+
const to = it.meta?._redirect_rule_to || '';
|
|
354
|
+
if (!from) continue;
|
|
355
|
+
const regex = it.meta?._redirect_rule_from_regex === '1';
|
|
356
|
+
const out = shape({from, to, code: codeOf(it.meta?._redirect_rule_status_code, 302), regex, text: `${from} -> ${to}`});
|
|
357
|
+
if (out.row) rows.push(out.row); else skipped.push(out.skip);
|
|
358
|
+
}
|
|
359
|
+
const more = Math.max(0, rows.length - OLD_REDIRECTS_MAX);
|
|
360
|
+
return {rows: rows.slice(0, OLD_REDIRECTS_MAX), skipped: skipped.slice(0, 500), more};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Fit the old site's redirects to this import.
|
|
365
|
+
*
|
|
366
|
+
* @param {{from: string, to: string, code: number}[]} rows
|
|
367
|
+
* @param {object} ctx
|
|
368
|
+
* @param {string[]} ctx.hosts - the old site's own hostnames
|
|
369
|
+
* @param {Map<string,string>} ctx.moved - old address (normalised) -> where the import puts it
|
|
370
|
+
* @param {(p: string) => string} ctx.normalisePath
|
|
371
|
+
* @returns {{redirects: {from: string, to: string, code: number, key: string, old: true}[], skipped: {text: string, reason: string}[]}}
|
|
372
|
+
*/
|
|
373
|
+
export function resolveOldRedirects(rows, {hosts = [], moved = new Map(), normalisePath}) {
|
|
374
|
+
const own = new Set(hosts.map(h => String(h).toLowerCase()));
|
|
375
|
+
const local = (address) => {
|
|
376
|
+
if (!isAbsolute(address)) return {path: address.startsWith('/') ? address : `/${address}`, elsewhere: false};
|
|
377
|
+
try {
|
|
378
|
+
const u = new URL(address.replace(/\*/g, '%2A'));
|
|
379
|
+
if (!own.has(u.hostname.toLowerCase())) return {path: address, elsewhere: true};
|
|
380
|
+
return {path: `${u.pathname}${u.search}`.replace(/%2A/gi, '*') || '/', elsewhere: false};
|
|
381
|
+
} catch { return {path: address, elsewhere: true}; }
|
|
382
|
+
};
|
|
383
|
+
const clean = [];
|
|
384
|
+
const skipped = [];
|
|
385
|
+
for (const r of rows || []) {
|
|
386
|
+
const text = `${r.from} -> ${r.to || 'gone'}`;
|
|
387
|
+
const f = local(r.from);
|
|
388
|
+
if (f.elsewhere) { skipped.push({text: clip(text), reason: 'The address it redirects is on another site.'}); continue; }
|
|
389
|
+
if (f.path.includes('?')) { skipped.push({text: clip(text), reason: 'It redirects an address with a ?query, and a redirect here answers a path.'}); continue; }
|
|
390
|
+
const wildcard = f.path.endsWith('/*');
|
|
391
|
+
const prefix = wildcard ? normalisePath(f.path.slice(0, -2)) : '';
|
|
392
|
+
const from = wildcard ? `${prefix === '/' ? '' : prefix}/*` : normalisePath(f.path);
|
|
393
|
+
const t = r.code === 410 ? {path: '', elsewhere: false} : local(r.to);
|
|
394
|
+
clean.push({from, to: t.path, external: t.elsewhere, code: r.code, wildcard});
|
|
395
|
+
}
|
|
396
|
+
const exact = new Map();
|
|
397
|
+
for (const r of clean) if (!r.wildcard && !exact.has(r.from)) exact.set(r.from, r);
|
|
398
|
+
const redirects = [];
|
|
399
|
+
for (const r of clean) {
|
|
400
|
+
let {to, code} = r;
|
|
401
|
+
let external = r.external;
|
|
402
|
+
let loop = false;
|
|
403
|
+
// Follow it through the old site's other redirects, then to wherever the import puts that address.
|
|
404
|
+
if (!external && code !== 410 && !to.includes('*')) {
|
|
405
|
+
const seen = new Set([r.from]);
|
|
406
|
+
loop = true; // until the chain ends: one longer than ten hops is treated as never ending
|
|
407
|
+
for (let hop = 0; hop < 10; hop++) {
|
|
408
|
+
const at = normalisePath(to);
|
|
409
|
+
if (seen.has(at)) break;
|
|
410
|
+
seen.add(at);
|
|
411
|
+
const next = exact.get(at);
|
|
412
|
+
if (next) {
|
|
413
|
+
if (next.code === 410) { code = 410; to = ''; loop = false; break; }
|
|
414
|
+
to = next.to;
|
|
415
|
+
if (next.external || to.includes('*')) { external = next.external; loop = false; break; }
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
if (moved.has(at)) {
|
|
419
|
+
const query = to.includes('?') ? to.slice(to.indexOf('?')) : '';
|
|
420
|
+
to = moved.get(at);
|
|
421
|
+
if (query && !to.includes('?')) to += query;
|
|
422
|
+
}
|
|
423
|
+
loop = false;
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
if (loop) { skipped.push({text: clip(`${r.from} -> ${r.to}`), reason: 'It leads back to itself through the old site\'s other redirects.'}); continue; }
|
|
428
|
+
// A path here is written the way SEO › Redirects keeps one: no doubled or trailing slash.
|
|
429
|
+
if (!external && to && !isAbsolute(to)) {
|
|
430
|
+
const [p, q] = to.split('?');
|
|
431
|
+
to = p.replace(/\/{2,}/g, '/').replace(/(.)\/+$/, '$1') + (q ? `?${q}` : '');
|
|
432
|
+
}
|
|
433
|
+
redirects.push({from: r.from, to, code, key: `old:${r.from}`, old: true});
|
|
434
|
+
}
|
|
435
|
+
return {redirects, skipped};
|
|
436
|
+
}
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* terms: [{taxonomy, id, slug, name, parent, description}] - every other taxonomy's terms
|
|
10
10
|
* (WooCommerce's product_cat, product_tag, pa_* attribute values…),
|
|
11
11
|
* items: [{id, type, title, slug, status, parent, order, date, dateGmt, link, guid,
|
|
12
|
-
* creator, content, excerpt, password, attachmentUrl, meta{}, terms[], comments[]}]}
|
|
12
|
+
* creator, content, excerpt, password, attachmentUrl, meta{}, oldSlugs[], terms[], comments[]}]}
|
|
13
13
|
*
|
|
14
14
|
* @module services/wpImport/wxr
|
|
15
15
|
*/
|
|
@@ -143,8 +143,11 @@ export function wxrFromTree(root) {
|
|
|
143
143
|
|
|
144
144
|
function readItem(node) {
|
|
145
145
|
const meta = {};
|
|
146
|
+
// Every address the item had before (WordPress keeps one _wp_old_slug per rename, and redirects each).
|
|
147
|
+
const oldSlugs = [];
|
|
146
148
|
for (const m of children(node, 'wp:postmeta')) {
|
|
147
149
|
const k = textOf(m, 'wp:meta_key');
|
|
150
|
+
if (k === '_wp_old_slug') { const s = decodeSlug(textOf(m, 'wp:meta_value').trim()); if (s && !oldSlugs.includes(s) && oldSlugs.length < 50) oldSlugs.push(s); }
|
|
148
151
|
if (k && !(k in meta)) meta[k] = textOf(m, 'wp:meta_value', {raw: true});
|
|
149
152
|
}
|
|
150
153
|
const terms = children(node, 'category').map(c => ({
|
|
@@ -182,6 +185,7 @@ function readItem(node) {
|
|
|
182
185
|
password: textOf(node, 'wp:post_password'),
|
|
183
186
|
attachmentUrl: textOf(node, 'wp:attachment_url'),
|
|
184
187
|
meta,
|
|
188
|
+
oldSlugs,
|
|
185
189
|
terms,
|
|
186
190
|
comments
|
|
187
191
|
};
|