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
21 changes: 19 additions & 2 deletions phpdotnet/phd/Redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
*
* Every `to=` value is validated against xml:ids collected from the
* language root and the docbook root (typically doc-base); a missing
* target aborts the build.
* target aborts the build. A redirect with a manual= attribute leads to
* another manual (e.g. manual="extensions" for doc-extensions), which is
* built on its own: its target is not validated.
*/
final class Redirects
{
Expand Down Expand Up @@ -93,6 +95,9 @@ public static function renderTo(string $outputDir, string $xmlRoot, OutputHandle
* redirect: its entries go to `in_place` so that the page is served at
* the requested URL instead of answering a 301.
*
* The target of a redirect with a manual= attribute is a page of that
* manual, served from /manual/{manual}/: it is emitted as `{manual}/{to}`.
*
* @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 Down Expand Up @@ -144,6 +149,14 @@ public static function parse(string $path): array
if ($from === '' || $to === '') {
throw new \Error(sprintf('Empty from/to in %s (scope=%s)', $path, $scope));
}
$manual = (string)$r['manual'];
if ($manual !== '' && !preg_match('/^[a-z][a-z0-9_-]*$/', $manual)) {
throw new \Error(sprintf('Invalid manual in %s: %s', $path, $manual));
}
// Only a page of this manual can be served at the requested URL.
if ($manual !== '' && $key === 'in_place') {
throw new \Error(sprintf('Cannot serve a page of another manual in place in %s: %s -> %s', $path, $from, $to));
}
// 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])
Expand All @@ -155,7 +168,7 @@ public static function parse(string $path): array
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;
$maps[$key][$from] = $manual === '' ? $to : "$manual/$to";
}
}

Expand Down Expand Up @@ -206,6 +219,10 @@ public static function findBrokenTargets(array $maps, array $ids, array $prefixe
$broken = [];
foreach ($maps as $scope => $entries) {
foreach ($entries as $from => $to) {
// A page of another manual (`{manual}/{to}`) cannot be checked here.
if (str_contains($to, '/')) {
continue;
}
$base = explode('#', $to, 2)[0];
foreach ($prefixes as $p) {
$key = $p . $base;
Expand Down
14 changes: 14 additions & 0 deletions tests/redirects/data/parse_004_extensions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<redirects>
<group scope="shortcut">
<redirect from="tut.php" to="install"/>
<redirect from="mysql.trace-mode" to="mysql.configuration#ini.mysql.trace-mode" manual="extensions"/>
</group>
<group scope="in-manual">
<redirect from="pool.submitto" to="pool.submitTo" manual="extensions"/>
<redirect from="only.french" to="seulement" manual="fr"/>
</group>
<group scope="both">
<redirect from="set.mongodb" to="book.mongodb" manual="extensions"/>
</group>
</redirects>
6 changes: 6 additions & 0 deletions tests/redirects/data/parse_004_manual_serve.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="apcu" to="book.apcu" manual="extensions"/>
</group>
</redirects>
6 changes: 6 additions & 0 deletions tests/redirects/data/parse_004_manual_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">
<redirect from="apcu" to="book.apcu" manual="../pecl"/>
</group>
</redirects>
28 changes: 28 additions & 0 deletions tests/redirects/parse_004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Redirects parse 004 - A redirect with a manual attribute targets that manual and is not validated
--FILE--
<?php
namespace phpdotnet\phd;

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

$maps = Redirects::parse(__DIR__ . '/data/parse_004_extensions.xml');
echo json_encode($maps, JSON_UNESCAPED_SLASHES) . PHP_EOL;

$ids = Redirects::collectXmlIds(__DIR__ . '/data/docs');
echo 'Broken targets: ' . count(Redirects::findBrokenTargets($maps, $ids)) . PHP_EOL;

foreach (['manual_value', 'manual_serve'] as $name) {
try {
Redirects::parse(__DIR__ . "/data/parse_004_$name.xml");
echo 'no error' . PHP_EOL;
} catch (\Error $e) {
echo $e->getMessage() . PHP_EOL;
}
}
?>
--EXPECTF--
{"shortcut":{"tut.php":"install","mysql.trace-mode":"extensions/mysql.configuration#ini.mysql.trace-mode"},"in_manual":{"pool.submitto":"extensions/pool.submitTo","only.french":"fr/seulement"},"both":{"set.mongodb":"extensions/book.mongodb"},"in_place":[]}
Broken targets: 0
Invalid manual in %sparse_004_manual_value.xml: ../pecl
Cannot serve a page of another manual in place in %sparse_004_manual_serve.xml: apcu -> book.apcu