beginTransaction(); // Deshabilitar FK checks para poder truncar todo sin errores de integridad $pdo->exec("SET FOREIGN_KEY_CHECKS = 0;"); $tables = [ 'skill_versions', 'skill_shares', 'skill_tags', 'skill_personas', 'skill_capabilities', 'skills', 'skill_folders' ]; foreach ($tables as $table) { $pdo->exec("TRUNCATE TABLE $table"); } $pdo->exec("SET FOREIGN_KEY_CHECKS = 1;"); $pdo->commit(); $db_status = "Base de datos reseteada (Tablas truncadas)"; } catch (Exception $e) { $pdo->rollBack(); echo json_encode(["error" => "Error en base de datos: " . $e->getMessage()]); exit; } /* ------------------------------------------------------------------- 2. LIMPIEZA DE ARCHIVOS (STORAGE) ------------------------------------------------------------------- */ function deleteDirectoryContents($dir) { if (!is_dir($dir)) return false; $files = array_diff(scandir($dir), array('.', '..')); foreach ($files as $file) { $path = "$dir/$file"; (is_dir($path)) ? deleteDir($path) : unlink($path); } return true; } // Función auxiliar para borrar carpetas completas function deleteDir($dirPath) { if (!is_dir($dirPath)) return; $files = array_diff(scandir($dirPath), array('.', '..')); foreach ($files as $file) { (is_dir("$dirPath/$file")) ? deleteDir("$dirPath/$file") : unlink("$dirPath/$file"); } return rmdir($dirPath); } $storage_cleaned = false; if (defined('STORAGE_PATH') && is_dir(STORAGE_PATH)) { $storage_cleaned = deleteDirectoryContents(STORAGE_PATH); } /* ------------------------------------------------------------------- 3. ACTUALIZACIÓN A GIT (Wipeout) ------------------------------------------------------------------- */ $gitOutput = []; if (is_dir('.git')) { // Eliminar del índice de git cualquier archivo que hayamos borrado físicamente exec("git add -A 2>&1", $gitOutput); exec("git commit -m 'System Reset: Wiped all skills and folders " . date('Y-m-d H:i:s') . "' 2>&1", $gitOutput); exec("git push origin main 2>&1", $gitOutput); // Ajusta la rama si es necesario } echo json_encode([ "success" => true, "database" => $db_status, "storage" => $storage_cleaned ? "Carpeta storage vaciada" : "No se pudo acceder a storage", "git" => $gitOutput ]);