domma-cms 1.69.1 → 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.
@@ -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
  };