File size: 3,893 Bytes
1e94321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

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 &mdash; 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';
    }
}