1. <?php
  2. /**
  3. * CLI tool to index timestamped content.
  4. *
  5. * Usage: php index_timestamps.php <input_file> <output_file>
  6. *
  7. * Input file: File containing timestamped content, one entry per line (e.g., "2023-10-27 10:00:00 - Content").
  8. * Output file: Index file. Format: timestamp => content (one entry per line).
  9. */
  10. if (count($argv) !== 3) {
  11. die("Usage: php index_timestamps.php <input_file> <output_file>\n");
  12. }
  13. $inputFile = $argv[1];
  14. $outputFile = $argv[2];
  15. $index = [];
  16. try {
  17. // Read the input file line by line
  18. $fileHandle = fopen($inputFile, 'r');
  19. if ($fileHandle) {
  20. while (($line = fgets($fileHandle)) !== false) {
  21. // Extract timestamp and content
  22. list($timestamp, $content) = explode(" - ", $line, 2); // Split into timestamp and content
  23. //Add to index
  24. $index[$timestamp] = $content;
  25. }
  26. fclose($fileHandle);
  27. } else {
  28. die("Could not open input file: $inputFile\n");
  29. }
  30. // Write the index to the output file
  31. $fileHandle = fopen($outputFile, 'w');
  32. if ($fileHandle) {
  33. foreach ($index as $timestamp => $content) {
  34. fputcsv($fileHandle, [$timestamp, $content]); // Write timestamp and content as CSV
  35. }
  36. fclose($fileHandle);
  37. } else {
  38. die("Could not open output file: $outputFile\n");
  39. }
  40. } catch (Exception $e) {
  41. die("An error occurred: " . $e->getMessage() . "\n");
  42. }
  43. ?>

Add your comment