[phd] master: Emit the shortcuts served in place in their own redirects.json map (#272)
| From: | Louis-Arnaud via GitHub | Date: | Wed, 18 Nov 2026 10:02:38 +0000 |
| Subject: | [phd] master: Emit the shortcuts served in place in their own redirects.json map (#272) | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-23975@lists.php.net to get a copy of this message | ||
Author: Louis-Arnaud (lacatoire)
Committer: GitHub (web-flow)
Pusher: lacatoire
Date: 2026-11-18T11:02:34+01:00
Commit: https://github.com/php/phd/commit/e597085807733d35a51d16e8454b7f31e340d4de
Raw diff: https://github.com/php/phd/commit/e597085807733d35a51d16e8454b7f31e340d4de.diff
Emit the shortcuts served in place in their own redirects.json map (#272)
A shortcut group marked serve="in-place" in redirects.xml is an expansion,
not a redirect. Its entries go to the new in_place map, so that web-php can
serve the page at the requested URL. The group cannot hold an anchor, which
would be lost, and a bare URI cannot be both redirected and served in place.
Changed paths:
A tests/redirects/data/parse_002_in_place.xml
A tests/redirects/data/parse_003_serve_anchor.xml
A tests/redirects/data/parse_003_serve_duplicate.xml
A tests/redirects/data/parse_003_serve_scope.xml
A tests/redirects/data/parse_003_serve_value.xml
A tests/redirects/parse_002.phpt
A tests/redirects/parse_003.phpt
M phpdotnet/phd/Redirects.php
M tests/redirects/render_001.phpt
M tests/redirects/render_003.phpt
Diff:
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<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
{
@@ -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 @@
+<?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>
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 @@
+<?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>
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 @@
+<?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>
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 @@
+<?xml version="1.0" encoding="utf-8"?>
+<redirects>
+ <group scope="in-manual" serve="in-place">
+ <redirect from="class.oldname" to="class.newname"/>
+ </group>
+</redirects>
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 @@
+<?xml version="1.0" encoding="utf-8"?>
+<redirects>
+ <group scope="shortcut" serve="inplace">
+ <redirect from="try" to="language.exceptions"/>
+ </group>
+</redirects>
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--
+<?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"}}
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--
+<?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
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":[]}