diff --git a/phpdotnet/phd/Redirects.php b/phpdotnet/phd/Redirects.php index 0d8082d9e..0ccf0233f 100644 --- a/phpdotnet/phd/Redirects.php +++ b/phpdotnet/phd/Redirects.php @@ -75,9 +75,10 @@ public static function renderTo(string $outputDir, string $xmlRoot, OutputHandle $out->v( sprintf( - 'Rendered %s (shortcut=%d, in_manual=%d, both=%d)', + 'Rendered %s (shortcut=%d, in_place=%d, in_manual=%d, both=%d)', $file, count($maps['shortcut']), + count($maps['in_place']), count($maps['in_manual']), count($maps['both']) ), @@ -86,9 +87,13 @@ public static function renderTo(string $outputDir, string $xmlRoot, OutputHandle } /** - * Parse a redirects.xml file into three scope-keyed maps. + * Parse a redirects.xml file into scope-keyed maps. * - * @return array{shortcut: array, in_manual: array, both: array} + * A shortcut group marked serve="in-place" is an expansion, not a + * redirect: its entries go to `in_place` so that the page is served at + * the requested URL instead of answering a 301. + * + * @return array{shortcut: array, in_manual: array, both: array, in_place: array} */ public static function parse(string $path): array { @@ -113,24 +118,43 @@ public static function parse(string $path): array ); } - $maps = ['shortcut' => [], 'in_manual' => [], 'both' => []]; + $maps = ['shortcut' => [], 'in_manual' => [], 'both' => [], 'in_place' => []]; foreach ($xml->group as $group) { $scope = (string)$group['scope']; $key = $scope === 'in-manual' ? 'in_manual' : $scope; - if (!array_key_exists($key, $maps)) { + if ($key === 'in_place' || !array_key_exists($key, $maps)) { throw new \Error("Unknown scope in $path: $scope"); } + $serve = (string)$group['serve']; + if ($serve !== '') { + if ($serve !== 'in-place') { + throw new \Error("Unknown serve value in $path: $serve"); + } + if ($key !== 'shortcut') { + throw new \Error("serve=\"in-place\" only applies to the shortcut scope in $path"); + } + $key = 'in_place'; + } + foreach ($group->redirect as $r) { $from = (string)$r['from']; $to = (string)$r['to']; if ($from === '' || $to === '') { throw new \Error(sprintf('Empty from/to in %s (scope=%s)', $path, $scope)); } - if (isset($maps[$key][$from])) { + // A bare URI is either redirected or served in place, never both. + $duplicate = $key === 'shortcut' || $key === 'in_place' + ? isset($maps['shortcut'][$from]) || isset($maps['in_place'][$from]) + : isset($maps[$key][$from]); + if ($duplicate) { throw new \Error(sprintf('Duplicate key in %s (scope=%s): %s', $path, $scope, $from)); } + // The anchor of a page that is not redirected to is lost. + if ($key === 'in_place' && str_contains($to, '#')) { + throw new \Error(sprintf('Cannot serve an anchor in place in %s: %s -> %s', $path, $from, $to)); + } $maps[$key][$from] = $to; } } diff --git a/tests/redirects/data/parse_002_in_place.xml b/tests/redirects/data/parse_002_in_place.xml new file mode 100644 index 000000000..b2a072ec8 --- /dev/null +++ b/tests/redirects/data/parse_002_in_place.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/tests/redirects/data/parse_003_serve_anchor.xml b/tests/redirects/data/parse_003_serve_anchor.xml new file mode 100644 index 000000000..0b5a7fc27 --- /dev/null +++ b/tests/redirects/data/parse_003_serve_anchor.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/redirects/data/parse_003_serve_duplicate.xml b/tests/redirects/data/parse_003_serve_duplicate.xml new file mode 100644 index 000000000..661315d35 --- /dev/null +++ b/tests/redirects/data/parse_003_serve_duplicate.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/tests/redirects/data/parse_003_serve_scope.xml b/tests/redirects/data/parse_003_serve_scope.xml new file mode 100644 index 000000000..349d97231 --- /dev/null +++ b/tests/redirects/data/parse_003_serve_scope.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/redirects/data/parse_003_serve_value.xml b/tests/redirects/data/parse_003_serve_value.xml new file mode 100644 index 000000000..0895e6170 --- /dev/null +++ b/tests/redirects/data/parse_003_serve_value.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/redirects/parse_002.phpt b/tests/redirects/parse_002.phpt new file mode 100644 index 000000000..2e9f41436 --- /dev/null +++ b/tests/redirects/parse_002.phpt @@ -0,0 +1,12 @@ +--TEST-- +Redirects parse 002 - A shortcut group marked serve="in-place" goes to its own map +--FILE-- + +--EXPECT-- +{"shortcut":{"tut.php":"tutorial","memory-limit":"ini.core#ini.memory-limit"},"in_manual":[],"both":[],"in_place":{"try":"language.exceptions"}} diff --git a/tests/redirects/parse_003.phpt b/tests/redirects/parse_003.phpt new file mode 100644 index 000000000..d0f643b44 --- /dev/null +++ b/tests/redirects/parse_003.phpt @@ -0,0 +1,22 @@ +--TEST-- +Redirects parse 003 - serve="in-place" is only valid for shortcuts without an anchor, and never twice +--FILE-- +getMessage() . PHP_EOL; + } +} +?> +--EXPECTF-- +Cannot serve an anchor in place in %sparse_003_serve_anchor.xml: memory-limit -> ini.core#ini.memory-limit +Unknown serve value in %sparse_003_serve_value.xml: inplace +serve="in-place" only applies to the shortcut scope in %sparse_003_serve_scope.xml +Duplicate key in %sparse_003_serve_duplicate.xml (scope=shortcut): try diff --git a/tests/redirects/render_001.phpt b/tests/redirects/render_001.phpt index 2c2859a90..7108b8d68 100644 --- a/tests/redirects/render_001.phpt +++ b/tests/redirects/render_001.phpt @@ -15,4 +15,4 @@ echo json_encode($maps) . PHP_EOL; ?> --EXPECT-- Broken targets: 0 -{"shortcut":{"if":"control-structures.if","anchored":"page.foo#some.anchor","bcmath":"bc"},"in_manual":{"class.oldname":"class.newname"},"both":{"installation":"install"}} +{"shortcut":{"if":"control-structures.if","anchored":"page.foo#some.anchor","bcmath":"bc"},"in_manual":{"class.oldname":"class.newname"},"both":{"installation":"install"},"in_place":[]} diff --git a/tests/redirects/render_003.phpt b/tests/redirects/render_003.phpt index 8ad4c2408..fa90ce7cd 100644 --- a/tests/redirects/render_003.phpt +++ b/tests/redirects/render_003.phpt @@ -26,4 +26,4 @@ unlink($lang . '/redirects.xml'); rmdir($lang); ?> --EXPECT-- -{"shortcut":{"if":"control-structures.if","anchored":"page.foo#some.anchor","bcmath":"bc"},"in_manual":{"class.oldname":"class.newname"},"both":{"installation":"install"}} +{"shortcut":{"if":"control-structures.if","anchored":"page.foo#some.anchor","bcmath":"bc"},"in_manual":{"class.oldname":"class.newname"},"both":{"installation":"install"},"in_place":[]}