Download web/src/controllers/BrowseController.php from datamatters24/research-document-archive: direct link, hf CLI and curl.
- Browser
- Download file 3.89 kB
-
https://huggingface.co/datamatters24/research-document-archive/resolve/main/web/src/controllers/BrowseController.php
- Command line
-
hf download hf://datamatters24/research-document-archive/web/src/controllers/BrowseController.php
-
curl -L -o BrowseController.php https://huggingface.co/datamatters24/research-document-archive/resolve/main/web/src/controllers/BrowseController.php
3.89 kB
| declare(strict_types=1); | |
| class BrowseController | |
| { | |
| /** | |
| * List all collections with their document counts. | |
| */ | |
| public function index(): void | |
| { | |
| // /browse with no collection — redirect to home which shows all collections | |
| header('Location: /'); | |
| http_response_code(302); | |
| } | |
| /** | |
| * Display a paginated list of documents within a specific collection. | |
| */ | |
| public function collection(string $collection): void | |
| { | |
| $collection = Security::sanitize($collection); | |
| // Validate the collection name against known sections | |
| $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']; | |
| // 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'; | |
| } | |
| } | |