<?php
/**
* CLI tool to index timestamped content.
*
* Usage: php index_timestamps.php <input_file> <output_file>
*
* Input file: File containing timestamped content, one entry per line (e.g., "2023-10-27 10:00:00 - Content").
* Output file: Index file. Format: timestamp => content (one entry per line).
*/
if (count($argv) !== 3) {
die("Usage: php index_timestamps.php <input_file> <output_file>\n");
}
$inputFile = $argv[1];
$outputFile = $argv[2];
$index = [];
try {
// Read the input file line by line
$fileHandle = fopen($inputFile, 'r');
if ($fileHandle) {
while (($line = fgets($fileHandle)) !== false) {
// Extract timestamp and content
list($timestamp, $content) = explode(" - ", $line, 2); // Split into timestamp and content
//Add to index
$index[$timestamp] = $content;
}
fclose($fileHandle);
} else {
die("Could not open input file: $inputFile\n");
}
// Write the index to the output file
$fileHandle = fopen($outputFile, 'w');
if ($fileHandle) {
foreach ($index as $timestamp => $content) {
fputcsv($fileHandle, [$timestamp, $content]); // Write timestamp and content as CSV
}
fclose($fileHandle);
} else {
die("Could not open output file: $outputFile\n");
}
} catch (Exception $e) {
die("An error occurred: " . $e->getMessage() . "\n");
}
?>
Add your comment