Skip to content

Commit f191010

Browse files
authored
[TASK] Create redirecs for api.typo3.org (#655)
References #654
1 parent f32fca1 commit f191010

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

packages/typo3-api/redirect.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
function reverseTransformUrl($newUrl)
4+
{
5+
// Parse the URL to get the path
6+
$parsedUrl = parse_url($newUrl);
7+
$path = $parsedUrl['path'];
8+
9+
// Check if the path starts with /main/classes/
10+
if (strpos($path, 'classes/') === 0) {
11+
// Remove the 'classes/' prefix and the initial part of the path
12+
$classPart = str_replace('classes/', '', $path);
13+
14+
// Remove the .html extension
15+
$classPart = str_replace('.html', '', $classPart);
16+
17+
// Split the class part by hyphens
18+
$parts = explode('-', $classPart);
19+
20+
// Initialize an array to hold the transformed parts
21+
$transformedParts = [];
22+
23+
// Iterate over the parts to construct the old path
24+
foreach ($parts as $part) {
25+
// Convert the part to capitalized format with underscores
26+
$transformedPart = preg_replace_callback('/[A-Z]/', function ($matches) {
27+
return '_' . strtolower($matches[0]);
28+
}, $part);
29+
30+
// Add the transformed part to the array
31+
$transformedParts[] = $transformedPart;
32+
}
33+
34+
// Join the transformed parts with '_1_1_'
35+
$oldClassPart = 'class_' . implode('_1_1_', $transformedParts);
36+
37+
// Remove any consecutive underscores
38+
$oldClassPart = preg_replace('/_{2,}/', '_', $oldClassPart);
39+
40+
// Add the .html extension
41+
$oldClassPart .= '.html';
42+
43+
// Construct the old path
44+
$oldPath = '' . $oldClassPart;
45+
46+
// Reconstruct the old URL
47+
$oldUrl = $oldPath;
48+
49+
return $oldUrl;
50+
} else {
51+
// If the path does not match the expected pattern, return the original URL
52+
return $newUrl;
53+
}
54+
}
55+
56+
// Read new URLs from a JSON file
57+
$newUrlsFile = __DIR__ . '/new_urls.json';
58+
if (!file_exists($newUrlsFile)) {
59+
die("File not found: $newUrlsFile");
60+
}
61+
62+
$newUrlsJson = file_get_contents($newUrlsFile);
63+
$newUrlsArray = json_decode($newUrlsJson, true);
64+
65+
// Check if JSON was parsed correctly
66+
if (json_last_error() !== JSON_ERROR_NONE) {
67+
die("Error parsing JSON: " . json_last_error_msg());
68+
}
69+
70+
// Prepare the redirect array
71+
$redirects = [];
72+
foreach ($newUrlsArray as $newUrl) {
73+
$oldUrl = reverseTransformUrl($newUrl);
74+
echo $oldUrl;
75+
$redirects[$oldUrl] = $newUrl;
76+
}
77+
78+
// Write the redirects to a new JSON file
79+
$redirectsFile = __DIR__ . '/redirects.json';
80+
$redirectsJson = json_encode($redirects, JSON_PRETTY_PRINT);
81+
82+
if (file_put_contents($redirectsFile, $redirectsJson) === false) {
83+
die("Error writing to file: $redirectsFile");
84+
}
85+
86+
echo "Redirects have been written to $redirectsFile\n";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{% set hasLooped = false %}
2+
[
3+
{% for element in documentationSet.indexes.classes -%}
4+
{% if loop.first and hasLooped %},{% endif %}
5+
{% set hasLooped = false %}
6+
{{ _self.link(project, element) }}
7+
{% if not loop.last -%},
8+
{% else %}
9+
{% set hasLooped = true %}
10+
{% endif %}
11+
{% endfor %}
12+
13+
{% for element in documentationSet.indexes.interfaces -%}
14+
{% if loop.first and hasLooped %},{% endif %}
15+
{% set hasLooped = false %}
16+
{{ _self.link(project, element) }}
17+
{% if not loop.last -%},
18+
{% else %}
19+
{% set hasLooped = true %}
20+
{% endif %}
21+
{% endfor %}
22+
]
23+
24+
{% macro link(project, element) %}
25+
"{{ element | route('url') }}"
26+
{% endmacro %}

0 commit comments

Comments
 (0)