|
| 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"; |
0 commit comments