Protokoll #912
| ID | 912 |
|---|---|
| Zeitstempel | 2025-12-20 17:40:55.435730 |
| Client | root |
| IP | Warning: Undefined array key "request_ip" in /var/www/dev.campus.systemische-tools.de/src/View/protokoll/show.php on line 13 Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/dev.campus.systemische-tools.de/src/View/protokoll/show.php on line 13 |
| Modell | claude-sonnet-4-20250514 |
| Status | completed |
| Tokens | 11,004 (Input: 0, Output: 0) |
| Dauer | 88 ms |
| Request-Zeit | Warning: Undefined array key "request_timestamp" in /var/www/dev.campus.systemische-tools.de/src/View/protokoll/show.php on line 18 |
| Response-Zeit | - |
Warning: Undefined array key "error_message" in /var/www/dev.campus.systemische-tools.de/src/View/protokoll/show.php on line 22
Request
{
"event": "PreToolUse",
"tool_name": "Edit",
"tool_input": {
"file_path": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Infrastructure\/SemanticExplorerRepository.php",
"old_string": " public function getSemanticStats(): array\n {\n return $this->db->query(\n 'SELECT\n COUNT(*) as total,\n SUM(CASE WHEN sentiment = \"positive\" THEN 1 ELSE 0 END) as positive,\n SUM(CASE WHEN sentiment = \"negative\" THEN 1 ELSE 0 END) as negative,\n SUM(CASE WHEN sentiment = \"neutral\" THEN 1 ELSE 0 END) as neutral,\n SUM(CASE WHEN sentiment = \"mixed\" THEN 1 ELSE 0 END) as mixed\n FROM chunk_semantics'\n )->fetch();\n }\n}",
"new_string": " public function getSemanticStats(): array\n {\n return $this->db->query(\n 'SELECT\n COUNT(*) as total,\n SUM(CASE WHEN sentiment = \"positive\" THEN 1 ELSE 0 END) as positive,\n SUM(CASE WHEN sentiment = \"negative\" THEN 1 ELSE 0 END) as negative,\n SUM(CASE WHEN sentiment = \"neutral\" THEN 1 ELSE 0 END) as neutral,\n SUM(CASE WHEN sentiment = \"mixed\" THEN 1 ELSE 0 END) as mixed\n FROM chunk_semantics'\n )->fetch();\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 }\n\n \/\/ Delete synonyms first\n $stmt = $this->db->prepare('DELETE FROM entity_synonyms WHERE entity_id = :id');\n $stmt->execute(['id' => $id]);\n\n \/\/ Delete chunk associations\n $stmt = $this->db->prepare('DELETE FROM chunk_entities WHERE entity_id = :id');\n $stmt->execute(['id' => $id]);\n\n \/\/ Delete entity\n $stmt = $this->db->prepare('DELETE FROM entities WHERE id = :id');\n\n return $stmt->execute(['id' => $id]);\n }\n\n public function getEntityTypes(): array\n {\n return ['person', 'organization', 'location', 'concept', 'method', 'tool', 'event', 'other'];\n }\n\n public function getAllEntitiesSimple(): array\n {\n return $this->db->query('SELECT id, name, type FROM entities ORDER BY name')->fetchAll();\n }\n\n \/\/ =========================================================================\n \/\/ RELATIONS - CRUD\n \/\/ =========================================================================\n\n public function getRelation(int $id): ?array\n {\n $stmt = $this->db->prepare(\n 'SELECT er.*,\n es.name as source_name, es.type as source_type,\n et.name as target_name, et.type as target_type\n FROM entity_relations er\n JOIN entities es ON er.source_entity_id = es.id\n JOIN entities et ON er.target_entity_id = et.id\n WHERE er.id = :id'\n );\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result === false ? null : $result;\n }\n\n public function createRelation(int $sourceId, int $targetId, string $type, float $strength = 1.0): int\n {\n $stmt = $this->db->prepare(\n 'INSERT INTO entity_relations (source_entity_id, target_entity_id, relation_type, strength, created_at)\n VALUES (:source, :target, :type, :strength, NOW())'\n );\n $stmt->execute([\n 'source' => $sourceId,\n 'target' => $targetId,\n 'type' => $type,\n 'strength' => $strength,\n ]);\n\n return (int) $this->db->lastInsertId();\n }\n\n public function updateRelation(int $id, string $type, float $strength): bool\n {\n $stmt = $this->db->prepare(\n 'UPDATE entity_relations SET relation_type = :type, strength = :strength WHERE id = :id'\n );\n\n return $stmt->execute(['id' => $id, 'type' => $type, 'strength' => $strength]);\n }\n\n public function deleteRelation(int $id): bool\n {\n $stmt = $this->db->prepare('DELETE FROM entity_relations WHERE id = :id');\n\n return $stmt->execute(['id' => $id]);\n }\n\n public function getRelationTypesList(): array\n {\n return ['RELATED_TO', 'PART_OF', 'DEVELOPED_BY', 'INFLUENCED_BY', 'USED_IN', 'WORKS_WITH', 'TEACHES', 'CREATED'];\n }\n\n \/\/ =========================================================================\n \/\/ TAXONOMY - CRUD\n \/\/ =========================================================================\n\n public function getTaxonomyTerm(int $id): ?array\n {\n $stmt = $this->db->prepare('SELECT * FROM taxonomy_terms WHERE id = :id');\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result === false ? null : $result;\n }\n\n public function createTaxonomyTerm(string $name, ?int $parentId = null): int\n {\n \/\/ Calculate depth and path\n $depth = 0;\n $path = $name;\n\n if ($parentId !== null) {\n $parent = $this->getTaxonomyTerm($parentId);\n if ($parent !== null) {\n $depth = (int) $parent['depth'] + 1;\n $path = $parent['path'] . '\/' . $name;\n }\n }\n\n $stmt = $this->db->prepare(\n 'INSERT INTO taxonomy_terms (name, parent_id, depth, path, created_at)\n VALUES (:name, :parent, :depth, :path, NOW())'\n );\n $stmt->execute([\n 'name' => $name,\n 'parent' => $parentId,\n 'depth' => $depth,\n 'path' => $path,\n ]);\n\n return (int) $this->db->lastInsertId();\n }\n\n public function updateTaxonomyTerm(int $id, string $name, ?int $parentId = null): bool\n {\n \/\/ Calculate new depth and path\n $depth = 0;\n $path = $name;\n\n if ($parentId !== null) {\n $parent = $this->getTaxonomyTerm($parentId);\n if ($parent !== null) {\n $depth = (int) $parent['depth'] + 1;\n $path = $parent['path'] . '\/' . $name;\n }\n }\n\n $stmt = $this->db->prepare(\n 'UPDATE taxonomy_terms SET name = :name, parent_id = :parent, depth = :depth, path = :path WHERE id = :id'\n );\n\n return $stmt->execute([\n 'id' => $id,\n 'name' => $name,\n 'parent' => $parentId,\n 'depth' => $depth,\n 'path' => $path,\n ]);\n }\n\n public function deleteTaxonomyTerm(int $id): bool\n {\n \/\/ Check for children\n $stmt = $this->db->prepare('SELECT COUNT(*) FROM taxonomy_terms WHERE parent_id = :id');\n $stmt->execute(['id' => $id]);\n\n if ((int) $stmt->fetchColumn() > 0) {\n return false; \/\/ Has children, cannot delete\n }\n\n \/\/ Delete chunk associations\n $stmt = $this->db->prepare('DELETE FROM chunk_taxonomy WHERE taxonomy_term_id = :id');\n $stmt->execute(['id' => $id]);\n\n \/\/ Delete term\n $stmt = $this->db->prepare('DELETE FROM taxonomy_terms WHERE id = :id');\n\n return $stmt->execute(['id' => $id]);\n }\n\n public function getTaxonomyTermsForSelect(): array\n {\n return $this->db->query(\n 'SELECT id, name, depth, path FROM taxonomy_terms ORDER BY path, name'\n )->fetchAll();\n }\n\n \/\/ =========================================================================\n \/\/ ONTOLOGY - CRUD\n \/\/ =========================================================================\n\n public function getOntologyClass(int $id): ?array\n {\n $stmt = $this->db->prepare('SELECT * FROM ontology_classes WHERE id = :id');\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result === false ? null : $result;\n }\n\n public function createOntologyClass(string $name, ?int $parentId = null, ?string $description = null, array $properties = []): int\n {\n $stmt = $this->db->prepare(\n 'INSERT INTO ontology_classes (name, parent_class_id, description, properties, created_at)\n VALUES (:name, :parent, :description, :properties, NOW())'\n );\n $stmt->execute([\n 'name' => $name,\n 'parent' => $parentId,\n 'description' => $description,\n 'properties' => json_encode($properties),\n ]);\n\n return (int) $this->db->lastInsertId();\n }\n\n public function updateOntologyClass(int $id, string $name, ?int $parentId = null, ?string $description = null, array $properties = []): bool\n {\n $stmt = $this->db->prepare(\n 'UPDATE ontology_classes SET name = :name, parent_class_id = :parent,\n description = :description, properties = :properties WHERE id = :id'\n );\n\n return $stmt->execute([\n 'id' => $id,\n 'name' => $name,\n 'parent' => $parentId,\n 'description' => $description,\n 'properties' => json_encode($properties),\n ]);\n }\n\n public function deleteOntologyClass(int $id): bool\n {\n \/\/ Check for subclasses\n $stmt = $this->db->prepare('SELECT COUNT(*) FROM ontology_classes WHERE parent_class_id = :id');\n $stmt->execute(... [TRUNCATED-44ccab54ff7dc543]"
}
}
Response
{
"tool_response": {
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Infrastructure\/SemanticExplorerRepository.php",
"oldString": " public function getSemanticStats(): array\n {\n return $this->db->query(\n 'SELECT\n COUNT(*) as total,\n SUM(CASE WHEN sentiment = \"positive\" THEN 1 ELSE 0 END) as positive,\n SUM(CASE WHEN sentiment = \"negative\" THEN 1 ELSE 0 END) as negative,\n SUM(CASE WHEN sentiment = \"neutral\" THEN 1 ELSE 0 END) as neutral,\n SUM(CASE WHEN sentiment = \"mixed\" THEN 1 ELSE 0 END) as mixed\n FROM chunk_semantics'\n )->fetch();\n }\n}",
"newString": " public function getSemanticStats(): array\n {\n return $this->db->query(\n 'SELECT\n COUNT(*) as total,\n SUM(CASE WHEN sentiment = \"positive\" THEN 1 ELSE 0 END) as positive,\n SUM(CASE WHEN sentiment = \"negative\" THEN 1 ELSE 0 END) as negative,\n SUM(CASE WHEN sentiment = \"neutral\" THEN 1 ELSE 0 END) as neutral,\n SUM(CASE WHEN sentiment = \"mixed\" THEN 1 ELSE 0 END) as mixed\n FROM chunk_semantics'\n )->fetch();\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 }\n\n \/\/ Delete synonyms first\n $stmt = $this->db->prepare('DELETE FROM entity_synonyms WHERE entity_id = :id');\n $stmt->execute(['id' => $id]);\n\n \/\/ Delete chunk associations\n $stmt = $this->db->prepare('DELETE FROM chunk_entities WHERE entity_id = :id');\n $stmt->execute(['id' => $id]);\n\n \/\/ Delete entity\n $stmt = $this->db->prepare('DELETE FROM entities WHERE id = :id');\n\n return $stmt->execute(['id' => $id]);\n }\n\n public function getEntityTypes(): array\n {\n return ['person', 'organization', 'location', 'concept', 'method', 'tool', 'event', 'other'];\n }\n\n public function getAllEntitiesSimple(): array\n {\n return $this->db->query('SELECT id, name, type FROM entities ORDER BY name')->fetchAll();\n }\n\n \/\/ =========================================================================\n \/\/ RELATIONS - CRUD\n \/\/ =========================================================================\n\n public function getRelation(int $id): ?array\n {\n $stmt = $this->db->prepare(\n 'SELECT er.*,\n es.name as source_name, es.type as source_type,\n et.name as target_name, et.type as target_type\n FROM entity_relations er\n JOIN entities es ON er.source_entity_id = es.id\n JOIN entities et ON er.target_entity_id = et.id\n WHERE er.id = :id'\n );\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result === false ? null : $result;\n }\n\n public function createRelation(int $sourceId, int $targetId, string $type, float $strength = 1.0): int\n {\n $stmt = $this->db->prepare(\n 'INSERT INTO entity_relations (source_entity_id, target_entity_id, relation_type, strength, created_at)\n VALUES (:source, :target, :type, :strength, NOW())'\n );\n $stmt->execute([\n 'source' => $sourceId,\n 'target' => $targetId,\n 'type' => $type,\n 'strength' => $strength,\n ]);\n\n return (int) $this->db->lastInsertId();\n }\n\n public function updateRelation(int $id, string $type, float $strength): bool\n {\n $stmt = $this->db->prepare(\n 'UPDATE entity_relations SET relation_type = :type, strength = :strength WHERE id = :id'\n );\n\n return $stmt->execute(['id' => $id, 'type' => $type, 'strength' => $strength]);\n }\n\n public function deleteRelation(int $id): bool\n {\n $stmt = $this->db->prepare('DELETE FROM entity_relations WHERE id = :id');\n\n return $stmt->execute(['id' => $id]);\n }\n\n public function getRelationTypesList(): array\n {\n return ['RELATED_TO', 'PART_OF', 'DEVELOPED_BY', 'INFLUENCED_BY', 'USED_IN', 'WORKS_WITH', 'TEACHES', 'CREATED'];\n }\n\n \/\/ =========================================================================\n \/\/ TAXONOMY - CRUD\n \/\/ =========================================================================\n\n public function getTaxonomyTerm(int $id): ?array\n {\n $stmt = $this->db->prepare('SELECT * FROM taxonomy_terms WHERE id = :id');\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result === false ? null : $result;\n }\n\n public function createTaxonomyTerm(string $name, ?int $parentId = null): int\n {\n \/\/ Calculate depth and path\n $depth = 0;\n $path = $name;\n\n if ($parentId !== null) {\n $parent = $this->getTaxonomyTerm($parentId);\n if ($parent !== null) {\n $depth = (int) $parent['depth'] + 1;\n $path = $parent['path'] . '\/' . $name;\n }\n }\n\n $stmt = $this->db->prepare(\n 'INSERT INTO taxonomy_terms (name, parent_id, depth, path, created_at)\n VALUES (:name, :parent, :depth, :path, NOW())'\n );\n $stmt->execute([\n 'name' => $name,\n 'parent' => $parentId,\n 'depth' => $depth,\n 'path' => $path,\n ]);\n\n return (int) $this->db->lastInsertId();\n }\n\n public function updateTaxonomyTerm(int $id, string $name, ?int $parentId = null): bool\n {\n \/\/ Calculate new depth and path\n $depth = 0;\n $path = $name;\n\n if ($parentId !== null) {\n $parent = $this->getTaxonomyTerm($parentId);\n if ($parent !== null) {\n $depth = (int) $parent['depth'] + 1;\n $path = $parent['path'] . '\/' . $name;\n }\n }\n\n $stmt = $this->db->prepare(\n 'UPDATE taxonomy_terms SET name = :name, parent_id = :parent, depth = :depth, path = :path WHERE id = :id'\n );\n\n return $stmt->execute([\n 'id' => $id,\n 'name' => $name,\n 'parent' => $parentId,\n 'depth' => $depth,\n 'path' => $path,\n ]);\n }\n\n public function deleteTaxonomyTerm(int $id): bool\n {\n \/\/ Check for children\n $stmt = $this->db->prepare('SELECT COUNT(*) FROM taxonomy_terms WHERE parent_id = :id');\n $stmt->execute(['id' => $id]);\n\n if ((int) $stmt->fetchColumn() > 0) {\n return false; \/\/ Has children, cannot delete\n }\n\n \/\/ Delete chunk associations\n $stmt = $this->db->prepare('DELETE FROM chunk_taxonomy WHERE taxonomy_term_id = :id');\n $stmt->execute(['id' => $id]);\n\n \/\/ Delete term\n $stmt = $this->db->prepare('DELETE FROM taxonomy_terms WHERE id = :id');\n\n return $stmt->execute(['id' => $id]);\n }\n\n public function getTaxonomyTermsForSelect(): array\n {\n return $this->db->query(\n 'SELECT id, name, depth, path FROM taxonomy_terms ORDER BY path, name'\n )->fetchAll();\n }\n\n \/\/ =========================================================================\n \/\/ ONTOLOGY - CRUD\n \/\/ =========================================================================\n\n public function getOntologyClass(int $id): ?array\n {\n $stmt = $this->db->prepare('SELECT * FROM ontology_classes WHERE id = :id');\n $stmt->execute(['id' => $id]);\n $result = $stmt->fetch();\n\n return $result === false ? null : $result;\n }\n\n public function createOntologyClass(string $name, ?int $parentId = null, ?string $description = null, array $properties = []): int\n {\n $stmt = $this->db->prepare(\n 'INSERT INTO ontology_classes (name, parent_class_id, description, properties, created_at)\n VALUES (:name, :parent, :description, :properties, NOW())'\n );\n $stmt->execute([\n 'name' => $name,\n 'parent' => $parentId,\n 'description' => $description,\n 'properties' => json_encode($properties),\n ]);\n\n return (int) $this->db->lastInsertId();\n }\n\n public function updateOntologyClass(int $id, string $name, ?int $parentId = null, ?string $description = null, array $properties = []): bool\n {\n $stmt = $this->db->prepare(\n 'UPDATE ontology_classes SET name = :name, parent_class_id = :parent,\n description = :description, properties = :properties WHERE id = :id'\n );\n\n return $stmt->execute([\n 'id' => $id,\n 'name' => $name,\n 'parent' => $parentId,\n 'description' => $description,\n 'properties' => json_encode($properties),\n ]);\n }\n\n public function deleteOntologyClass(int $id): bool\n {\n \/\/ Check for subclasses\n $stmt = $this->db->prepare('SELECT COUNT(*) FROM ontology_classes WHERE parent_class_id = :id');\n $stmt->execute(... [TRUNCATED-44ccab54ff7dc543]",
"originalFile": "<?php\n\nnamespace Infrastructure;\n\nuse Infrastructure\\Config\\DatabaseFactory;\n\n\/**\n * SemanticExplorerRepository - Database Access for Semantic Explorer\n *\n * Handles all database operations for documents, chunks, entities,\n * relations, taxonomy, ontology, and semantics.\n *\/\nclass SemanticExplorerRepository\n{\n private \\PDO $db;\n\n public function __construct()\n {\n $this->db = 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 ... [TRUNCATED-ac261bdede1b7799]",
"structuredPatch": [
{
"oldStart": 498,
"oldLines": 4,
"newStart": 498,
"newLines": 300,
"lines": [
" FROM chunk_semantics'",
" )->fetch();",
" }",
"+",
"+ \/\/ =========================================================================",
"+ \/\/ ENTITIES - CRUD",
"+ \/\/ =========================================================================",
"+",
"+ public function createEntity(string $name, string $type, ?string $description = null): int",
"+ {",
"+ $stmt = $this->db->prepare(",
"+ 'INSERT INTO entities (name, canonical_name, type, description, created_at, updated_at)",
"+ VALUES (:name, :canonical, :type, :description, NOW(), NOW())'",
"+ );",
"+ $stmt->execute([",
"+ 'name' => $name,",
"+ 'canonical' => strtolower(trim($name)),",
"+ 'type' => $type,",
"+ 'description' => $description,",
"+ ]);",
"+",
"+ return (int) $this->db->lastInsertId();",
"+ }",
"+",
"+ public function updateEntity(int $id, string $name, string $type, ?string $description = null): bool",
"+ {",
"+ $stmt = $this->db->prepare(",
"+ 'UPDATE entities SET name = :name, canonical_name = :canonical, type = :type,",
"+ description = :description, updated_at = NOW() WHERE id = :id'",
"+ );",
"+",
"+ return $stmt->execute([",
"+ 'id' => $id,",
"+ 'name' => $name,",
"+ 'canonical' => strtolower(trim($name)),",
"+ 'type' => $type,",
"+ 'description' => $description,",
"+ ]);",
"+ }",
"+",
"+ public function deleteEntity(int $id): bool",
"+ {",
"+ \/\/ Check for relations",
"+ $stmt = $this->db->prepare(",
"+ 'SELECT COUNT(*) FROM entity_relations",
"+ WHERE source_entity_id = :id OR target_entity_id = :id2'",
"+ );",
"+ $stmt->execute(['id' => $id, 'id2' => $id]);",
"+",
"+ if ((int) $stmt->fetchColumn() > 0) {",
"+ return false; \/\/ Has relations, cannot delete",
"+ }",
"+",
"+ \/\/ Delete synonyms first",
"+ $stmt = $this->db->prepare('DELETE FROM entity_synonyms WHERE entity_id = :id');",
"+ $stmt->execute(['id' => $id]);",
"+",
"+ \/\/ Delete chunk associations",
"+ $stmt = $this->db->prepare('DELETE FROM chunk_entities WHERE entity_id = :id');",
"+ $stmt->execute(['id' => $id]);",
"+",
"+ \/\/ Delete entity",
"+ $stmt = $this->db->prepare('DELETE FROM entities WHERE id = :id');",
"+",
"+ return $stmt->execute(['id' => $id]);",
"+ }",
"+",
"+ public function getEntityTypes(): array",
"+ {",
"+ return ['person', 'organization', 'location', 'concept', 'method', 'tool', 'event', 'other'];",
"+ }",
"+",
"+ public function getAllEntitiesSimple(): array",
"+ {",
"+ return $this->db->query('SELECT id, name, type FROM entities ORDER BY name')->fetchAll();",
"+ }",
"+",
"+ \/\/ =========================================================================",
"+ \/\/ RELATIONS - CRUD",
"+ \/\/ =========================================================================",
"+",
"+ public function getRelation(int $id): ?array",
"+ {",
"+ $stmt = $this->db->prepare(",
"+ 'SELECT er.*,",
"+ es.name as source_name, es.type as source_type,",
"+ et.name as target_name, et.type as target_type",
"+ FROM entity_relations er",
"+ JOIN entities es ON er.source_entity_id = es.id",
"+ JOIN entities et ON er.target_entity_id = et.id",
"+ WHERE er.id = :id'",
"+ );",
"+ $stmt->execute(['id' => $id]);",
"+ $result = $stmt->fetch();",
"+",
"+ return $result === false ? null : $result;",
"+ }",
"+",
"+ public function createRelation(int $sourceId, int $targetId, string $type, float $strength = 1.0): int",
"+ {",
"+ $stmt = $this->db->prepare(",
"+ 'INSERT INTO entity_relations (source_entity_id, target_entity_id, relation_type, strength, created_at)",
"+ VALUES (:source, :target, :type, :strength, NOW())'",
"+ );",
"+ $stmt->execute([",
"+ 'source' => $sourceId,",
"+ 'target' => $targetId,",
"+ 'type' => $type,",
"+ 'strength' => $strength,",
"+ ]);",
"+",
"+ return (int) $this->db->lastInsertId();",
"+ }",
"+",
"+ public function updateRelation(int $id, string $type, float $strength): bool",
"+ {",
"+ $stmt = $this->db->prepare(",
"+ 'UPDATE entity_relations SET relation_type = :type, strength = :strength WHERE id = :id'",
"+ );",
"+",
"+ return $stmt->execute(['id' => $id, 'type' => $type, 'strength' => $strength]);",
"+ }",
"+",
"+ public function deleteRelation(int $id): bool",
"+ {",
"+ $stmt = $this->db->prepare('DELETE FROM entity_relations WHERE id = :id');",
"+",
"+ return $stmt->execute(['id' => $id]);",
"+ }",
"+",
"+ public function getRelationTypesList(): array",
"+ {",
"+ return ['RELATED_TO', 'PART_OF', 'DEVELOPED_BY', 'INFLUENCED_BY', 'USED_IN', 'WORKS_WITH', 'TEACHES', 'CREATED'];",
"+ }",
"+",
"+ \/\/ =========================================================================",
"+ \/\/ TAXONOMY - CRUD",
"+ \/\/ =========================================================================",
"+",
"+ public function getTaxonomyTerm(int $id): ?array",
"+ {",
"+ $stmt = $this->db->prepare('SELECT * FROM taxonomy_terms WHERE id = :id');",
"+ $stmt->execute(['id' => $id]);",
"+ $result = $stmt->fetch();",
"+",
"+ return $result === false ? null : $result;",
"+ }",
"+",
"+ public function createTaxonomyTerm(string $name, ?int $parentId = null): int",
"+ {",
"+ \/\/ Calculate depth and path",
"+ $depth = 0;",
"+ $path = $name;",
"+",
"+ if ($parentId !== null) {",
"+ $parent = $this->getTaxonomyTerm($parentId);",
"+ if ($parent !== null) {",
"+ $depth = (int) $parent['depth'] + 1;",
"+ $path = $parent['path'] . '\/' . $name;",
"+ }",
"+ }",
"+",
"+ $stmt = $this->db->prepare(",
"+ 'INSERT INTO taxonomy_terms (name, parent_id, depth, path, created_at)",
"+ VALUES (:name, :parent, :depth, :path, NOW())'",
"+ );",
"+ $stmt->execute([",
"+ 'name' => $name,",
"+ 'parent' => $parentId,",
"+ 'depth' => $depth,",
"+ 'path' => $path,",
"+ ]);",
"+",
"+ return (int) $this->db->lastInsertId();",
"+ }",
"+",
"+ public function updateTaxonomyTerm(int $id, string $name, ?int $parentId = null): bool",
"+ {",
"+ \/\/ Calculate new depth and path",
"+ $depth = 0;",
"+ $path = $name;",
"+",
"+ if ($parentId !== null) {",
"+ $parent = $this->getTaxonomyTerm($parentId);",
"+ if ($parent !== null) {",
"+ $depth = (int) $parent['depth'] + 1;",
"+ $path = $parent['path'] . '\/' . $name;",
"+ }",
"+ }",
"+",
"+ $stmt = $this->db->prepare(",
"+ 'UPDATE taxonomy_terms SET name = :name, parent_id = :parent, depth = :depth, path = :path WHERE id = :id'",
"+ );",
"+",
"+ return $stmt->execute([",
"+ 'id' => $id,",
"+ 'name' => $name,",
"+ 'parent' => $parentId,",
"+ 'depth' => $depth,",
"+ 'path' => $path,",
"+ ]);",
"+ }",
"+",
"+ public function deleteTaxonomyTerm(int $id): bool",
"+ {",
"+ \/\/ Check for children",
"+ $stmt = $this->db->prepare('SELECT COUNT(*) FROM taxonomy_terms WHERE parent_id = :id');",
"+ $stmt->execute(['id' => $id]);",
"+",
"+ if ((int) $stmt->fetchColumn() > 0) {",
"+ return false; \/\/ Has children, cannot delete",
"+ }",
"+",
"+ \/\/ Delete chunk associations",
"+ $stmt = $this->db->prepare('DELETE FROM chunk_taxonomy WHERE taxonomy_term_id = :id');",
"+ $stmt->execute(['id' => $id]);",
"+",
"+ \/\/ Delete term",
"+ $stmt = $this->db->prepare('DELETE FROM taxonomy_terms WHERE id = :id');",
"+",
"+ return $stmt->execute(['id' => $id]);",
"+ }",
"+",
"+ public function getTaxonomyTermsForSelect(): array",
"+ {",
"+ return $this->db->query(",
"+ 'SELECT id, name, depth, path FROM taxonomy_terms ORDER BY path, name'",
"+ )->fetchAll();",
"+ }",
"+",
"+ \/\/ =========================================================================",
"+ \/\/ ONTOLOGY - CRUD",
"+ \/\/ =========================================================================",
"+",
"+ public function getOntologyClass(int $id): ?array",
"+ {",
"+ $stmt = $this->db->prepare('SELECT * FROM ontology_classes WHERE id = :id');",
"+ $stmt->execute(['id' => $id]);",
"+ $result = $stmt->fetch();",
"+",
"+ return $result === false ? null : $result;",
"+ }",
"+",
"+ public function createOntologyClass(string $name, ?int $parentId = null, ?string $description = null, array $properties = []): int",
"+ {",
"+ $stmt = $this->db->prepare(",
"+ 'INSERT INTO ontology_classes (name, parent_class_id, description, properties, created_at)",
"+ VALUES (:name, :parent, :description, :properties, NOW())'",
"+ );",
"+ $stmt->execute([",
"+ 'name' => $name,",
"+ 'parent' => $parentId,",
"+ 'description' => $description,",
"+ 'properties' => json_encode($properties),",
"+ ]);",
"+",
"+ return (int) $this->db->lastInsertId();",
"+ }",
"+",
"+ public function updateOntologyClass(int $id, string $name, ?int $parentId = null, ?string $description = null, array $properties = []): bool",
"+ {",
"+ $stmt = $this->db->prepare(",
"+ 'UPDATE ontology_classes SET name = :name, parent_class_id = :parent,",
"+ description = :description, properties = :properties WHERE id = :id'",
"+ );",
"+",
"+ return $stmt->execute([",
"+ 'id' => $id,",
"+ 'name' => $name,",
"+ 'parent' => $parentId,",
"+ 'description' => $description,",
"+ 'properties' => json_encode($properties),",
"+ ]);",
"+ }",
"+",
"+ public function deleteOntologyClass(int $id): bool",
"+ {",
"+ \/\/ Check for subclasses",
"+ $stmt = $this->db->prepare('SELECT COUNT(*) FROM ontology_classes WHERE parent_class_id = :id');",
"+ $stmt->execute(['id' => $id]);",
"+",
"+ if ((int) $stmt->fetchColumn() > 0) {",
"+ return false; \/\/ Has subclasses, cannot delete",
"+ }",
"+",
"+ \/\/ Delete entity classifications",
"+ $stmt = $this->db->prepare('DELETE FROM entity_classifications WHERE ontology_class_id = :id');",
"+ $stmt->execute(['id' => $id]);",
"+",
"+ \/\/ Delete class",
"+ $stmt = $this->db->prepare('DELETE FROM ontology_classes WHERE id = :id');",
"+",
"+ return $stmt->execute(['id' => $id]);",
"+ }",
"+",
"+ public function getOntologyClassesForSelect(): array",
"+ {",
"+ return $this->db->query('SELECT id, name FROM ontology_classes ORDER BY name')->fetchAll();",
"+ }",
" }"
]
}
],
"userModified": false,
"replaceAll": false
}
}