From 81fecfbc599942a2a88021781c622df80c76e9d1 Mon Sep 17 00:00:00 2001 From: Ryan Prather Date: Mon, 13 May 2024 21:07:58 -0400 Subject: [PATCH] Source Commands --- src/Command/IngestBibleCommand.php | 110 ++++++++++++++++++ src/Command/IngestReferenceCommand.php | 153 +++++++++++++++++++++++++ 2 files changed, 263 insertions(+) create mode 100644 src/Command/IngestBibleCommand.php create mode 100644 src/Command/IngestReferenceCommand.php diff --git a/src/Command/IngestBibleCommand.php b/src/Command/IngestBibleCommand.php new file mode 100644 index 0000000..8f4c33a --- /dev/null +++ b/src/Command/IngestBibleCommand.php @@ -0,0 +1,110 @@ +emi = $emi; + } + + protected function configure(): void + { + $this + ->addArgument('directory', InputArgument::REQUIRED, 'Directory to crawl') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $this->io = new SymfonyStyle($input, $output); + $this->dir = $input->getArgument('directory'); + + if (!$this->dir) { + $this->io->error('Directory not specific or does not exist'); + return Command::FAILURE; + } + + $this->io->note("Crawling {$this->dir} and ingesting Bible passages..."); + + foreach($this->getFiles() as $file) { + $this->io->info("Processing ".basename($file)); + $this->processFile($file); + } + + return Command::SUCCESS; + } + + private function getFiles(): array + { + $this->files = glob($this->dir . "/*/*.md"); + + if(!$this->files || count($this->files) === 0) { + $this->io->warning("No files found in this directory\n{$this->dir}"); + $this->files = []; + } + natsort($this->files); + + return $this->files; + } + + private function processFile(string $file) + { + $bible = new Bible(); + + $match = []; + if(preg_match("/([\d]+) \- ([^\/]+)\/\d?[a-zA-Z]+([\d]+)\.md/", $file, $match)) { + $ndx = (int) $match[1]; + $book = str_replace(" ", "", $match[2]); + $chapter = (int) $match[3]; + } + + if (preg_match("/(\d)([^\d]+)/", $book, $match)) { + $book = $match[1]." ".$match[2]; + } + + $bible->setBook($book); + $bible->setBookIndex($ndx); + $bible->setChapter($chapter); + + $content = file_get_contents($file); + foreach(explode("\n", $content) as $verse) { + $match = []; + if (preg_match("/^([\d]+). ([^\d]+)/", $verse, $match)) { + $bible->setVerse($match[1]); + $bible->setContent($match[2]); + } + + $this->emi->persist($bible); + $this->emi->flush(); + } + } +} diff --git a/src/Command/IngestReferenceCommand.php b/src/Command/IngestReferenceCommand.php new file mode 100644 index 0000000..02e08a8 --- /dev/null +++ b/src/Command/IngestReferenceCommand.php @@ -0,0 +1,153 @@ +emi = $emi; + } + + /** + * {@inheritdoc} + */ + protected function configure(): void + { + $this + ->addArgument('directory', InputArgument::REQUIRED, 'Directory to crawl') + ->addArgument('name', InputArgument::REQUIRED, 'Name of the reference') + ->addArgument('type', InputArgument::REQUIRED, 'Type of the reference') + ->addArgument('label', InputArgument::REQUIRED, 'Label of the reference') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $this->io = new SymfonyStyle($input, $output); + $this->dir = $input->getArgument('directory'); + $this->name = $input->getArgument('name'); + $this->type = $input->getArgument('type'); + $this->label = $input->getArgument('label'); + + if (!$this->dir || !file_exists($this->dir) ||!is_dir($this->dir)) { + $this->io->error('Directory not specific or does not exist'); + return Command::FAILURE; + } + + if (!$this->name) { + $this->io->error('No name specified'); + return Command::FAILURE; + } + + if (!$this->type) { + $this->io->error('No type specified'); + return Command::FAILURE; + } + + if (!$this->label) { + $this->io->error('No label specified'); + return Command::FAILURE; + } + + $this->io->note("Crawling {$this->dir} and ingesting {$this->name} as {$this->type}:{$this->label}"); + + foreach ($this->getFiles() as $file) { + $ref = $this->processFile($file); + + $this->emi->persist($ref); + } + + $this->emi->flush(); + + return Command::SUCCESS; + } + + public function getFiles(): array + { + $this->files = glob($this->dir . '/*.md'); + + if(!$this->files || count($this->files) === 0) { + $this->io->warning("No files found in this directory\n{$this->dir}"); + $this->files = []; + } + + return $this->files; + } + + public function processFile(string $file): Reference|bool + { + $this->io->info("Processing {$file}"); + $md = trim(file_get_contents($file)); + $ref = new Reference(); + + if (!$md) { + $this->io->warning("File is empty\n{$file}"); + return false; + } + + $match = []; + $label = str_replace("{\$ndx}", "", $this->label); + if(preg_match("/([\d]+)/", $file, $match)) { + $ndx = ltrim($match[1], "0"); + $label = str_replace("{\$ndx}", $ndx, $this->label); + $ref->setNdx($ndx); + } elseif (preg_match("/\(([^\)]+)\)/", $file, $match)) { + $label = $match[1]; + } + + $ref->setContent($md); + $ref->setName($this->name); + $ref->setType($this->type); + $ref->setLabel($label); + + $this->io->success("Ingested {$this->name} as {$this->type}:{$label}"); + + return $ref; + } +}