From 83b5a599c9859dae70baf29fd57c8d5e19507ea9 Mon Sep 17 00:00:00 2001 From: Zach DeCook Date: Wed, 26 Dec 2018 12:29:49 -0500 Subject: * Database: Allow songs to be imported from a textfile --- laravel/app/Console/Commands/AddSongs.php | 91 +++++++++++++++++++++++++++++++ laravel/app/Song.php | 11 ++++ 2 files changed, 102 insertions(+) create mode 100644 laravel/app/Console/Commands/AddSongs.php create mode 100644 laravel/app/Song.php (limited to 'laravel/app') diff --git a/laravel/app/Console/Commands/AddSongs.php b/laravel/app/Console/Commands/AddSongs.php new file mode 100644 index 0000000..94cf3c0 --- /dev/null +++ b/laravel/app/Console/Commands/AddSongs.php @@ -0,0 +1,91 @@ +argument('file'); + $handle = fopen($filename, "r"); + + $theSong = [ + 'text' => '', + 'verse' => '', + 'number' => FALSE, + ]; + + if ( !$handle ){ + $this->error("Couldn't read file '$filename'"); + return 1; + } + while (($line = fgets($handle)) !== false) + { + // If we see a number, then that is what song we are on. + $matches = array(); + if ( preg_match("(^(X?C?B?\d+)\. )", $line, $matches) ) + { + if ( $theSong['text'] && $theSong['number'] && $theSong['title'] + && ( $theSong['number'] != $matches[1] ) ) { + Song::create( $theSong ); + $this->line( "Created $theSong[title]" ); + $theSong = [ + 'text' => '', + 'verse' => '', + 'key' => NULL, + 'author' => NULL, + ]; + } + $theSong['number'] = $matches[1]; + $theSong['title'] = trim($line); + $this->line( "Creating $theSong[title]..." ); + } + + $theSong['text'] .= $line; + + if ( preg_match( "/\{p?\d*\((.+m?)\)\}/", $line, $matches) + || preg_match("/^{Key: ?([^ ]*).*}/i", $line, $matches) + ) { + $theSong['key'] = $matches[1]; + } + if ( preg_match( AUTHOR_REGEX, $line, $matches ) ) { + $theSong['author'] = $matches[1]; + } + } + fclose($handle); + Song::create( $theSong ); + } +} diff --git a/laravel/app/Song.php b/laravel/app/Song.php new file mode 100644 index 0000000..876617f --- /dev/null +++ b/laravel/app/Song.php @@ -0,0 +1,11 @@ +