<?php
/**
* Merges datasets from multiple HTML documents for maintenance tasks.
* Handles potential errors gracefully.
*
* @param array $htmlFiles An array of paths to HTML files.
* @param string $outputFile The path to the output CSV file.
* @return bool True on success, false on failure.
*/
function mergeHtmlDatasets(array $htmlFiles, string $outputFile): bool
{
$allData = []; // Array to store data from all files
foreach ($htmlFiles as $htmlFile) {
try {
$html = file_get_contents($htmlFile); // Read HTML content
if ($html === false) {
error_log("Error reading file: " . $htmlFile); // Log error
continue; // Skip to the next file
}
// Extract data from HTML (Example: using simple regex - adjust as needed)
preg_match_all('/<div class="task">(.*?)</div>/s', $html, $matches);
if (!empty($matches[0])) {
foreach ($matches[0] as $taskHtml) {
// Extract task details (Example: name, description, status)
preg_match('/<h2 class="task-name">(.*?)</h2>/', $taskHtml, $taskName);
preg_match('/<p class="task-description">(.*?)<\/p>/', $taskHtml, $taskDescription);
preg_match('/<span class="task-status">(.*?)<\/span>/', $taskHtml, $taskStatus);
$allData[] = [
'name' => trim($taskName[0]),
'description' => trim($taskDescription[0]),
'status' => trim($taskStatus[0]),
'file' => $htmlFile, // Add file source for traceability
];
}
} else {
error_log("No tasks found in file: " . $htmlFile); // Log if no tasks are found
}
} catch (Exception $e) {
error_log("Error processing file " . $htmlFile . ": " . $e->getMessage()); // Log exception
continue; // Skip to the next file
}
}
if (empty($allData)) {
error_log("No data extracted from any files.");
return false; // Return false if no data was extracted
}
// Write data to CSV file
$csvFile = fopen($outputFile, 'w');
if ($csvFile === false) {
error_log("Error opening output file: " . $outputFile);
return false;
}
// Write header row
fputcsv($csvFile, ['name', 'description', 'status', 'file']);
// Write data rows
foreach ($allData as $dataRow) {
fputcsv($csvFile, [$dataRow['name'], $dataRow['description'], $dataRow['status'], $dataRow['file']]);
}
fclose($csvFile); // Close the output file
return true; // Return true on success
}
?>
Add your comment