-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathverify-index.php
More file actions
176 lines (148 loc) Β· 5.85 KB
/
verify-index.php
File metadata and controls
176 lines (148 loc) Β· 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Verify index.html structure and links validity
*
* Checks:
* - Template placeholders are replaced
* - Version sections exist
* - Links are properly formatted
* - Relative documentation links point to existing files
*
* Environment Variables:
* - VERIFY_DOCS_BASE_URL: Base URL for documentation files (default: https://nextcloud.github.io/documentation/)
* Set to empty string to skip file existence checks
*/
# If custom domain is enabled, this should still handle the redirection
$base_url = getenv('VERIFY_DOCS_BASE_URL') ?: 'https://nextcloud.github.io/documentation/';
$html_file = 'build/index.html';
if (!file_exists($html_file)) {
fwrite(STDERR, "β οΈ $html_file not found\n");
exit(0);
}
$content = file_get_contents($html_file);
// Verify template placeholders are replaced
if (strpos($content, 'SERVER SUPPORTED BLOCK') !== false || strpos($content, 'SERVER LEGACY BLOCK') !== false) {
fwrite(STDERR, "β ERROR: Template placeholders not replaced in index.html!\n");
exit(1);
}
// Extract all href attributes
$links = [];
if (preg_match_all('/href=(["\'])([^\'"]+)\1/', $content, $matches)) {
$links = array_unique($matches[2]);
}
fprintf(STDERR, "β Found %d unique links in index.html\n", count($links));
// Categorize links
$fragment_links = array_filter($links, fn($l) => strpos($l, '#') === 0);
$external_links = array_filter($links, fn($l) => preg_match('~^https?://~', $l));
$relative_links = array_filter($links, fn($l) => strpos($l, '#') !== 0 && !preg_match('~^https?://~', $l));
fprintf(STDERR, " - Fragment links: %d\n", count($fragment_links));
fprintf(STDERR, " - External links: %d\n", count($external_links));
fprintf(STDERR, " - Relative links: %d\n", count($relative_links));
// Verify structure is present
if (!preg_match('/<h2>Nextcloud \d+/', $content)) {
fwrite(STDERR, "β ERROR: No version sections found in index.html!\n");
exit(1);
}
$version_count = preg_match_all('/<h2>Nextcloud (\d+)/', $content, $versions);
fprintf(STDERR, "β Found %d version sections: %s\n", $version_count, implode(', ', $versions[1]));
// Validate documentation links format (should be server/VERSION/)
// Check both relative links (server/latest, server/stable, etc.) and external docs links
$relative_docs_links = array_filter($relative_links, fn($l) => preg_match('~^server/(latest|stable|\d+)/~', $l));
$external_docs_links = array_filter($external_links, fn($l) => preg_match('~docs\.[^/]+/server/~', $l));
$all_docs_links = array_merge($relative_docs_links, $external_docs_links);
if (!empty($all_docs_links)) {
$invalid_docs = array_filter($all_docs_links, fn($l) =>
!preg_match('~(server|/server)/(latest|stable|\d+)/~', $l)
);
if (!empty($invalid_docs)) {
fprintf(STDERR, "β οΈ WARNING: %d potentially malformed documentation links detected:\n", count($invalid_docs));
foreach (array_slice($invalid_docs, 0, 3) as $link) {
fprintf(STDERR, " - %s\n", $link);
}
} else {
fprintf(STDERR, "β All %d documentation links are properly formatted\n", count($all_docs_links));
}
}
// Check accessibility of external links (sample first 5)
fprintf(STDERR, "\nVerifying external links...\n");
$unreachable = [];
$external_sample = array_slice($external_links, 0, 5);
foreach ($external_sample as $link) {
$context = stream_context_create([
'http' => [
'method' => 'HEAD',
'timeout' => 5,
'ignore_errors' => true,
'user_agent' => 'Mozilla/5.0'
]
]);
$response = @get_headers($link, true, $context);
if ($response === false) {
fprintf(STDERR, " β οΈ %s - connection failed (may be temporary)\n", $link);
} else {
$status_line = $response[0] ?? '';
if (preg_match('/\d{3}/', $status_line, $match)) {
$status = (int)$match[0];
if ($status >= 400) {
$unreachable[] = [$link, $status];
fprintf(STDERR, " β οΈ %s returned %d\n", $link, $status);
} else {
fprintf(STDERR, " β %s\n", $link);
}
}
}
}
if (count($unreachable) >= 3) {
fprintf(STDERR, "\nβ ERROR: %d links are unreachable!\n", count($unreachable));
exit(1);
}
// Test documentation file existence on deployed site (if base URL is configured)
if ($base_url) {
fprintf(STDERR, "\nVerifying documentation files on %s...\n", $base_url);
$relative_docs = array_filter($relative_links, fn($l) => preg_match('~^server/(latest|stable|\d+)/~', $l));
if (!empty($relative_docs)) {
$missing_files = [];
$timeout_files = [];
foreach ($relative_docs as $relative_path) {
$full_url = rtrim($base_url, '/') . '/' . $relative_path;
$context = stream_context_create([
'http' => [
'method' => 'HEAD',
'timeout' => 5,
'ignore_errors' => true,
'user_agent' => 'Mozilla/5.0'
]
]);
$response = @get_headers($full_url, true, $context);
if ($response === false) {
$timeout_files[] = $relative_path;
fprintf(STDERR, " β οΈ %s - timeout/offline\n", $relative_path);
} else {
$status_line = $response[0] ?? '';
if (preg_match('/\d{3}/', $status_line, $match)) {
$status = (int)$match[0];
if ($status === 404) {
$missing_files[] = $relative_path;
fprintf(STDERR, " β %s - 404 Not Found\n", $relative_path);
} else if ($status >= 400) {
fprintf(STDERR, " β οΈ %s - HTTP %d\n", $relative_path, $status);
} else if ($status === 200 || $status === 301 || $status === 302) {
fprintf(STDERR, " β %s\n", $relative_path);
}
}
}
}
if (!empty($missing_files)) {
fprintf(STDERR, "\nβ ERROR: %d documentation files not found:\n", count($missing_files));
foreach ($missing_files as $file) {
fprintf(STDERR, " - %s\n", $file);
}
exit(1);
}
if (count($timeout_files) >= count($relative_docs) / 2) {
fprintf(STDERR, "\nβ οΈ WARNING: %d/%d files timed out (site may be offline)\n", count($timeout_files), count($relative_docs));
}
}
}
fprintf(STDERR, "\nβ index.html validation passed\n");
exit(0);