|
1 | 1 | <?php |
2 | 2 | require_once 'server-block.php'; |
3 | 3 |
|
| 4 | +/** |
| 5 | + * Get the GitHub API headers with optional authentication |
| 6 | + */ |
| 7 | +function get_github_headers(): string { |
| 8 | + $headers = 'User-Agent: Nextcloud Documentation Builder'; |
| 9 | + if ($token = getenv('GITHUB_TOKEN')) { |
| 10 | + $headers .= "\r\nAuthorization: token $token"; |
| 11 | + } |
| 12 | + return $headers; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Get the repository name for a given version |
| 17 | + * Nextcloud moved to nextcloud-releases/server starting with version 32 |
| 18 | + */ |
| 19 | +function get_repo_for_version(int $version): string { |
| 20 | + return $version >= 32 ? 'nextcloud-releases/server' : 'nextcloud/server'; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Check if a version is officially released by querying the GitHub tag |
| 25 | + */ |
| 26 | +function is_version_released(int $version): bool { |
| 27 | + $repo = get_repo_for_version($version); |
| 28 | + $url = sprintf('https://api.github.com/repos/%s/releases/tags/v%d.0.0', $repo, $version); |
| 29 | + |
| 30 | + $context = stream_context_create([ |
| 31 | + 'http' => [ |
| 32 | + 'header' => get_github_headers(), |
| 33 | + 'timeout' => 5, |
| 34 | + 'ignore_errors' => true |
| 35 | + ] |
| 36 | + ]); |
| 37 | + |
| 38 | + $response = @file_get_contents($url, false, $context); |
| 39 | + |
| 40 | + if (isset($http_response_header) && is_array($http_response_header)) { |
| 41 | + return strpos($http_response_header[0] ?? '', '200') !== false; |
| 42 | + } |
| 43 | + |
| 44 | + return $response !== false; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Fetch release date for a version from GitHub API |
| 49 | + */ |
| 50 | +function get_release_date(int $version): ?int { |
| 51 | + $repo = get_repo_for_version($version); |
| 52 | + $url = sprintf('https://api.github.com/repos/%s/releases/tags/v%d.0.0', $repo, $version); |
| 53 | + |
| 54 | + $context = stream_context_create([ |
| 55 | + 'http' => [ |
| 56 | + 'header' => get_github_headers(), |
| 57 | + 'timeout' => 5 |
| 58 | + ] |
| 59 | + ]); |
| 60 | + |
| 61 | + $response = @file_get_contents($url, false, $context); |
| 62 | + if ($response === false) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + $data = json_decode($response, true); |
| 67 | + $publishedAt = $data['published_at'] ?? $data['created_at'] ?? null; |
| 68 | + return $publishedAt ? strtotime($publishedAt) : null; |
| 69 | +} |
| 70 | + |
| 71 | +// Parse and validate command-line arguments |
4 | 72 | /** @var int[] */ |
5 | 73 | $branches = array_slice($argv, 1); |
6 | 74 | $branches = array_filter($branches, fn($b) => is_numeric($b)); |
7 | 75 | rsort($branches, SORT_NUMERIC); |
8 | | - |
9 | | -// We added this script when we had only 12 and above. |
10 | 76 | $branches = array_filter($branches, fn($b) => $b >= 12); |
11 | 77 |
|
12 | 78 | if (empty($branches)) { |
13 | 79 | fwrite(STDERR, "Error: No valid stable branches were provided. Please pass at least one numeric branch >= 12.\n"); |
14 | 80 | exit(1); |
15 | 81 | } |
16 | 82 |
|
17 | | -// Generate current development version section |
18 | | -$sections = [generate_section(($branches[0] + 1), 0)]; |
| 83 | +// Fetch release information from GitHub |
| 84 | +$released_branches = []; |
| 85 | +$oneYearAgo = time() - (365 * 24 * 60 * 60); |
| 86 | +$foundOutOfSupport = false; |
19 | 87 |
|
20 | | -// Generate stable version section |
21 | | -foreach ($branches as $index => $v) { |
22 | | - $sections[] = generate_section($v, $index + 1); |
| 88 | +foreach ($branches as $branch) { |
| 89 | + if ($foundOutOfSupport) { |
| 90 | + fwrite(STDERR, "⊘ Version $branch not checked (older than first out-of-support version)\n"); |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + if (!is_version_released($branch)) { |
| 95 | + fwrite(STDERR, "✗ Version $branch not released (tag v$branch.0.0 not found)\n"); |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + $releaseTime = get_release_date($branch); |
| 100 | + if ($releaseTime === null) { |
| 101 | + $releaseTime = time(); |
| 102 | + } |
| 103 | + |
| 104 | + $released_branches[$branch] = $releaseTime; |
| 105 | + $releaseType = $releaseTime >= $oneYearAgo ? 'stable' : 'unsupported'; |
| 106 | + fwrite(STDERR, "✓ Version $branch released on " . date('Y-m-d', $releaseTime) . " ($releaseType)\n"); |
| 107 | + |
| 108 | + if ($releaseType === 'unsupported') { |
| 109 | + $foundOutOfSupport = true; |
| 110 | + } |
23 | 111 | } |
24 | 112 |
|
25 | | -// Insert into index.html |
26 | | -$index = file_get_contents(__DIR__ . '/index-template.html'); |
| 113 | +// Determine development version |
| 114 | +if (isset($released_branches[$branches[0]])) { |
| 115 | + $devVersion = $branches[0] + 1; |
| 116 | + $devStatus = 'development'; |
| 117 | +} else { |
| 118 | + $devVersion = $branches[0]; |
| 119 | + $devStatus = 'upcoming'; |
| 120 | +} |
27 | 121 |
|
28 | | -$supported = array_slice($sections, 0, 4); |
29 | | -$legacy = array_slice($sections, 4); |
| 122 | +fwrite(STDERR, "⬗ Version $devVersion ($devStatus)\n"); |
30 | 123 |
|
31 | | -$index = str_replace( |
32 | | - '<!-- SERVER SUPPORTED BLOCK -->', |
33 | | - implode("\n", $supported), |
34 | | - $index |
35 | | -); |
| 124 | +// Collect released stable versions within support window |
| 125 | +$stableVersions = []; |
| 126 | +foreach ($branches as $branch) { |
| 127 | + if (isset($released_branches[$branch]) && $released_branches[$branch] >= $oneYearAgo) { |
| 128 | + $stableVersions[] = $branch; |
| 129 | + } elseif (isset($released_branches[$branch])) { |
| 130 | + // Once we hit an unsupported version, stop |
| 131 | + break; |
| 132 | + } |
| 133 | +} |
36 | 134 |
|
37 | | -$index = str_replace( |
38 | | - '<!-- SERVER LEGACY BLOCK -->', |
39 | | - implode("\n", $legacy), |
40 | | - $index |
41 | | -); |
| 135 | +// Generate sections with proper indices |
| 136 | +$supported = [generate_section($devVersion, 0)]; |
| 137 | +foreach ($stableVersions as $idx => $version) { |
| 138 | + // Index 3 for the oldest supported version if there are multiple |
| 139 | + $index = ($idx + 1 === count($stableVersions) && count($stableVersions) > 1) ? 3 : $idx + 1; |
| 140 | + $supported[] = generate_section($version, $index); |
| 141 | +} |
42 | 142 |
|
43 | | -$index = str_replace( |
44 | | - '<!-- CURRENT_YEAR -->', |
45 | | - date('Y'), |
46 | | - $index |
47 | | -); |
| 143 | +// Generate legacy sections (released but outside support window) |
| 144 | +$legacy = []; |
| 145 | +foreach ($branches as $branch) { |
| 146 | + if (isset($released_branches[$branch]) && !in_array($branch, $stableVersions)) { |
| 147 | + $legacy[] = generate_section($branch, 1); |
| 148 | + } |
| 149 | +} |
48 | 150 |
|
| 151 | +// Generate and write final index.html |
| 152 | +$index = file_get_contents(__DIR__ . '/index-template.html'); |
| 153 | +$index = str_replace('<!-- SERVER SUPPORTED BLOCK -->', implode("\n", $supported), $index); |
| 154 | +$index = str_replace('<!-- SERVER LEGACY BLOCK -->', implode("\n", $legacy), $index); |
| 155 | +$index = str_replace('<!-- CURRENT_YEAR -->', date('Y'), $index); |
49 | 156 | file_put_contents(__DIR__ . '/index.html', $index); |
0 commit comments