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