1. <?php
  2. /**
  3. * Merges datasets from multiple HTML documents for maintenance tasks.
  4. * Handles potential errors gracefully.
  5. *
  6. * @param array $htmlFiles An array of paths to HTML files.
  7. * @param string $outputFile The path to the output CSV file.
  8. * @return bool True on success, false on failure.
  9. */
  10. function mergeHtmlDatasets(array $htmlFiles, string $outputFile): bool
  11. {
  12. $allData = []; // Array to store data from all files
  13. foreach ($htmlFiles as $htmlFile) {
  14. try {
  15. $html = file_get_contents($htmlFile); // Read HTML content
  16. if ($html === false) {
  17. error_log("Error reading file: " . $htmlFile); // Log error
  18. continue; // Skip to the next file
  19. }
  20. // Extract data from HTML (Example: using simple regex - adjust as needed)
  21. preg_match_all('/<div class="task">(.*?)</div>/s', $html, $matches);
  22. if (!empty($matches[0])) {
  23. foreach ($matches[0] as $taskHtml) {
  24. // Extract task details (Example: name, description, status)
  25. preg_match('/<h2 class="task-name">(.*?)</h2>/', $taskHtml, $taskName);
  26. preg_match('/<p class="task-description">(.*?)<\/p>/', $taskHtml, $taskDescription);
  27. preg_match('/<span class="task-status">(.*?)<\/span>/', $taskHtml, $taskStatus);
  28. $allData[] = [
  29. 'name' => trim($taskName[0]),
  30. 'description' => trim($taskDescription[0]),
  31. 'status' => trim($taskStatus[0]),
  32. 'file' => $htmlFile, // Add file source for traceability
  33. ];
  34. }
  35. } else {
  36. error_log("No tasks found in file: " . $htmlFile); // Log if no tasks are found
  37. }
  38. } catch (Exception $e) {
  39. error_log("Error processing file " . $htmlFile . ": " . $e->getMessage()); // Log exception
  40. continue; // Skip to the next file
  41. }
  42. }
  43. if (empty($allData)) {
  44. error_log("No data extracted from any files.");
  45. return false; // Return false if no data was extracted
  46. }
  47. // Write data to CSV file
  48. $csvFile = fopen($outputFile, 'w');
  49. if ($csvFile === false) {
  50. error_log("Error opening output file: " . $outputFile);
  51. return false;
  52. }
  53. // Write header row
  54. fputcsv($csvFile, ['name', 'description', 'status', 'file']);
  55. // Write data rows
  56. foreach ($allData as $dataRow) {
  57. fputcsv($csvFile, [$dataRow['name'], $dataRow['description'], $dataRow['status'], $dataRow['file']]);
  58. }
  59. fclose($csvFile); // Close the output file
  60. return true; // Return true on success
  61. }
  62. ?>

Add your comment