| <?php |
|
|
| declare(strict_types=1); |
|
|
| class BrowseController |
| { |
| |
| |
| |
| public function index(): void |
| { |
| |
| header('Location: /'); |
| http_response_code(302); |
| } |
|
|
| |
| |
| |
| public function collection(string $collection): void |
| { |
| $collection = Security::sanitize($collection); |
|
|
| |
| $validCollections = [ |
| 'jfk_assassination', |
| 'cia_declassified', |
| 'cia_stargate', |
| 'cia_mkultra', |
| 'lincoln_archives', |
| 'house_resolutions', |
| 'doj_disclosures', |
| 'nasa_apod', |
| 'nasa_epic', |
| 'area51_cia', |
| 'court_records', |
| 'foia', |
| 'house_oversight', |
| ]; |
|
|
| if (!in_array($collection, $validCollections, true)) { |
| http_response_code(404); |
| echo '<!DOCTYPE html><html><head><title>Not Found</title></head>'; |
| echo '<body><h1>404 — Collection Not Found</h1></body></html>'; |
| return; |
| } |
|
|
| $config = require __DIR__ . '/../config.php'; |
| $perPage = $config['app']['per_page']; |
|
|
| |
| $page = max(1, (int) ($_GET['page'] ?? 1)); |
| $offset = ($page - 1) * $perPage; |
|
|
| |
| $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); |
|
|
| |
| 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) { |
| |
| } |
| } |
|
|
| require __DIR__ . '/../views/browse.php'; |
| } |
| } |
|
|