| 1 | <?php |
| 2 | require_once __DIR__ . '/folderNode.php'; |
| 3 | |
| 4 | header('Content-Type: application/json; charset=utf-8'); |
| 5 | header('Access-Control-Allow-Origin: *'); |
| 6 | header('Access-Control-Allow-Methods: GET, HEAD, OPTIONS'); |
| 7 | header('Access-Control-Allow-Headers: *'); |
| 8 | header('X-Robots-Tag: all'); |
| 9 | |
| 10 | if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'OPTIONS') { |
| 11 | http_response_code(204); |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | $dataRoot = __DIR__ . DIRECTORY_SEPARATOR . 'data'; |
| 16 | $chaptersRoot = $dataRoot . DIRECTORY_SEPARATOR . 'chapters'; |
| 17 | |
| 18 | $allJson = loadAllJson($dataRoot); |
| 19 | $visited = []; |
| 20 | $baseUrl = getBaseUrl(); |
| 21 | |
| 22 | $chaptersTree = loadFolderTree($chaptersRoot); |
| 23 | $chapters = $chaptersTree->jsonRecursive(); |
| 24 | |
| 25 | usort($chapters, fn($a, $b) => ($a->number ?? 0) <=> ($b->number ?? 0)); |
| 26 | |
| 27 | foreach ($chapters as $chapterObj) { |
| 28 | $chapter = json_decode(json_encode($chapterObj), true); |
| 29 | $chapterLink = normalizeWikiPath($chapter['wikiPath'] ?? ''); |
| 30 | $relPath = normalizeDataRelPath($chapterLink); |
| 31 | if ($relPath !== '') |
| 32 | $visited[$relPath] = true; |
| 33 | } |
| 34 | |
| 35 | $index = []; |
| 36 | foreach ($chapters as $chapterObj) { |
| 37 | $chapter = json_decode(json_encode($chapterObj), true); |
| 38 | $chapterLink = normalizeWikiPath($chapter['wikiPath'] ?? ''); |
| 39 | |
| 40 | $index[] = buildChapterNode($chapter, $chapterLink, $allJson, $visited, $baseUrl); |
| 41 | } |
| 42 | |
| 43 | echo json_encode($index, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| 44 | |
| 45 | function loadAllJson(string $dataRoot): array |
| 46 | { |
| 47 | $map = []; |
| 48 | |
| 49 | $it = new RecursiveIteratorIterator( |
| 50 | new RecursiveDirectoryIterator($dataRoot, FilesystemIterator::SKIP_DOTS) |
| 51 | ); |
| 52 | |
| 53 | foreach ($it as $file) { |
| 54 | /** @var SplFileInfo $file */ |
| 55 | if (strtolower($file->getExtension()) !== 'json') |
| 56 | continue; |
| 57 | |
| 58 | $path = $file->getPathname(); |
| 59 | $rel = str_replace('\\', '/', substr($path, strlen($dataRoot))); |
| 60 | if ($rel === '') |
| 61 | $rel = '/' . $file->getFilename(); |
| 62 | if ($rel[0] !== '/') |
| 63 | $rel = '/' . $rel; |
| 64 | |
| 65 | try { |
| 66 | $raw = file_get_contents($path); |
| 67 | if ($raw === false) |
| 68 | continue; |
| 69 | $json = json_decode($raw, true, 512, JSON_THROW_ON_ERROR); |
| 70 | if (!is_array($json)) |
| 71 | continue; |
| 72 | } catch (Throwable $e) { |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | $json['_relPath'] = $rel; |
| 77 | $map[$rel] = $json; |
| 78 | } |
| 79 | |
| 80 | return $map; |
| 81 | } |
| 82 | |
| 83 | function buildChapterNode(array $chapter, string $chapterLink, array $allJson, array &$visited, string $baseUrl): array |
| 84 | { |
| 85 | $name = $chapterLink !== '' ? basename($chapterLink, '.json') : ($chapter['name'] ?? 'chapter'); |
| 86 | $fullChapterLink = $chapterLink !== '' ? $baseUrl . $chapterLink : ''; |
| 87 | |
| 88 | $node = [ |
| 89 | 'name' => $name, |
| 90 | 'type' => $chapter['type'] ?? 'chapter', |
| 91 | 'pageType' => 'page', |
| 92 | 'link' => $fullChapterLink, |
| 93 | 'children' => [], |
| 94 | ]; |
| 95 | |
| 96 | $folderIncludes = extractFolderIncludes($chapter, ['subheaders']); |
| 97 | |
| 98 | $children = []; |
| 99 | |
| 100 | foreach (($chapter['subheaders'] ?? []) as $subheader) { |
| 101 | if (!is_array($subheader)) |
| 102 | continue; |
| 103 | |
| 104 | $subNode = [ |
| 105 | 'name' => $subheader['name'] ?? 'subheader', |
| 106 | 'type' => 'subheader', |
| 107 | 'pageType' => 'page child element', |
| 108 | 'link' => $fullChapterLink, |
| 109 | 'children' => [], |
| 110 | ]; |
| 111 | |
| 112 | $subIncludes = extractFolderIncludes($subheader); |
| 113 | $subNode['children'] = mergeChildren( |
| 114 | buildChildrenFromFolderIncludes($subIncludes, $allJson, $visited, $baseUrl) |
| 115 | ); |
| 116 | |
| 117 | $children[] = $subNode; |
| 118 | } |
| 119 | |
| 120 | $children = mergeChildren( |
| 121 | $children, |
| 122 | buildChildrenFromFolderIncludes($folderIncludes, $allJson, $visited, $baseUrl) |
| 123 | ); |
| 124 | |
| 125 | $node['children'] = $children; |
| 126 | |
| 127 | return $node; |
| 128 | } |
| 129 | |
| 130 | function buildChildrenFromFolderIncludes(array $folders, array $allJson, array &$visited, string $baseUrl): array |
| 131 | { |
| 132 | $children = []; |
| 133 | $rels = array_keys($allJson); |
| 134 | sort($rels, SORT_NATURAL | SORT_FLAG_CASE); |
| 135 | foreach ($folders as $folder) { |
| 136 | $folderRel = '/' . trim(str_replace('\\', '/', $folder), '/'); |
| 137 | foreach ($rels as $rel) { |
| 138 | if (str_starts_with($rel, $folderRel . '/')) { |
| 139 | $children[] = buildDataLinkNode($rel, '', $allJson, $visited, $baseUrl); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return $children; |
| 145 | } |
| 146 | |
| 147 | function buildDataLinkNode(string $relPath, string $label, array $allJson, array &$visited, string $baseUrl): array |
| 148 | { |
| 149 | $link = $baseUrl . '/data' . $relPath; |
| 150 | $json = $allJson[$relPath] ?? null; |
| 151 | |
| 152 | $name = $label; |
| 153 | $type = 'linkedData'; |
| 154 | |
| 155 | if (is_array($json)) { |
| 156 | $type = $json['type'] ?? 'data'; |
| 157 | if ($name === '') { |
| 158 | $name = $json['name'] ?? ($json['title'] ?? basename($relPath, '.json')); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | $node = [ |
| 163 | 'name' => $name !== '' ? $name : basename($relPath, '.json'), |
| 164 | 'type' => $type, |
| 165 | 'pageType' => 'data link', |
| 166 | 'link' => $link, |
| 167 | 'children' => [], |
| 168 | ]; |
| 169 | |
| 170 | if (!is_array($json)) |
| 171 | return $node; |
| 172 | |
| 173 | if (isset($visited[$relPath])) |
| 174 | return $node; |
| 175 | |
| 176 | $visited[$relPath] = true; |
| 177 | |
| 178 | $folderChildren = buildChildrenFromFolderIncludes(extractFolderIncludes($json), $allJson, $visited, $baseUrl); |
| 179 | $node['children'] = mergeChildren($folderChildren); |
| 180 | |
| 181 | return $node; |
| 182 | } |
| 183 | |
| 184 | function normalizeWikiPath(string $path): string |
| 185 | { |
| 186 | $path = str_replace('\\', '/', $path); |
| 187 | if ($path === '') |
| 188 | return ''; |
| 189 | |
| 190 | if (!str_starts_with($path, '/')) |
| 191 | $path = '/' . $path; |
| 192 | |
| 193 | return $path; |
| 194 | } |
| 195 | |
| 196 | function getBaseUrl(): string |
| 197 | { |
| 198 | $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; |
| 199 | $host = $_SERVER['HTTP_HOST'] ?? 'localhost'; |
| 200 | $scriptName = $_SERVER['SCRIPT_NAME'] ?? '/dataIndex.php'; |
| 201 | $dir = rtrim(str_replace('\\', '/', dirname($scriptName)), '/'); |
| 202 | return $scheme . '://' . $host . ($dir === '' ? '' : $dir); |
| 203 | } |
| 204 | |
| 205 | function normalizeDataRelPath(string $path): string |
| 206 | { |
| 207 | $path = trim($path); |
| 208 | if ($path === '') |
| 209 | return ''; |
| 210 | |
| 211 | $path = str_replace('\\', '/', $path); |
| 212 | if (str_starts_with($path, '/data/')) |
| 213 | $path = substr($path, strlen('/data')); |
| 214 | if (str_starts_with($path, 'data/')) |
| 215 | $path = substr($path, strlen('data')); |
| 216 | |
| 217 | if (!str_starts_with($path, '/')) |
| 218 | $path = '/' . $path; |
| 219 | |
| 220 | return $path; |
| 221 | } |
| 222 | |
| 223 | function extractFolderIncludes(mixed $value, array $skipKeys = []): array |
| 224 | { |
| 225 | $folders = []; |
| 226 | |
| 227 | if (is_array($value)) { |
| 228 | foreach ($value as $k => $v) { |
| 229 | if (in_array($k, $skipKeys, true)) |
| 230 | continue; |
| 231 | $folders = array_merge($folders, extractFolderIncludes($v, $skipKeys)); |
| 232 | } |
| 233 | return $folders; |
| 234 | } |
| 235 | |
| 236 | if (is_string($value)) { |
| 237 | return extractFolderIncludesFromString($value); |
| 238 | } |
| 239 | |
| 240 | return $folders; |
| 241 | } |
| 242 | |
| 243 | function extractFolderIncludesFromString(string $text): array |
| 244 | { |
| 245 | $folders = []; |
| 246 | $pattern = '/\[\[\[\s*([a-zA-Z0-9_.-]+)\s*\]\]\]/'; |
| 247 | |
| 248 | if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) { |
| 249 | foreach ($matches as $m) { |
| 250 | $folders[] = $m[1]; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return $folders; |
| 255 | } |
| 256 | |
| 257 | function mergeChildren(array ...$groups): array |
| 258 | { |
| 259 | $merged = []; |
| 260 | $seen = []; |
| 261 | |
| 262 | foreach ($groups as $children) { |
| 263 | foreach ($children as $child) { |
| 264 | if (!is_array($child)) |
| 265 | continue; |
| 266 | |
| 267 | $key = ($child['link'] ?? '') . '|' . ($child['name'] ?? '') . '|' . ($child['pageType'] ?? ''); |
| 268 | if (isset($seen[$key])) |
| 269 | continue; |
| 270 | |
| 271 | $seen[$key] = true; |
| 272 | $merged[] = $child; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return $merged; |
| 277 | } |