File size: 4,299 Bytes
0919819
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3fdff9
0919819
 
d3fdff9
0919819
 
 
d3fdff9
0919819
 
 
 
 
 
 
d3fdff9
 
0919819
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
<?php

declare(strict_types=1);

class DocumentController
{
    /**
     * Show a single document with its page list, entity summary, and PDF viewer.
     */
    public function show(string $id): void
    {
        $docId = Security::validateId($id);
        if ($docId === false) {
            http_response_code(400);
            echo '<!DOCTYPE html><html><head><title>Bad Request</title></head>';
            echo '<body><h1>400 &mdash; Invalid Document ID</h1></body></html>';
            return;
        }

        $docModel = new Document();
        $document = $docModel->findById($docId);

        if ($document === null) {
            http_response_code(404);
            echo '<!DOCTYPE html><html><head><title>Not Found</title></head>';
            echo '<body><h1>404 &mdash; Document Not Found</h1></body></html>';
            return;
        }

        // Page metadata (not full text)
        $pageModel = new Page();
        $pages     = $pageModel->getByDocument($docId);

        // Entity summary: count by entity type for this document
        $db = Database::getInstance();
        $entities = [];
        try {
            $entities = $db->fetchAll(
                'SELECT entity_type, COUNT(*)::int AS entity_count,
                        array_agg(DISTINCT entity_text ORDER BY entity_text) AS examples
                 FROM entities
                 WHERE document_id = :docId
                 GROUP BY entity_type
                 ORDER BY entity_count DESC',
                ['docId' => $docId]
            );
        } catch (\PDOException $e) {
            // Entities table may be empty or not fully populated
        }

        // Crisis events for this document
        $crisisEvents = [];
        try {
            $eventModel   = new Event();
            $crisisEvents = $eventModel->getForDocument($docId);
        } catch (\PDOException $e) {
            // Table may not exist yet
        }

        // Topic classifications
        $topics = [];
        try {
            $topics = $db->fetchAll(
                "SELECT feature_json FROM document_features
                 WHERE document_id = :docId AND feature_name = 'topic_distribution'",
                ['docId' => $docId]
            );
            if (!empty($topics[0]['feature_json'])) {
                $topicData = json_decode($topics[0]['feature_json'], true) ?: [];
                arsort($topicData);
                $topics = $topicData;
            } else {
                $topics = [];
            }
        } catch (\PDOException $e) {
            $topics = [];
        }

        // Keywords
        $keywords = [];
        try {
            $keywords = $db->fetchAll(
                'SELECT keyword, score FROM document_keywords
                 WHERE document_id = :docId AND keyword != :placeholder
                 ORDER BY score DESC LIMIT 15',
                ['docId' => $docId, 'placeholder' => '_no_keywords_']
            );
        } catch (\PDOException $e) {
            $keywords = [];
        }

        // Forensic metadata, redaction summary, sentiment
        $forensic = [];
        $redactionSummary = null;
        $sentiment = null;
        try {
            $forensicRows = $db->fetchAll(
                "SELECT feature_name, feature_json FROM document_features
                 WHERE document_id = :docId AND feature_name IN ('forensic_metadata', 'redaction_summary', 'sentiment')",
                ['docId' => $docId]
            );
            foreach ($forensicRows as $row) {
                if ($row['feature_name'] === 'forensic_metadata') {
                    $forensic = json_decode($row['feature_json'], true) ?: [];
                } elseif ($row['feature_name'] === 'redaction_summary') {
                    $redactionSummary = json_decode($row['feature_json'], true) ?: [];
                } elseif ($row['feature_name'] === 'sentiment') {
                    $sentiment = json_decode($row['feature_json'], true) ?: [];
                }
            }
        } catch (\PDOException $e) {
            // Not available yet
        }

        // PDF viewer URL
        $pdfUrl    = '/pdf/' . $docId;
        $viewerUrl = '/assets/vendor/pdfjs/web/viewer.html?file=' . urlencode($pdfUrl);

        require __DIR__ . '/../views/document.php';
    }
}