'; echo '

404 — Collection Not Found

'; return; } $config = require __DIR__ . '/../config.php'; $perPage = $config['app']['per_page']; // Pagination $page = max(1, (int) ($_GET['page'] ?? 1)); $offset = ($page - 1) * $perPage; // Sort $sort = $_GET['sort'] ?? 'processed_at'; $sort = Security::sanitize($sort); $collectionNames = [ 'jfk_assassination' => 'JFK Assassination Records', 'cia_declassified' => 'CIA Declassified', 'cia_stargate' => 'CIA Stargate Program', 'cia_mkultra' => 'CIA MKUltra', 'lincoln_archives' => 'Lincoln Archives', 'house_resolutions' => 'House Resolutions', 'nasa_apod' => 'NASA APOD', 'nasa_epic' => 'NASA EPIC', 'area51_cia' => 'Area 51 / CIA Declassified', 'court_records' => 'Court Records', 'foia' => 'FOIA Releases', 'house_oversight' => 'House Oversight', ]; $collectionName = $collectionNames[$collection] ?? ucwords(str_replace('_', ' ', $collection)); $docModel = new Document(); $documents = $docModel->getByCollection($collection, $perPage, $offset, $sort); $total = $docModel->countByCollection($collection); $totalPages = (int) ceil($total / $perPage); // Attach top topic to each document if (!empty($documents)) { $db = Database::getInstance(); $docIds = array_column($documents, 'id'); $placeholders = implode(',', array_fill(0, count($docIds), '?')); try { $topicRows = $db->fetchAll( "SELECT document_id, feature_json FROM document_features WHERE document_id IN ($placeholders) AND feature_name = 'topic_distribution'", $docIds ); $topicMap = []; foreach ($topicRows as $row) { $data = json_decode($row['feature_json'], true) ?: []; arsort($data); $top = array_slice($data, 0, 2, true); $topicMap[$row['document_id']] = $top; } foreach ($documents as &$doc) { $doc['topics'] = $topicMap[$doc['id']] ?? []; } unset($doc); } catch (\PDOException $e) { // Topics not available yet } } require __DIR__ . '/../views/browse.php'; } }