File size: 2,102 Bytes
457056e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

declare(strict_types=1);

class NetworkController
{
    public function index(): void
    {
        $db = Database::getInstance();

        // Get available sections
        $sections = $db->fetchAll(
            "SELECT DISTINCT source_section FROM entity_relationships ORDER BY source_section"
        );

        $section = $_GET['section'] ?? '';
        $section = Security::sanitize($section);
        $entityType = $_GET['type'] ?? 'PERSON';
        $entityType = in_array($entityType, ['PERSON', 'ORG']) ? $entityType : 'PERSON';
        $minCount = max(2, (int)($_GET['min'] ?? 10));

        $relationships = [];
        $nodes = [];

        if ($section) {
            // Get top relationships for this section
            $relationships = $db->fetchAll(
                "SELECT entity_a, entity_b, co_occurrence_count, document_count, sample_doc_ids
                 FROM entity_relationships
                 WHERE source_section = :section AND entity_a_type = :type
                 AND co_occurrence_count >= :minCount
                 ORDER BY co_occurrence_count DESC
                 LIMIT 200",
                ['section' => $section, 'type' => $entityType, 'minCount' => $minCount]
            );

            // Build node set with degree counts
            $nodeDegree = [];
            foreach ($relationships as $rel) {
                $a = $rel['entity_a'];
                $b = $rel['entity_b'];
                $nodeDegree[$a] = ($nodeDegree[$a] ?? 0) + (int)$rel['co_occurrence_count'];
                $nodeDegree[$b] = ($nodeDegree[$b] ?? 0) + (int)$rel['co_occurrence_count'];
            }
            arsort($nodeDegree);
            $nodes = $nodeDegree;
        }

        // Stats
        $stats = $db->fetchAll(
            "SELECT entity_a_type as type, source_section as section,
                    COUNT(*) as rels, SUM(co_occurrence_count) as cooccurrences
             FROM entity_relationships
             GROUP BY entity_a_type, source_section
             ORDER BY rels DESC"
        );

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