Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions phpdotnet/phd/Redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
),
Expand All @@ -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<string,string>, in_manual: array<string,string>, both: array<string,string>}
* 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<string,string>, in_manual: array<string,string>, both: array<string,string>, in_place: array<string,string>}
*/
public static function parse(string $path): array
{
Expand All @@ -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;
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/redirects/data/parse_002_in_place.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<redirects>
<group scope="shortcut" serve="in-place">
<redirect from="try" to="language.exceptions"/>
</group>
<group scope="shortcut">
<redirect from="tut.php" to="tutorial"/>
<redirect from="memory-limit" to="ini.core#ini.memory-limit"/>
</group>
</redirects>
6 changes: 6 additions & 0 deletions tests/redirects/data/parse_003_serve_anchor.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<redirects>
<group scope="shortcut" serve="in-place">
<redirect from="memory-limit" to="ini.core#ini.memory-limit"/>
</group>
</redirects>
9 changes: 9 additions & 0 deletions tests/redirects/data/parse_003_serve_duplicate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<redirects>
<group scope="shortcut" serve="in-place">
<redirect from="try" to="language.exceptions"/>
</group>
<group scope="shortcut">
<redirect from="try" to="language.exceptions"/>
</group>
</redirects>
6 changes: 6 additions & 0 deletions tests/redirects/data/parse_003_serve_scope.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<redirects>
<group scope="in-manual" serve="in-place">
<redirect from="class.oldname" to="class.newname"/>
</group>
</redirects>
6 changes: 6 additions & 0 deletions tests/redirects/data/parse_003_serve_value.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<redirects>
<group scope="shortcut" serve="inplace">
<redirect from="try" to="language.exceptions"/>
</group>
</redirects>
12 changes: 12 additions & 0 deletions tests/redirects/parse_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Redirects parse 002 - A shortcut group marked serve="in-place" goes to its own map
--FILE--
<?php
namespace phpdotnet\phd;

require_once __DIR__ . '/../setup.php';

echo json_encode(Redirects::parse(__DIR__ . '/data/parse_002_in_place.xml')) . PHP_EOL;
?>
--EXPECT--
{"shortcut":{"tut.php":"tutorial","memory-limit":"ini.core#ini.memory-limit"},"in_manual":[],"both":[],"in_place":{"try":"language.exceptions"}}
22 changes: 22 additions & 0 deletions tests/redirects/parse_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Redirects parse 003 - serve="in-place" is only valid for shortcuts without an anchor, and never twice
--FILE--
<?php
namespace phpdotnet\phd;

require_once __DIR__ . '/../setup.php';

foreach (['serve_anchor', 'serve_value', 'serve_scope', 'serve_duplicate'] as $name) {
try {
Redirects::parse(__DIR__ . "/data/parse_003_$name.xml");
echo 'no error' . PHP_EOL;
} catch (\Error $e) {
echo $e->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
2 changes: 1 addition & 1 deletion tests/redirects/render_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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":[]}
2 changes: 1 addition & 1 deletion tests/redirects/render_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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":[]}
Loading