Files
wevads-platform/public/hamid-kb-api.php
2026-04-07 03:04:16 +02:00

57 lines
3.9 KiB
PHP

<?php
require_once('/opt/wevads/config/credentials.php');
header('Content-Type: application/json');
error_reporting(0);
$pdo = get_pdo('adx_system');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$input = json_decode(file_get_contents('php://input'), true);
$action = $input['action'] ?? $_GET['action'] ?? 'search';
$query = $input['query'] ?? $_GET['query'] ?? '';
$limit = min((int)($input['limit'] ?? 10), 50);
switch ($action) {
case 'search': echo json_encode(searchKB($pdo, $query, $limit)); break;
case 'stats': echo json_encode(getStats($pdo)); break;
case 'all': echo json_encode(getAllKB($pdo, $limit)); break;
default: echo json_encode(['error' => 'Action invalide']);
}
function searchKB($pdo, $query, $limit) {
$results = [];
$words = preg_split('/\s+/', mb_strtolower($query));
$stop = ['pour','dans','avec','cette','faire','comment','quoi','sont','tout','tous','les','des','une','que','qui','est','le','la','un'];
$kw = array_values(array_filter($words, function($w) use ($stop) { return mb_strlen($w) > 2 && !in_array($w, $stop); }));
if (empty($kw)) return getAllKB($pdo, $limit);
$like = '%' . $kw[0] . '%';
try { $stmt = $pdo->prepare("SELECT id, question as title, LEFT(answer, 500) as content, 'qa' as type FROM admin.commonia_knowledge WHERE LOWER(question) LIKE ? OR LOWER(answer) LIKE ? LIMIT 3");
$stmt->execute([$like, $like, $limit]); $results = array_merge($results, $stmt->fetchAll(PDO::FETCH_ASSOC)); } catch (Exception $e) {}
try { $stmt = $pdo->prepare("SELECT id, title, LEFT(content, 500), 'article' as type FROM admin.knowledge_base WHERE LOWER(title) LIKE ? OR LOWER(content) LIKE ? LIMIT 3");
$stmt->execute([$like, $like, $limit]); $results = array_merge($results, $stmt->fetchAll(PDO::FETCH_ASSOC)); } catch (Exception $e) {}
try { $stmt = $pdo->prepare("SELECT id, title, LEFT(content, 500), 'article' as type FROM admin.commonia_articles WHERE LOWER(title) LIKE ? OR LOWER(content) LIKE ? LIMIT 3");
$stmt->execute([$like, $like, $limit]); $results = array_merge($results, $stmt->fetchAll(PDO::FETCH_ASSOC)); } catch (Exception $e) {}
try { $stmt = $pdo->prepare("SELECT id, title, LEFT(summary, 500) as content, 'discussion' as type FROM admin.chatbot_conversations_history WHERE LOWER(title) LIKE ? OR LOWER(summary) LIKE ? LIMIT 3");
$stmt->execute([$like, $like, $limit]); $results = array_merge($results, $stmt->fetchAll(PDO::FETCH_ASSOC)); } catch (Exception $e) {}
return ['success' => true, 'query' => $query, 'keywords' => $kw, 'count' => count($results), 'results' => array_slice($results, 0, $limit)];
}
function getAllKB($pdo, $limit) {
$results = [];
try { $stmt = $pdo->query("(SELECT question as title, LEFT(answer,300) as content, 'qa' as type FROM admin.commonia_knowledge ORDER BY id DESC LIMIT 3) UNION ALL (SELECT title, LEFT(content,300), 'article' FROM admin.knowledge_base ORDER BY id DESC LIMIT 3) UNION ALL (SELECT title, LEFT(content,300), 'article' FROM admin.commonia_articles ORDER BY id DESC LIMIT 3) UNION ALL (SELECT title, LEFT(summary,300), 'discussion' FROM admin.chatbot_conversations_history ORDER BY id DESC LIMIT 3)");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (Exception $e) {}
return ['success' => true, 'count' => count($results), 'results' => $results];
}
function getStats($pdo) {
$s = [];
try { $s['qa'] = (int)$pdo->query("SELECT COUNT(*) FROM admin.commonia_knowledge")->fetchColumn();
$s['articles'] = (int)$pdo->query("SELECT COUNT(*) FROM admin.knowledge_base")->fetchColumn() + (int)$pdo->query("SELECT COUNT(*) FROM admin.commonia_articles")->fetchColumn();
$s['discussions'] = (int)$pdo->query("SELECT COUNT(*) FROM admin.chatbot_conversations_history")->fetchColumn();
$s['total'] = $s['qa'] + $s['articles'] + $s['discussions']; } catch (Exception $e) {}
return ['success' => true, 'stats' => $s];
}