Skip to content

Commit 1fa165a

Browse files
committed
Support API reference URLs from the old site
Old site index is committed instead of fetched as that site is going to be replaced with this one, so we expect not to be able to fetch it in the future. Tested with: - /manual/en/class.hack.vectortv.php - /manual/en/hack.vectortv.clear.php - /manual/en/function.hack.hh.asio.curl-exec.php
1 parent 8603d3a commit 1fa165a

7 files changed

+148
-0
lines changed

bin/build.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ function hhvm_to_yaml(): void {
99
(new GuidesIndexBuildStep())->buildAll();
1010
(new APIIndexBuildStep())->buildAll();
1111

12+
(new APILegacyRedirectsBuildStep())->buildAll();
13+
1214
(new StaticResourceMapBuildStep())->buildAll();
1315

1416
(new MergedMarkdownBuildStep())->buildAll();

legacy-docs-site-index.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?hh // strict
2+
3+
namespace HHVM\UserDocumentation;
4+
5+
final class APILegacyRedirectsBuildStep extends BuildStep {
6+
use CodegenBuildStep;
7+
8+
/* curl http://docs.hhvm.com/manual/en/search-index.json \
9+
* > legacy-docs-site-index.json
10+
*
11+
* This file is committed instead of fetched as we expect that site to
12+
* be taken down soon.
13+
*/
14+
const string LEGACY_INDEX = LocalConfig::ROOT.'/legacy-docs-site-index.json';
15+
16+
public function buildAll(): void {
17+
Log::i("\nAPILegacyRedirectsBuild");
18+
$this->createIndex();
19+
}
20+
21+
private function createIndex(
22+
): void {
23+
$index = $this->generateIndexData();
24+
25+
$code = $this->writeCode(
26+
'APILegacyRedirectData.hhi',
27+
$index,
28+
);
29+
file_put_contents(
30+
BuildPaths::APIDOCS_LEGACY_REDIRECTS,
31+
$code,
32+
);
33+
}
34+
35+
private function generateIndexData(): array<string, string> {
36+
$old_index = json_decode(file_get_contents(self::LEGACY_INDEX));
37+
$old_classes = [];
38+
$old_methods = [];
39+
$old_functions = [];
40+
41+
Log::v("\nProcessing old site index");
42+
foreach ($old_index as $entry) {
43+
Log::v('.');
44+
list ($name, $id, $type) = $entry;
45+
46+
$name = html_entity_decode($name);
47+
48+
if ($type === 'phpdoc:classref') {
49+
$name = explode('<', $name)[0]; // remove generics
50+
$old_classes[$name] = $id;
51+
continue;
52+
}
53+
if ($type !== 'refentry') {
54+
continue;
55+
}
56+
$parts = (new Vector(explode('::', $name)))
57+
->map($x ==> explode('<', $x)[0]);
58+
59+
if (count($parts) === 1) {
60+
$old_functions[$parts[0]] = $id;
61+
continue;
62+
}
63+
64+
invariant(
65+
count($parts) === 2,
66+
"Definition %s has %d parts",
67+
$name,
68+
count($parts),
69+
);
70+
71+
$old_methods[implode('::', $parts)] = $id;
72+
}
73+
74+
Log::v("\nCross-referencing with current site index");
75+
76+
$old_ids_to_new_urls = [];
77+
78+
$classes = (Map {})
79+
->setAll(APIIndex::getClassIndex(APIDefinitionType::CLASS_DEF))
80+
->setAll(APIIndex::getClassIndex(APIDefinitionType::INTERFACE_DEF))
81+
->setAll(APIIndex::getClassIndex(APIDefinitionType::TRAIT_DEF));
82+
83+
foreach ($classes as $class) {
84+
Log::v('.');
85+
$old_id = idx($old_classes, $class['name']);
86+
87+
if ($old_id) {
88+
$old_ids_to_new_urls[$old_id] =
89+
URLBuilder::getPathForClass($class);
90+
}
91+
92+
foreach ($class['methods'] as $method) {
93+
$name = $class['name'].'::'.$method['name'];
94+
$old_id = idx($old_methods, $class['name'].'::'.$method['name']);
95+
if ($old_id) {
96+
$old_ids_to_new_urls[$old_id] =
97+
URLBuilder::getPathForMethod($method);
98+
}
99+
}
100+
}
101+
102+
foreach (APIIndex::getFunctionIndex() as $function) {
103+
Log::v('.');
104+
$old_id = idx($old_functions, $function['name']);
105+
if ($old_id) {
106+
$old_ids_to_new_urls[$old_id] =
107+
URLBuilder::getPathForFunction($function);
108+
}
109+
}
110+
111+
return $old_ids_to_new_urls;
112+
}
113+
}

src/build/BuildPaths.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
const string APIDOCS_MARKDOWN = LocalConfig::BUILD_DIR.'/api-markdown';
1111
const string APIDOCS_HTML = LocalConfig::BUILD_DIR.'/api-html';
1212
const string APIDOCS_INDEX = LocalConfig::BUILD_DIR.'/api-index.php';
13+
const string APIDOCS_LEGACY_REDIRECTS
14+
= LocalConfig::BUILD_DIR.'/api-legacy-redirects.php';
1315
const string APIDOCS_NAV_DATA = LocalConfig::BUILD_DIR.'/api-nav.js';
1416

1517
const string GUIDES_MARKDOWN = LocalConfig::ROOT.'/guides';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?hh // decl
2+
3+
namespace HHVM\UserDocumentation;
4+
5+
final class APILegacyRedirectData {
6+
public static function getIndex(
7+
): array<string, string> {
8+
/* CODEGEN GOES HERE */
9+
}
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?hh // strict
2+
3+
use HHVM\UserDocumentation\BuildPaths;
4+
use HHVM\UserDocumentation\APILegacyRedirectData;
5+
6+
require_once(BuildPaths::APIDOCS_LEGACY_REDIRECTS);
7+
8+
final class APILegacyRedirectController extends WebController {
9+
public async function respond(): Awaitable<void> {
10+
$id = $this->getRequiredStringParam('legacy_id');
11+
$url = idx(APILegacyRedirectData::getIndex(), $id);
12+
if ($url) {
13+
throw new RedirectException($url);
14+
}
15+
throw new HTTPNotFoundException();
16+
}
17+
}

src/site/Router.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ private function getRoutes(
2121
=> RedirectToGuideFirstPageController::class,
2222
'/{product:(?:hack|hhvm)}/{guide}/{page}'
2323
=> GuidePageController::class,
24+
'/manual/en/{legacy_id}.php'
25+
=> APILegacyRedirectController::class,
26+
2427
'/__content'
2528
=> WebPageContentController::class,
2629
'/s/{checksum}{file:/.+}'

0 commit comments

Comments
 (0)