gdc_cmod/script.php
mahdahar caf8b332f4 feat: rewrite batch PDF script to use curl endpoint with localhost bypass
Changes:
- app/Filters/RoleFilter.php: Add localhost/127.0.0.1 bypass at start of before()
- app/Controllers/ReportController.php: Add localhost bypass in generatePdf()
- script.php: Rewrite to use curl to http://glenlis/cmod/report/{accessnumber}/pdf

Benefits:
- Eliminates 94-second CLI database queries
- Uses optimized web endpoint (fast database connections)
- Simple curl-based implementation, no CI4 bootstrapping
- Maintains all existing logging (AUDIT_REQUESTS, ORU files)
2026-03-11 09:11:47 +07:00

154 lines
4.4 KiB
PHP

<?php
/**
* Batch PDF Generator CLI Script
* Uses curl to CI4 web endpoint (avoids slow CLI database queries)
*
* Usage: php script.php accessnumbers.txt
*
* Input file format: One accessnumber per line
* Example:
* 202403110001
* 202403110002
* 202403110003
*/
// Check command line arguments
if ($argc < 2) {
echo "Usage: php script.php <accessnumbers_file>\n";
echo "Example: php script.php batch.txt\n\n";
echo "File format: One accessnumber per line\n";
exit(1);
}
$inputFile = $argv[1];
if (!file_exists($inputFile)) {
echo "Error: File not found: $inputFile\n";
exit(1);
}
// Base URL for CI4 endpoint
$baseUrl = 'http://glenlis/cmod/report';
// Read accessnumbers from file
$lines = file($inputFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === false) {
echo "Error: Could not read file: $inputFile\n";
exit(1);
}
$accessnumbers = array_filter(array_map('trim', $lines));
$total = count($accessnumbers);
if ($total === 0) {
echo "Error: No accessnumbers found in file\n";
exit(1);
}
echo "Processing $inputFile\n";
echo "Found $total access number(s) via $baseUrl\n";
echo str_repeat("-", 60) . "\n";
// Statistics
$stats = [
'success' => 0,
'failed' => 0,
'details' => []
];
// Process each accessnumber
foreach ($accessnumbers as $index => $accessnumber) {
$current = $index + 1;
$startTime = microtime(true);
echo "[$current/$total] $accessnumber... ";
try {
$url = "$baseUrl/$accessnumber/pdf";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($curlError) {
throw new \Exception("cURL error: $curlError");
}
if ($httpCode !== 200) {
throw new \Exception("HTTP $httpCode");
}
$data = json_decode($response, true);
if (!isset($data['success']) || !$data['success']) {
$error = $data['error'] ?? 'Unknown error';
throw new \Exception($error);
}
$jobId = $data['jobId'] ?? 'unknown';
$lang = $data['lang'] ?? 'Unknown';
$isRegen = $data['isRegen'] ?? false;
$regenText = $isRegen ? ' (regen)' : '';
$elapsed = round((microtime(true) - $startTime) * 1000);
echo "QUEUED (job: $jobId, lang: $lang$regenText, ${elapsed}ms)\n";
$stats['success']++;
$stats['details'][] = [
'accessnumber' => $accessnumber,
'status' => 'success',
'jobId' => $jobId,
'language' => $lang,
'time_ms' => $elapsed
];
} catch (\Exception $e) {
$elapsed = round((microtime(true) - $startTime) * 1000);
echo "FAILED (" . $e->getMessage() . ", ${elapsed}ms)\n";
$stats['failed']++;
$stats['details'][] = [
'accessnumber' => $accessnumber,
'status' => 'failed',
'error' => $e->getMessage(),
'time_ms' => $elapsed
];
}
}
echo str_repeat("-", 60) . "\n";
echo "Complete: " . $stats['success'] . " queued, " . $stats['failed'] . " failed\n";
// Save detailed log
$logFile = 'batch_pdf_' . date('Ymd_His') . '.log';
$logContent = "Batch PDF Generation Log\n";
$logContent .= "Generated: " . date('Y-m-d H:i:s') . "\n";
$logContent .= "Input file: $inputFile\n";
$logContent .= "Endpoint: $baseUrl\n";
$logContent .= "Total: $total, Success: {$stats['success']}, Failed: {$stats['failed']}\n";
$logContent .= str_repeat("-", 60) . "\n";
foreach ($stats['details'] as $detail) {
$logContent .= $detail['accessnumber'] . " | " . $detail['status'];
if (isset($detail['jobId'])) {
$logContent .= " | job: " . $detail['jobId'];
$logContent .= " | lang: " . $detail['language'];
}
if (isset($detail['error'])) {
$logContent .= " | error: " . $detail['error'];
}
$logContent .= " | " . $detail['time_ms'] . "ms\n";
}
file_put_contents($logFile, $logContent);
echo "Log saved to: $logFile\n";
exit($stats['failed'] > 0 ? 1 : 0);