From ed7898291e69c815d5debeebc2266d8712bb1a08 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sat, 10 Sep 2022 17:15:40 -0700 Subject: [PATCH] Add clear flag for index (fix #23) --- lib/Command/Index.php | 21 +++++++++++++++++++++ lib/Db/TimelineWrite.php | 9 +++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/Command/Index.php b/lib/Command/Index.php index 728aaa1f..6ac203a8 100644 --- a/lib/Command/Index.php +++ b/lib/Command/Index.php @@ -131,12 +131,33 @@ class Index extends Command { 'f', InputOption::VALUE_NONE, 'Refresh existing entries' + ) + ->addOption( + 'clear', + null, + InputOption::VALUE_NONE, + 'Clear existing index before creating a new one (SLOW)' ); } protected function execute(InputInterface $input, OutputInterface $output): int { // Get options and arguments $refresh = $input->getOption('refresh') ? true : false; + $clear = $input->getOption('clear') ? true : false; + + // Clear index if asked for this + if ($clear && $input->isInteractive()) { + $output->write("Are you sure you want to clear the existing index? (y/N): "); + $answer = trim(fgets(STDIN)); + if ($answer !== 'y') { + $output->writeln("Aborting"); + return 1; + } + } + if ($clear) { + $this->timelineWrite->clear(); + $output->writeln("Cleared existing index"); + } // Run with the static process try { diff --git a/lib/Db/TimelineWrite.php b/lib/Db/TimelineWrite.php index c9520807..c8fbf9f1 100644 --- a/lib/Db/TimelineWrite.php +++ b/lib/Db/TimelineWrite.php @@ -106,4 +106,13 @@ class TimelineWrite { WHERE `fileid` = ?'; $this->connection->executeStatement($sql, [$file->getId()], [\PDO::PARAM_INT]); } + + /** + * Clear the entire index. Does not need confirmation! + * @param File $file + */ + public function clear() { + $sql = 'TRUNCATE TABLE *PREFIX*memories'; + $this->connection->executeStatement($sql); + } } \ No newline at end of file