{
"tool_response": {
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Infrastructure\/SemanticExplorerRepository.php",
"oldString": " public function getOntologyClassesForSelect(): array\n {\n return $this->db->query('SELECT id, name FROM ontology_classes ORDER BY name')->fetchAll();\n }\n}",
"newString": " public function getOntologyClassesForSelect(): array\n {\n return $this->db->query('SELECT id, name FROM ontology_classes ORDER BY name')->fetchAll();\n }\n\n \/\/ =========================================================================\n \/\/ GRAPH\n \/\/ =========================================================================\n\n \/**\n * Returns graph data for D3.js visualization.\n *\n * @return array{nodes: array, links: array, stats: array}\n *\/\n public function getGraphData(): array\n {\n \/\/ Get all entities\n $entities = $this->db->query(\n 'SELECT id, name, type FROM entities ORDER BY name'\n )->fetchAll();\n\n \/\/ Get all relations\n $relations = $this->db->query(\n 'SELECT source_entity_id, target_entity_id, relation_type, strength\n FROM entity_relations'\n )->fetchAll();\n\n \/\/ Build node index for link resolution\n $nodeIndex = [];\n $nodes = [];\n foreach ($entities as $i => $entity) {\n $nodeIndex[$entity['id']] = $i;\n $nodes[] = [\n 'id' => 'entity_' . $entity['id'],\n 'label' => $entity['name'],\n 'type' => strtoupper($entity['type'] ?? 'OTHER'),\n 'entityId' => (int) $entity['id'],\n ];\n }\n\n \/\/ Build links with index references\n $links = [];\n foreach ($relations as $relation) {\n $sourceId = $relation['source_entity_id'];\n $targetId = $relation['target_entity_id'];\n\n \/\/ Skip if entity not found\n if (!isset($nodeIndex[$sourceId]) || !isset($nodeIndex[$targetId])) {\n continue;\n }\n\n $links[] = [\n 'source' => $nodeIndex[$sourceId],\n 'target' => $nodeIndex[$targetId],\n 'type' => $relation['relation_type'],\n 'strength' => (float) ($relation['strength'] ?? 1.0),\n ];\n }\n\n \/\/ Get unique types for stats\n $entityTypes = array_unique(array_column($nodes, 'type'));\n $relationTypes = array_unique(array_column($links, 'type'));\n\n return [\n 'nodes' => $nodes,\n 'links' => $links,\n 'stats' => [\n 'nodes' => count($nodes),\n 'links' => count($links),\n 'entityTypes' => count($entityTypes),\n 'relationTypes' => count($relationTypes),\n ],\n ];\n }\n}",
"originalFile": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Infrastructure;\n\n\/\/ @responsibility: Persistenz für Semantic-Explorer (Entitäten, Relationen, Taxonomie, Ontologie)\n\nuse Infrastructure\\Config\\DatabaseFactory;\n\nclass SemanticExplorerRepository\n{\n private \\PDO $db;\n\n public function __construct(?\\PDO $pdo = null)\n {\n $this->db = $pdo ?? DatabaseFactory::content();\n }\n\n \/\/ =========================================================================\n \/\/ DOCUMENTS\n \/\/ =========================================================================\n\n public function getDocumentStats(): array\n {\n return $this->db->query(\n 'SELECT\n COUNT(*) as total,\n SUM(CASE WHEN status = \"done\" THEN 1 ELSE 0 END) as processed,\n SUM(CASE WHEN status = \"error\" THEN 1 ELSE 0 END) as errors\n FROM documents'\n )->fetch();\n }\n\n public function getDocuments(): array\n {\n return $this->db->query(\n 'SELECT d.id, d.filename, d.folder_path, d.mime_type, d.file_size,\n d.status, d.imported_at, d.processed_at,\n (SELECT COUNT(*) FROM chunks WHERE document_id = d.id) as chunk_count\n FROM documents d\n ORDER BY d.imported_at DESC'\n )->fetchAll();\n }\n\n public function getDocumentsFiltered(string $status = '', string $search = ''): array\n {\n $sql = 'SELECT d.id, d.filename, d.folder_path, d.source_path, d.mime_type,\n d.file_size, d.status, d.imported_at, d.processed_at, d.error_message,\n (SELECT COUNT(*) FROM chunks WHERE document_id = d.id) as chunk_count,\n (SELECT COALESCE(SUM(token_count), 0) FROM chunks WHERE document_id = d.id) as token_count\n FROM documents d WHERE 1=1';\n\n $params = [];\n\n if ($status !== '') {\n $sql .= ' AND d.status = :status';\n $params['status'] = $status;\n }\n\n if ($search !== '') {\n $sql .= ' AND (d.filename LIKE :search OR d.source_path LIKE :search2)';\n $params['search'] = '%' . $search . '%';\n $params['search2'] = '%' . $search . '%';\n }\n\n $sql .= ' ORDER BY d.imported_at DESC';\n\n $stmt = $this->db->prepare($sql);\n $stmt->execute($params);\n\n return $stmt->fetchAll();\n }\n\n public function getDocument(int $id): ?array\n {\n $stmt = $this->db->prepare('SELECT * FROM documents WHERE id = :id');\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result === false ? null : $result;\n }\n\n \/\/ =========================================================================\n \/\/ CHUNKS\n \/\/ =========================================================================\n\n public function getChunkStats(): array\n {\n return $this->db->query(\n 'SELECT\n COUNT(*) as total,\n COALESCE(SUM(token_count), 0) as tokens,\n SUM(CASE WHEN qdrant_id IS NOT NULL THEN 1 ELSE 0 END) as embedded\n FROM chunks'\n )->fetch();\n }\n\n public function getRecentChunks(int $limit = 5): array\n {\n $stmt = $this->db->prepare(\n 'SELECT c.id, c.content, c.token_count, c.created_at, c.qdrant_id, d.filename\n FROM chunks c\n JOIN documents d ON c.document_id = d.id\n ORDER BY c.created_at DESC\n LIMIT :limit'\n );\n $stmt->bindValue(':limit', $limit, \\PDO::PARAM_INT);\n $stmt->execute();\n\n return $stmt->fetchAll();\n }\n\n public function getChunksForDocument(int $documentId): array\n {\n $stmt = $this->db->prepare(\n 'SELECT c.id, c.chunk_index, c.content, c.token_count, c.heading_path,\n c.metadata, c.qdrant_id, c.created_at\n FROM chunks c\n WHERE c.document_id = :id\n ORDER BY c.chunk_index'\n );\n $stmt->execute(['id' => $documentId]);\n\n return $stmt->fetchAll();\n }\n\n public function getChunksFiltered(string $search = '', string $embedded = '', int $limit = 50, int $offset = 0): array\n {\n $sql = 'SELECT c.id, c.chunk_index, c.content, c.token_count, c.qdrant_id, c.created_at,\n d.filename, d.id as document_id\n FROM chunks c\n JOIN documents d ON c.document_id = d.id\n WHERE 1=1';\n\n $params = [];\n\n if ($search !== '') {\n $sql .= ' AND c.content LIKE :search';\n $params['search'] = '%' . $search . '%';\n }\n\n if ($embedded === 'yes') {\n $sql .= ' AND c.qdrant_id IS NOT NULL';\n } elseif ($embedded === 'no') {\n $sql .= ' AND c.qdrant_id IS NULL';\n }\n\n $sql .= ' ORDER BY c.created_at DESC LIMIT ' . $limit . ' OFFSET ' . $offset;\n\n $stmt = $this->db->prepare($sql);\n $stmt->execute($params);\n\n return $stmt->fetchAll();\n }\n\n public function getChunksCount(string $search = '', string $embedded = ''): int\n {\n $sql = 'SELECT COUNT(*) FROM chunks c\n JOIN documents d ON c.document_id = d.id\n WHERE 1=1';\n\n $params = [];\n\n if ($search !== '') {\n $sql .= ' AND c.content LIKE :search';\n $params['search'] = '%' . $search . '%';\n }\n\n if ($embedded === 'yes') {\n $sql .= ' AND c.qdrant_id IS NOT NULL';\n } elseif ($embedded === 'no') {\n $sql .= ' AND c.qdrant_id IS NULL';\n }\n\n $stmt = $this->db->prepare($sql);\n $stmt->execute($params);\n\n return (int) $stmt->fetchColumn();\n }\n\n public function getChunk(int $id): ?array\n {\n $stmt = $this->db->prepare(\n 'SELECT c.*, d.filename, d.source_path, d.id as document_id\n FROM chunks c\n JOIN documents d ON c.document_id = d.id\n WHERE c.id = :id'\n );\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result === false ? null : $result;\n }\n\n public function getChunkByDocumentAndIndex(int $documentId, int $chunkIndex): ?array\n {\n $stmt = $this->db->prepare(\n 'SELECT id, chunk_index FROM chunks\n WHERE document_id = :doc_id AND chunk_index = :idx'\n );\n $stmt->execute(['doc_id' => $documentId, 'idx' => $chunkIndex]);\n $result = $stmt->fetch();\n\n return $result === false ? null : $result;\n }\n\n public function getChunkById(int $chunkId): ?array\n {\n $stmt = $this->db->prepare(\n 'SELECT c.*, d.filename, d.source_path\n FROM chunks c\n JOIN documents d ON c.document_id = d.id\n WHERE c.id = :id'\n );\n $stmt->execute(['id' => $chunkId]);\n $result = $stmt->fetch();\n\n return $result === false ? null : $result;\n }\n\n \/\/ =========================================================================\n \/\/ ENTITIES\n \/\/ =========================================================================\n\n public function getEntitiesFiltered(string $type = '', string $search = '', int $limit = 100): array\n {\n $sql = 'SELECT e.*,\n COUNT(DISTINCT ce.chunk_id) as chunk_count,\n COUNT(DISTINCT er.id) as relation_count\n FROM entities e\n LEFT JOIN chunk_entities ce ON e.id = ce.entity_id\n LEFT JOIN entity_relations er ON e.id = er.source_entity_id OR e.id = er.target_entity_id\n WHERE 1=1';\n\n $params = [];\n\n if ($type !== '') {\n $sql .= ' AND e.type = :type';\n $params['type'] = $type;\n }\n\n if ($search !== '') {\n $sql .= ' AND (e.name LIKE :search OR e.description LIKE :search2)';\n $params['search'] = '%' . $search . '%';\n $params['search2'] = '%' . $search . '%';\n }\n\n $sql .= ' GROUP BY e.id ORDER BY chunk_count DESC, e.name LIMIT ' . $limit;\n\n $stmt = $this->db->prepare($sql);\n $stmt->execute($params);\n\n return $stmt->fetchAll();\n }\n\n public function getEntityStats(): array\n {\n return $this->db->query(\n 'SELECT type, COUNT(*) as count FROM entities GROUP BY type'\n )->fetchAll();\n }\n\n public function getEntity(int $id): ?array\n {\n $stmt = $this->db->prepare('SELECT * FROM entities WHERE id = :id');\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result === false ? null : $result;\n }\n\n public function getEntitySynonyms(int $entityId): array\n {\n $stmt = $this->db->prepare('SELECT * FROM entity_synonyms WHERE entity_id = :id');\n $stmt->execute(['id' => $entityId]);\n\n return $stmt->fetchAll();\n }\n\n public function getOutgoingRelations(int $entityId): array\n {\n $stmt = $this->db->prepare(\n 'SELECT er.*, e.name as target_name, e.type as target_type\n FROM entity_relations er\n JOIN entities e ON er.target_entity_id = e.id\n WHERE er.source_entity_id = :id\n ORDER BY er.strength DESC'\n );\n $stmt->execute(['id' => $entityId]);\n\n return $stmt->fetchAll();\n }\n\n public function getIncomingRelations(int $entityId): array\n {\n $stmt = $this->db->prepare(\n 'SELECT er.*, e.name as source_name, e.type as source_type\n FROM entity_relations er\n JOIN entities e ON er.source_entity_id = e.id\n WHERE er.target_entity_id = :id\n ORDER BY er.strength DESC'\n );\n $stmt->execute(['id' => $entityId]);\n\n return $stmt->fetchAll();\n }\n\n public function getChunksForEntity(int $entityId, int $limit = 20): array\n {\n $stmt = $this->db->prepare(\n '... [TRUNCATED-3ca52b9eb3f2c30f]",
"structuredPatch": [
{
"oldStart": 845,
"oldLines": 4,
"newStart": 845,
"newLines": 74,
"lines": [
" {",
" return $this->db->query('SELECT id, name FROM ontology_classes ORDER BY name')->fetchAll();",
" }",
"+",
"+ \/\/ =========================================================================",
"+ \/\/ GRAPH",
"+ \/\/ =========================================================================",
"+",
"+ \/**",
"+ * Returns graph data for D3.js visualization.",
"+ *",
"+ * @return array{nodes: array, links: array, stats: array}",
"+ *\/",
"+ public function getGraphData(): array",
"+ {",
"+ \/\/ Get all entities",
"+ $entities = $this->db->query(",
"+ 'SELECT id, name, type FROM entities ORDER BY name'",
"+ )->fetchAll();",
"+",
"+ \/\/ Get all relations",
"+ $relations = $this->db->query(",
"+ 'SELECT source_entity_id, target_entity_id, relation_type, strength",
"+ FROM entity_relations'",
"+ )->fetchAll();",
"+",
"+ \/\/ Build node index for link resolution",
"+ $nodeIndex = [];",
"+ $nodes = [];",
"+ foreach ($entities as $i => $entity) {",
"+ $nodeIndex[$entity['id']] = $i;",
"+ $nodes[] = [",
"+ 'id' => 'entity_' . $entity['id'],",
"+ 'label' => $entity['name'],",
"+ 'type' => strtoupper($entity['type'] ?? 'OTHER'),",
"+ 'entityId' => (int) $entity['id'],",
"+ ];",
"+ }",
"+",
"+ \/\/ Build links with index references",
"+ $links = [];",
"+ foreach ($relations as $relation) {",
"+ $sourceId = $relation['source_entity_id'];",
"+ $targetId = $relation['target_entity_id'];",
"+",
"+ \/\/ Skip if entity not found",
"+ if (!isset($nodeIndex[$sourceId]) || !isset($nodeIndex[$targetId])) {",
"+ continue;",
"+ }",
"+",
"+ $links[] = [",
"+ 'source' => $nodeIndex[$sourceId],",
"+ 'target' => $nodeIndex[$targetId],",
"+ 'type' => $relation['relation_type'],",
"+ 'strength' => (float) ($relation['strength'] ?? 1.0),",
"+ ];",
"+ }",
"+",
"+ \/\/ Get unique types for stats",
"+ $entityTypes = array_unique(array_column($nodes, 'type'));",
"+ $relationTypes = array_unique(array_column($links, 'type'));",
"+",
"+ return [",
"+ 'nodes' => $nodes,",
"+ 'links' => $links,",
"+ 'stats' => [",
"+ 'nodes' => count($nodes),",
"+ 'links' => count($links),",
"+ 'entityTypes' => count($entityTypes),",
"+ 'relationTypes' => count($relationTypes),",
"+ ],",
"+ ];",
"+ }",
" }"
]
}
],
"userModified": false,
"replaceAll": false
}
}