Remove all references to `storage/ticks`, including code that migrates those files. Reviewed-on: https://gitea.subcultureofone.org/greg/tkr/pulls/27 Co-authored-by: Greg Sarjeant <greg@subcultureofone.org> Co-committed-by: Greg Sarjeant <greg@subcultureofone.org>
This commit is contained in:
parent
3867bc36cf
commit
edd7f6effe
@ -25,10 +25,6 @@ if (!(preg_match('/setup$/', $path))) {
|
||||
// database validation
|
||||
$dbMgr = new Database();
|
||||
$dbMgr->validate();
|
||||
|
||||
// filesystem Migration
|
||||
// TODO - delete
|
||||
$fsMgr->migrate();
|
||||
} catch (SetupException $e) {
|
||||
$e->handle();
|
||||
exit;
|
||||
|
@ -7,12 +7,6 @@ class Filesystem {
|
||||
$this->validateStorageSubdirs();
|
||||
}
|
||||
|
||||
// TODO - delete this
|
||||
public function migrate(): void{
|
||||
$this->migrateTickFiles();
|
||||
$this->moveTicksToDatabase();
|
||||
}
|
||||
|
||||
// Make sure the storage/ directory exists and is writable
|
||||
private function validateStorageDir(): void{
|
||||
if (!is_dir(STORAGE_DIR)) {
|
||||
@ -36,7 +30,6 @@ class Filesystem {
|
||||
$storageSubdirs = array();
|
||||
$storageSubdirs[] = CSS_UPLOAD_DIR;
|
||||
$storageSubdirs[] = DATA_DIR;
|
||||
$storageSubdirs[] = TICKS_DIR;
|
||||
|
||||
foreach($storageSubdirs as $storageSubdir){
|
||||
if (!is_dir($storageSubdir)) {
|
||||
@ -58,100 +51,4 @@ class Filesystem {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Delete this sometime before 1.0
|
||||
// Add mood to tick files
|
||||
private function migrateTickFiles(): void {
|
||||
$files = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator(TICKS_DIR, RecursiveDirectoryIterator::SKIP_DOTS)
|
||||
);
|
||||
|
||||
foreach ($files as $file) {
|
||||
if ($file->isFile() && str_ends_with($file->getFilename(), '.txt')) {
|
||||
$this->migrateTickFile($file->getPathname());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Delete this sometime before 1.0
|
||||
// Add mood field to tick files if necessary
|
||||
private function migrateTickFile($filepath): void {
|
||||
$lines = file($filepath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
$modified = false;
|
||||
|
||||
// &$line creates a reference to the line in the array ($lines)
|
||||
// so I can modify it in place
|
||||
foreach ($lines as &$line) {
|
||||
$fields = explode('|', $line);
|
||||
if (count($fields) === 2) {
|
||||
// Convert id|text to id|emoji|text
|
||||
$line = $fields[0] . '||' . $fields[1];
|
||||
$modified = true;
|
||||
}
|
||||
}
|
||||
unset($line);
|
||||
|
||||
if ($modified) {
|
||||
file_put_contents($filepath, implode("\n", $lines) . "\n");
|
||||
// TODO: log properly
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Delete this sometime before 1.0
|
||||
// Move ticks into database
|
||||
private function moveTicksToDatabase(){
|
||||
// It's a temporary migration function, so I'm not going to sweat the
|
||||
// order of operations to let me reuse the global database.
|
||||
$db = Database::get();
|
||||
$count = $db->query("SELECT COUNT(*) FROM tick")->fetchColumn();
|
||||
|
||||
// Only migrate from filesystem if there are no ticks already in the database.
|
||||
if ($count !== 0){
|
||||
return;
|
||||
}
|
||||
|
||||
$files = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator(TICKS_DIR, RecursiveDirectoryIterator::SKIP_DOTS)
|
||||
);
|
||||
|
||||
foreach ($files as $file) {
|
||||
if ($file->isFile() && str_ends_with($file->getFilename(), '.txt')) {
|
||||
// Construct the date from the path and filename
|
||||
$dir = pathinfo($file, PATHINFO_DIRNAME);
|
||||
$dir_parts = explode('/', trim($dir, '/'));
|
||||
[$year, $month] = array_slice($dir_parts, -2);
|
||||
$day = pathinfo($file, PATHINFO_FILENAME);
|
||||
|
||||
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
foreach ($lines as $line) {
|
||||
// Get the time and the text, but discard the mood.
|
||||
// I've decided against using it
|
||||
$fields = explode('|', $line);
|
||||
$time = $fields[0];
|
||||
$tick = $fields[2];
|
||||
|
||||
$dateTime = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', "$year-$month-$day $time");
|
||||
$tickDateTimeUTC = $dateTime->format('Y-m-d H:i:s');
|
||||
|
||||
$ticks[] = [$tickDateTimeUTC, $tick];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort the ticks by dateTime
|
||||
usort($ticks, function($a, $b) {
|
||||
return strcmp($a[0], $b[0]);
|
||||
});
|
||||
|
||||
// Save ticks to database
|
||||
foreach ($ticks as $tick){
|
||||
// Yes, silly, but I'm testing out the datetime/string SQLite conversion
|
||||
$dateTime = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', "$tick[0]");
|
||||
$timestamp = $dateTime->format('Y-m-d H:i:s');
|
||||
$tickText = $tick[1];
|
||||
|
||||
$stmt = $db->prepare("INSERT INTO tick(timestamp, tick) values (?, ?)");
|
||||
$stmt->execute([$timestamp, $tickText]);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user