{
"tool_response": {
"type": "text",
"file": {
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Infrastructure\/SemanticExplorerRepository.php",
"content": "\n \/\/ =========================================================================\n \/\/ TAXONOMY\n \/\/ =========================================================================\n\n public function getTaxonomyTerms(): array\n {\n return $this->db->query(\n 'SELECT t.*,\n COUNT(DISTINCT ct.chunk_id) as chunk_count,\n (SELECT COUNT(*) FROM taxonomy_terms WHERE parent_id = t.id) as children_count\n FROM taxonomy_terms t\n LEFT JOIN chunk_taxonomy ct ON t.id = ct.taxonomy_term_id\n GROUP BY t.id\n ORDER BY t.path, t.name'\n )->fetchAll();\n }\n\n public function getTaxonomyStats(): array\n {\n return $this->db->query(\n 'SELECT\n (SELECT COUNT(*) FROM taxonomy_terms) as total_terms,\n (SELECT COUNT(*) FROM taxonomy_terms WHERE parent_id IS NULL) as root_terms,\n (SELECT MAX(depth) FROM taxonomy_terms) as max_depth,\n (SELECT COUNT(DISTINCT chunk_id) FROM chunk_taxonomy) as tagged_chunks'\n )->fetch();\n }\n\n \/\/ =========================================================================\n \/\/ ONTOLOGY\n \/\/ =========================================================================\n\n public function getOntologyClasses(): array\n {\n return $this->db->query(\n 'SELECT oc.*,\n COUNT(DISTINCT ec.entity_id) as entity_count,\n (SELECT COUNT(*) FROM ontology_classes WHERE parent_class_id = oc.id) as subclass_count\n FROM ontology_classes oc\n LEFT JOIN entity_classifications ec ON oc.id = ec.ontology_class_id\n GROUP BY oc.id\n ORDER BY oc.name'\n )->fetchAll();\n }\n\n public function getOntologyStats(): array\n {\n return $this->db->query(\n 'SELECT\n (SELECT COUNT(*) FROM ontology_classes) as total_classes,\n (SELECT COUNT(*) FROM ontology_classes WHERE parent_class_id IS NULL) as root_classes,\n (SELECT COUNT(DISTINCT entity_id) FROM entity_classifications) as classified_entities'\n )->fetch();\n }\n\n \/\/ =========================================================================\n \/\/ SEMANTICS\n \/\/ =========================================================================\n\n public function getSemanticsFiltered(string $sentiment = '', int $limit = 50, int $offset = 0): array\n {\n $sql = 'SELECT cs.*, c.content, c.token_count, d.filename, d.id as document_id\n FROM chunk_semantics cs\n JOIN chunks c ON cs.chunk_id = c.id\n JOIN documents d ON c.document_id = d.id\n WHERE 1=1';\n\n $params = [];\n\n if ($sentiment !== '') {\n $sql .= ' AND cs.sentiment = :sentiment';\n $params['sentiment'] = $sentiment;\n }\n\n $sql .= ' ORDER BY cs.analyzed_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 getSemanticsCount(string $sentiment = ''): int\n {\n $sql = 'SELECT COUNT(*) FROM chunk_semantics cs WHERE 1=1';\n $params = [];\n\n if ($sentiment !== '') {\n $sql .= ' AND cs.sentiment = :sentiment';\n $params['sentiment'] = $sentiment;\n }\n\n $stmt = $this->db->prepare($sql);\n $stmt->execute($params);\n\n return (int) $stmt->fetchColumn();\n }\n\n public function getEntitySemanticsFiltered(string $search = '', string $type = '', int $limit = 50, int $offset = 0): array\n {\n $sql = 'SELECT e.id, e.name, e.type, e.description,\n ce.chunk_id, c.chunk_index, d.id as document_id, d.filename\n FROM entities e\n LEFT JOIN chunk_entities ce ON e.id = ce.entity_id\n LEFT JOIN chunks c ON ce.chunk_id = c.id\n LEFT JOIN documents d ON c.document_id = d.id\n WHERE e.description IS NOT NULL';\n\n $params = [];\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 if ($type !== '') {\n $sql .= ' AND e.type = :type';\n $params['type'] = $type;\n }\n\n $sql .= ' ORDER BY e.name 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 getEntitySemanticsCount(string $search = '', string $type = ''): int\n {\n $sql = 'SELECT COUNT(DISTINCT e.id) FROM entities e WHERE e.description IS NOT NULL';\n $params = [];\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 if ($type !== '') {\n $sql .= ' AND e.type = :type';\n $params['type'] = $type;\n }\n\n $stmt = $this->db->prepare($sql);\n $stmt->execute($params);\n\n return (int) $stmt->fetchColumn();\n }\n\n \/\/ =========================================================================\n \/\/ ENTITIES - CRUD\n \/\/ =========================================================================\n\n public function createEntity(string $name, string $type, ?string $description = null): int\n {\n $stmt = $this->db->prepare(\n 'INSERT INTO entities (name, canonical_name, type, description, created_at, updated_at)\n VALUES (:name, :canonical, :type, :description, NOW(), NOW())'\n );\n $stmt->execute([\n 'name' => $name,\n 'canonical' => strtolower(trim($name)),\n 'type' => $type,\n 'description' => $description,\n ]);\n\n return (int) $this->db->lastInsertId();\n }\n\n public function updateEntity(int $id, string $name, string $type, ?string $description = null): bool\n {\n $stmt = $this->db->prepare(\n 'UPDATE entities SET name = :name, canonical_name = :canonical, type = :type,\n description = :description, updated_at = NOW() WHERE id = :id'\n );\n\n return $stmt->execute([\n 'id' => $id,\n 'name' => $name,\n 'canonical' => strtolower(trim($name)),\n 'type' => $type,\n 'description' => $description,\n ]);\n }\n\n public function deleteEntity(int $id): bool\n {\n \/\/ Check for relations\n $stmt = $this->db->prepare(\n 'SELECT COUNT(*) FROM entity_relations\n WHERE source_entity_id = :id OR target_entity_id = :id2'\n );\n $stmt->execute(['id' => $id, 'id2' => $id]);\n\n if ((int) $stmt->fetchColumn() > 0) {\n return false; \/\/ Has relations, cannot delete\n }",
"numLines": 200,
"startLine": 400,
"totalLines": 918
}
}
}