diff --git a/app/Console/Commands/ImportLexicalEntries.php b/app/Console/Commands/ImportLexicalEntries.php new file mode 100644 index 0000000..856d0f3 --- /dev/null +++ b/app/Console/Commands/ImportLexicalEntries.php @@ -0,0 +1,47 @@ +truncate(); + + $importHandler = new LexiconEntryImportHandler(); + $importHandler->run(); + } +} diff --git a/app/Handlers/LexiconEntryImportHandler.php b/app/Handlers/LexiconEntryImportHandler.php new file mode 100644 index 0000000..34534ed --- /dev/null +++ b/app/Handlers/LexiconEntryImportHandler.php @@ -0,0 +1,65 @@ + + * Date: 10/6/20 + * Time: 8:28 PM + */ +class LexiconEntryImportHandler +{ + use LexiconEntryTrait; + + const REPO_FOLDER = '/gwt/'; + + public function run() : void + { + // Iterate over each folder in the repo + $handle = opendir($this->getFolder()); + + while (($dir = readdir($handle)) !== false) { + if (is_dir($this->getFolder() . $dir)) { + $this->scanLexiconDirectory($this->getFolder() . $dir); + } + // Silently ignoring files at this level + } + + closedir($handle); + } + + protected function scanLexiconDirectory(string $dir) : void + { + $handle = opendir($dir); + + while (($item = readdir($handle)) !== false) { + if ((substr($item, 0, 1) === 'g') && is_file($dir . '/' . $item)) { + $parts = explode('.', substr($item, 1)); + $this->importEntryFile($dir . '/' . $item, $parts[0]); + } + } + + closedir($handle); + } + + protected function importEntryFile(string $file, string $stongsNumber) : LexicalEntry + { + $contents = file_get_contents($file); + list($lemma, $commentary) = $this->parseEntry($contents); + + $entry = LexicalEntry::create([ + 'id' => $stongsNumber, + 'lemma' => $lemma, + 'commentary' => $commentary, + ]); + + return $entry; + } + + protected function getFolder() : string + { + return storage_path() . self::REPO_FOLDER; + } +} diff --git a/app/Handlers/LexiconEntryTrait.php b/app/Handlers/LexiconEntryTrait.php new file mode 100644 index 0000000..9be6cb0 --- /dev/null +++ b/app/Handlers/LexiconEntryTrait.php @@ -0,0 +1,38 @@ + + * Date: 10/6/20 + * Time: 8:34 PM + */ +trait LexiconEntryTrait +{ + /** + * Extract the lexeme and the commentary portion into separate variables + * + * @param $entry + * @return array + */ + public function parseEntry($entry) + { + $lexeme = ''; + $commentary = ''; + + $n = 0; + foreach(preg_split('~[\r\n]+~', $entry) as $line){ + if(empty($line) or ctype_space($line)) continue; // skip only spaces + + if ($n === 0) { + $lexeme = ltrim($line, '#'); + } else { + $commentary .= $line . "\n"; + } + $n++; + } + + return [$lexeme, $commentary]; + } +} diff --git a/app/Handlers/LexiconHandler.php b/app/Handlers/LexiconHandler.php index 1158b4b..b7d224a 100644 --- a/app/Handlers/LexiconHandler.php +++ b/app/Handlers/LexiconHandler.php @@ -11,7 +11,7 @@ use Illuminate\Support\Facades\URL; */ class LexiconHandler { - use BookXmlFilesTrait; + use BookXmlFilesTrait, LexiconEntryTrait; public function getEntriesJson($book, $chapter, $verse) { @@ -33,14 +33,14 @@ class LexiconHandler public function formatForJson($id, $entry) { - list($lexeme, $commentary) = $this->parseEntry($entry); + list($lemma, $commentary) = $this->parseEntry($entry); return [ 'type' => 'lexical-entries', 'id' => $id, 'attributes' => [ 'strongs-number' => $id, - 'lexeme' => $lexeme, + 'lemma' => $lemma, 'commentary' => $commentary, ], 'links' => [ @@ -49,32 +49,6 @@ class LexiconHandler ]; } - /** - * Extract the lexeme and the commentary portion into separate variables - * - * @param $entry - * @return array - */ - public function parseEntry($entry) - { - $lexeme = ''; - $commentary = ''; - - $n = 0; - foreach(preg_split('~[\r\n]+~', $entry) as $line){ - if(empty($line) or ctype_space($line)) continue; // skip only spaces - - if ($n === 0) { - $lexeme = ltrim($line, '#'); - } else { - $commentary .= $line . "\n"; - } - $n++; - } - - return [$lexeme, $commentary]; - } - public function getEntriesByVerse($book, $chapter, $verse) { $xmlFile = $this->getBookXmlFile('/ulb/', $book);